Skip to content

Commit

Permalink
✨ [FEAT] Add tests for CirkwiParser (refs #3947)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Nov 15, 2024
1 parent 11d8e2f commit 747eb36
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 23 deletions.
3 changes: 2 additions & 1 deletion geotrek/cirkwi/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CirkwiParser(AttachmentParserMixin, Parser):
create = False
provider = "Cirkwi"
eid = 'eid'
auth = ()
default_language = settings.MODELTRANSLATION_DEFAULT_LANGUAGE
field_options = {
"geom": {"required": True},
Expand Down Expand Up @@ -193,7 +194,7 @@ def filter_description(self, src, val):
if step_descriptions:
desc += "<ol>\r\n"
for step_description in step_descriptions:
desc += f"<li>{step_description}</li>\r\n"
desc += f"<li>{step_description}</li>"
desc += "</ol>"
return desc

Expand Down
78 changes: 71 additions & 7 deletions geotrek/cirkwi/tests/data/circuits.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<circuits version="2">
<listing_ids nb_objects="2" first="0" rows="100">
<id>10925</id>
<id>10926</id>
</listing_ids>
<circuit date_creation="1295825772" date_modification="1295828456" id_circuit="10925"
id_utilisateur="1">
<informations>
Expand Down Expand Up @@ -60,6 +64,58 @@
</medias>
</information>
</informations>
<locomotions>
<locomotion type="Marche" id_locomotion="2" difficulte="5" duree="7200"/>
</locomotions>
<infos_parcours>
<info_parcours numero="1" id_info_parcours="2784327" date_modification="0">
<categories>
<categorie nom="Autres" id_categorie="6"/>
</categories>
<informations>
<information langue="fr">
<titre>Étape 1</titre>
<description>
Au départ du terminal des car-ferrys.
</description>
<medias/>
</information>
</informations>
<adresse>
<code_insee>62193</code_insee>
<position>
<lat>50.9647654</lat>
<lng>1.8614236</lng>
</position>
<altitude>5.54</altitude>
</adresse>
</info_parcours>
<info_parcours numero="2" id_info_parcours="2784328" date_modification="0">
<categories>
<categorie nom="Autres" id_categorie="6"/>
</categories>
<informations>
<information langue="fr">
<titre>Étape 2</titre>
<description>
Virer à droite, direction la plage-corniche de la Côte d'Opale.
</description>
<medias/>
</information>
</informations>
<adresse>
<code_insee>62193</code_insee>
<position>
<lat>50.9588189</lat>
<lng>1.8521179</lng>
</position>
<altitude>4.37</altitude>
</adresse>
</info_parcours>
</infos_parcours>
<fichier_trace
url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx"
info_parcours="false" />
<sens_circuit id="1" label="aller_simple">
<adresse>
<num>74</num>
Expand All @@ -83,13 +139,6 @@
<ddr id_ddr="45" />
<ddr id_ddr="53" />
</ddrs>
<locomotions>
<locomotion type="voiture" id_locomotion="2" difficulte="1" duree="45" jours="0" />
<locomotion type="vtt" id_locomotion="3" difficulte="3" duree="129" />
</locomotions>
<fichier_trace
url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx"
info_parcours="false" />
<infos_parcours>
<info_parcours numero="1" date_modification="1365413342" id_info_parcours="357">
<categories>
Expand Down Expand Up @@ -201,4 +250,19 @@
<diffusion cirkwi="1" mobile="1" reseau="1" />
</sens_circuit>
</circuit>
<circuit date_creation="1295825772" date_modification="1295828456" id_circuit="10926"
id_utilisateur="1">
<informations>
<information langue="fr">
<titre>Le patrimoine de Plancoët à vélo</titre>
<description>Laissez-vous guider par ce chemin</description>
</information>
</informations>
<locomotions>
<locomotion type="Vélo" id_locomotion="3" difficulte="5" duree="0"/>
</locomotions>
<fichier_trace
url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx"
info_parcours="false" />
</circuit>
</circuits>
42 changes: 42 additions & 0 deletions geotrek/cirkwi/tests/data/poi.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<pois version="2">
<listing_ids nb_objects="2" first="0" rows="100">
<id>357</id>
<id>358</id>
</listing_ids>
<poi date_modification="1365413342" id_poi="357" id_utilisateur="1" id_source="1">
<categories>
<categorie nom="Monuments et Architecture" id_categorie="15" />
Expand Down Expand Up @@ -82,4 +86,42 @@
</qrcodelink>
<diffusion cirkwi="1" mobile="1" reseau="1" partage="2" />
</poi>
<poi date_modification="1365413342" id_poi="358" id_utilisateur="1" id_source="1">
<categories>
<categorie nom="Monuments et Architecture" id_categorie="15" />
</categories>
<informations>
<information langue="fr">
<titre>Tour de lancienne église Sainte-Gertrude 2</titre>
<description>A Tenneville, ce site reposant vous fera découvrir</description>
<informations_complementaires>
<information_complementaire>
<titre>Horaire</titre>
<description>Ouvert du Lundi au Vendredi de 8h à 19h</description>
</information_complementaire>
<information_complementaire>
<titre>Contact</titre>
<description>Téléphone: 01 02 03 04 05</description>
</information_complementaire>
</informations_complementaires>
</information>
<information langue="en">
<titre>Titre en anglais</titre>
<description>Description en anglais</description>
</information>
</informations>
<adresse>
<num>1</num>
<rue>route de Bastogne</rue>
<ville>Tenneville</ville>
<cp>6970</cp>
<pays code_pays="BE">Belgique</pays>
<position>
<lat>48.7125946</lat>
<lng>6.1442926</lng>
</position>
<altitude>468.0</altitude>
</adresse>
<diffusion cirkwi="1" mobile="1" reseau="1" partage="2" />
</poi>
</pois>
10 changes: 10 additions & 0 deletions geotrek/cirkwi/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import factory

from .. import models


class CirkwiLocomotionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.CirkwiLocomotion

name = factory.Sequence(lambda n: "Cirkwi Locomotion %s" % n)
51 changes: 41 additions & 10 deletions geotrek/cirkwi/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@

from geotrek.cirkwi.parsers import (CirkwiTouristicContentParser,
CirkwiTrekParser)
from geotrek.cirkwi.tests.factories import CirkwiLocomotionFactory
from geotrek.common.models import FileType
from geotrek.common.utils import testdata
from geotrek.tourism.models import TouristicContent
from geotrek.trekking.models import Trek
from geotrek.trekking.tests.factories import (DifficultyLevelFactory,
PracticeFactory)


class TestCirkwiTrekParserFr(CirkwiTrekParser):
url = 'https://example.net/'
create = True
default_language = 'fr'

class TestCirkwiTrekParserFrNoCreate(CirkwiTrekParser):
url = 'https://example.net/'
create = False
default_language = 'fr'

class TestCirkwiTrekParserEn(CirkwiTrekParser):
url = 'https://example.net/'
Expand Down Expand Up @@ -58,6 +66,10 @@ class CirkwiParserTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.filetype = FileType.objects.create(type="Photographie")
cls.locomotion = CirkwiLocomotionFactory(eid=2, name="Marche")
cls.practice = PracticeFactory(name="Pédestre", cirkwi=cls.locomotion)
PracticeFactory.create(name_fr="Vélo", name="Vélo")
cls.difficulty = DifficultyLevelFactory(cirkwi_level=5)

def make_dummy_get(self, data_filename):
def dummy_get(url, *args, **kwargs):
Expand All @@ -81,21 +93,32 @@ def dummy_get(url, *args, **kwargs):
@mock.patch('requests.get')
def test_create_treks(self, mocked_get):
mocked_get.side_effect = self.make_dummy_get('circuits.xml')
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTrekParserFr', verbosity=0)
output = io.StringIO()
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTrekParserFr', verbosity=2, stdout=output)
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTrekParserEn', verbosity=0)
self.assertEqual(Trek.objects.count(), 1)
t = Trek.objects.first()
self.assertEqual(t.name_fr, "Le patrimoine de Plancoët")
self.assertEqual(Trek.objects.count(), 2)
t = Trek.objects.get(name_fr="Le patrimoine de Plancoët")
self.assertEqual(t.name_en, "Title en")
self.assertEqual(t.description_fr, 'Laissez-vous guider par ce chemin\n\n\nHoraires: Tous les jours sauf le Dimanche')
self.assertEqual(t.description_en, "Description en")
self.assertEqual(t.practice, self.practice)
self.assertEqual(t.description_teaser_fr, 'Laissez-vous guider par ce chemin')
self.assertEqual(t.description_teaser_en, "Description en")
self.assertIn("Horaires: Tous les jours sauf le Dimanche", t.description_fr)
self.assertIn("Au départ du terminal des car-ferrys.", t.description_fr)
self.assertIn("Virer à droite, direction la plage-corniche de la Côte d'Opale.", t.description_fr)
self.assertAlmostEqual(t.geom[0][0], 977776.9692000002)
self.assertAlmostEqual(t.geom[0][1], 6547354.842799998)
attachement = t.attachments.last()
self.assertEqual(attachement.title, '')
self.assertEqual(attachement.legend, 'Le patrimoine de Plancoët')
self.assertEqual(attachement.author, 'Manon')
self.assertEqual(attachement.attachment_file.size, len(testdata.IMG_FILE))
self.assertEqual(t.duration, 2.0)
t = Trek.objects.get(name_fr="Le patrimoine de Plancoët à vélo")
self.assertEqual(t.practice.name, "Vélo")
# Assert created Ciwki Locomotion and mapped it to Practice
self.assertEqual(t.practice.cirkwi.name, "Vélo")
self.assertEqual(t.practice.cirkwi.eid, 3)
self.assertIn("Cirkwi Locomotion 'Vélo' n'existait pas dans Geotrek-Admin. Il a été créé automatiquement,", output.getvalue())

@mock.patch('requests.get')
def test_create_touristic_content_no_type(self, mocked_get):
Expand All @@ -105,16 +128,24 @@ def test_create_touristic_content_no_type(self, mocked_get):
self.assertIn("Type 1 'Eglise' does not exist for category 'Monuments et Architecture'. Please add it",
output.getvalue())

@mock.patch('requests.get')
def test_create_trek_with_missing_locomotion(self, mocked_get):
output = io.StringIO()
mocked_get.side_effect = self.make_dummy_get('circuits_wrong_locomotion.xml')
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTrekParserFrNoCreate', verbosity=2, stdout=output)
self.assertIn("Cirkwi Locomotion '['Aviron', '8']' n'existe pas dans Geotrek-Admin. Merci de l'ajouter,",
output.getvalue())


@mock.patch('requests.get')
def test_create_touristic_content(self, mocked_get):
mocked_get.side_effect = self.make_dummy_get('poi.xml')
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTouristicContentParserFr', verbosity=0)
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTouristicContentParserEn', verbosity=0)
self.assertEqual(TouristicContent.objects.count(), 1)
tc = TouristicContent.objects.first()
self.assertEqual(tc.name_fr, "Tour de lancienne église Sainte-Gertrude")
self.assertEqual(TouristicContent.objects.count(), 2)
tc = TouristicContent.objects.get(name_fr="Tour de lancienne église Sainte-Gertrude")
self.assertEqual(tc.name_en, "Titre en anglais")
self.assertEqual(tc.description_fr, 'A Tenneville, ce site reposant vous fera découvrir\n\n\nHoraire: Ouvert du Lundi au Vendredi de 8h à 19h')
self.assertEqual(tc.description_fr, 'A Tenneville, ce site reposant vous fera découvrir')
self.assertEqual(tc.description_en, "Description en anglais")
self.assertEqual(tc.practical_info_fr, "<strong>Adresse : </strong><br>1 route de Bastogne<br>6970 Tenneville<br><br><strong>Horaire : </strong><br>Ouvert du Lundi au Vendredi de 8h à 19h<br><br><strong>Contact : </strong><br>Téléphone: 01 02 03 04 05<br>")
self.assertEqual(str(tc.category), "Monuments et Architecture")
Expand Down
6 changes: 1 addition & 5 deletions geotrek/tourism/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.test.utils import override_settings

from geotrek.core.tests import factories as core_factories
from geotrek.tourism.models import TouristicContent, TouristicContentType, TouristicEventOrganizer
from geotrek.tourism.models import TouristicContentType, TouristicEventOrganizer
from geotrek.tourism.tests import factories as tourism_factories
from geotrek.tourism.tests.factories import (InformationDeskFactory,
InformationDeskTypeFactory,
Expand Down Expand Up @@ -133,10 +133,6 @@ def test_spatial_link_from_trek_with_practice_distance_respects_limit(self):
self.assertNotIn(self.event, self.trek.touristic_events.all())

def test_spatial_link_default_ordering(self):
print(self.content2)
print(self.content)
print(self.trek.touristic_contents.all())
print(TouristicContent.objects.count(), 2)
self.assertEqual(self.trek.touristic_contents.all()[0], self.content)
self.assertEqual(self.trek.touristic_contents.all()[1], self.content2)

Expand Down

0 comments on commit 747eb36

Please sign in to comment.