diff options
Diffstat (limited to 'tests.py')
-rw-r--r-- | tests.py | 341 |
1 files changed, 330 insertions, 11 deletions
@@ -1,4 +1,4 @@ -# Copyright (C) 2017-2018 Alban Gruin +# Copyright (C) 2017-2019 Alban Gruin # # celcatsanitizer is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published @@ -13,13 +13,46 @@ # You should have received a copy of the GNU Affero General Public License # along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. +from unittest import mock + from django.test import TestCase from django.utils import timezone -from .models import Course, Group, Room, Source, Timetable, Year +from .management.parsers.abstractparser import ParserError +from .management.parsers.ups2018 import Parser as UPS2018Parser +from .models import Course, Group, Module, Room, Source, Timetable, Year +from .templatetags.rooms import format_rooms from .utils import tz_now import datetime +import os + + +def mock_requests_get(*args, **kwargs): + class MockedResponse: + def __init__(self, content=""): + self.encoding = "utf-8" + self.content = content + self.status = 200 + + def raise_for_status(self): + return + + def mocked_response_from_file(filename): + module_dir = os.path.dirname(__file__) + filepath = os.path.join(module_dir, filename) + with open(filepath, "r") as response: + return MockedResponse(response.read()) + + if args[0] == "https://example.org/2018": + 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 mocked_response_from_file("tests/data/2018/empty.html") + + return MockedResponse("<html></html>") class CourseTestCase(TestCase): @@ -51,7 +84,7 @@ class CourseTestCase(TestCase): for group in (cma, tda2, self.tpa21, cmb, tdb2, self.tpb21,): course = Course.objects.create( name="{0} course".format(group.name), type="cours", - source=source, begin=dt, end=dt) + source=source, begin=dt, end=dt, celcat_id=0) course.groups.add(group) def test_get_courses_for_group(self): @@ -300,13 +333,13 @@ class RoomTestCase(TestCase): group = Group.objects.create(celcat_name="L1 info s2 CMA", source=self.source) - self.rooms = [Room.objects.create(name="0"), - Room.objects.create(name="1"), - Room.objects.create(name="2"), - Room.objects.create(name="3"), - Room.objects.create(name="4"), - Room.objects.create(name="5"), - Room.objects.create(name="6")] + self.rooms = [ + Room.objects.create(name=str(i)) + for i in range(5) + ] + [ + Room.objects.create(name="Amphi {}".format(i)) + for i in range(5, 7) + ] hours = [({"begin": datetime.time(hour=14, minute=0)},), ({"begin": datetime.time(hour=16, minute=0)},), @@ -326,10 +359,28 @@ class RoomTestCase(TestCase): end = begin + datetime.timedelta(hours=rn.get("duration", 2)) course = Course.objects.create(source=self.source, - begin=begin, end=end) + begin=begin, end=end, + celcat_id=0) course.groups.add(group) course.rooms.add(room) + def test_format(self): + amphis = self.rooms[-2:] + + self.assertEqual(format_rooms([]), "") + self.assertEqual(format_rooms(self.rooms[:1]), "Salle 0") + self.assertEqual(format_rooms(self.rooms[:2]), "Salles 0, 1") + self.assertEqual(format_rooms([amphis[0]]), "Amphi 5") + self.assertEqual(format_rooms(amphis), "Amphi 5, Amphi 6") + self.assertEqual(format_rooms([amphis[0]] + self.rooms[:1]), + "Amphi 5, salle 0") + self.assertEqual(format_rooms([amphis[0]] + self.rooms[:2]), + "Amphi 5, salles 0, 1") + self.assertEqual(format_rooms(amphis + self.rooms[:1]), + "Amphi 5, Amphi 6, salle 0") + self.assertEqual(format_rooms(amphis + self.rooms[:2]), + "Amphi 5, Amphi 6, salles 0, 1") + def test_qsjps(self): begin = timezone.make_aware(datetime.datetime.combine( self.day, datetime.time(hour=15, minute=0))) @@ -345,3 +396,271 @@ class RoomTestCase(TestCase): self.assertNotIn(self.rooms[4], rooms) self.assertIn(self.rooms[5], rooms) self.assertIn(self.rooms[6], rooms) + + +class UPS2018ParserTestCase(TestCase): + @mock.patch("requests.get", side_effect=mock_requests_get) + def setUp(self, *args, **kwargs): + source = Source.objects.create(url="https://example.org/2018") + self.room = Room.objects.create(name="Salle quelconque") + self.room2 = Room.objects.create(name="Salle quelconque 2") + + self.group = Group(celcat_name="L3 Info s1 CMA") + self.group.source = source + self.group.save() + + self.parser = UPS2018Parser(source) + + def test_get_event(self): + get_event = self.parser._Parser__get_event + count = Course.objects.count() + module_count = Module.objects.count() + + event = get_event( + {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00", + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque;AAA" + "<br>L3 Info s1 CMA;L3 Info s1 TDA2<br>" + "Salle quelconque;Salle quelconque 2<br>Commentaire", "id": "0", + "tag": ["abc", "def", "AAA"]}, + timezone.make_aware(datetime.datetime(2018, 9, 21)), + timezone.make_aware(datetime.datetime(2018, 9, 1)), + timezone.make_aware(datetime.datetime(2018, 10, 1)), + 2018, 38) + + ngroup = Group.objects.filter(name="L3 Info s1 TDA2").first() + self.assertIsNotNone(ngroup) + + self.assertEqual(event.name, "Cours quelconque, AAA") + self.assertEqual(event.type, "COURS/TD") + self.assertIn(self.group, event.groups.all()) + self.assertIn(ngroup, event.groups.all()) + self.assertEqual(event.groups.count(), 2) + self.assertIn(self.room, event.rooms.all()) + self.assertIn(self.room2, event.rooms.all()) + self.assertEqual(event.rooms.count(), 2) + self.assertEqual(event.notes, "Commentaire") + self.assertEqual(event.celcat_id, 0) + self.assertEqual(event.begin, timezone.make_aware( + datetime.datetime(2018, 9, 21, 10, 0, 0))) + self.assertEqual(event.end, timezone.make_aware( + datetime.datetime(2018, 9, 21, 12, 0, 0))) + self.assertEqual(event.module.name, "AAA") + + count += 1 + module_count += 1 + + self.assertEqual(count, Course.objects.count()) + self.assertEqual(module_count, Module.objects.count()) + + events = [ + { + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque" + "<br>L3 Info s1 CMA<br>Salle quelconque", + "name": "Cours quelconque", "type": "COURS/TD", + "group": self.group, + "room": self.room, + "id": "1", + "tag": ["aaa", "Cours"], + "meta": 1 + }, + { + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque" + "<br>L3 Info s1 TDA2<br>Salle quelconque 3", + "name": "Cours quelconque", + "type": "COURS/TD", + "group": ngroup, + "notes": "Salle quelconque 3", + "id": "2" + }, + { + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque" + "<br>L3 Info s1 CMA<br>Salle quelconque 3<br>Commentaire", + "name": "Cours quelconque", + "type": "COURS/TD", + "group": self.group, + "notes": "Salle quelconque 3\nCommentaire", + "id": "3" + }, + { + "text": "(10:00-12:00)<br>COURS/TD" + "<br>L3 Info s1 CMA<br>Salle quelconque 3", + "name": "COURS/TD", + "group": self.group, + "notes": "Salle quelconque 3", + "id": "4" + }, + { + "text": "COURS/TD<br>L3 Info s1 CMA<br>Salle quelconque 3", + "name": "COURS/TD", + "group": self.group, + "notes": "Salle quelconque 3", + "id": "5" + }, + { + "text": "L3 Info s1 CMA<br>Salle quelconque", + "name": "Sans nom", + "group": self.group, + "room": self.room, + "id": "6" + }, + { + "text": "L3 Info s1 CMA<br>Salle quelconque 3", + "name": "Sans nom", + "group": self.group, + "notes": "Salle quelconque 3", + "id": "7" + }, + { + "text": "(10:00-12:00)<br>L3 Info s1 CMA<br>Salle quelconque", + "name": "Sans nom", + "group": self.group, + "room": self.room, + "id": "8" + } + ] + + for e in events: + event = get_event( + {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00", + "text": e["text"], "id": e["id"], "tag": e.get("tag", [])}, + timezone.make_aware(datetime.datetime(2018, 9, 21)), + timezone.make_aware(datetime.datetime(2018, 9, 1)), + timezone.make_aware(datetime.datetime(2018, 10, 1)), + 2018, 38) + + self.assertEqual(event.name, e["name"]) + self.assertIn(e["group"], event.groups.all()) + self.assertEqual(event.groups.count(), 1) + self.assertEqual(str(event.celcat_id), e["id"]) + + if "type" in e: + self.assertEqual(event.type, e["type"]) + else: + self.assertIsNone(event.type) + + if "room" in e: + self.assertIn(e["room"], event.rooms.all()) + self.assertEqual(event.rooms.count(), 1) + else: + self.assertEqual(event.rooms.count(), 0) + + if "notes" in e: + self.assertEqual(event.notes, e["notes"]) + else: + self.assertIsNone(event.notes) + + if "tag" in e: + self.assertEqual(event.module.name, e["tag"][e["meta"]]) + module_count += 1 + else: + self.assertIsNone(event.module) + + count += 1 + + self.assertEqual(count, Course.objects.count()) + self.assertEqual(module_count, Module.objects.count()) + + event = get_event( + {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00", + "text": "Global Event"}, + timezone.make_aware(datetime.datetime(2018, 9, 21)), + timezone.make_aware(datetime.datetime(2018, 9, 1)), + timezone.make_aware(datetime.datetime(2018, 10, 1)), + 2018, 38) + self.assertIsNone(event) + self.assertEqual(count, Course.objects.count()) + + event = get_event( + {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00", + "text": "L3 Info s1 CMA<br>Salle quelconque 2"}, + timezone.make_aware(datetime.datetime(2018, 9, 21)), + timezone.make_aware(datetime.datetime(2018, 9, 1)), + timezone.make_aware(datetime.datetime(2018, 10, 1)), + 2018, 39) + self.assertIsNone(event) + self.assertEqual(count, Course.objects.count()) + + @mock.patch("requests.get", side_effect=mock_requests_get) + def test_get_events(self, *args, **kwargs): + self.parser.get_source() + courses = [ + {"begin": + timezone.make_aware(datetime.datetime(2018, 9, 21, 10, 00, 00)), + "end": + timezone.make_aware(datetime.datetime(2018, 9, 21, 12, 00, 00)), + "id": 0}, + {"begin": + timezone.make_aware(datetime.datetime(2018, 10, 22, 10, 00, 00)), + "end": + timezone.make_aware(datetime.datetime(2018, 10, 22, 12, 00, 00)), + "id": 2} + ] + + for i, course in enumerate(self.parser.get_events( + timezone.make_aware(datetime.datetime(2018, 9, 21)))): + self.assertEqual(course.name, "Cours quelconque") + self.assertEqual(course.type, "COURS/TD") + self.assertIn(self.group, course.groups.all()) + self.assertEqual(course.groups.count(), 1) + self.assertIn(self.room, course.rooms.all()) + self.assertEqual(course.rooms.count(), 1) + self.assertIsNone(course.notes) + self.assertEqual(course.begin, courses[i]["begin"]) + self.assertEqual(course.end, courses[i]["end"]) + self.assertEqual(course.celcat_id, courses[i]["id"]) + self.assertEqual(course.module, None) + + self.assertEqual(i, len(courses) - 1) + + @mock.patch("requests.get", side_effect=mock_requests_get) + def test_get_source(self, *args, **kwargs): + events = self.parser.get_source() + self.assertEquals(events, [ + [{ + "start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00", + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque" + "<br>L3 Info s1 CMA<br>Salle quelconque", + "id": "0", + "tag": [], + }], [{ + "start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00", + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque" + "<br>L3 Info s1 CMA<br>Salle quelconque", + "id": "1", + "tag": [], + }, { + "start": "2018-10-22T10:00:00", "end": "2018-10-22T12:00:00", + "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque" + "<br>L3 Info s1 CMA<br>Salle quelconque", + "id": "2", + "tag": [], + }], [], [], [], [], [], [], [], [], []]) + + 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" + ]) |