5
5
from django .core .files .storage .memory import InMemoryStorage
6
6
from django .test import TestCase
7
7
8
- from open_inwoner .openzaak .import_export import CatalogusConfigImport , ZGWConfigExport
8
+ from open_inwoner .openzaak .import_export import ZGWConfigExport , ZGWConfigImport
9
9
from open_inwoner .openzaak .models import (
10
10
CatalogusConfig ,
11
11
ZaakTypeConfig ,
@@ -342,20 +342,24 @@ def setUp(self):
342
342
'{"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": ""}}' ,
343
343
'{"model": "openzaak.zaaktyperesultaattypeconfig", "fields": {"zaaktype_config": ["ztc-id-a-0", "DM-0", "123456789"], "resultaattype_url": "https://bar.maykinmedia.nl", "omschrijving": "resultaat", "zaaktype_uuids": "[]", "description": "description new"}}' ,
344
344
]
345
+ self .json_dupes = [
346
+ '{"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": ""}}' ,
347
+ ]
345
348
self .jsonl = "\n " .join (self .json_lines )
349
+ self .jsonl_with_dupes = "\n " .join (self .json_lines + self .json_dupes )
346
350
347
351
def test_import_jsonl_update_success (self ):
348
352
mocks = ZGWExportImportMockData ()
349
353
self .storage .save ("import.jsonl" , io .StringIO (self .jsonl ))
350
354
351
- import_result = CatalogusConfigImport .import_from_jsonl_file_in_django_storage (
355
+ import_result = ZGWConfigImport .import_from_jsonl_file_in_django_storage (
352
356
"import.jsonl" , self .storage
353
357
)
354
358
355
359
# check import
356
360
self .assertEqual (
357
361
import_result ,
358
- CatalogusConfigImport (
362
+ ZGWConfigImport (
359
363
total_rows_processed = 5 ,
360
364
catalogus_configs_imported = 1 ,
361
365
zaaktype_configs_imported = 1 ,
@@ -435,16 +439,16 @@ def test_import_jsonl_missing_statustype_config(self):
435
439
# we use `asdict` and replace the Exceptions with string representations
436
440
# because for Exceptions raised from within dataclasses, equality ==/is identity
437
441
import_result = dataclasses .asdict (
438
- CatalogusConfigImport .import_from_jsonl_file_in_django_storage (
442
+ ZGWConfigImport .import_from_jsonl_file_in_django_storage (
439
443
"import.jsonl" , self .storage
440
444
)
441
445
)
442
446
expected_error = ZGWImportError (
443
- "ZaakTypeStatusTypeConfig not found in target environment: omschrijving = bogus, "
444
- "ZaakTypeConfig identificatie = ztc-id-a-0, Catalogus domein = DM-0, Catalogus rsin = 123456789 "
447
+ "ZaakTypeStatusTypeConfig not found in target environment: omschrijving = ' bogus' , "
448
+ "ZaakTypeConfig identificatie = ' ztc-id-a-0' "
445
449
)
446
450
import_expected = dataclasses .asdict (
447
- CatalogusConfigImport (
451
+ ZGWConfigImport (
448
452
total_rows_processed = 6 ,
449
453
catalogus_configs_imported = 1 ,
450
454
zaaktype_configs_imported = 1 ,
@@ -481,16 +485,16 @@ def test_import_jsonl_update_statustype_config_missing_zt_config(self):
481
485
# we use `asdict` and replace the Exceptions with string representations
482
486
# because for Exceptions raised from within dataclasses, equality ==/is identity
483
487
import_result = dataclasses .asdict (
484
- CatalogusConfigImport .import_from_jsonl_file_in_django_storage (
488
+ ZGWConfigImport .import_from_jsonl_file_in_django_storage (
485
489
"import.jsonl" , self .storage
486
490
)
487
491
)
488
492
expected_error = ZGWImportError (
489
- "ZaakTypeStatusTypeConfig not found in target environment: omschrijving = status omschrijving, "
490
- "ZaakTypeConfig identificatie = bogus, Catalogus domein = DM-1, Catalogus rsin = 666666666 "
493
+ "ZaakTypeStatusTypeConfig not found in target environment: omschrijving = ' status omschrijving' , "
494
+ "ZaakTypeConfig identificatie = ' bogus' "
491
495
)
492
496
import_expected = dataclasses .asdict (
493
- CatalogusConfigImport (
497
+ ZGWConfigImport (
494
498
total_rows_processed = 6 ,
495
499
catalogus_configs_imported = 1 ,
496
500
zaaktype_configs_imported = 1 ,
@@ -513,7 +517,7 @@ def test_import_jsonl_update_statustype_config_missing_zt_config(self):
513
517
self .assertEqual (ZaakTypeStatusTypeConfig .objects .count (), 1 )
514
518
self .assertEqual (ZaakTypeResultaatTypeConfig .objects .count (), 1 )
515
519
516
- def test_import_jsonl_update_reports_duplicates (self ):
520
+ def test_import_jsonl_update_reports_duplicate_db_records (self ):
517
521
mocks = ZGWExportImportMockData ()
518
522
519
523
ZaakTypeResultaatTypeConfigFactory (
@@ -527,16 +531,16 @@ def test_import_jsonl_update_reports_duplicates(self):
527
531
# we use `asdict` and replace the Exceptions with string representations
528
532
# because for Exceptions raised from within dataclasses, equality ==/is identity
529
533
import_result = dataclasses .asdict (
530
- CatalogusConfigImport .import_from_jsonl_file_in_django_storage (
534
+ ZGWConfigImport .import_from_jsonl_file_in_django_storage (
531
535
"import.jsonl" , self .storage
532
536
)
533
537
)
534
538
expected_error = ZGWImportError (
535
- "Got multiple results for ZaakTypeResultaatTypeConfig: omschrijving = resultaat, "
536
- "ZaakTypeConfig identificatie = ztc-id-a-0, Catalogus domein = DM-0, Catalogus rsin = 123456789 "
539
+ "Got multiple results for ZaakTypeResultaatTypeConfig: omschrijving = ' resultaat' , "
540
+ "ZaakTypeConfig identificatie = ' ztc-id-a-0' "
537
541
)
538
542
import_expected = dataclasses .asdict (
539
- CatalogusConfigImport (
543
+ ZGWConfigImport (
540
544
total_rows_processed = 5 ,
541
545
catalogus_configs_imported = 1 ,
542
546
zaaktype_configs_imported = 1 ,
@@ -552,6 +556,39 @@ def test_import_jsonl_update_reports_duplicates(self):
552
556
# check import
553
557
self .assertEqual (import_result , import_expected )
554
558
559
+ def test_import_jsonl_update_reports_duplicate_natural_keys_in_upload_file (self ):
560
+ mocks = ZGWExportImportMockData ()
561
+
562
+ self .storage .save ("import.jsonl" , io .StringIO (self .jsonl_with_dupes ))
563
+
564
+ # we use `asdict` and replace the Exceptions with string representations
565
+ # because for Exceptions raised from within dataclasses, equality ==/is identity
566
+ import_result = dataclasses .asdict (
567
+ ZGWConfigImport .import_from_jsonl_file_in_django_storage (
568
+ "import.jsonl" , self .storage
569
+ )
570
+ )
571
+ expected_error = ZGWImportError (
572
+ "ZaakTypeStatusTypeConfig was processed multiple times because it contains duplicate "
573
+ "natural keys: omschrijving = 'status omschrijving', ZaakTypeConfig identificatie = 'ztc-id-a-0'"
574
+ )
575
+ import_expected = dataclasses .asdict (
576
+ ZGWConfigImport (
577
+ total_rows_processed = 6 ,
578
+ catalogus_configs_imported = 1 ,
579
+ zaaktype_configs_imported = 1 ,
580
+ zaak_informatie_object_type_configs_imported = 1 ,
581
+ zaak_status_type_configs_imported = 1 ,
582
+ zaak_resultaat_type_configs_imported = 1 ,
583
+ import_errors = [expected_error ],
584
+ ),
585
+ )
586
+ import_result ["import_errors" ][0 ] = str (import_result ["import_errors" ][0 ])
587
+ import_expected ["import_errors" ][0 ] = str (import_expected ["import_errors" ][0 ])
588
+
589
+ # check import
590
+ self .assertEqual (import_result , import_expected )
591
+
555
592
def test_import_jsonl_fails_with_catalogus_domein_rsin_mismatch (self ):
556
593
service = ServiceFactory (slug = "service-0" )
557
594
CatalogusConfigFactory (
@@ -569,18 +606,16 @@ def test_import_jsonl_fails_with_catalogus_domein_rsin_mismatch(self):
569
606
with self .assertLogs (
570
607
logger = "open_inwoner.openzaak.import_export" , level = "ERROR"
571
608
) as cm :
572
- import_result = CatalogusConfigImport .from_jsonl_stream_or_string (
573
- import_line
574
- )
609
+ import_result = ZGWConfigImport .from_jsonl_stream_or_string (import_line )
575
610
self .assertEqual (
576
611
cm .output ,
577
612
[
578
613
# error from trying to load existing CatalogusConfig
579
614
"ERROR:open_inwoner.openzaak.import_export:"
580
- "CatalogusConfig not found in target environment: Domein = BAR, Rsin = 987654321" ,
615
+ "CatalogusConfig not found in target environment: Domein = ' BAR' , Rsin = ' 987654321' " ,
581
616
# error from deserializing nested ZGW objects
582
617
"ERROR:open_inwoner.openzaak.import_export:"
583
- "ZaakTypeConfig not found in target environment: Identificatie = ztc-id-a-0, Catalogus domein = DM-0, Catalogus rsin = 123456789" ,
618
+ "ZaakTypeConfig not found in target environment: Identificatie = ' ztc-id-a-0' , Catalogus domein = ' DM-0' , Catalogus rsin = ' 123456789' " ,
584
619
],
585
620
)
586
621
@@ -598,7 +633,7 @@ def test_import_jsonl_fails_with_catalogus_domein_rsin_mismatch(self):
598
633
def test_bad_import_types (self ):
599
634
for bad_type in (set (), list (), b"" ):
600
635
with self .assertRaises (ValueError ):
601
- CatalogusConfigImport .from_jsonl_stream_or_string (bad_type )
636
+ ZGWConfigImport .from_jsonl_stream_or_string (bad_type )
602
637
603
638
def test_valid_input_types_are_accepted (self ):
604
639
ZGWExportImportMockData ()
@@ -609,10 +644,10 @@ def test_valid_input_types_are_accepted(self):
609
644
self .jsonl ,
610
645
):
611
646
with self .subTest (f"Input type { type (input )} " ):
612
- import_result = CatalogusConfigImport .from_jsonl_stream_or_string (input )
647
+ import_result = ZGWConfigImport .from_jsonl_stream_or_string (input )
613
648
self .assertEqual (
614
649
import_result ,
615
- CatalogusConfigImport (
650
+ ZGWConfigImport (
616
651
total_rows_processed = 5 ,
617
652
catalogus_configs_imported = 1 ,
618
653
zaaktype_configs_imported = 1 ,
@@ -628,9 +663,7 @@ def test_import_is_atomic(self):
628
663
bad_jsonl = self .jsonl + "\n " + bad_line
629
664
630
665
with self .assertRaises (KeyError ):
631
- CatalogusConfigImport .from_jsonl_stream_or_string (
632
- stream_or_string = bad_jsonl
633
- )
666
+ ZGWConfigImport .from_jsonl_stream_or_string (stream_or_string = bad_jsonl )
634
667
635
668
counts = (
636
669
CatalogusConfig .objects .count (),
@@ -654,8 +687,6 @@ def setUp(self):
654
687
655
688
def test_exports_can_be_imported (self ):
656
689
export = ZGWConfigExport .from_catalogus_configs (CatalogusConfig .objects .all ())
657
- import_result = CatalogusConfigImport .from_jsonl_stream_or_string (
658
- export .as_jsonl ()
659
- )
690
+ import_result = ZGWConfigImport .from_jsonl_stream_or_string (export .as_jsonl ())
660
691
661
692
self .assertEqual (import_result .total_rows_processed , 5 )
0 commit comments