Skip to content

Commit e117baa

Browse files
[#267] Test improvements
1 parent b075fee commit e117baa

File tree

3 files changed

+156
-18
lines changed

3 files changed

+156
-18
lines changed

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

+121-13
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,7 @@ def setUp(self):
22352235
self.partij = PartijFactory.create()
22362236
super().setUp()
22372237

2238-
def test_bsn_valid_create(self):
2238+
def test_valid_create(self):
22392239
data = {
22402240
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
22412241
"anderePartijIdentificator": "anderePartijIdentificator",
@@ -2251,8 +2251,7 @@ def test_bsn_valid_create(self):
22512251
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
22522252
self.assertEqual(PartijIdentificator.objects.all().count(), 1)
22532253

2254-
def test_bsn_valid_create_sub_identificator_van(self):
2255-
# sub_identificator_van not allowed for partij_identificator_code_soort_object_id = 'bsn'
2254+
def test_valid_create_sub_identificator_van(self):
22562255
partij_identificator = PartijIdentificatorFactory.create(
22572256
partij=self.partij,
22582257
andere_partij_identificator="anderePartijIdentificator",
@@ -2264,19 +2263,19 @@ def test_bsn_valid_create_sub_identificator_van(self):
22642263

22652264
data = {
22662265
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2266+
"sub_identificator_van": {"uuid": str(partij_identificator.uuid)},
22672267
"partijIdentificator": {
22682268
"codeObjecttype": "natuurlijk_persoon",
22692269
"codeSoortObjectId": "bsn",
22702270
"objectId": "296648875",
22712271
"codeRegister": "brp",
22722272
},
22732273
}
2274-
data["sub_identificator_van"] = {"uuid": str(partij_identificator.uuid)}
22752274
response = self.client.post(self.list_url, data)
22762275
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
22772276
self.assertEqual(PartijIdentificator.objects.all().count(), 2)
22782277

2279-
def test_bsn_invalid_create_global_unique(self):
2278+
def test_invalid_create_global_unique(self):
22802279
PartijIdentificatorFactory.create(
22812280
partij=self.partij,
22822281
partij_identificator_code_objecttype="natuurlijk_persoon",
@@ -2294,11 +2293,31 @@ def test_bsn_invalid_create_global_unique(self):
22942293
"codeRegister": "brp",
22952294
},
22962295
}
2297-
2296+
sub_identificator_van = PartijIdentificatorFactory.create(
2297+
partij=self.partij,
2298+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
2299+
partij_identificator_code_soort_object_id="kvk_nummer",
2300+
partij_identificator_object_id="12345678",
2301+
partij_identificator_code_register="hr",
2302+
)
2303+
with self.subTest("ok_subtest_global_1"):
2304+
# sub_identificator_van is set
2305+
data["sub_identificator_van"] = {"uuid": str(sub_identificator_van.uuid)}
2306+
response = self.client.post(self.list_url, data)
2307+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2308+
with self.subTest("failed_subtest_global_1"):
2309+
# combination sub_identificator_van and partij_identificator arleady exists
2310+
data["sub_identificator_van"] = {"uuid": str(sub_identificator_van.uuid)}
2311+
response = self.client.post(self.list_url, data)
2312+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2313+
self.assertEqual(response.data["code"], "invalid")
2314+
self.assertEqual(response.data["title"], "Invalid input.")
2315+
self.assertEqual(
2316+
response.data["invalid_params"][0]["reason"],
2317+
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie.",
2318+
)
22982319
with self.subTest("failed_subtest_global_2"):
2299-
# new partij, same data_values
2300-
partij = PartijFactory.create()
2301-
data["identificeerdePartij"] = {"uuid": str(partij.uuid)}
2320+
# same partij, same data_values
23022321
response = self.client.post(self.list_url, data)
23032322
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
23042323
self.assertEqual(response.data["code"], "invalid")
@@ -2319,7 +2338,7 @@ def test_bsn_invalid_create_global_unique(self):
23192338
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie.",
23202339
)
23212340

2322-
def test_bsn_valid_partial_update_unique(self):
2341+
def test_valid_update_simple_field(self):
23232342
partij_identificator = PartijIdentificatorFactory.create(
23242343
partij=self.partij,
23252344
andere_partij_identificator="anderePartijIdentificator",
@@ -2352,7 +2371,29 @@ def test_bsn_valid_partial_update_unique(self):
23522371
},
23532372
)
23542373

2355-
def test_bsn_valid_update_unique(self):
2374+
def test_valid_update_partij(self):
2375+
partij_identificator = PartijIdentificatorFactory.create(
2376+
andere_partij_identificator="anderePartijIdentificator",
2377+
partij_identificator_code_objecttype="natuurlijk_persoon",
2378+
partij_identificator_code_soort_object_id="bsn",
2379+
partij_identificator_object_id="123456782",
2380+
partij_identificator_code_register="brp",
2381+
)
2382+
2383+
data = {
2384+
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2385+
}
2386+
detail_url = reverse(
2387+
"klantinteracties:partijidentificator-detail",
2388+
kwargs={"uuid": str(partij_identificator.uuid)},
2389+
)
2390+
2391+
response = self.client.patch(detail_url, data)
2392+
self.assertEqual(response.status_code, status.HTTP_200_OK)
2393+
data = response.json()
2394+
self.assertEqual(data["identificeerdePartij"]["uuid"], str(self.partij.uuid))
2395+
2396+
def test_valid_update_unique(self):
23562397
partij_identificator = PartijIdentificatorFactory.create(
23572398
partij=self.partij,
23582399
andere_partij_identificator="anderePartijIdentificator",
@@ -2392,7 +2433,7 @@ def test_bsn_valid_update_unique(self):
23922433
},
23932434
)
23942435

2395-
def test_bsn_invalid_update_unique_already_exists(self):
2436+
def test_invalid_update_unique_already_exists(self):
23962437
partij_identificator_a = PartijIdentificatorFactory.create(
23972438
partij=self.partij,
23982439
partij_identificator_code_objecttype="natuurlijk_persoon",
@@ -2434,7 +2475,7 @@ def test_bsn_invalid_update_unique_already_exists(self):
24342475
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie.",
24352476
)
24362477

2437-
def test_bsn_invalid_update_unique_set_sub_identificator_van_self(self):
2478+
def test_invalid_update_unique_set_sub_identificator_van_self(self):
24382479
partij_identificator_a = PartijIdentificatorFactory.create(
24392480
partij=self.partij,
24402481
partij_identificator_code_objecttype="natuurlijk_persoon",
@@ -2508,6 +2549,73 @@ def test_vestigingsnummer_invalid_create_without_sub_identificator_van(self):
25082549
in response.data["invalid_params"][0]["reason"]
25092550
)
25102551

2552+
def test_vestigingsnummer_invalid_create_without_partij(self):
2553+
sub_identificator_van = PartijIdentificatorFactory.create(
2554+
partij=self.partij,
2555+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
2556+
partij_identificator_code_soort_object_id="kvk_nummer",
2557+
partij_identificator_object_id="12345678",
2558+
partij_identificator_code_register="hr",
2559+
)
2560+
2561+
data = {
2562+
"identificeerdePartij": None,
2563+
"sub_identificator_van": {"uuid": str(sub_identificator_van.uuid)},
2564+
"partijIdentificator": {
2565+
"codeObjecttype": "vestiging",
2566+
"codeSoortObjectId": "vestigingsnummer",
2567+
"objectId": "296648875154",
2568+
"codeRegister": "hr",
2569+
},
2570+
}
2571+
2572+
response = self.client.post(self.list_url, data)
2573+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2574+
self.assertEqual(response.data["code"], "invalid")
2575+
self.assertEqual(response.data["title"], "Invalid input.")
2576+
self.assertTrue(
2577+
"Het is niet mogelijk om een partij_identificator te maken zonder de partijwaartoe"
2578+
in response.data["invalid_params"][0]["reason"]
2579+
)
2580+
2581+
def test_vestigingsnummer_invalid_create_external_partij(self):
2582+
partij = PartijFactory.create()
2583+
sub_identificator_van = PartijIdentificatorFactory.create(
2584+
partij=partij,
2585+
partij_identificator_code_objecttype="niet_natuurlijk_persoon",
2586+
partij_identificator_code_soort_object_id="kvk_nummer",
2587+
partij_identificator_object_id="12345678",
2588+
partij_identificator_code_register="hr",
2589+
)
2590+
2591+
# sub_identificator_van partij is different from vestigingsnummer
2592+
data = {
2593+
"identificeerdePartij": {"uuid": str(self.partij.uuid)},
2594+
"sub_identificator_van": {"uuid": str(sub_identificator_van.uuid)},
2595+
"partijIdentificator": {
2596+
"codeObjecttype": "vestiging",
2597+
"codeSoortObjectId": "vestigingsnummer",
2598+
"objectId": "296648875154",
2599+
"codeRegister": "hr",
2600+
},
2601+
}
2602+
2603+
response = self.client.post(self.list_url, data)
2604+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
2605+
self.assertEqual(response.data["code"], "invalid")
2606+
self.assertEqual(response.data["title"], "Invalid input.")
2607+
self.assertEqual(
2608+
response.data["invalid_params"][0]["reason"],
2609+
"Je moet een `sub_identifier_van` selecteren die tot dezelfde partij behoort.",
2610+
)
2611+
2612+
# valida case sub_identificator_van partij equal vestigingsnummer partij
2613+
sub_identificator_van.partij = self.partij
2614+
sub_identificator_van.save()
2615+
response = self.client.post(self.list_url, data)
2616+
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
2617+
self.assertEqual(PartijIdentificator.objects.all().count(), 2)
2618+
25112619
def test_vestigingsnummer_invalid_create_invalid_sub_identificator_van(self):
25122620
sub_identificator_van = PartijIdentificatorFactory.create(
25132621
partij=self.partij,

src/openklant/components/klantinteracties/models/validators.py

+31-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ def validate_not_self_assigned(self):
5858

5959
def validate_sub_identificator_van_for_vestigingsnummer(self):
6060
"""
61-
- Validation that if the `sub_identificator_van` is not selected raise a error, for
62-
partij_identificator with CodeSoortObjectId = `vestigingsnummer`.
63-
64-
- Validation that when the partij_identificator has CodeSoortObjectId = `vestigingsnummer`,
65-
the `sub_identificator_van` must have CodeSoortObjectId = `kvk_nummer`.
61+
- Validation that when the partij_identificator has CodeSoortObjectId = `vestigingsnummer`:
62+
- if the `sub_identificator_van` is not selected
63+
- the `sub_identificator_van` must have CodeSoortObjectId = `kvk_nummer`.
64+
- cannot be assigned to a null Partij
65+
- cannot be assigned to a Partij that doesn't have another partij_identificator
66+
with CodeSoortObjectId = `kvk_nummer`
6667
6768
"""
6869
if not self.sub_identificator_van:
@@ -88,6 +89,31 @@ def validate_sub_identificator_van_for_vestigingsnummer(self):
8889
}
8990
)
9091

92+
if not self.partij:
93+
raise ValidationError(
94+
{
95+
"sub_identificator_van": _(
96+
"Het is niet mogelijk om een partij_identificator te maken zonder de partij"
97+
"waartoe deze behoort te specificeren."
98+
)
99+
}
100+
)
101+
102+
if (
103+
not self.queryset.filter(partij=self.partij)
104+
.filter(
105+
partij_identificator_code_soort_object_id=PartijIdentificatorCodeSoortObjectId.kvk_nummer.value
106+
)
107+
.exists()
108+
):
109+
raise ValidationError(
110+
{
111+
"sub_identificator_van": _(
112+
"Je moet een `sub_identifier_van` selecteren die tot dezelfde partij behoort."
113+
)
114+
}
115+
)
116+
91117
def validate_unique_partij_identificator_locally(self):
92118
"""
93119
Validation that a single Partij can only have a single partij_identificator_code_soort_object_id type locally

src/openklant/components/klantinteracties/tests/test_validators.py

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
PartijIdentificatorCodeSoortObjectId,
88
)
99
from openklant.components.klantinteracties.models.tests.factories.partijen import (
10+
PartijFactory,
1011
PartijIdentificatorFactory,
1112
)
1213
from openklant.components.klantinteracties.models.validators import (
@@ -384,7 +385,9 @@ def test_valid_global_uniqueness(self):
384385

385386
def test_valid_relation_sub_identificator_van(self):
386387
# check self relation and sub_identificator_van allowed cases
388+
partij = PartijFactory.create()
387389
sub_identificator_van = PartijIdentificatorFactory.create(
390+
partij=partij,
388391
partij_identificator_code_objecttype=PartijIdentificatorCodeObjectType.niet_natuurlijk_persoon.value,
389392
partij_identificator_code_soort_object_id=PartijIdentificatorCodeSoortObjectId.kvk_nummer.value,
390393
partij_identificator_object_id="12345678",
@@ -399,6 +402,7 @@ def test_valid_relation_sub_identificator_van(self):
399402
},
400403
sub_identificator_van=sub_identificator_van,
401404
instance=None,
405+
partij=partij,
402406
).validate()
403407

404408
def test_invalid_self_relation_sub_identificator_van(self):

0 commit comments

Comments
 (0)