Skip to content

Commit

Permalink
✅ [TEST] Add tests for Annotation Categories in HD Views (refs #4032)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Aug 1, 2024
1 parent d467a06 commit 32ce2eb
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
83 changes: 80 additions & 3 deletions geotrek/api/tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,54 @@ 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
)
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()
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 13 additions & 2 deletions geotrek/common/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
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


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())

0 comments on commit 32ce2eb

Please sign in to comment.