diff options
author | Alban Gruin | 2018-10-09 20:23:21 +0200 |
---|---|---|
committer | Alban Gruin | 2018-12-31 12:14:03 +0100 |
commit | 4980d9cb47c99056b2f228408bab536393e100bb (patch) | |
tree | 6e0b43c4127f932958b257b588d69d4705940ed5 /tests.py | |
parent | 070d613b29359465dc8ffdd1dc3ffb116717fc3b (diff) |
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 <alban at pa1ch dot fr>
Diffstat (limited to 'tests.py')
-rw-r--r-- | tests.py | 32 |
1 files changed, 30 insertions, 2 deletions
@@ -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("<html></html>") + return mocked_response_from_file("tests/data/2018/empty.html") + + return MockedResponse("<html></html>") 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" + ]) |