From 4980d9cb47c99056b2f228408bab536393e100bb Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Tue, 9 Oct 2018 20:23:21 +0200 Subject: tests: test du comportement du parseur lorsque la source est mauvaise Il est possible que la source renvoie des pages vides ou incorrectes, il est donc nécessaire de tester le comportement du parseur dans ces cas-là. Ajout d’un modèle de page vide dans les données de test. Signed-off-by: Alban Gruin --- tests.py | 32 +++++++++++++++++++++++++++-- tests/data/2018/empty.html | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 tests/data/2018/empty.html diff --git a/tests.py b/tests.py index 8ec90ba..89aefdb 100644 --- a/tests.py +++ b/tests.py @@ -18,6 +18,7 @@ from unittest import mock from django.test import TestCase from django.utils import timezone +from .management.parsers.abstractparser import ParserError from .management.parsers.ups2018 import Parser as UPS2018Parser from .models import Course, Group, Room, Source, Timetable, Year from .utils import tz_now @@ -43,12 +44,14 @@ def mock_requests_get(*args, **kwargs): return MockedResponse(response.read()) if args[0] == "https://example.org/2018": - if not kwargs["params"]: + if "params" not in kwargs or not kwargs["params"]: return mocked_response_from_file("tests/data/2018/september.html") elif kwargs["params"].get("Date") == "20181001": return mocked_response_from_file("tests/data/2018/october.html") else: - return MockedResponse("") + return mocked_response_from_file("tests/data/2018/empty.html") + + return MockedResponse("") class CourseTestCase(TestCase): @@ -581,3 +584,28 @@ class UPS2018ParserTestCase(TestCase): def test_get_update_date(self): # Pas de date de mise à jour dans ce format self.assertIsNone(self.parser.get_update_date()) + + +class UPS2018BrokenSourceTestCase(TestCase): + @mock.patch("requests.get") + def test_broken_source(self, mock_get): + mock_get.return_value = mock_requests_get("") + + source = Source.objects.create(url="https://example.org/2018") + with self.assertRaises(ParserError): + UPS2018Parser(source) + + @mock.patch("requests.get") + def test_half_broken_source(self, mock_get): + source = Source.objects.create(url="https://example.org/2018") + mock_get.side_effect = [ + mock_requests_get(""), + mock_requests_get(source.url) + ] + + parser = UPS2018Parser(source) + self.assertEqual(parser.months, [ + "September, 2018", "October, 2018", "November, 2018", + "December, 2018", "January, 2019", "February, 2019", "March, 2019", + "April, 2019", "May, 2019", "June, 2019", "July, 2019" + ]) diff --git a/tests/data/2018/empty.html b/tests/data/2018/empty.html new file mode 100644 index 0000000..dde78b6 --- /dev/null +++ b/tests/data/2018/empty.html @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.1