Skip to content

Commit bced8cd

Browse files
committed
[#2920] Fix check for natural keys duplicates in ZGW imports
- The check for natural keys duplicates produced false positives because the same `omschrijving` can be used for different types of ZGW objects (e.g. "Afgehandel" for status and resulaat). The check for duplicate natural keys is now scoped to the type of ZGW configuration.
1 parent 3192304 commit bced8cd

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/open_inwoner/openzaak/import_export.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def from_jsonl_stream_or_string(cls, stream_or_string: IO | str) -> Self:
334334

335335
rows_successfully_processed = 0
336336
import_errors = []
337-
natural_keys_seen = set()
337+
natural_keys_seen = defaultdict(set)
338338
for line in cls._lines_iter_from_jsonl_stream_or_string(stream_or_string):
339339
try:
340340
(deserialized_object,) = serializers.deserialize(
@@ -351,10 +351,11 @@ def from_jsonl_stream_or_string(cls, stream_or_string: IO | str) -> Self:
351351
import_errors.append(error)
352352
else:
353353
source_config = deserialized_object.object
354-
if (natural_key := source_config.natural_key()) in natural_keys_seen:
354+
natural_key = source_config.natural_key()
355+
if natural_key in natural_keys_seen[source_config.__class__.__name__]:
355356
import_errors.append(ZGWImportError.from_jsonl(line))
356357
continue
357-
natural_keys_seen.add(natural_key)
358+
natural_keys_seen[source_config.__class__.__name__].add(natural_key)
358359
try:
359360
match source_config:
360361
case CatalogusConfig():

src/open_inwoner/openzaak/tests/test_import_export.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
class ZGWExportImportMockData:
29-
def __init__(self, count=0):
29+
def __init__(self, count=0, with_dupes=False):
3030
self.original_url = f"https://foo.{count}.maykinmedia.nl"
3131
self.original_uuid = "a1591906-3368-470a-a957-4b8634c275a1"
3232
self.service = ServiceFactory(slug=f"service-{count}")
@@ -78,6 +78,14 @@ def __init__(self, count=0):
7878
omschrijving="informatieobject",
7979
zaaktype_uuids=[self.original_uuid],
8080
)
81+
if with_dupes:
82+
self.ztc_resultaat_2 = ZaakTypeResultaatTypeConfigFactory(
83+
zaaktype_config=self.ztc,
84+
resultaattype_url=self.original_url,
85+
omschrijving="status omschrijving", # test dupes across models
86+
zaaktype_uuids=[self.original_uuid],
87+
description="",
88+
)
8189

8290

8391
class ExportObjectTests(TestCase):
@@ -344,6 +352,7 @@ def setUp(self):
344352
]
345353
self.json_dupes = [
346354
'{"model": "openzaak.zaaktypestatustypeconfig", "fields": {"zaaktype_config": ["ztc-id-a-0", "DM-0", "123456789"], "statustype_url": "https://bar.maykinmedia.nl", "omschrijving": "status omschrijving", "statustekst": "statustekst nieuw", "zaaktype_uuids": "[]", "status_indicator": "", "status_indicator_text": "", "document_upload_description": "", "description": "status", "notify_status_change": true, "action_required": false, "document_upload_enabled": true, "call_to_action_url": "", "call_to_action_text": "", "case_link_text": ""}}',
355+
'{"model": "openzaak.zaaktyperesultaattypeconfig", "fields": {"zaaktype_config": ["ztc-id-a-0", "DM-0", "123456789"], "resultaattype_url": "https://bar.maykinmedia.nl", "omschrijving": "status omschrijving", "zaaktype_uuids": "[]", "description": "description new"}}',
347356
]
348357
self.jsonl = "\n".join(self.json_lines)
349358
self.jsonl_with_dupes = "\n".join(self.json_lines + self.json_dupes)
@@ -557,7 +566,7 @@ def test_import_jsonl_update_reports_duplicate_db_records(self):
557566
self.assertEqual(import_result, import_expected)
558567

559568
def test_import_jsonl_update_reports_duplicate_natural_keys_in_upload_file(self):
560-
mocks = ZGWExportImportMockData()
569+
mocks = ZGWExportImportMockData(with_dupes=True)
561570

562571
self.storage.save("import.jsonl", io.StringIO(self.jsonl_with_dupes))
563572

@@ -574,12 +583,13 @@ def test_import_jsonl_update_reports_duplicate_natural_keys_in_upload_file(self)
574583
)
575584
import_expected = dataclasses.asdict(
576585
ZGWConfigImport(
577-
total_rows_processed=6,
586+
total_rows_processed=7,
578587
catalogus_configs_imported=1,
579588
zaaktype_configs_imported=1,
580589
zaak_informatie_object_type_configs_imported=1,
581590
zaak_status_type_configs_imported=1,
582-
zaak_resultaat_type_configs_imported=1,
591+
# resultaat_type_config with "status omschrijving" should be imported
592+
zaak_resultaat_type_configs_imported=2,
583593
import_errors=[expected_error],
584594
),
585595
)

0 commit comments

Comments
 (0)