Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed Jul 5, 2024
1 parent 33cc2ba commit 510686f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
81 changes: 80 additions & 1 deletion backend/project/api/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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())),
)
File renamed without changes.
50 changes: 50 additions & 0 deletions backend/project/observations/tests/factories.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 510686f

Please sign in to comment.