From 865dda6f023e06b65f05b52c35d232d2185aa52b Mon Sep 17 00:00:00 2001 From: Chatewgne Date: Fri, 22 Nov 2024 13:22:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20[TEST]=20Add=20tests=20for=20Cirkwi?= =?UTF-8?q?=20Parser=20(refs=20#3947)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geotrek/cirkwi/parsers.py | 14 +++---------- ...cuits_wrong_locomotion_and_difficulty.xml} | 18 +++++++++++++++- geotrek/cirkwi/tests/test_parsers.py | 21 ++++++++++++++----- 3 files changed, 36 insertions(+), 17 deletions(-) rename geotrek/cirkwi/tests/data/{circuits_wrong_locomotion.xml => circuits_wrong_locomotion_and_difficulty.xml} (93%) diff --git a/geotrek/cirkwi/parsers.py b/geotrek/cirkwi/parsers.py index 3a4d1c9c48..2285ba0fe4 100644 --- a/geotrek/cirkwi/parsers.py +++ b/geotrek/cirkwi/parsers.py @@ -1,4 +1,3 @@ -import requests import xml.etree.ElementTree as ET from django.conf import settings @@ -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 @@ -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']) @@ -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) @@ -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", diff --git a/geotrek/cirkwi/tests/data/circuits_wrong_locomotion.xml b/geotrek/cirkwi/tests/data/circuits_wrong_locomotion_and_difficulty.xml similarity index 93% rename from geotrek/cirkwi/tests/data/circuits_wrong_locomotion.xml rename to geotrek/cirkwi/tests/data/circuits_wrong_locomotion_and_difficulty.xml index 84ca8cd859..34f31b8b14 100644 --- a/geotrek/cirkwi/tests/data/circuits_wrong_locomotion.xml +++ b/geotrek/cirkwi/tests/data/circuits_wrong_locomotion_and_difficulty.xml @@ -1,7 +1,8 @@ - + 10925 10926 + 10926 @@ -265,4 +266,19 @@ url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx" info_parcours="false" /> + + + + Le patrimoine de Plancoët à vélo, difficulté inconnue + Laissez-vous guider par ce chemin + + + + + + + diff --git a/geotrek/cirkwi/tests/test_parsers.py b/geotrek/cirkwi/tests/test_parsers.py index 7ecca6a08f..179fdfaf8a 100644 --- a/geotrek/cirkwi/tests/test_parsers.py +++ b/geotrek/cirkwi/tests/test_parsers.py @@ -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' @@ -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 @@ -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") @@ -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") @@ -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,", @@ -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):