factory/apps/mtm/tests.py

70 lines
2.6 KiB
Python

from django.test import TestCase
from apps.mtm.models import Material, Process, Route, RoutePack
from apps.mtm.serializers import RouteSerializer
class RouteMaterialOutCreateSerializerTests(TestCase):
def setUp(self):
self.process = Process.objects.create(name='切割')
self.product = Material.objects.create(
name='产品A',
number='CP-A',
type=Material.MA_TYPE_GOOD,
process=self.process,
)
self.material_in = Material.objects.create(
name='原料A',
number='YL-A',
type=Material.MA_TYPE_MAINSO,
)
self.routepack = RoutePack.objects.create(name='产品A工艺', material=self.product)
def test_create_route_and_visible_halfgood_in_one_serializer_save(self):
serializer = RouteSerializer(data={
'routepack': self.routepack.id,
'process': self.process.id,
'material_in': self.material_in.id,
'material_out_tracking': Material.MA_TRACKING_SINGLE,
'material_out_create': {
'name': '分支半成品A',
'number': 'BCP-A',
'specification': 'S1',
'model': 'M1',
'unit': '',
},
})
serializer.is_valid(raise_exception=True)
route = serializer.save()
material_out = route.material_out
self.assertEqual(material_out.type, Material.MA_TYPE_HALFGOOD)
self.assertEqual(material_out.process, self.process)
self.assertEqual(material_out.tracking, Material.MA_TRACKING_SINGLE)
self.assertFalse(material_out.is_hidden)
self.assertEqual(material_out.name, '分支半成品A')
self.assertEqual(material_out.unit, '')
self.assertTrue(Route.objects.filter(pk=route.pk, material_out=material_out).exists())
def test_existing_and_new_material_out_are_mutually_exclusive(self):
existing = Material.objects.create(
name='已有半成品',
number='BCP-OLD',
type=Material.MA_TYPE_HALFGOOD,
process=self.process,
)
serializer = RouteSerializer(data={
'routepack': self.routepack.id,
'process': self.process.id,
'material_in': self.material_in.id,
'material_out': existing.id,
'material_out_create': {
'name': '新半成品',
'number': 'BCP-NEW',
},
})
self.assertFalse(serializer.is_valid())
self.assertIn('non_field_errors', serializer.errors)