From 32ce2ebd1babafc3cb2d791bb61d321899976a34 Mon Sep 17 00:00:00 2001 From: Chatewgne Date: Wed, 31 Jul 2024 12:41:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20[TEST]=20Add=20tests=20for=20Annota?= =?UTF-8?q?tion=20Categories=20in=20HD=20Views=20(refs=20#4032)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geotrek/api/tests/test_v2.py | 83 ++++++++++++++++++++++++++++-- geotrek/common/tests/test_forms.py | 15 +++++- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/geotrek/api/tests/test_v2.py b/geotrek/api/tests/test_v2.py index 820b6e9ee6..cffcbfc58d 100644 --- a/geotrek/api/tests/test_v2.py +++ b/geotrek/api/tests/test_v2.py @@ -482,8 +482,47 @@ def setUpTestData(cls): cls.site2.themes.add(cls.theme3) cls.label_3 = common_factory.LabelFactory() cls.site2.labels.add(cls.label_3) + cls.annotationcategory = common_factory.AnnotationCategoryFactory(label='A category') + annotations = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + 259.26707830454814, + 148.1029483470403 + ] + }, + "properties": { + "name": "Point 1", + "annotationId": 1234, + "annotationType": "point", + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + 540.4490176472651, + 161.10558138022952 + ] + }, + "properties": { + "name": "Point 2", + "annotationId": 2, + "annotationType": "point", + } + } + ] + } + annotations_categories = {'1234': str(cls.annotationcategory.pk)} cls.hdviewpoint_trek = common_factory.HDViewPointFactory( - content_object=cls.treks[0] + content_object=cls.treks[0], + annotations=annotations, + annotations_categories=annotations_categories ) cls.hdviewpoint_poi = common_factory.HDViewPointFactory( content_object=cls.poi @@ -491,7 +530,6 @@ def setUpTestData(cls): cls.hdviewpoint_site = common_factory.HDViewPointFactory( content_object=cls.site ) - cls.annotationcategory = common_factory.AnnotationCategoryFactory() def check_number_elems_response(self, response, model): json_response = response.json() @@ -2488,7 +2526,46 @@ def test_hdviewpoint_detail_content(self): json_response.get('picture_tiles_url'), f"http://testserver/api/hdviewpoint/drf/hdviewpoints/{self.hdviewpoint_trek.pk}/tiles/%7Bz%7D/%7Bx%7D/%7By%7D.png?source=vips" ) - json.dumps(json_response.get('annotations')) + annotations = json_response.get('annotations') + serialized_annotations = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + 259.26707830454814, + 148.1029483470403 + ] + }, + "properties": { + "name": "Point 1", + "annotationId": 1234, + "annotationType": "point", + "category": self.annotationcategory.pk + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + 540.4490176472651, + 161.10558138022952 + ] + }, + "properties": { + "name": "Point 2", + "annotationId": 2, + "annotationType": "point", + "category": None + } + } + ] + } + self.assertEqual(str(self.annotationcategory), 'A category') + self.assertJSONEqual(json.dumps(serialized_annotations), json.dumps(annotations)) self.assertIsNone(json_response.get('site')) self.assertIsNone(json_response.get('poi')) self.assertEqual(json_response.get('trek').get('uuid'), str(self.treks[0].uuid)) diff --git a/geotrek/common/tests/test_forms.py b/geotrek/common/tests/test_forms.py index eed68fd074..fe5758c43c 100644 --- a/geotrek/common/tests/test_forms.py +++ b/geotrek/common/tests/test_forms.py @@ -1,7 +1,7 @@ import json from django.test import TestCase from geotrek.common.forms import HDViewPointAnnotationForm -from geotrek.common.tests.factories import HDViewPointFactory +from geotrek.common.tests.factories import AnnotationCategoryFactory, HDViewPointFactory from geotrek.trekking.tests.factories import TrekFactory @@ -9,12 +9,23 @@ class HDViewPointAnnotateFormTest(TestCase): @classmethod def setUpTestData(cls): cls.vp = HDViewPointFactory(content_object=TrekFactory()) + cls.annotationcategory = AnnotationCategoryFactory() def test_annotation_form(self): geojson = '{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [7903.518111498841, 3618.542606516288]}, "properties": {"name": "Test point", "annotationId": 13, "annotationType": "point"}}]}' data = { - "annotations": geojson + "annotations": geojson, + "annotations_categories": f'{{"13":"{self.annotationcategory.pk}"}}' } form = HDViewPointAnnotationForm(instance=self.vp, data=data) form.save() self.assertEqual(self.vp.annotations, json.loads(geojson)) + self.assertEqual(self.vp.annotations_categories, {'13': str(self.annotationcategory.pk)}) + + def test_annotation_empty_values_form(self): + data = { + "annotations": "", + "annotations_categories": "" + } + form = HDViewPointAnnotationForm(instance=self.vp, data=data) + self.assertTrue(form.is_valid())