Skip to content

Commit

Permalink
load signage: raise error when year is NaN, move update_gis function
Browse files Browse the repository at this point in the history
  • Loading branch information
njourdane committed Sep 27, 2023
1 parent 225659d commit 3d4d622
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
5 changes: 1 addition & 4 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ CHANGELOG
2.100.2+dev (XXXX-XX-XX)
------------------------

**Bug fixes**

- Allow to load a signage with the year set to None

**Maintenance**

- Upgrade `django-mapentity` to 8.6.1. New authentication system for screamshotter and convertit by token instead of IP detection.
Expand All @@ -17,6 +13,7 @@ CHANGELOG
**Bug fixes**

- Fix missing update rights for Infrastructure Condition and Infrastructure Type with no structure in Admin Site (#3747)
- Allow to load a signage with the year set to None, raise error if set to NaN (#3611)


2.100.2 (2023-09-12)
Expand Down
22 changes: 22 additions & 0 deletions geotrek/common/tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Dict

import fiona


def update_gis(input_file_path: str, output_file_path: str, new_properties: Dict):
'''
Utility function that reads a GIS file (GeoPackage or Shapefile), update some properties,
then write a new shapefile (typically in /tmp). Useful to test specific property values.
'''

with fiona.open(input_file_path) as source:
with fiona.open(output_file_path,
mode='w',
crs=source.crs,
driver=source.driver,
schema=source.schema) as dest:
for feat in source:
dest.write(fiona.Feature(
geometry=feat.geometry,
properties={**feat.properties, **new_properties}
))
5 changes: 4 additions & 1 deletion geotrek/signage/management/commands/loadsignage.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ def handle(self, *args, **options):

year = feature.get(field_implantation_year) if field_implantation_year in available_fields else default_year
if year:
year = int(year) if str(year).isdigit() else default_year
if str(year).isdigit():
year = int(year)
else:
raise CommandError('Invalid year: "%s" is not a number.' % year)
else:
year = None

Expand Down
50 changes: 27 additions & 23 deletions geotrek/signage/tests/test_command_signage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import os
from io import StringIO
from typing import Dict
import tempfile

import fiona

from django.contrib.gis.geos.error import GEOSException
from django.core.management import call_command
from django.test import TestCase
Expand All @@ -14,6 +11,7 @@
from geotrek.signage.tests.factories import SignageFactory
from geotrek.signage.models import Signage
from geotrek.authent.tests.factories import StructureFactory
from geotrek.common.tests.utils import update_gis


class SignageCommandTest(TestCase):
Expand All @@ -24,25 +22,6 @@ class SignageCommandTest(TestCase):
def setUpTestData(cls):
cls.path = PathFactory.create()

def _update_gis(self, input_file_path: str, output_file_path: str, new_properties: Dict):
'''
Utility function that reads a GIS file (GeoPackage or Shapefile), update some properties,
then write a new shapefile (typically in /tmp). Useful to test specific property values.
Can eventually be moved somewhere else, since it's not specifically related to signages.
'''

with fiona.open(input_file_path) as source:
with fiona.open(output_file_path,
mode='w',
crs=source.crs,
driver=source.driver,
schema=source.schema) as dest:
for feat in source:
dest.write(fiona.Feature(
geometry=feat.geometry,
properties={**feat.properties, **new_properties}
))

def test_load_signage(self):
output = StringIO()
structure = StructureFactory.create(name='structure')
Expand Down Expand Up @@ -81,7 +60,7 @@ def test_load_signage_none_year(self):

with tempfile.TemporaryDirectory() as tmp_dir:
output_file_path = os.path.join(tmp_dir, 'signage_none_year.shp')
self._update_gis(input_file_path, output_file_path, {'year': None})
update_gis(input_file_path, output_file_path, {'year': None})

output = StringIO()
StructureFactory.create(name='structure')
Expand All @@ -100,6 +79,31 @@ def test_load_signage_none_year(self):
value = Signage.objects.first()
self.assertEqual(None, value.implantation_year)

def test_load_signage_bad_year(self):
'''Loading a signage with an invalid year should fail.'''

input_file_path = os.path.join(os.path.dirname(__file__), 'data', 'signage.shp')

with tempfile.TemporaryDirectory() as tmp_dir:
output_file_path = os.path.join(tmp_dir, 'signage_none_year.shp')
update_gis(input_file_path, output_file_path, {'year': 'not a number'})

output = StringIO()
StructureFactory.create(name='structure')

with self.assertRaisesRegex(CommandError, 'Invalid year: "not a number" is not a number.'):
call_command(
'loadsignage',
output_file_path,
type_default='label',
name_default='name',
condition_default='condition',
structure_default='structure',
description_default='description',
year_field='year',
stdout=output
)

def test_load_signage_bad_multipoints_error(self):
output = StringIO()
StructureFactory.create(name='structure')
Expand Down

0 comments on commit 3d4d622

Please sign in to comment.