Skip to content

Commit

Permalink
✨ [#69] added filter set to categorierelaties endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Jan 23, 2024
1 parent e4f6a46 commit e0a04c8
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from openklant.components.klantinteracties.api.serializers.partijen import (
PartijSerializer,
)
from openklant.components.klantinteracties.models.partijen import Partij
from openklant.components.klantinteracties.models.partijen import (
CategorieRelatie,
Partij,
)
from openklant.components.utils.filters import ExpandFilter


Expand Down Expand Up @@ -138,3 +141,83 @@ def filter_identificator_register(self, queryset, name, value):
)
except ValueError:
return queryset.none()


class CategorieRelatieFilterSet(FilterSet):
partij__url = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van de partij url"),
method="filter_partij_url",
)
partij__uuid = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van de partij uuid"),
method="filter_partij_uuid",
)
partij__nummer = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van het partij nummer"),
method="filter_partij_nummer",
)
categorie__naam = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van de categorie naam."),
method="filter_categorie_naam",
)
categorie__uuid = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van de categorie uuid."),
method="filter_categorie_uuid",
)
categorie__url = filters.CharFilter(
help_text=_("Zoek categorie relatie object op basis van de categorie url."),
method="filter_categorie_url",
)

class Meta:
model = CategorieRelatie
fields = (
"partij__url",
"partij__uuid",
"partij__nummer",
"categorie__url",
"categorie__uuid",
"categorie__naam",
"begin_datum",
"eind_datum",
)

def filter_partij_uuid(self, queryset, name, value):
try:
partij_uuid = uuid.UUID(value)
return queryset.filter(partij__uuid=partij_uuid)
except ValueError:
return queryset.none()

def filter_partij_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.split("/")[-1])
return queryset.filter(partij__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_partij_nummer(self, queryset, name, value):
try:
return queryset.filter(partij__nummer=value)
except ValueError:
return queryset.none()

def filter_categorie_uuid(self, queryset, name, value):
try:
categorie_uuid = uuid.UUID(value)
return queryset.filter(categorie__uuid=categorie_uuid)
except ValueError:
return queryset.none()

def filter_categorie_url(self, queryset, name, value):
try:
url_uuid = uuid.UUID(value.split("/")[-1])
return queryset.filter(categorie__uuid=url_uuid)
except ValueError:
return queryset.none()

def filter_categorie_naam(self, queryset, name, value):
try:
return queryset.filter(categorie__naam=value)
except ValueError:
return queryset.none()
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from openklant.components.klantinteracties.api.validators import (
categorie_exists,
categorie_relatie_exists,
partij_exists,
partij_identificator_exists,
partij_is_organisatie,
Expand Down Expand Up @@ -92,16 +93,18 @@ class CategorieRelatieForeignKeySerializer(serializers.HyperlinkedModelSerialize
)

class Meta:
model = Categorie
model = CategorieRelatie
fields = (
"uuid",
"url",
"categorie_naam",
"begin_datum",
"eind_datum",
)
extra_kwargs = {
"uuid": {"required": True, "validators": [categorie_exists]},
"uuid": {"required": True, "validators": [categorie_relatie_exists]},
"url": {
"view_name": "klantinteracties:categorie-detail",
"view_name": "klantinteracties:categorierelatie-detail",
"lookup_field": "uuid",
"help_text": _("De unieke URL van deze categorie binnen deze API."),
},
Expand Down Expand Up @@ -193,7 +196,9 @@ class CategorieRelatieSerializer(serializers.HyperlinkedModelSerializer):
categorie = CategorieForeignKeySerializer(
required=True,
allow_null=True,
help_text=_("De categorie waar de categorie relatie aan gelinkt is."),
help_text=_(
"De categorie waar de categorie relatie aan gelinkt is: Let op: Dit attribuut is EXPERIMENTEEL."
),
)
begin_datum = serializers.DateField(
allow_null=True,
Expand Down
194 changes: 194 additions & 0 deletions src/openklant/components/klantinteracties/api/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
KlantcontactFactory,
)
from openklant.components.klantinteracties.models.tests.factories.partijen import (
CategorieFactory,
CategorieRelatieFactory,
PartijFactory,
PartijIdentificatorFactory,
)
Expand Down Expand Up @@ -568,3 +570,195 @@ def test_filter_identificator_register(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)


class TestCategorieRelatieFilterset(APITestCase):
url = reverse("klantinteracties:categorierelatie-list")

def setUp(self):
super().setUp()
self.partij = PartijFactory.create(nummer="1111111111")
self.partij2 = PartijFactory.create(nummer="2222222222")
self.partij3 = PartijFactory.create(nummer="3333333333")
self.partij4 = PartijFactory.create(nummer="4444444444")
self.partij5 = PartijFactory.create(nummer="5555555555")

self.categorie = CategorieFactory.create(naam="een")
self.categorie2 = CategorieFactory.create(naam="twee")
self.categorie3 = CategorieFactory.create(naam="drie")
self.categorie4 = CategorieFactory.create(naam="vier")
self.categorie5 = CategorieFactory.create(naam="vijf")

self.categorie_relatie = CategorieRelatieFactory.create(
partij=self.partij, categorie=self.categorie
)
self.categorie_relatie2 = CategorieRelatieFactory.create(
partij=self.partij2, categorie=self.categorie2
)
self.categorie_relatie3 = CategorieRelatieFactory.create(
partij=self.partij3, categorie=self.categorie3
)
self.categorie_relatie4 = CategorieRelatieFactory.create(
partij=self.partij4, categorie=self.categorie4
)
self.categorie_relatie5 = CategorieRelatieFactory.create(
partij=self.partij5, categorie=self.categorie5
)

def test_filter_partij_url(self):
partij_url = f"http://testserver/klantinteracties/api/v1/partijen/{str(self.partij5.uuid)}"
response = self.client.get(
self.url,
{"partij__url": partij_url},
content_type="application/json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]

self.assertEqual(1, len(data))
self.assertEqual(str(self.categorie_relatie5.uuid), data[0]["uuid"])

with self.subTest("no_matches_found_return_nothing"):
response = self.client.get(
self.url,
{
"partij__url": f"http://testserver/klantinteracties/api/v1/partijen/{str(uuid4())}"
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

with self.subTest("invalid_value_returns_empty_query"):
response = self.client.get(self.url, {"partij__url": "ValueError"})
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

def test_filter_partij_uuid(self):
response = self.client.get(
self.url,
{"partij__uuid": str(self.partij5.uuid)},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]

self.assertEqual(1, len(data))
self.assertEqual(str(self.categorie_relatie5.uuid), data[0]["uuid"])

with self.subTest("no_matches_found_return_nothing"):
response = self.client.get(
self.url,
{"partij__uuid": str(uuid4())},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

with self.subTest("invalid_value_returns_empty_query"):
response = self.client.get(self.url, {"partij__uuid": "ValueError"})
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

def test_filter_partij_nummer(self):
response = self.client.get(
self.url,
{"partij__nummer": "5555555555"},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]

self.assertEqual(1, len(data))
self.assertEqual(str(self.categorie_relatie5.uuid), data[0]["uuid"])

with self.subTest("no_matches_found_return_nothing"):
response = self.client.get(
self.url,
{"partij__nummer": "8584395394"},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

def test_filter_categorie_url(self):
categorie_url = f"http://testserver/klantinteracties/api/v1/categorieen/{str(self.categorie5.uuid)}"
response = self.client.get(
self.url,
{"categorie__url": categorie_url},
content_type="application/json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]

self.assertEqual(1, len(data))
self.assertEqual(str(self.categorie_relatie5.uuid), data[0]["uuid"])

with self.subTest("no_matches_found_return_nothing"):
response = self.client.get(
self.url,
{
"categorie__url": f"http://testserver/klantinteracties/api/v1/categorieen/{str(uuid4())}"
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

with self.subTest("invalid_value_returns_empty_query"):
response = self.client.get(self.url, {"categorie__url": "ValueError"})
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

def test_filter_categorie_uuid(self):
response = self.client.get(
self.url,
{"categorie__uuid": str(self.categorie5.uuid)},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]

self.assertEqual(1, len(data))
self.assertEqual(str(self.categorie_relatie5.uuid), data[0]["uuid"])

with self.subTest("no_matches_found_return_nothing"):
response = self.client.get(
self.url,
{"categorie__uuid": str(uuid4())},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

with self.subTest("invalid_value_returns_empty_query"):
response = self.client.get(self.url, {"categorie__uuid": "ValueError"})
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)

def test_filter_categorie_naam(self):
response = self.client.get(
self.url,
{"categorie__naam": "vijf"},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()["results"]

self.assertEqual(1, len(data))
self.assertEqual(str(self.categorie_relatie5.uuid), data[0]["uuid"])

with self.subTest("no_matches_found_return_nothing"):
response = self.client.get(
self.url,
{"categorie__naam": "zes"},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json()["count"], 0)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
actor_is_valid_instance,
betrokkene_exists,
bijlage_exists,
categorie_exists,
categorie_relatie_exists,
contactpersoon_exists,
digitaal_adres_exists,
internetaak_exists,
Expand Down Expand Up @@ -36,6 +38,8 @@
OnderwerpobjectFactory,
)
from openklant.components.klantinteracties.models.tests.factories.partijen import (
CategorieFactory,
CategorieRelatieFactory,
ContactpersoonFactory,
OrganisatieFactory,
PartijFactory,
Expand Down Expand Up @@ -78,6 +82,22 @@ def test_bijlage(self):
with self.assertRaises(serializers.ValidationError):
bijlage_exists(str(uuid4()))

def test_categorie(self):
categorie = CategorieFactory.create()
with self.subTest("exists"):
self.assertIsNone(categorie_exists(categorie.uuid))
with self.subTest("doesn't_exist"):
with self.assertRaises(serializers.ValidationError):
categorie_exists(str(uuid4()))

def test_categorie_relatie(self):
categorie_relatie = CategorieRelatieFactory.create()
with self.subTest("exists"):
self.assertIsNone(categorie_relatie_exists(categorie_relatie.uuid))
with self.subTest("doesn't_exist"):
with self.assertRaises(serializers.ValidationError):
categorie_relatie_exists(str(uuid4()))

def test_contactpersoon(self):
contactpersoon = ContactpersoonFactory.create()
with self.subTest("exists"):
Expand Down
Loading

0 comments on commit e0a04c8

Please sign in to comment.