diff --git a/backend/project/api/tests/test_serializers.py b/backend/project/api/tests/test_serializers.py index 883c348..e69e84b 100644 --- a/backend/project/api/tests/test_serializers.py +++ b/backend/project/api/tests/test_serializers.py @@ -1,7 +1,10 @@ -from django.test import TestCase +from django.test import RequestFactory, TestCase from project.accounts.tests.factories import UserFactory from project.api.serializers.accounts import AccountSerializer +from project.api.serializers.common import EndpointsSerializer, SettingsSerializer +from project.observations.models import Area, ObservationCategory +from project.observations.tests.factories import AreaFactory class AccountSerializerTestCase(TestCase): @@ -19,3 +22,79 @@ def test_password_check_success_after_change(self): user.refresh_from_db() self.assertTrue(user.check_password("new_password")) + + +class SettingsSerializerTestCase(TestCase): + def test_settings_serializer(self): + """Test settings serializer""" + self.maxDiff = None + AreaFactory.create_batch(5) + cat_root_1 = ObservationCategory.add_root(label="Root 1") + cat_root_2 = ObservationCategory.add_root(label="Root 2") + cat_root_1.add_child(label="Child 1") + cat_root_1.add_child(label="Child 2") + serializer = SettingsSerializer( + { + "areas": Area.objects.all(), + "categories": ObservationCategory.objects.filter(depth=1).order_by( + "label" + ), + "endpoints": EndpointsSerializer().data, + }, + context={"request": RequestFactory().get("/")}, + ) + data = { + "categories": [ + { + "id": cat_root_1.id, + "label": cat_root_1.label, + "description": "", + "pictogram": None, + "children": [ + { + "children": [], + "description": "", + "id": child.id, + "label": child.label, + "pictogram": None, + } + for child in cat_root_1.get_children() + ], + }, + { + "id": cat_root_2.id, + "description": "", + "label": cat_root_2.label, + "pictogram": None, + "children": [], + }, + ], + "areas": [ + { + "id": area.id, + "name": area.name, + "bbox": [ + (area.geom.extent[0], area.geom.extent[3]), + (area.geom.extent[2], area.geom.extent[1]), + ], + } + for area in Area.objects.all() + ], + "endpoints": { + "observations": "http://testserver/api/observations/", + "signup": "http://testserver/api/accounts/sign-up/", + "token": { + "token": "http://testserver/api/token/", + "token_refresh": "http://testserver/api/token/refresh/", + "token_verify": "http://testserver/api/token/verify/", + }, + "user": { + "me": "http://testserver/api/accounts/me/", + "observations": "http://testserver/api/accounts/me/observations/", + }, + }, + } + self.assertDictEqual( + dict(serializer.data), + dict(sorted(data.items())), + ) diff --git a/backend/project/observations/tests.py b/backend/project/observations/tests/__init__.py similarity index 100% rename from backend/project/observations/tests.py rename to backend/project/observations/tests/__init__.py diff --git a/backend/project/observations/tests/factories.py b/backend/project/observations/tests/factories.py new file mode 100644 index 0000000..e41a7d9 --- /dev/null +++ b/backend/project/observations/tests/factories.py @@ -0,0 +1,50 @@ +import factory.fuzzy +from django.contrib.gis.geos import Polygon +from factory import random +from faker import Faker +from faker.providers import geo + +from project.observations.models import Area, ObservationCategory + +fake = Faker() +fake.add_provider(geo) + + +class FuzzyPolygon(factory.fuzzy.BaseFuzzyAttribute): + """Yields random polygon""" + + def __init__(self, length=None, **kwargs): + if length is None: + length = random.randgen.randrange(3, 20, 1) + if length < 3: + raise Exception("Polygon needs to be 3 or greater in length.") + self.length = length + super().__init__(**kwargs) + + def get_random_coords(self): + return ( + fake.longitude(), + fake.latitude(), + ) + + def fuzz(self): + prefix = suffix = self.get_random_coords() + coords = [self.get_random_coords() for __ in range(self.length - 1)] + return Polygon([prefix] + coords + [suffix]) + + +class AreaFactory(factory.django.DjangoModelFactory): + class Meta: + model = Area + + name = factory.Faker("city") + geom = FuzzyPolygon() + description = factory.Faker("text") + + +class CategoryFactory(factory.django.DjangoModelFactory): + class Meta: + model = ObservationCategory + + label = factory.Faker("word") + description = factory.Faker("text")