Skip to content

Commit

Permalink
✅ [TEST] Add tests for Cirkwi Parser (refs #3947)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Nov 22, 2024
1 parent f41030e commit 44c8c58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
14 changes: 3 additions & 11 deletions geotrek/cirkwi/parsers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
import xml.etree.ElementTree as ET

from django.conf import settings
Expand All @@ -8,7 +7,7 @@
from geotrek.common.utils.parsers import get_geom_from_gpx
from geotrek.trekking.models import DifficultyLevel
from geotrek.cirkwi.models import CirkwiLocomotion
from geotrek.common.parsers import AttachmentParserMixin, GlobalImportError, Parser, RowImportError
from geotrek.common.parsers import AttachmentParserMixin, Parser, RowImportError
from geotrek.tourism.models import TouristicContent, TouristicContentType1
from geotrek.trekking.models import Trek, Practice

Expand Down Expand Up @@ -77,10 +76,7 @@ def next_row(self):
}
if self.updated_after:
params['end-time'] = self.updated_after
response = requests.get(self.url, params=params, auth=self.auth)
if response.status_code != 200:
raise GlobalImportError(_(u"Failed to download {url}. HTTP status code {status_code}").format(
url=self.url, status_code=response.status_code))
response = self.request_or_retry(self.url, params=params, auth=self.auth)
# Save objects count
self.nb = int(ET.fromstring(response.content).find("listing_ids", {}).attrib['nb_objects'])

Expand All @@ -89,10 +85,7 @@ def next_row(self):
while first <= self.nb:
params['first'] = first
params['rows'] = self.rows
response = requests.get(self.url, params=params, auth=self.auth)
if response.status_code != 200:
raise GlobalImportError(_(u"Failed to download {url}. HTTP status code {status_code}").format(
url=self.url, status_code=response.status_code))
response = self.request_or_retry(self.url, params=params, auth=self.auth)
xml_root = ET.fromstring(response.content)
# Yield objects given XML path in 'results_path'
entries = xml_root.findall(self.results_path)
Expand Down Expand Up @@ -236,7 +229,6 @@ def filter_points_reference(self, src, val):

class CirkwiTouristicContentParser(CirkwiParser):
model = TouristicContent
default_language = settings.MODELTRANSLATION_DEFAULT_LANGUAGE
results_path = 'poi'
fields = {
"eid": "@@id_poi",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<circuits version="2">
<listing_ids nb_objects="2" first="0" rows="100">
<listing_ids nb_objects="3" first="0" rows="100">
<id>10925</id>
<id>10926</id>
<id>10926</id>
</listing_ids>
<circuit date_creation="1295825772" date_modification="1295828456" id_circuit="10925"
id_utilisateur="1">
Expand Down Expand Up @@ -265,4 +266,19 @@
url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx"
info_parcours="false" />
</circuit>
<circuit date_creation="1295825772" date_modification="1295828456" id_circuit="10927"
id_utilisateur="1">
<informations>
<information langue="fr">
<titre>Le patrimoine de Plancoët à vélo, difficulté inconnue</titre>
<description>Laissez-vous guider par ce chemin</description>
</information>
</informations>
<locomotions>
<locomotion type="Marche" id_locomotion="2" difficulte="3" duree="0"/>
</locomotions>
<fichier_trace
url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx"
info_parcours="false" />
</circuit>
</circuits>
21 changes: 16 additions & 5 deletions geotrek/cirkwi/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class TestCirkwiTrekParserFrNoCreate(CirkwiTrekParser):


class TestCirkwiTrekParserFrUpdateOnly(CirkwiTrekParser):
# Also tests using filename instead of url
filename = "geotrek/cirkwi/tests/data/circuits_updated.xml"
url = 'https://example.net/'
create = False
update_only = True
default_language = 'fr'
Expand All @@ -52,6 +51,8 @@ class TestCirkwiTouristicContentParserFr(CirkwiTouristicContentParser):


class TestCirkwiTouristicContentDoNotCreateParserEn(CirkwiTouristicContentParser):
# Also tests using filename instead of url
filename = "geotrek/cirkwi/tests/data/poi.xml"
url = 'https://example.net/'
default_language = 'en'
# English parser must not delete attachments created by French parser
Expand Down Expand Up @@ -102,11 +103,14 @@ 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')
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(), 2)

# Test values for french parsing
t = Trek.objects.get(name_fr="Le patrimoine de Plancoët")
self.assertEqual(t.eid, '10925')
self.assertEqual(t.name_en, "Title en")
Expand All @@ -124,14 +128,19 @@ def test_create_treks(self, mocked_get):
self.assertEqual(attachement.author, 'Manon')
self.assertEqual(attachement.attachment_file.size, len(testdata.IMG_FILE))
self.assertEqual(t.duration, 2.0)

# Test values for english parsing
t = Trek.objects.get(name_fr="Le patrimoine de Plancoët à vélo")
self.assertEqual(t.eid, '10926')
self.assertEqual(t.practice.name, "Vélo")
# Assert created Ciwki Locomotion and mapped it to Practice
# Assert created Cirkwi Locomotion and mapped it to Practice
self.assertEqual(t.practice.cirkwi.name, "Vélo")
self.assertEqual(t.practice.cirkwi.eid, 3)
self.assertEqual(t.description_teaser_fr, 'Laissez-vous guider par ce chemin')
self.assertIn("Cirkwi Locomotion 'Vélo' n'existait pas dans Geotrek-Admin. Il a été créé automatiquement,", output.getvalue())

# Test update
mocked_get.side_effect = self.make_dummy_get('circuits_updated.xml')
call_command('import', 'geotrek.cirkwi.tests.test_parsers.TestCirkwiTrekParserFrUpdateOnly', verbosity=2)
self.assertEqual(Trek.objects.count(), 2)
t = Trek.objects.get(name_fr="Le patrimoine de Plancoët à VTT")
Expand All @@ -149,9 +158,9 @@ def test_create_touristic_content_no_type(self, mocked_get):
output.getvalue())

@mock.patch('requests.get')
def test_create_trek_with_missing_locomotion(self, mocked_get):
def test_create_trek_with_missing_locomotion_and_difficulty(self, mocked_get):
output = io.StringIO()
mocked_get.side_effect = self.make_dummy_get('circuits_wrong_locomotion.xml')
mocked_get.side_effect = self.make_dummy_get('circuits_wrong_locomotion_and_difficulty.xml')
# Test Locomotion does not exist
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,",
Expand All @@ -162,6 +171,8 @@ def test_create_trek_with_missing_locomotion(self, mocked_get):
stdout=output)
self.assertIn("Aucune Pratique ne correspond à la Locomotion Cirkwi 'Aviron' (id: '8'). Merci de l'ajouter,",
output.getvalue())
self.assertIn("Niveau de Difficulté ayant le niveau Cirkwi '3' n'existe pas dans Geotrek-Admin. Merci de l'ajouter,",
output.getvalue())

@mock.patch('requests.get')
def test_create_touristic_content(self, mocked_get):
Expand Down

0 comments on commit 44c8c58

Please sign in to comment.