from types import SimpleNamespace from unittest.mock import MagicMock, patch from django.test import SimpleTestCase from apps.mtm.models import Material from apps.wpm.views import MlogbwViewSet class MlogbwViewSetTests(SimpleTestCase): @patch("apps.wpm.views.MlogViewSet.lock_and_check_can_update") @patch("apps.wpm.views.Mlogbw.cal_count_notok") @patch("apps.wpm.views.Mlogbw.objects.get_or_create") @patch("apps.wpm.views.Mlogb.objects.filter") def test_perform_create_syncs_fix_output_without_route( self, mlogb_filter, mlogbw_get_or_create, cal_count_notok, lock_and_check_can_update, ): wm = object() wpr = SimpleNamespace(wm=wm) material = SimpleNamespace(tracking=Material.MA_TRACKING_SINGLE) mlog = SimpleNamespace( route=None, is_fix=True, cal_mlog_count_from_mlogb=MagicMock(), ) mlogb_in = SimpleNamespace( mlog=mlog, route=None, wm_in=wm, material_in=material, ) mlogb_out = object() mlogb_qs = MagicMock() mlogb_qs.exists.return_value = True mlogb_qs.__iter__.return_value = iter([mlogb_out]) mlogb_filter.return_value = mlogb_qs ins = SimpleNamespace( mlogb=mlogb_in, wpr=wpr, number="2606P1095-3", ) serializer = MagicMock() serializer.save.return_value = ins lock_and_check_can_update.return_value = mlog MlogbwViewSet().perform_create(serializer) mlogbw_get_or_create.assert_called_once_with( mlogb=mlogb_out, wpr=wpr, defaults={ "number": "2606P1095-3", "mlogbw_from": ins, }, ) self.assertEqual(cal_count_notok.call_count, 2) mlog.cal_mlog_count_from_mlogb.assert_called_once_with()