Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed May 13, 2024
1 parent 0cdbed6 commit 1ce0c65
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 37 deletions.
1 change: 0 additions & 1 deletion georiviere/contribution/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ class Meta:
ordering = ("label",)


# noinspection PyTypedDict
class CustomContributionTypeField(models.Model):
class FieldTypeChoices(models.TextChoices):
"""Choices for field type"""
Expand Down
32 changes: 31 additions & 1 deletion georiviere/contribution/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from factory import django, fuzzy, SubFactory, Sequence
from factory import django, fuzzy, SubFactory, Sequence, post_generation
from mapentity.tests.factories import PointFactory

from georiviere.contribution import models
from georiviere.observations.tests.factories import StationFactory
from georiviere.portal.tests.factories import PortalFactory


Expand Down Expand Up @@ -132,3 +133,32 @@ class Meta:
model = models.TypePollution

label = Sequence(lambda n: f'Type pollution {n}')


class CustomContributionTypeFactory(django.DjangoModelFactory):
class Meta:
model = models.CustomContributionType

label = Sequence(lambda n: f'Custom contribution type {n}')

@post_generation
def add_stations(obj, create, extracted, **kwargs):
stations = kwargs.get('stations', [])
with_station = kwargs.get('with_station', False)

if not stations and with_station:
stations = [StationFactory.create()]

if stations:
obj.stations.set(stations)


class CustomContributionTypeFieldFactory(django.DjangoModelFactory):
class Meta:
model = models.CustomContributionTypeField

label = Sequence(lambda n: f'Custom contribution type field {n}')
internal_identifier = Sequence(lambda n: f'custom_contribution_type_field_{n}')
value_type = models.CustomContributionTypeField.FieldTypeChoices.STRING
required = False
custom_type = SubFactory(CustomContributionTypeFactory)
58 changes: 57 additions & 1 deletion georiviere/contribution/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

from mapentity.tests.factories import SuperUserFactory

from georiviere.contribution.tests.factories import CustomContributionTypeFactory, CustomContributionTypeFieldFactory

class ContributionAdminTest(TestCase):

class ContributionAdminTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.super_user = SuperUserFactory.create()
Expand Down Expand Up @@ -67,3 +68,58 @@ def test_get_typepollution_add_admin_view(self):
url_add = reverse('admin:contribution_typepollution_add')
response = self.client.get(url_add)
self.assertEqual(response.status_code, 200)


class CustomContributionAdminTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.super_user = SuperUserFactory.create()
cls.custom_contribution_type = CustomContributionTypeFactory.create()

def setUp(self):
self.client.force_login(self.super_user)

def test_list_customcontributiontype_admin_view(self):
url_list = reverse('admin:contribution_customcontributiontype_changelist')
response = self.client.get(url_list)
self.assertEqual(response.status_code, 200)

def test_detail_customcontributiontype_admin_view(self):
url_detail = reverse('admin:contribution_customcontributiontype_change',
args=[self.custom_contribution_type.pk])
response = self.client.get(url_detail)
self.assertEqual(response.status_code, 200)

def test_get_customcontributiontype_add_admin_view(self):
url_add = reverse('admin:contribution_customcontributiontype_add')
response = self.client.get(url_add)
self.assertEqual(response.status_code, 200)


class CustomContributionTypeFieldAdminTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.super_user = SuperUserFactory.create()
cls.custom_contribution_type = CustomContributionTypeFactory.create()
cls.field = CustomContributionTypeFieldFactory.create(custom_type=cls.custom_contribution_type)

def setUp(self):
self.client.force_login(self.super_user)

def test_list_customcontributiontypefield_admin_view(self):
url_list = reverse('admin:contribution_customcontributiontypefield_changelist')
response = self.client.get(url_list)
self.assertEqual(response.status_code, 200)

def test_detail_customcontributiontypefield_admin_view(self):
""" Detail view of a custom type field in admin (extra config) """
url_detail = reverse('admin:contribution_customcontributiontypefield_change',
args=[self.field.pk])
response = self.client.get(url_detail)
self.assertEqual(response.status_code, 200)

def test_get_customcontributiontypefield_add_admin_view(self):
""" Unable to add custom type field directly by admin """
url_add = reverse('admin:contribution_customcontributiontypefield_add')
response = self.client.get(url_add)
self.assertEqual(response.status_code, 403)
Loading

0 comments on commit 1ce0c65

Please sign in to comment.