Fix bulk-created individual defect IDs

This commit is contained in:
caoqianming 2026-07-31 11:00:01 +08:00
parent 074e759c29
commit 1f98b81ce3
3 changed files with 35 additions and 0 deletions

View File

@ -21,6 +21,7 @@ from apps.wpmw.models import Wpr, WprDefect
from ..qm.models import Defect, Ftest from ..qm.models import Defect, Ftest
from django.db.models import Count, Q from django.db.models import Count, Q
from apps.utils.tasks import ctask_run from apps.utils.tasks import ctask_run
from apps.utils.snowflake import idWorker
from apps.mtm.models import Process, WmScope from apps.mtm.models import Process, WmScope
from django.db.models import F from django.db.models import F
@ -214,6 +215,7 @@ def restore_mlog_wpr_defects(mlog: Mlog):
continue continue
restored.extend( restored.extend(
WprDefect( WprDefect(
id=idWorker.get_id(),
wpr=wpr, wpr=wpr,
defect_id=item["defect_id"], defect_id=item["defect_id"],
is_main=item.get("is_main", False), is_main=item.get("is_main", False),

View File

@ -548,6 +548,30 @@ class WMaterialScopeTests(SimpleTestCase):
class WprDefectLifecycleTests(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") @patch("apps.wpmw.models.WprDefect.objects.filter")
def test_change_inventory_never_changes_individual_defects(self, defect_filter): def test_change_inventory_never_changes_individual_defects(self, defect_filter):
material = Material(id="1", tracking=Material.MA_TRACKING_SINGLE) 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.bulk_create")
@patch("apps.wpm.services.WprDefect.objects.filter") @patch("apps.wpm.services.WprDefect.objects.filter")
@patch("apps.wpm.services.Wpr.objects.select_for_update") @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( def test_restore_replaces_current_defects_exactly(
self, self,
get_id,
select_for_update, select_for_update,
defect_filter, defect_filter,
bulk_create, bulk_create,
@ -698,6 +727,8 @@ class MlogWprDefectSnapshotTests(SimpleTestCase):
[(str(item.defect_id), item.is_main) for item in created], [(str(item.defect_id), item.is_main) for item in created],
[("10", True), ("11", False)], [("10", True), ("11", False)],
) )
self.assertEqual([str(item.id) for item in created], ["100", "101"])
self.assertEqual(get_id.call_count, 2)
@skipUnless( @skipUnless(

View File

@ -5,6 +5,7 @@ from apps.utils.models import BaseModel
from apps.mtm.models import Material from apps.mtm.models import Material
from rest_framework.exceptions import ParseError from rest_framework.exceptions import ParseError
from apps.wpm.models import WmStateOption, Mlogbw, Handoverbw from apps.wpm.models import WmStateOption, Mlogbw, Handoverbw
from apps.utils.snowflake import idWorker
from apps.utils.tools import update_dict from apps.utils.tools import update_dict
from apps.inm.models import MIOItemw from apps.inm.models import MIOItemw
from django.db.models import F, Value from django.db.models import F, Value
@ -130,6 +131,7 @@ class Wpr(BaseModel):
WprDefect.objects.filter(wpr=self).delete() WprDefect.objects.filter(wpr=self).delete()
WprDefect.objects.bulk_create([ WprDefect.objects.bulk_create([
WprDefect( WprDefect(
id=idWorker.get_id(),
wpr=self, wpr=self,
defect_id=item["defect_id"], defect_id=item["defect_id"],
is_main=item.get("is_main", False), is_main=item.get("is_main", False),