aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests.py296
-rw-r--r--tests/data/2018/empty.html50
-rw-r--r--tests/data/2018/october.html50
-rw-r--r--tests/data/2018/september.html51
4 files changed, 440 insertions, 7 deletions
diff --git a/tests.py b/tests.py
index 2568688..627602c 100644
--- a/tests.py
+++ b/tests.py
@@ -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 .management.parsers.abstractparser import ParserError
+from .management.parsers.ups2018 import Parser as UPS2018Parser
from .models import Course, Group, 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):
@@ -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)},),
@@ -330,6 +363,23 @@ class RoomTestCase(TestCase):
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 +395,235 @@ 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()
+
+ 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"},
+ 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.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(count, Course.objects.count() - 1)
+ count += 1
+
+ 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,
+ },
+ {
+ "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"
+ },
+ {
+ "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",
+ },
+ {
+ "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"
+ },
+ {
+ "text": "COURS/TD<br>L3 Info s1 CMA<br>Salle quelconque 3",
+ "name": "COURS/TD",
+ "group": self.group,
+ "notes": "Salle quelconque 3"
+ },
+ {
+ "text": "L3 Info s1 CMA<br>Salle quelconque",
+ "name": "Sans nom",
+ "group": self.group,
+ "room": self.room
+ },
+ {
+ "text": "L3 Info s1 CMA<br>Salle quelconque 3",
+ "name": "Sans nom",
+ "group": self.group,
+ "notes": "Salle quelconque 3"
+ },
+ {
+ "text": "(10:00-12:00)<br>L3 Info s1 CMA<br>Salle quelconque",
+ "name": "Sans nom",
+ "group": self.group,
+ "room": self.room
+ }
+ ]
+
+ for e in events:
+ event = get_event(
+ {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
+ "text": e["text"]},
+ 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)
+
+ 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)
+
+ self.assertEqual(count, Course.objects.count() - 1)
+ count += 1
+
+ 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))},
+ {"begin":
+ timezone.make_aware(datetime.datetime(2018, 10, 22, 10, 00, 00)),
+ "end":
+ timezone.make_aware(datetime.datetime(2018, 10, 22, 12, 00, 00))}
+ ]
+
+ 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(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"
+ }], [{
+ "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"
+ }, {
+ "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"
+ }], [], [], [], [], [], [], [], [], []])
+
+ 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>
diff --git a/tests/data/2018/october.html b/tests/data/2018/october.html
new file mode 100644
index 0000000..6f326f9
--- /dev/null
+++ b/tests/data/2018/october.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 selected="selected" 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 = [{"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"}, {"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"}];;
+}
+</script>
+
+<script>
+courses();
+do_something_else();
+</script>
diff --git a/tests/data/2018/september.html b/tests/data/2018/september.html
new file mode 100644
index 0000000..3db6cbc
--- /dev/null
+++ b/tests/data/2018/september.html
@@ -0,0 +1,51 @@
+<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 selected="selected" 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 = [{"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"}];;
+}
+</script>
+
+<script>
+courses();
+do_something_else();
+</script>