Skip to content

Commit 8460c14

Browse files
[#239] Fixed test
1 parent 3e151a4 commit 8460c14

File tree

3 files changed

+266
-11
lines changed

3 files changed

+266
-11
lines changed

src/openklant/components/klantinteracties/api/serializers/partijen.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def validate(self, attrs):
444444

445445
return super().validate(attrs)
446446

447-
def assert_partij(self, partij):
447+
def validate_partij(self, partij):
448448
if not partij:
449449
raise serializers.ValidationError(
450450
{"identificeerdePartij": _("Dit veld is vereist.")},
@@ -454,13 +454,13 @@ def assert_partij(self, partij):
454454
@handle_db_exceptions
455455
@transaction.atomic
456456
def update(self, instance, validated_data):
457-
self.assert_partij(validated_data["partij"])
457+
self.validate_partij(validated_data["partij"])
458458
return super().update(instance, validated_data)
459459

460460
@handle_db_exceptions
461461
@transaction.atomic
462462
def create(self, validated_data):
463-
self.assert_partij(validated_data["partij"])
463+
self.validate_partij(validated_data["partij"])
464464
return super().create(validated_data)
465465

466466

@@ -634,6 +634,11 @@ def validate_partij_identificatoren(self, attrs):
634634
return attrs
635635

636636
def update_or_create_partij_identificator(self, partij_identificator):
637+
sub_identificator_van = partij_identificator["sub_identificator_van"]
638+
if isinstance(sub_identificator_van, PartijIdentificator):
639+
partij_identificator["sub_identificator_van"] = {
640+
"uuid": sub_identificator_van.uuid
641+
}
637642
partij_identificator_serializer = PartijIdentificatorSerializer(
638643
data=partij_identificator
639644
)

src/openklant/components/klantinteracties/api/tests/test_partijen.py

+256-1
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,260 @@ def test_invalid_create_where_partij_is_selected(self):
25032503

25042504
self.assertEqual(Partij.objects.all().count(), 1)
25052505

2506+
def test_partially_update_with_null_values(self):
2507+
partij = PartijFactory.create(
2508+
nummer="1298329191",
2509+
interne_notitie="interneNotitie",
2510+
voorkeurs_digitaal_adres=None,
2511+
voorkeurs_rekeningnummer=None,
2512+
indicatie_geheimhouding=True,
2513+
voorkeurstaal="ndl",
2514+
indicatie_actief=True,
2515+
soort_partij="persoon",
2516+
)
2517+
2518+
detail_url = reverse(
2519+
"klantinteracties:partij-detail", kwargs={"uuid": str(partij.uuid)}
2520+
)
2521+
2522+
with self.subTest("partij_identificatoren_not_specified"):
2523+
data = {"soortPartij": "organisatie"}
2524+
self.assertEqual(partij.soort_partij, "persoon")
2525+
self.assertEqual(partij.partijidentificator_set.count(), 0)
2526+
response = self.client.patch(detail_url, data)
2527+
response_data = response.json()
2528+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2529+
self.assertEqual(response_data["partijIdentificatoren"], [])
2530+
self.assertEqual(response_data["soortPartij"], "organisatie")
2531+
self.assertEqual(partij.partijidentificator_set.count(), 0)
2532+
2533+
BsnPartijIdentificatorFactory.create(
2534+
partij=partij, partij_identificator_object_id="296648875"
2535+
)
2536+
# Resend update request
2537+
# No changes to the partij_identificator because the value wasn't specified in PATCH
2538+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2539+
response = self.client.patch(detail_url, data)
2540+
response_data = response.json()
2541+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2542+
2543+
partij = Partij.objects.get(uuid=partij.uuid)
2544+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2545+
self.assertEqual(len(response_data["partijIdentificatoren"]), 1)
2546+
self.assertEqual(response_data["soortPartij"], "organisatie")
2547+
self.assertEqual(partij.soort_partij, "organisatie")
2548+
2549+
with self.subTest("partij_identificatoren_null_value"):
2550+
data = {
2551+
"soortPartij": "persoon",
2552+
"partijIdentificatoren": None,
2553+
}
2554+
self.assertEqual(partij.soort_partij, "organisatie")
2555+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2556+
2557+
# No changes to the partij_identificator because the value was None in PATCH
2558+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2559+
response = self.client.patch(detail_url, data)
2560+
response_data = response.json()
2561+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2562+
2563+
partij = Partij.objects.get(uuid=partij.uuid)
2564+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2565+
self.assertEqual(len(response_data["partijIdentificatoren"]), 1)
2566+
self.assertEqual(response_data["soortPartij"], "persoon")
2567+
self.assertEqual(partij.soort_partij, "persoon")
2568+
2569+
with self.subTest("partij_identificatoren_empty_list_value"):
2570+
data = {
2571+
"soortPartij": "organisatie",
2572+
"partijIdentificatoren": [],
2573+
}
2574+
self.assertEqual(partij.soort_partij, "persoon")
2575+
self.assertEqual(partij.partijidentificator_set.count(), 1)
2576+
2577+
# Delete all partij_identificatoren because the value was [] in PATCH
2578+
response = self.client.patch(detail_url, data)
2579+
response_data = response.json()
2580+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2581+
partij = Partij.objects.get(uuid=partij.uuid)
2582+
self.assertEqual(partij.partijidentificator_set.count(), 0)
2583+
self.assertEqual(len(response_data["partijIdentificatoren"]), 0)
2584+
self.assertEqual(response_data["soortPartij"], "organisatie")
2585+
self.assertEqual(partij.soort_partij, "organisatie")
2586+
2587+
def test_partially_update_where_all_partij_identificatoren_have_uuid(self):
2588+
partij = PartijFactory.create(
2589+
nummer="1298329191",
2590+
interne_notitie="interneNotitie",
2591+
voorkeurs_digitaal_adres=None,
2592+
voorkeurs_rekeningnummer=None,
2593+
soort_partij="persoon",
2594+
indicatie_geheimhouding=True,
2595+
voorkeurstaal="ndl",
2596+
indicatie_actief=True,
2597+
)
2598+
detail_url = reverse(
2599+
"klantinteracties:partij-detail", kwargs={"uuid": str(partij.uuid)}
2600+
)
2601+
bsn = BsnPartijIdentificatorFactory.create(
2602+
partij=partij, partij_identificator_object_id="296648875"
2603+
)
2604+
kvk_nummer = KvkNummerPartijIdentificatorFactory.create(
2605+
partij=partij, partij_identificator_object_id="12345678"
2606+
)
2607+
vestigingsnummer = VestigingsnummerPartijIdentificatorFactory.create(
2608+
partij=partij, partij_identificator_object_id="111122223333"
2609+
)
2610+
2611+
# changes are only for objectId
2612+
data = {
2613+
"soortPartij": "organisatie",
2614+
"partijIdentificatoren": [
2615+
{
2616+
"uuid": str(bsn.uuid),
2617+
"partijIdentificator": {
2618+
"codeObjecttype": "natuurlijk_persoon",
2619+
"codeSoortObjectId": "bsn",
2620+
"objectId": "123456782",
2621+
"codeRegister": "brp",
2622+
},
2623+
},
2624+
{
2625+
"uuid": str(kvk_nummer.uuid),
2626+
"partijIdentificator": {
2627+
"codeObjecttype": "niet_natuurlijk_persoon",
2628+
"codeSoortObjectId": "kvk_nummer",
2629+
"objectId": "11112222",
2630+
"codeRegister": "hr",
2631+
},
2632+
},
2633+
{
2634+
"uuid": str(vestigingsnummer.uuid),
2635+
"sub_identificator_van": {"uuid": str(kvk_nummer.uuid)},
2636+
"partijIdentificator": {
2637+
"codeObjecttype": "vestiging",
2638+
"codeSoortObjectId": "vestigingsnummer",
2639+
"objectId": "444455556666",
2640+
"codeRegister": "hr",
2641+
},
2642+
},
2643+
],
2644+
}
2645+
response = self.client.patch(detail_url, data)
2646+
response_data = response.json()
2647+
2648+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2649+
partij = Partij.objects.get(pk=partij.pk)
2650+
2651+
self.assertEqual(len(response_data["partijIdentificatoren"]), 3)
2652+
self.assertEqual(response_data["soortPartij"], "organisatie")
2653+
self.assertEqual(partij.soort_partij, "organisatie")
2654+
new_bsn = partij.partijidentificator_set.get(
2655+
partij_identificator_code_soort_object_id="bsn"
2656+
)
2657+
new_kvk_nummer = partij.partijidentificator_set.get(
2658+
partij_identificator_code_soort_object_id="kvk_nummer"
2659+
)
2660+
new_vestigingsnummer = partij.partijidentificator_set.get(
2661+
partij_identificator_code_soort_object_id="vestigingsnummer"
2662+
)
2663+
# assert that they are the same objects
2664+
self.assertEqual(new_bsn.uuid, bsn.uuid)
2665+
self.assertEqual(new_kvk_nummer.uuid, kvk_nummer.uuid)
2666+
self.assertEqual(new_vestigingsnummer.uuid, vestigingsnummer.uuid)
2667+
# assert that the object_ids have been updated
2668+
self.assertEqual(new_bsn.partij_identificator_object_id, "123456782")
2669+
self.assertEqual(new_kvk_nummer.partij_identificator_object_id, "11112222")
2670+
self.assertEqual(
2671+
new_vestigingsnummer.partij_identificator_object_id, "444455556666"
2672+
)
2673+
2674+
def test_partially_update_where_not_all_partij_identificatoren_have_uuid(self):
2675+
partij = PartijFactory.create(
2676+
nummer="1298329191",
2677+
interne_notitie="interneNotitie",
2678+
voorkeurs_digitaal_adres=None,
2679+
voorkeurs_rekeningnummer=None,
2680+
soort_partij="persoon",
2681+
indicatie_geheimhouding=True,
2682+
voorkeurstaal="ndl",
2683+
indicatie_actief=True,
2684+
)
2685+
detail_url = reverse(
2686+
"klantinteracties:partij-detail", kwargs={"uuid": str(partij.uuid)}
2687+
)
2688+
bsn = BsnPartijIdentificatorFactory.create(
2689+
partij=partij, partij_identificator_object_id="296648875"
2690+
)
2691+
kvk_nummer = KvkNummerPartijIdentificatorFactory.create(
2692+
partij=partij, partij_identificator_object_id="12345678"
2693+
)
2694+
vestigingsnummer = VestigingsnummerPartijIdentificatorFactory.create(
2695+
partij=partij, partij_identificator_object_id="111122223333"
2696+
)
2697+
2698+
# changes are only for objectId
2699+
data = {
2700+
"soortPartij": "organisatie",
2701+
"partijIdentificatoren": [
2702+
{
2703+
"partijIdentificator": {
2704+
"codeObjecttype": "natuurlijk_persoon",
2705+
"codeSoortObjectId": "bsn",
2706+
"objectId": "123456782",
2707+
"codeRegister": "brp",
2708+
},
2709+
},
2710+
{
2711+
"uuid": str(kvk_nummer.uuid),
2712+
"partijIdentificator": {
2713+
"codeObjecttype": "niet_natuurlijk_persoon",
2714+
"codeSoortObjectId": "kvk_nummer",
2715+
"objectId": "11112222",
2716+
"codeRegister": "hr",
2717+
},
2718+
},
2719+
{
2720+
"uuid": str(vestigingsnummer.uuid),
2721+
"sub_identificator_van": {"uuid": str(kvk_nummer.uuid)},
2722+
"partijIdentificator": {
2723+
"codeObjecttype": "vestiging",
2724+
"codeSoortObjectId": "vestigingsnummer",
2725+
"objectId": "444455556666",
2726+
"codeRegister": "hr",
2727+
},
2728+
},
2729+
],
2730+
}
2731+
response = self.client.patch(detail_url, data)
2732+
response_data = response.json()
2733+
2734+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2735+
partij = Partij.objects.get(pk=partij.pk)
2736+
self.assertEqual(len(response_data["partijIdentificatoren"]), 3)
2737+
self.assertEqual(response_data["soortPartij"], "organisatie")
2738+
self.assertEqual(partij.soort_partij, "organisatie")
2739+
new_bsn = partij.partijidentificator_set.get(
2740+
partij_identificator_code_soort_object_id="bsn"
2741+
)
2742+
new_kvk_nummer = partij.partijidentificator_set.get(
2743+
partij_identificator_code_soort_object_id="kvk_nummer"
2744+
)
2745+
new_vestigingsnummer = partij.partijidentificator_set.get(
2746+
partij_identificator_code_soort_object_id="vestigingsnummer"
2747+
)
2748+
# assert bsn was deleted and then created again with new values
2749+
self.assertNotEqual(new_bsn.uuid, bsn.uuid)
2750+
# assert that they are the same objects
2751+
self.assertEqual(new_kvk_nummer.uuid, kvk_nummer.uuid)
2752+
self.assertEqual(new_vestigingsnummer.uuid, vestigingsnummer.uuid)
2753+
# assert that the object_ids have been updated
2754+
self.assertEqual(new_bsn.partij_identificator_object_id, "123456782")
2755+
self.assertEqual(new_kvk_nummer.partij_identificator_object_id, "11112222")
2756+
self.assertEqual(
2757+
new_vestigingsnummer.partij_identificator_object_id, "444455556666"
2758+
)
2759+
25062760

25072761
class PartijIdentificatorTests(APITestCase):
25082762
def test_list_partij_identificator(self):
@@ -2576,6 +2830,7 @@ def test_update_partij_identificator(self):
25762830
partij, partij2 = PartijFactory.create_batch(2)
25772831
partij_identificator = BsnPartijIdentificatorFactory.create(
25782832
partij=partij,
2833+
andere_partij_identificator="anderePartijIdentificator",
25792834
partij_identificator_object_id="296648875",
25802835
)
25812836

@@ -2916,7 +3171,7 @@ def test_invalid_create_partij_required(self):
29163171
def test_invalid_create_duplicate_code_soort_object_id_for_partij(self):
29173172
BsnPartijIdentificatorFactory.create(
29183173
partij=self.partij,
2919-
partij_identificator_object_id="296648875",
3174+
partij_identificator_object_id="123456782",
29203175
)
29213176

29223177
data = {

src/openklant/components/klantinteracties/openapi.yaml

+2-7
Original file line numberDiff line numberDiff line change
@@ -3065,8 +3065,8 @@ components:
30653065
partijIdentificatoren:
30663066
type: array
30673067
items:
3068-
$ref: '#/components/schemas/PartijIdentificatorForeignkey'
3069-
readOnly: true
3068+
$ref: '#/components/schemas/PartijIdentificator'
3069+
nullable: true
30703070
description: Partij-identificatoren die hoorde bij deze partij.
30713071
soortPartij:
30723072
allOf:
@@ -3107,7 +3107,6 @@ components:
31073107
- categorieRelaties
31083108
- digitaleAdressen
31093109
- indicatieActief
3110-
- partijIdentificatoren
31113110
- rekeningnummers
31123111
- soortPartij
31133112
- url
@@ -4836,7 +4835,6 @@ components:
48364835
uuid:
48374836
type: string
48384837
format: uuid
4839-
readOnly: true
48404838
description: Unieke (technische) identificatiecode van de partij-identificator.
48414839
url:
48424840
type: string
@@ -4871,10 +4869,8 @@ components:
48714869
the child identificator could specify a vestigingsnummer that is unique
48724870
for the KVK number).
48734871
required:
4874-
- identificeerdePartij
48754872
- partijIdentificator
48764873
- url
4877-
- uuid
48784874
PartijIdentificatorForeignkey:
48794875
type: object
48804876
properties:
@@ -5391,7 +5387,6 @@ components:
53915387
uuid:
53925388
type: string
53935389
format: uuid
5394-
readOnly: true
53955390
description: Unieke (technische) identificatiecode van de partij-identificator.
53965391
url:
53975392
type: string

0 commit comments

Comments
 (0)