Skip to content

Commit

Permalink
Fix flaky tests (GeotrekCE#4348)
Browse files Browse the repository at this point in the history
* fix flaky translation test

* fix flaky translation test

* fix syn mobile test that move old sync test

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* fix altimetry translations

* clean translation reset mixins in tests

* fix tmp sync mobile dir

* fix coverage in DEBUG mode

* revert parser exception behaviour

* fix flaky ordered test result

* cover by using limit arg

* fix ytranslation in request middleware

* add test coverage

* typo

* typo

* delete maxDiff
  • Loading branch information
submarcos authored Nov 6, 2024
1 parent ab81503 commit 63f01f6
Show file tree
Hide file tree
Showing 52 changed files with 316 additions and 419 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ coverage:
rm ./var/.coverage*

test:
$(docker_compose) run -e ENV=tests --rm web ./manage.py test --noinput --parallel
$(docker_compose) run -e ENV=tests --rm web ./manage.py test --shuffle --noinput --parallel

test_nds:
$(docker_compose) run -e ENV=tests_nds --rm web ./manage.py test --noinput --parallel
$(docker_compose) run -e ENV=tests_nds --rm web ./manage.py test --shuffle --noinput --parallel

test_nav:
casperjs test --baseurl=$(baseurl) geotrek/jstests/nav-*.js
Expand Down
27 changes: 13 additions & 14 deletions geotrek/altimetry/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.gis.geos import GEOSGeometry
from django.utils import translation
from django.utils.translation import gettext as _
from django.utils.translation import gettext as _, get_language
from django.contrib.gis.geos import LineString
from django.conf import settings
from django.db import connection
Expand Down Expand Up @@ -70,6 +70,7 @@ def profile_svg(cls, profile, language):
Most of the job done here is dedicated to preparing
nice labels scales.
"""
lang = language or get_language()
ceil_elevation, floor_elevation = cls.altimetry_limits(profile)
config = dict(show_legend=False,
print_values=False,
Expand All @@ -89,19 +90,17 @@ def profile_svg(cls, profile, language):
style.colors = (settings.ALTIMETRIC_PROFILE_COLOR,)
style.font_family = settings.ALTIMETRIC_PROFILE_FONT
line_chart = pygal.XY(fill=True, style=style, **config)
if language:
translation.activate(language)
line_chart.x_title = _("Distance (m)")
line_chart.y_title = _("Altitude (m)")
line_chart.show_minor_x_labels = False
line_chart.x_labels_major_count = 5
line_chart.show_minor_y_labels = False
line_chart.truncate_label = 50
line_chart.range = [floor_elevation, ceil_elevation]
line_chart.no_data_text = _("Altimetry data not available")
translation.deactivate()
line_chart.add('', [(int(v[0]), int(v[3])) for v in profile])
return line_chart.render()
with translation.override(lang):
line_chart.x_title = _("Distance (m)")
line_chart.y_title = _("Elevation (m)")
line_chart.show_minor_x_labels = False
line_chart.x_labels_major_count = 5
line_chart.show_minor_y_labels = False
line_chart.truncate_label = 50
line_chart.range = [floor_elevation, ceil_elevation]
line_chart.no_data_text = _("Elevation data not available")
line_chart.add('', [(int(v[0]), int(v[3])) for v in profile])
return line_chart.render()

@classmethod
def _nice_extent(cls, geom):
Expand Down
6 changes: 3 additions & 3 deletions geotrek/altimetry/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-10-25 15:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -42,10 +42,10 @@ msgstr ""
msgid "Distance (m)"
msgstr ""

msgid "Altitude (m)"
msgid "Elevation (m)"
msgstr ""

msgid "Altimetry data not available"
msgid "Elevation data not available"
msgstr ""

msgid "3D Length"
Expand Down
6 changes: 3 additions & 3 deletions geotrek/altimetry/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-10-25 15:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -42,10 +42,10 @@ msgstr ""
msgid "Distance (m)"
msgstr ""

msgid "Altitude (m)"
msgid "Elevation (m)"
msgstr ""

msgid "Altimetry data not available"
msgid "Elevation data not available"
msgstr ""

msgid "3D Length"
Expand Down
6 changes: 3 additions & 3 deletions geotrek/altimetry/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-10-25 15:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -42,10 +42,10 @@ msgstr ""
msgid "Distance (m)"
msgstr ""

msgid "Altitude (m)"
msgid "Elevation (m)"
msgstr ""

msgid "Altimetry data not available"
msgid "Elevation data not available"
msgstr ""

msgid "3D Length"
Expand Down
8 changes: 4 additions & 4 deletions geotrek/altimetry/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-10-25 15:17+0200\n"
"PO-Revision-Date: 2015-10-06 12:11+0100\n"
"Language-Team: \n"
"Language: fr\n"
Expand All @@ -18,7 +18,7 @@ msgstr ""
"X-Generator: Poedit 1.7.5\n"

msgid "Altimetry"
msgstr "Altimetrie"
msgstr "Altimétrie"

msgid "elevation"
msgstr "altitude"
Expand All @@ -41,10 +41,10 @@ msgstr "pente"
msgid "Distance (m)"
msgstr "Distance (m)"

msgid "Altitude (m)"
msgid "Elevation (m)"
msgstr "Altitude (m)"

msgid "Altimetry data not available"
msgid "Elevation data not available"
msgstr "Données altimétriques non disponibles"

msgid "3D Length"
Expand Down
6 changes: 3 additions & 3 deletions geotrek/altimetry/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-10-25 15:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -42,10 +42,10 @@ msgstr ""
msgid "Distance (m)"
msgstr ""

msgid "Altitude (m)"
msgid "Elevation (m)"
msgstr ""

msgid "Altimetry data not available"
msgid "Elevation data not available"
msgstr ""

msgid "3D Length"
Expand Down
6 changes: 3 additions & 3 deletions geotrek/altimetry/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-10-25 15:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -42,10 +42,10 @@ msgstr ""
msgid "Distance (m)"
msgstr ""

msgid "Altitude (m)"
msgid "Elevation (m)"
msgstr ""

msgid "Altimetry data not available"
msgid "Elevation data not available"
msgstr ""

msgid "3D Length"
Expand Down
4 changes: 1 addition & 3 deletions geotrek/altimetry/tests/test_elevation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.db import connection
from django.contrib.gis.geos import MultiLineString, LineString, Point
from django.utils import translation

from geotrek.core.models import Path, Topology
from geotrek.core.tests.factories import TopologyFactory
Expand Down Expand Up @@ -127,8 +126,7 @@ def test_elevation_svg_output(self):
geom = LineString((1.5, 2.5, 8), (2.5, 2.5, 10),
srid=settings.SRID)
profile = AltimetryHelper.elevation_profile(geom)
language = translation.get_language()
svg = AltimetryHelper.profile_svg(profile, language)
svg = AltimetryHelper.profile_svg(profile, 'en')
self.assertIn('Generated with pygal'.encode(), svg)
self.assertIn(settings.ALTIMETRIC_PROFILE_BACKGROUND.encode(), svg)
self.assertIn(settings.ALTIMETRIC_PROFILE_COLOR.encode(), svg)
Expand Down
25 changes: 13 additions & 12 deletions geotrek/api/management/commands/sync_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from zipfile import ZipFile

import cairosvg
from PIL import Image
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.management.base import BaseCommand, CommandError
Expand All @@ -19,7 +20,6 @@
from django.utils import translation
from django.utils.translation import gettext as _
from modeltranslation.utils import build_localized_fieldname
from PIL import Image

from geotrek.api.mobile.views.common import FlatPageViewSet, SettingsView
from geotrek.api.mobile.views.trekking import TrekViewSet
Expand Down Expand Up @@ -181,7 +181,7 @@ def sync_media_file(self, field, prefix=None, directory='', zipfile=None):

def sync_pictograms(self, model, directory='', zipfile=None, size=None):
for obj in model.objects.all():
if not obj.pictogram:
if not obj.pictogram or not os.path.exists(obj.pictogram.path):
continue
file_name, file_extension = os.path.splitext(obj.pictogram.name)
if file_extension == '.svg':
Expand All @@ -205,8 +205,8 @@ def sync_pictograms(self, model, directory='', zipfile=None, size=None):
zipfile.write(dst, name)
if self.verbosity == 2:
self.stdout.write(
"\x1b[36m**\x1b[0m \x1b[1m{directory}{url}/{name}\x1b[0m \x1b[32mcopied\x1b[0m".format(
directory=directory, url=obj.pictogram.url, name=name))
"\x1b[36m**\x1b[0m \x1b[1m{directory}{url}\x1b[0m \x1b[32mcopied\x1b[0m".format(
directory=directory, url=obj.pictogram.url))

def close_zip(self, zipfile, name):
if self.verbosity == 2:
Expand Down Expand Up @@ -325,6 +325,11 @@ def sync_trek_by_pk_media(self, trek):
if not self.skip_tiles:
self.sync_trek_tiles(trek, trekid_zipfile)

if trek.resized_pictures:
for picture, thdetail in trek.resized_pictures[:settings.MOBILE_NUMBER_PICTURES_SYNC]:
self.sync_media_file(thdetail, prefix=trek.pk, directory=url_trek,
zipfile=trekid_zipfile)

for poi in trek.published_pois.annotate(geom_type=GeometryType("geom")).filter(geom_type="POINT"):
if poi.resized_pictures:
for picture, thdetail in poi.resized_pictures[:settings.MOBILE_NUMBER_PICTURES_SYNC]:
Expand All @@ -340,10 +345,7 @@ def sync_trek_by_pk_media(self, trek):
for picture, thdetail in touristic_event.resized_pictures[:settings.MOBILE_NUMBER_PICTURES_SYNC]:
self.sync_media_file(thdetail, prefix=trek.pk, directory=url_trek,
zipfile=trekid_zipfile)
if trek.resized_pictures:
for picture, thdetail in trek.resized_pictures[:settings.MOBILE_NUMBER_PICTURES_SYNC]:
self.sync_media_file(thdetail, prefix=trek.pk, directory=url_trek,
zipfile=trekid_zipfile)

for desk in trek.information_desks.all().annotate(geom_type=GeometryType("geom")).filter(geom_type="POINT"):
if desk.resized_picture:
self.sync_media_file(desk.resized_picture, prefix=trek.pk, directory=url_trek,
Expand Down Expand Up @@ -406,8 +408,7 @@ def sync_global_media(self):
self.close_zip(self.zipfile_settings, zipname_settings)

def sync_trek_tiles(self, trek, zipfile):
""" Add tiles to zipfile for the specified Trek object.
"""
""" Add tiles to zipfile for the specified Trek object."""

if self.verbosity == 2:
self.stdout.write("\x1b[36m**\x1b[0m \x1b[1mnolang/{}/tiles/\x1b[0m ...".format(trek.pk), ending="")
Expand Down Expand Up @@ -490,7 +491,7 @@ def check_dst_root_is_empty(self):

def rename_root(self):
if os.path.exists(self.dst_root):
tmp_root2 = os.path.join(os.path.dirname(self.dst_root), 'deprecated_sync_mobile')
tmp_root2 = os.path.join(os.path.dirname(self.dst_root), f'{os.path.basename(self.dst_root)}_deprecated_sync_mobile')
os.rename(self.dst_root, tmp_root2)
if os.path.exists(tmp_root2):
shutil.rmtree(tmp_root2)
Expand Down Expand Up @@ -536,7 +537,7 @@ def handle(self, *args, **options):
'ignore_errors': True,
'tiles_dir': settings.MOBILE_TILES_PATH,
}
sync_mobile_tmp_dir = os.path.join(settings.TMP_DIR, 'sync_mobile')
sync_mobile_tmp_dir = tempfile.TemporaryDirectory(dir=settings.TMP_DIR).name
if options['empty_tmp_folder']:
for dir in os.listdir(sync_mobile_tmp_dir):
shutil.rmtree(os.path.join(sync_mobile_tmp_dir, dir))
Expand Down
2 changes: 1 addition & 1 deletion geotrek/api/mobile/serializers/trekking.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_parking_location(self, obj):
return obj.parking_location.transform(settings.API_SRID, clone=True).coords

def get_arrival_city(self, obj):
qs = City.objects.all()
qs = City.objects.filter(published=True)
if obj.end_point:
city = qs.filter(geom__covers=(obj.end_point, 0)).first()
if city:
Expand Down
2 changes: 1 addition & 1 deletion geotrek/api/tests/test_mobile/test_api_mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_trek_detail(self):
self.assertEqual(len(json_response['properties']['districts']), 1)
self.assertEqual(len(json_response['properties']['cities']), 1)
self.assertEqual(json_response['properties']['departure_city'], self.city.code)
self.assertEqual(json_response['properties']['arrival_city'], self.city.code)
self.assertEqual(json_response['properties']['arrival_city'], self.city.code, json_response)

def test_trek_detail_no_parking_location(self):
trek_no_parking = trek_factory.TrekFactory(name_fr='no_parking', parking_location=None, published_fr=True)
Expand Down
4 changes: 0 additions & 4 deletions geotrek/api/tests/test_mobile/test_api_mobile_flatpage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf import settings
from django.urls import reverse
from django.test.testcases import TestCase
from django.utils import translation

from geotrek.common.tests.factories import TargetPortalFactory
from geotrek.flatpages.tests.factories import FlatPageFactory, MenuItemFactory
Expand Down Expand Up @@ -347,6 +346,3 @@ def test_flatpage_detail_link_only(self):
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.json()["external_url"], "https://test.localhost/")
self.assertEqual(resp.json()["content"], "")

def tearDown(self):
translation.deactivate()
Loading

0 comments on commit 63f01f6

Please sign in to comment.