Skip to content

Commit

Permalink
[#310] Add validate_bag_id + migrations
Browse files Browse the repository at this point in the history
[#310] Update migrations

[#310] Create tests

[#310] Fix old tests
  • Loading branch information
danielmursa-dev committed Jan 30, 2025
1 parent 5639fa0 commit 1a21eb9
Show file tree
Hide file tree
Showing 16 changed files with 716 additions and 134 deletions.
44 changes: 22 additions & 22 deletions src/openklant/components/contactgegevens/api/tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_persoon_detail(self):
geslacht="m",
voorvoegsel="",
voornamen="John",
adres_nummeraanduiding_id="nummeraanduiding_id",
adres_nummeraanduiding_id="1234567890000001",
adres_adresregel1="adresregel1",
adres_adresregel2="adresregel2",
adres_adresregel3="adresregel3",
Expand All @@ -30,7 +30,7 @@ def test_persoon_detail(self):
)

expected_adres = {
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_create_persoon(self):
"geslacht": "m",
"voornamen": "Devin",
"adres": {
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -88,7 +88,7 @@ def test_create_persoon(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -105,7 +105,7 @@ def test_update_persoon(self):
geslacht="m",
voorvoegsel="",
voornamen="Devin",
adres_nummeraanduiding_id="nummeraanduiding_id",
adres_nummeraanduiding_id="1234567890000001",
adres_adresregel1="adresregel1",
adres_adresregel2="adresregel2",
adres_adresregel3="adresregel3",
Expand All @@ -132,7 +132,7 @@ def test_update_persoon(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -149,7 +149,7 @@ def test_update_persoon(self):
"voorvoegsel": "changed",
"voornamen": "changed",
"adres": {
"nummeraanduidingId": "changed",
"nummeraanduidingId": "1234567890000002",
"adresregel1": "changed",
"adresregel2": "changed",
"adresregel3": "changed",
Expand All @@ -169,7 +169,7 @@ def test_update_persoon(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "changed",
"nummeraanduidingId": "1234567890000002",
"adresregel1": "changed",
"adresregel2": "changed",
"adresregel3": "changed",
Expand All @@ -186,7 +186,7 @@ def test_update_partial_persoon(self):
geslacht="m",
voorvoegsel="",
voornamen="Devin",
adres_nummeraanduiding_id="nummeraanduiding_id",
adres_nummeraanduiding_id="1234567890000001",
adres_adresregel1="adresregel1",
adres_adresregel2="adresregel2",
adres_adresregel3="adresregel3",
Expand All @@ -213,7 +213,7 @@ def test_update_partial_persoon(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -237,7 +237,7 @@ def test_update_partial_persoon(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_organisatie_detail(self):
handelsnaam="Devin Townsend",
oprichtingsdatum="1980-02-23",
opheffingsdatum="2020-09-05",
adres_nummeraanduiding_id="nummeraanduiding_id",
adres_nummeraanduiding_id="1234567890000001",
adres_adresregel1="adresregel1",
adres_adresregel2="adresregel2",
adres_adresregel3="adresregel3",
Expand All @@ -279,7 +279,7 @@ def test_organisatie_detail(self):
)

expected_adres = {
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_create_organisatie(self):
"handelsnaam": "Devin Townsend",
"oprichtingsdatum": "1996-03-12",
"adres": {
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -329,7 +329,7 @@ def test_create_organisatie(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -343,7 +343,7 @@ def test_update_organisatie(self):
handelsnaam="Devin Townsend",
oprichtingsdatum="1996-03-12",
opheffingsdatum=None,
adres_nummeraanduiding_id="nummeraanduiding_id",
adres_nummeraanduiding_id="1234567890000001",
adres_adresregel1="adresregel1",
adres_adresregel2="adresregel2",
adres_adresregel3="adresregel3",
Expand All @@ -367,7 +367,7 @@ def test_update_organisatie(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -381,7 +381,7 @@ def test_update_organisatie(self):
"oprichtingsdatum": "1996-03-13",
"opheffingsdatum": "2023-11-22",
"adres": {
"nummeraanduidingId": "changed",
"nummeraanduidingId": "1234567890000002",
"adresregel1": "changed",
"adresregel2": "changed",
"adresregel3": "changed",
Expand All @@ -397,7 +397,7 @@ def test_update_organisatie(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "changed",
"nummeraanduidingId": "1234567890000002",
"adresregel1": "changed",
"adresregel2": "changed",
"adresregel3": "changed",
Expand All @@ -411,7 +411,7 @@ def test_update_partial_organisatie(self):
handelsnaam="Devin Townsend",
oprichtingsdatum="1996-03-12",
opheffingsdatum=None,
adres_nummeraanduiding_id="nummeraanduiding_id",
adres_nummeraanduiding_id="1234567890000001",
adres_adresregel1="adresregel1",
adres_adresregel2="adresregel2",
adres_adresregel3="adresregel3",
Expand All @@ -435,7 +435,7 @@ def test_update_partial_organisatie(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand All @@ -457,7 +457,7 @@ def test_update_partial_organisatie(self):
self.assertEqual(
data["adres"],
{
"nummeraanduidingId": "nummeraanduiding_id",
"nummeraanduidingId": "1234567890000001",
"adresregel1": "adresregel1",
"adresregel2": "adresregel2",
"adresregel3": "adresregel3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Generated by Django 4.2.17 on 2025-01-28 09:10

import logging
import openklant.utils.validators
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.db import migrations, models
from openklant.utils.validators import validate_bag_id

logger = logging.getLogger(__name__)


def _check_records(records):
total_failed_records = 0
for record in records:
if record.adres_nummeraanduiding_id:
try:
validate_bag_id(record.adres_nummeraanduiding_id)
except ValidationError:
logger.warning(
"%s(pk=%s, uuid=%s) Field: 'adres_nummeraanduiding_id'. Invalid BAG ID: %s",
type(record).__qualname__,
record.pk,
record.uuid,
record.adres_nummeraanduiding_id,
)
total_failed_records += 1
return total_failed_records


def _check_records_field_length(apps, schema_editor):
Organisatie = apps.get_model("contactgegevens", "Organisatie")
Persoon = apps.get_model("contactgegevens", "Persoon")

for model in [Organisatie, Persoon]:
records = model.objects.all()
if total_failed_records := _check_records(records):
raise IntegrityError(
"The migration cannot proceed due to %s records that don't comply with the %s model's requirements. "
"Possible data inconsistency or mapping error."
% (total_failed_records, model.__qualname__)
)


class Migration(migrations.Migration):

dependencies = [
(
"contactgegevens",
"0004_alter_organisatie_adres_land_alter_organisatie_land_and_more",
),
]

operations = [
migrations.RunPython(
code=_check_records_field_length,
reverse_code=migrations.RunPython.noop,
),
migrations.AlterField(
model_name="organisatie",
name="adres_nummeraanduiding_id",
field=models.CharField(
blank=True,
help_text="Identificatie van het adres bij de Basisregistratie Adressen en Gebouwen.",
max_length=16,
validators=[
openklant.utils.validators.CustomRegexValidator(
message="Ongeldige nummeraanduiding BAG-ID", regex="^[0-9]{16}$"
)
],
verbose_name="nummeraanduiding ID",
),
),
migrations.AlterField(
model_name="persoon",
name="adres_nummeraanduiding_id",
field=models.CharField(
blank=True,
help_text="Identificatie van het adres bij de Basisregistratie Adressen en Gebouwen.",
max_length=16,
validators=[
openklant.utils.validators.CustomRegexValidator(
message="Ongeldige nummeraanduiding BAG-ID", regex="^[0-9]{16}$"
)
],
verbose_name="nummeraanduiding ID",
),
),
]
5 changes: 3 additions & 2 deletions src/openklant/components/contactgegevens/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from vng_api_common.descriptors import GegevensGroepType

from openklant.utils.validators import validate_country
from openklant.utils.validators import validate_bag_id, validate_country


class AdresMixin(models.Model):
Expand All @@ -13,7 +13,8 @@ class AdresMixin(models.Model):
help_text=_(
"Identificatie van het adres bij de Basisregistratie Adressen en Gebouwen."
),
max_length=255,
max_length=16,
validators=[validate_bag_id],
blank=True,
)
adres_adresregel1 = models.CharField(
Expand Down
6 changes: 4 additions & 2 deletions src/openklant/components/contactgegevens/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ components:
type: string
description: Identificatie van het adres bij de Basisregistratie Adressen
en Gebouwen.
maxLength: 255
pattern: ^[0-9]{16}$
maxLength: 16
adresregel1:
type: string
description: Eerste deel van het adres dat niet voorkomt in de Basisregistratie
Expand Down Expand Up @@ -642,7 +643,8 @@ components:
type: string
description: Identificatie van het adres bij de Basisregistratie Adressen
en Gebouwen.
maxLength: 255
pattern: ^[0-9]{16}$
maxLength: 16
adresregel1:
type: string
description: Eerste deel van het adres dat niet voorkomt in de Basisregistratie
Expand Down
Loading

0 comments on commit 1a21eb9

Please sign in to comment.