def setUp(self): self.content = TouristicContentFactory.create() cat = self.content.category cat.type1_label = 'Michelin' cat.save() self.category2 = TouristicContentCategoryFactory() self.login()
def test_create_content_espritparc_not_fail_type2_does_not_exist( self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'espritparc.json') with open(filename, 'r') as f: return json.load(f) filename = os.path.join(os.path.dirname(__file__), 'data', 'espritparc.json') mocked.return_value.status_code = 200 mocked.return_value.json = mocked_json mocked.return_value.content = b'Fake image' FileType.objects.create(type="Photographie") category = TouristicContentCategoryFactory( label="Miels et produits de la ruche") TouristicContentType1Factory(label="Miel", category=category) TouristicContentType1Factory(label="Gelée royale, propolis et pollen", category=category) TouristicContentType1Factory(label="Pollen", category=category) TouristicContentType1Factory(label="Cire", category=category) output = io.StringIO() call_command('import', 'geotrek.tourism.tests.test_parsers.EspritParc', filename, verbosity=2, stdout=output) self.assertIn( "Type 2 'Bienvenue à la ferme' does not exist for category 'Miels et produits de la ruche'. Please add it", output.getvalue())
def test_create_content_espritparc_retry(self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeContent.json') with open(filename, 'r') as f: return json.load(f) def side_effect(url, params, auth, code): response = requests.Response() response.status_code = code response.url = url if code == 200: response.json = mocked_json return response mocked.side_effect = [ side_effect(EauViveParser.url, None, None, 503), side_effect(EauViveParser.url, None, None, 503), side_effect(EauViveParser.url, None, None, 200) ] FileType.objects.create(type="Photographie") TouristicContentCategoryFactory(label="Eau vive") TouristicContentType1Factory(label="Type A") TouristicContentType1Factory(label="Type B") call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser') self.assertEqual(TouristicContent.objects.count(), 1)
def test_create_content_apidae(self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeContent.json') with io.open(filename, 'r', encoding='utf8') as f: return json.load(f) mocked.return_value.status_code = 200 mocked.return_value.json = mocked_json FileType.objects.create(type=u"Photographie") category = TouristicContentCategoryFactory(label=u"Eau vive") TouristicContentTypeFactory(label=u"Type A", in_list=1) TouristicContentTypeFactory(label=u"Type B", in_list=1) call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser', verbosity=0) self.assertEqual(TouristicContent.objects.count(), 1) content = TouristicContent.objects.get() self.assertEqual(content.eid, u"479743") self.assertEqual(content.name, u"Quey' Raft") self.assertEqual(content.description[:27], u"Au pied du château médiéval") self.assertEqual(content.description_teaser[:24], u"Des descentes familiales") self.assertEqual(content.contact[:24], u"Château Queyras<br>05350") self.assertEqual(content.email, u"*****@*****.**") self.assertEqual(content.website, u"http://www.queyraft.com") self.assertEqual(round(content.geom.x), 1000157) self.assertEqual(round(content.geom.y), 6413576) self.assertEqual(content.practical_info[:39], "<b>Ouverture:</b><br>Du 01/05 au 31/10.") self.assertTrue(u"<br><b>Capacité totale:</b><br>10<br>" in content.practical_info) self.assertTrue(u"><br><b>Services:</b><br>Test, Test2, Test3, Test4<br>" in content.practical_info) self.assertTrue(content.published) self.assertEqual(content.category, category) self.assertQuerysetEqual( content.type1.all(), ['<TouristicContentType: Type A>', '<TouristicContentType: Type B>'] ) self.assertQuerysetEqual(content.type2.all(), []) self.assertEqual(Attachment.objects.count(), 3) self.assertEqual(Attachment.objects.first().content_object, content)
def test_create_content_apidae_failed(self, mocked): mocked.return_value.status_code = 404 FileType.objects.create(type="Photographie") TouristicContentCategoryFactory(label="Eau vive") TouristicContentType1Factory(label="Type A") TouristicContentType1Factory(label="Type B") with self.assertRaises(CommandError): call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser', verbosity=2)
def test_create_esprit(self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'espritparc.json') with open(filename, 'r') as f: return json.load(f) filename = os.path.join(os.path.dirname(__file__), 'data', 'espritparc.json') mocked.return_value.status_code = 200 mocked.return_value.json = mocked_json mocked.return_value.content = b'Fake image' FileType.objects.create(type="Photographie") category = TouristicContentCategoryFactory( label="Miels et produits de la ruche") TouristicContentType1Factory(label="Miel", category=category) TouristicContentType1Factory(label="Gelée royale, propolis et pollen", category=category) TouristicContentType1Factory(label="Pollen", category=category) TouristicContentType1Factory(label="Cire", category=category) TouristicContentType2Factory(label="Hautes Alpes Naturellement", category=category) TouristicContentType2Factory(label="Bienvenue à la ferme", category=category) TouristicContentType2Factory(label="Agriculture biologique", category=category) call_command('import', 'geotrek.tourism.tests.test_parsers.EspritParc', filename, verbosity=0) self.assertEqual(TouristicContent.objects.count(), 24) content = TouristicContent.objects.all() eid = [ "PDT44", "PDT46", "PDT47", "PDT48", "PDT51", "PDT52", "PDT53", "PDT93", "PDT94", "PDT95", "PDT186", "PDT260", "PDT261", "PDT842", "PDT471", "PDT503", "PDT504", "PDT505", "PDT506", "PDT795", "PDT797", "PDT799", "PDT836", "PDT837" ] name = [ "miel de montagne", "miel de haute montagne", "miel de printemps d'embrun", "gel\xe9e royale de montagne", "pollen de montagne", "miel de haute montagne bio", "miel de for\xeat", "miel de pissenlit", "miel de haute montagne du valgaudemar", "pollen frais de montagne", "miel de printemps de l'embrunais", "pollen de fleurs de montagne", "pain de cire", "miel de montagne toutes fleurs", "miel cuv\xe9e sp\xe9ciale d'ancelle", "miel des ecrins" ] for one in content: self.assertIn(one.eid, eid) self.assertIn(one.name.lower(), name) self.assertEqual(one.category, category)
def test_filetype_structure_none(self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeContent.json') with io.open(filename, 'r', encoding='utf8') as f: return json.load(f) mocked.return_value.status_code = 200 mocked.return_value.json = mocked_json FileType.objects.create(type=u"Photographie", structure=None) TouristicContentCategoryFactory(label=u"Eau vive") TouristicContentType1Factory(label=u"Type A") TouristicContentType1Factory(label=u"Type B") call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser', verbosity=0) self.assertEqual(TouristicContent.objects.count(), 1)
def test_create_content_tourinsoft_v3(self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'tourinsoftContentV3.json') with open(filename, 'r') as f: return json.load(f) mocked.return_value.status_code = 200 mocked.return_value.json = mocked_json mocked.return_value.content = b'Fake image' FileType.objects.create(type="Photographie") category = TouristicContentCategoryFactory(label="Où dormir") source = RecordSourceFactory(name="CDT 28") portal = TargetPortalFactory(name="Itinérance") call_command('import', 'geotrek.tourism.tests.test_parsers.HOT28v3', verbosity=0) self.assertEqual(TouristicContent.objects.count(), 1) content = TouristicContent.objects.get() self.assertEqual(content.eid, "HOTCEN0280010001") self.assertEqual(content.name, "Hôtel du Perche") self.assertEqual(content.description[:27], "") self.assertEqual(content.description_teaser[:26], "A deux pas du centre ville") self.assertEqual( content.contact[:73], "<strong>Adresse :</strong><br>Rue de la Bruyère<br>28400 NOGENT-LE-ROTROU" ) self.assertEqual(content.email, "*****@*****.**") self.assertEqual(content.website, "http://www.hotel-du-perche.com") self.assertEqual(round(content.geom.x), 537329) self.assertEqual(round(content.geom.y), 6805504) self.assertEqual(content.practical_info[:49], "<strong>Langues parlées :</strong><br>Anglais<br>") self.assertIn("du 01/01/2019 au 21/07/2019", content.practical_info) self.assertIn("<strong>Équipements :</strong><br>Bar<br>Parking<br>", content.practical_info) self.assertTrue(content.published) self.assertEqual(content.source.get(), source) self.assertEqual(content.portal.get(), portal) self.assertEqual(content.category, category) self.assertEqual(content.type1.get().label, "Hôtels") self.assertEqual(content.type2.get().label, "****") self.assertEqual(Attachment.objects.count(), 3) self.assertEqual(Attachment.objects.first().content_object, content)
def test_create_content_espritparc_failed(self, mocked): mocked.return_value.status_code = 404 FileType.objects.create(type="Photographie") category = TouristicContentCategoryFactory( label="Miels et produits de la ruche") TouristicContentType1Factory(label="Miel", category=category) TouristicContentType1Factory(label="Gelée royale, propolis et pollen", category=category) TouristicContentType1Factory(label="Pollen", category=category) TouristicContentType1Factory(label="Cire", category=category) TouristicContentType2Factory(label="Hautes Alpes Naturellement", category=category) TouristicContentType2Factory(label="Bienvenue à la ferme", category=category) TouristicContentType2Factory(label="Agriculture biologique", category=category) with self.assertRaises(CommandError): call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser', verbosity=2)
def test_create_content_espritparc_retry_fail(self, mocked): def mocked_json(): filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeContent.json') with open(filename, 'r') as f: return json.load(f) def side_effect(url, allow_redirects, params): response = requests.Response() response.status_code = 503 response.url = url return response mocked.side_effect = side_effect FileType.objects.create(type="Photographie") TouristicContentCategoryFactory(label="Eau vive") TouristicContentType1Factory(label="Type A") TouristicContentType1Factory(label="Type B") with self.assertRaisesRegex( CommandError, "Failed to download %s. HTTP status code 503" % EauViveParser.url): call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser')
def setUp(self): self.category = TouristicContentCategoryFactory() self.login()