aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2018-10-09 20:23:21 +0200
committerAlban Gruin2018-12-31 12:14:03 +0100
commit4980d9cb47c99056b2f228408bab536393e100bb (patch)
tree6e0b43c4127f932958b257b588d69d4705940ed5
parent070d613b29359465dc8ffdd1dc3ffb116717fc3b (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>
-rw-r--r--tests.py32
-rw-r--r--tests/data/2018/empty.html50
2 files changed, 80 insertions, 2 deletions
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("<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"
+ ])
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 @@
+<script>
+function do_something() {
+alert("something");
+}
+</script>
+
+ <option value="August, 2017">August, 2017</option>
+ <option value="September, 2017">September, 2017</option>
+ <option value="October, 2017">October, 2017</option>
+ <option value="November, 2017">November, 2017</option>
+ <option value="December, 2017">December, 2017</option>
+ <option value="January, 2018">January, 2018</option>
+ <option value="February, 2018">February, 2018</option>
+ <option value="March, 2018">March, 2018</option>
+ <option value="April, 2018">April, 2018</option>
+ <option value="May, 2018">May, 2018</option>
+ <option value="June, 2018">June, 2018</option>
+ <option value="July, 2018">July, 2018</option>
+ <option value="August, 2018">August, 2018</option>
+ <option value="September, 2018">September, 2018</option>
+ <option value="October, 2018">October, 2018</option>
+ <option value="November, 2018">November, 2018</option>
+ <option value="December, 2018">December, 2018</option>
+ <option value="January, 2019">January, 2019</option>
+ <option value="February, 2019">February, 2019</option>
+ <option value="March, 2019">March, 2019</option>
+ <option value="April, 2019">April, 2019</option>
+ <option value="May, 2019">May, 2019</option>
+ <option value="June, 2019">June, 2019</option>
+ <option value="July, 2019">July, 2019</option>
+
+<script>
+function do_something_else() {
+var v = "a variable";
+var vv = "another_variable";
+do_something();
+}
+</script>
+
+<script>
+function courses() {
+var v = {};
+v.events.list = [];;
+}
+</script>
+
+<script>
+courses();
+do_something_else();
+</script>