Skip to content

Commit

Permalink
Improve unit tests for mobile API and sync
Browse files Browse the repository at this point in the history
  • Loading branch information
justinefricou committed Feb 14, 2025
1 parent bd18979 commit 9cb15df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 3 additions & 2 deletions geotrek/api/tests/test_mobile/test_api_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from geotrek.tourism.tests import factories as tourism_factory
from geotrek.zoning.tests import factories as zoning_factory
from geotrek.sensitivity.tests import factories as sensitivity_factory
from geotrek.sensitivity.models import SensitiveArea


GEOJSON_STRUCTURE = sorted([
Expand Down Expand Up @@ -365,11 +366,11 @@ def test_sensitive_area_list(self):

for feature in json_response.get('features'):
# test dim 2 ok
self.assertEqual(len(feature.get('geometry').get('coordinates')[0][0]), 2)
self.assertEqual(sorted(feature.keys()), DETAIL_GEOJSON_STRUCTURE)
self.assertEqual(len(feature.get('geometry').get('coordinates')[0][0]), 2)
self.assertEqual(sorted(feature.get('properties').keys()),
SENSITIVE_AREA_LIST_PROPERTIES_GEOJSON_STRUCTURE)
sensitive_area_obj = self.trek.published_sensitive_areas.get(pk=feature.get('id'))
sensitive_area_obj = SensitiveArea.objects.get(pk=feature.get('id'))
for i, month in enumerate(['period{:02}'.format(p) for p in range(1, 13)]):
self.assertEqual(getattr(sensitive_area_obj.species, month),
feature.get('properties').get('period')[i])
Expand Down
18 changes: 13 additions & 5 deletions geotrek/api/tests/test_mobile/test_sync_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ def setUpTestData(cls):
cls.touristic_event_portal_b = TouristicEventFactory(geom='SRID=%s;POINT(700001 6600001)' % settings.SRID,
published=True, portals=[cls.portal_b])

trek1_geom_envelope = cls.trek_1.geom.envelope
cls.sensitive_area_species = SensitiveAreaFactory(geom=trek1_geom_envelope, published=True)
cls.sensitive_area_regulatory = SensitiveAreaFactory(geom=trek1_geom_envelope, published=True)
treks_1_4_envelope = MultiLineString(cls.trek_1.geom, cls.trek_4.geom).envelope
cls.sensitive_area_species = SensitiveAreaFactory(geom=treks_1_4_envelope, published=True)
cls.sensitive_area_regulatory = SensitiveAreaFactory(geom=treks_1_4_envelope, published=True)

cls.attachment_content_1 = AttachmentImageFactory.create(content_object=cls.touristic_content)
cls.attachment_event_1 = AttachmentImageFactory.create(content_object=cls.touristic_event)
Expand Down Expand Up @@ -445,10 +445,18 @@ def test_sync_sensitive_areas_by_treks(self):
output = StringIO()
management.call_command('sync_mobile', self.sync_directory, url='http://localhost:8000',
skip_tiles=True, verbosity=2, stdout=output)
with open(os.path.join(self.sync_directory, 'en', str(self.trek_1.pk), 'sensitive_areas.geojson'), 'r') as f:
# Check results for trek_1 as a simple Trek:
filepath_trek_data = os.path.join('en', str(self.trek_1.pk), 'sensitive_areas.geojson')
with open(os.path.join(self.sync_directory, filepath_trek_data), 'r') as f:
sensitive_areas_geojson = json.load(f)
self.assertEqual(len(sensitive_areas_geojson['features']), 2)
self.assertIn('en/{pk}/sensitive_areas.geojson'.format(pk=str(self.trek_1.pk)), output.getvalue())
self.assertIn(filepath_trek_data, output.getvalue())
# Check results for trek_1 as a parent Trek and trek_4 as its child:
filepath_child_trek_data = os.path.join('en', str(self.trek_1.pk), 'sensitive_areas', '{pk}.geojson'.format(pk=str(self.trek_4.pk)))
with open(os.path.join(self.sync_directory, filepath_child_trek_data), 'r') as f:
sensitive_areas_geojson = json.load(f)
self.assertEqual(len(sensitive_areas_geojson['features']), 2)
self.assertIn(filepath_child_trek_data, output.getvalue())

def test_medias_treks(self):
output = StringIO()
Expand Down

0 comments on commit 9cb15df

Please sign in to comment.