From 1f98b81ce3e03f706a90daea18e61a90d5d64eaf Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 31 Jul 2026 11:00:01 +0800 Subject: [PATCH] Fix bulk-created individual defect IDs --- apps/wpm/services.py | 2 ++ apps/wpm/tests.py | 31 +++++++++++++++++++++++++++++++ apps/wpmw/models.py | 2 ++ 3 files changed, 35 insertions(+) diff --git a/apps/wpm/services.py b/apps/wpm/services.py index da0b96cf..b86b55fe 100644 --- a/apps/wpm/services.py +++ b/apps/wpm/services.py @@ -21,6 +21,7 @@ from apps.wpmw.models import Wpr, WprDefect from ..qm.models import Defect, Ftest from django.db.models import Count, Q from apps.utils.tasks import ctask_run +from apps.utils.snowflake import idWorker from apps.mtm.models import Process, WmScope from django.db.models import F @@ -214,6 +215,7 @@ def restore_mlog_wpr_defects(mlog: Mlog): continue restored.extend( WprDefect( + id=idWorker.get_id(), wpr=wpr, defect_id=item["defect_id"], is_main=item.get("is_main", False), diff --git a/apps/wpm/tests.py b/apps/wpm/tests.py index 0ce84e2c..6954cd04 100644 --- a/apps/wpm/tests.py +++ b/apps/wpm/tests.py @@ -548,6 +548,30 @@ class WMaterialScopeTests(SimpleTestCase): class WprDefectLifecycleTests(SimpleTestCase): + @patch("apps.wpmw.models.WprDefect.objects.bulk_create") + @patch("apps.wpmw.models.WprDefect.objects.filter") + @patch( + "apps.wpmw.models.idWorker.get_id", + side_effect=["100", "101"], + ) + def test_replace_defects_assigns_unique_ids_before_bulk_create( + self, + get_id, + defect_filter, + bulk_create, + ): + wpr = Wpr(id="1") + + wpr.replace_defects([ + {"defect_id": "10", "is_main": True}, + {"defect_id": "11", "is_main": False}, + ]) + + defect_filter.return_value.delete.assert_called_once_with() + created = bulk_create.call_args.args[0] + self.assertEqual([str(item.id) for item in created], ["100", "101"]) + self.assertEqual(get_id.call_count, 2) + @patch("apps.wpmw.models.WprDefect.objects.filter") def test_change_inventory_never_changes_individual_defects(self, defect_filter): material = Material(id="1", tracking=Material.MA_TRACKING_SINGLE) @@ -671,8 +695,13 @@ class MlogWprDefectSnapshotTests(SimpleTestCase): @patch("apps.wpm.services.WprDefect.objects.bulk_create") @patch("apps.wpm.services.WprDefect.objects.filter") @patch("apps.wpm.services.Wpr.objects.select_for_update") + @patch( + "apps.wpm.services.idWorker.get_id", + side_effect=["100", "101"], + ) def test_restore_replaces_current_defects_exactly( self, + get_id, select_for_update, defect_filter, bulk_create, @@ -698,6 +727,8 @@ class MlogWprDefectSnapshotTests(SimpleTestCase): [(str(item.defect_id), item.is_main) for item in created], [("10", True), ("11", False)], ) + self.assertEqual([str(item.id) for item in created], ["100", "101"]) + self.assertEqual(get_id.call_count, 2) @skipUnless( diff --git a/apps/wpmw/models.py b/apps/wpmw/models.py index f45bc23c..a655e2e0 100644 --- a/apps/wpmw/models.py +++ b/apps/wpmw/models.py @@ -5,6 +5,7 @@ from apps.utils.models import BaseModel from apps.mtm.models import Material from rest_framework.exceptions import ParseError from apps.wpm.models import WmStateOption, Mlogbw, Handoverbw +from apps.utils.snowflake import idWorker from apps.utils.tools import update_dict from apps.inm.models import MIOItemw from django.db.models import F, Value @@ -130,6 +131,7 @@ class Wpr(BaseModel): WprDefect.objects.filter(wpr=self).delete() WprDefect.objects.bulk_create([ WprDefect( + id=idWorker.get_id(), wpr=self, defect_id=item["defect_id"], is_main=item.get("is_main", False),