Skip to content

Commit 4db8d67

Browse files
[#267] Created PartijIdentificatorUniquenessValidatorTests
1 parent 0a8c10b commit 4db8d67

8 files changed

+620
-197
lines changed

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,16 @@ class PartijIdentificatorGroepTypeSerializer(GegevensGroepSerializer):
365365
class Meta:
366366
model = PartijIdentificator
367367
gegevensgroep = "partij_identificator"
368-
validators = []
368+
extra_kwargs = {
369+
"code_register": {"required": True},
370+
"code_objecttype": {"required": True},
371+
"code_soort_object_id": {"required": True},
372+
"object_id": {"required": True},
373+
}
374+
375+
def validate(self, attrs):
376+
PartijIdentificatorTypesValidator(partij_identificator=attrs).validate()
377+
return super().validate(attrs)
369378

370379

371380
class PartijIdentificatorSerializer(
@@ -414,13 +423,12 @@ def validate(self, attrs):
414423
instance = getattr(self, "instance", None)
415424
partij_identificator = get_field_value(self, attrs, "partij_identificator")
416425
sub_identificator_van = get_field_value(self, attrs, "sub_identificator_van")
417-
if sub_identificator_van:
426+
partij = get_field_value(self, attrs, "partij")
427+
if "sub_identificator_van" in attrs and sub_identificator_van:
418428
sub_identificator_van = PartijIdentificator.objects.get(
419429
uuid=sub_identificator_van["uuid"]
420430
)
421-
PartijIdentificatorTypesValidator(
422-
partij_identificator=partij_identificator
423-
).validate()
431+
424432
PartijIdentificatorUniquenessValidator(
425433
queryset=(
426434
PartijIdentificator.objects.exclude(pk=instance.pk)
@@ -437,7 +445,12 @@ def validate(self, attrs):
437445
def update(self, instance, validated_data):
438446
if partij := validated_data.get("partij", None):
439447
validated_data["partij"] = Partij.objects.get(uuid=partij["uuid"])
440-
448+
# TODO FAI QUESTO test
449+
breakpoint()
450+
if sub_identificator_van := validated_data.get("sub_identificator_van", None):
451+
validated_data["sub_identificator_van"] = PartijIdentificator.objects.get(
452+
uuid=sub_identificator_van["uuid"]
453+
)
441454
return super().update(instance, validated_data)
442455

443456
@transaction.atomic

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

+411-13
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 4.2.17 on 2025-02-20 09:51
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
(
11+
"klantinteracties",
12+
"0027_alter_betrokkene_bezoekadres_nummeraanduiding_id_and_more",
13+
),
14+
]
15+
16+
operations = [
17+
migrations.AddField(
18+
model_name="partijidentificator",
19+
name="sub_identificator_van",
20+
field=models.ForeignKey(
21+
blank=True,
22+
help_text="Expresses that one PartijIdentificator is bound to another one",
23+
null=True,
24+
on_delete=django.db.models.deletion.SET_NULL,
25+
related_name="parent_partij_identificator",
26+
to="klantinteracties.partijidentificator",
27+
verbose_name="sub identificator van",
28+
),
29+
),
30+
]

src/openklant/components/klantinteracties/migrations/0028_partijidentificator_sub_identificator_van_and_more.py

-66
This file was deleted.

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

-30
Original file line numberDiff line numberDiff line change
@@ -414,36 +414,6 @@ class PartijIdentificator(models.Model):
414414
class Meta:
415415
verbose_name = _("partij identificator")
416416
verbose_name_plural = _("partij identificatoren")
417-
constraints = [
418-
# TODO: cahnge msg
419-
models.UniqueConstraint(
420-
fields=[
421-
"sub_identificator_van",
422-
"partij_identificator_code_objecttype",
423-
"partij_identificator_code_soort_object_id",
424-
"partij_identificator_object_id",
425-
"partij_identificator_code_register",
426-
],
427-
condition=models.Q(sub_identificator_van__isnull=False),
428-
name="scoped_identificator_globally_unique",
429-
),
430-
# TODO: cahnge msg
431-
models.UniqueConstraint(
432-
fields=[
433-
"partij_identificator_code_objecttype",
434-
"partij_identificator_code_soort_object_id",
435-
"partij_identificator_object_id",
436-
"partij_identificator_code_register",
437-
],
438-
name="non_scoped_identificator_globally_unique",
439-
condition=models.Q(sub_identificator_van__isnull=True),
440-
),
441-
# TODO: cahnge msg
442-
models.CheckConstraint(
443-
check=~models.Q(sub_identificator_van=models.F("id")),
444-
name="check_sub_identificator_van_not_self",
445-
),
446-
]
447417

448418
def __str__(self):
449419
soort_object = self.partij_identificator_code_soort_object_id

src/openklant/components/klantinteracties/models/tests/test_partij_identificator.py

-78
This file was deleted.

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, queryset, partij_identificator, sub_identificator_van, instan
2727
def check(self):
2828
self.check_self_relation()
2929
self.check_sub_identificator_van()
30+
self.check_identificator_partij_unique()
3031
self.check_identificator_globally_unique()
3132

3233
def check_self_relation(self):
@@ -92,6 +93,17 @@ def check_sub_identificator_van(self):
9293
}
9394
)
9495

96+
def check_identificator_partij_unique(self):
97+
return
98+
if self.queryset.filter(**filters).exists():
99+
raise ValidationError(
100+
{
101+
"__all__": _(
102+
"`PartijIdentificator` moet uniek zijn, er bestaat er al een met deze gegevenscombinatie."
103+
)
104+
}
105+
)
106+
95107
def check_identificator_globally_unique(self):
96108
"""
97109
Checks models constraints
@@ -168,10 +180,10 @@ class PartijIdentificatorTypesValidator:
168180

169181
def __init__(self, partij_identificator: dict) -> None:
170182
"""Initialize validator"""
171-
self.code_register = partij_identificator.get("code_register", "")
172-
self.code_objecttype = partij_identificator.get("code_objecttype", "")
173-
self.code_soort_object_id = partij_identificator.get("code_soort_object_id", "")
174-
self.object_id = partij_identificator.get("object_id", "")
183+
self.code_register = partij_identificator["code_register"]
184+
self.code_objecttype = partij_identificator["code_objecttype"]
185+
self.code_soort_object_id = partij_identificator["code_soort_object_id"]
186+
self.object_id = partij_identificator["object_id"]
175187

176188
def validate(self) -> None:
177189
"""Run all validations"""

0 commit comments

Comments
 (0)