diff --git a/georiviere/portal/tests/test_serializers/test_contribution.py b/georiviere/portal/tests/test_serializers/test_contribution.py index b3c1e62e..4fe81524 100644 --- a/georiviere/portal/tests/test_serializers/test_contribution.py +++ b/georiviere/portal/tests/test_serializers/test_contribution.py @@ -3,15 +3,25 @@ from django.test import TestCase import json +from georiviere.contribution.models import CustomContribution +from georiviere.observations.tests.factories import StationFactory from georiviere.portal.validators import validate_json_schema from georiviere.contribution.tests.factories import (TypePollutionFactory, NaturePollutionFactory, FishSpeciesFactory, InvasiveSpeciesFactory, DeadSpeciesFactory, HeritageObservationFactory, HeritageSpeciesFactory, DiseaseTypeFactory, LandingTypeFactory, SeverityTypeTypeFactory, - JamTypeFactory) + JamTypeFactory, CustomContributionTypeFactory, + CustomContributionTypeBooleanFieldFactory, + CustomContributionTypeFloatFieldFactory, + CustomContributionTypeIntegerFieldFactory, + CustomContributionTypeStringFieldFactory, + CustomContributionTypeTextFieldFactory, + CustomContributionTypeDateFieldFactory, + CustomContributionTypeDatetimeFieldFactory, + CustomContributionFactory) -from georiviere.portal.serializers.contribution import ContributionSchemaSerializer +from georiviere.portal.serializers.contribution import ContributionSchemaSerializer, CustomContributionSerializer # TODO: Add tests on every possibilities validate with json schema @@ -59,3 +69,84 @@ def test_contribution_with_subtypes(self): with open(filename) as f: json_data = json.load(f) self.assertEqual(json_data, data) + + +class CustomContributionSerializerTestCase(TestCase): + @classmethod + def setUpTestData(cls): + cls.custom_contrib_type = CustomContributionTypeFactory() + cls.bool_field = CustomContributionTypeBooleanFieldFactory(custom_type=cls.custom_contrib_type) + cls.float_field = CustomContributionTypeFloatFieldFactory(custom_type=cls.custom_contrib_type) + cls.integer_field = CustomContributionTypeIntegerFieldFactory(custom_type=cls.custom_contrib_type) + cls.string_field = CustomContributionTypeStringFieldFactory(custom_type=cls.custom_contrib_type) + cls.text_field = CustomContributionTypeTextFieldFactory(custom_type=cls.custom_contrib_type) + cls.date_field = CustomContributionTypeDateFieldFactory(custom_type=cls.custom_contrib_type) + cls.datetime_field = CustomContributionTypeDatetimeFieldFactory(custom_type=cls.custom_contrib_type) + cls.custom_contrib = CustomContributionFactory( + custom_type=cls.custom_contrib_type, + data={ + cls.bool_field.key: True, + cls.float_field.key: 1.0, + cls.integer_field.key: 1, + cls.string_field.key: "toto", + cls.text_field.key: "toto", + cls.date_field.key: "2020-01-01", + cls.datetime_field.key: "2020-01-01T00:00:00Z" + } + ) + + def get_serializer_context(self): + return { + "lang": "fr", + "portal_pk": 1, + "custom_type": self.custom_contrib_type + } + + def get_serializer(self, **kwargs): + custom_contrib = (CustomContribution.objects.with_type_values(self.custom_contrib_type) + .get(pk=self.custom_contrib.pk)) + return CustomContributionSerializer(custom_contrib, context=self.get_serializer_context(), **kwargs) + + def get_serializer_data(self): + serializer = self.get_serializer() + return serializer.data + + def test_full_render(self): + data = self.get_serializer_data() + fields = list(data.keys()) + self.assertIn(self.bool_field.key, fields) + self.assertIn(self.integer_field.key, fields) + self.assertIn(self.float_field.key, fields) + self.assertIn(self.string_field.key, fields) + self.assertIn(self.text_field.key, fields) + self.assertIn(self.date_field.key, fields) + self.assertIn(self.datetime_field.key, fields) + + def test_station_required_if_linked_to_custom_type(self): + self.custom_contrib_type.stations.add(StationFactory()) + serializer = self.get_serializer() + self.assertTrue(serializer.fields['station'].required) + + def test_station_not_required_if_linked_to_custom_type(self): + serializer = self.get_serializer() + self.assertFalse(serializer.fields['station'].required) + + def test_password_field_not_exists_if_not_defined_in_custom_type(self): + serializer = self.get_serializer() + self.assertNotIn('password', serializer.fields) + + def test_password_is_required_if_defined_in_custom_type(self): + self.custom_contrib_type.password = "toto" + self.custom_contrib_type.save() + serializer = self.get_serializer() + self.assertTrue(serializer.fields['password'].required) + + def test_password_should_match_if_defined_in_custom_type(self): + self.custom_contrib_type.password = "toto" + self.custom_contrib_type.save() + serializer = self.get_serializer(data={"password": "toto"}) + serializer.is_valid() + self.assertNotIn('password', serializer.errors) + serializer = self.get_serializer(data={"password": "tata"}) + serializer.is_valid() + self.assertIn('password', serializer.errors)