6 Commits

Author SHA1 Message Date
  Alban Gruin dfdfeb2d70 tests: [WIP] on dirait que ça a amélioré des trucs 4 months ago
  Alban Gruin 1192140846 Merge branch 'ag/tests' into ag/parser 4 months ago
  Alban Gruin 44e17c23fc tests: test du tag format_rooms 9 months ago
  Alban Gruin 4980d9cb47 tests: test du comportement du parseur lorsque la source est mauvaise 8 months ago
  Alban Gruin 070d613b29 tests: vérification du nombre de cours dans le test du parseur 8 months ago
  Alban Gruin 0bafcacdad tests: ajout de tests pour le parseur UPS2018 9 months ago
4 changed files with 447 additions and 9 deletions
  1. 296
    9
      tests.py
  2. 50
    0
      tests/data/2018/empty.html
  3. 50
    0
      tests/data/2018/october.html
  4. 51
    0
      tests/data/2018/september.html

+ 296
- 9
tests.py View File

@@ -13,13 +13,46 @@
13 13
 #    You should have received a copy of the GNU Affero General Public License
14 14
 #    along with celcatsanitizer.  If not, see <http://www.gnu.org/licenses/>.
15 15
 
16
+from unittest import mock
17
+
16 18
 from django.test import TestCase
17 19
 from django.utils import timezone
18 20
 
21
+from .management.parsers.abstractparser import ParserError
22
+from .management.parsers.ups2018 import Parser as UPS2018Parser
19 23
 from .models import Course, Group, Room, Source, Timetable, Year
24
+from .templatetags.rooms import format_rooms
20 25
 from .utils import tz_now
21 26
 
22 27
 import datetime
28
+import os
29
+
30
+
31
+def mock_requests_get(*args, **kwargs):
32
+    class MockedResponse:
33
+        def __init__(self, content=""):
34
+            self.encoding = "utf-8"
35
+            self.content = content
36
+            self.status = 200
37
+
38
+        def raise_for_status(self):
39
+            return
40
+
41
+    def mocked_response_from_file(filename):
42
+        module_dir = os.path.dirname(__file__)
43
+        filepath = os.path.join(module_dir, filename)
44
+        with open(filepath, "r") as response:
45
+            return MockedResponse(response.read())
46
+
47
+    if args[0] == "https://example.org/2018":
48
+        if "params" not in kwargs or not kwargs["params"]:
49
+            return mocked_response_from_file("tests/data/2018/september.html")
50
+        elif kwargs["params"].get("Date") == "20181001":
51
+            return mocked_response_from_file("tests/data/2018/october.html")
52
+        else:
53
+            return mocked_response_from_file("tests/data/2018/empty.html")
54
+
55
+    return MockedResponse("<html></html>")
23 56
 
24 57
 
25 58
 class CourseTestCase(TestCase):
@@ -51,7 +84,7 @@ class CourseTestCase(TestCase):
51 84
         for group in (cma, tda2, self.tpa21, cmb, tdb2, self.tpb21,):
52 85
             course = Course.objects.create(
53 86
                 name="{0} course".format(group.name), type="cours",
54
-                source=source, begin=dt, end=dt)
87
+                source=source, begin=dt, end=dt, celcat_id=0)
55 88
             course.groups.add(group)
56 89
 
57 90
     def test_get_courses_for_group(self):
@@ -300,13 +333,13 @@ class RoomTestCase(TestCase):
300 333
         group = Group.objects.create(celcat_name="L1 info s2 CMA",
301 334
                                      source=self.source)
302 335
 
303
-        self.rooms = [Room.objects.create(name="0"),
304
-                      Room.objects.create(name="1"),
305
-                      Room.objects.create(name="2"),
306
-                      Room.objects.create(name="3"),
307
-                      Room.objects.create(name="4"),
308
-                      Room.objects.create(name="5"),
309
-                      Room.objects.create(name="6")]
336
+        self.rooms = [
337
+            Room.objects.create(name=str(i))
338
+            for i in range(5)
339
+        ] + [
340
+            Room.objects.create(name="Amphi {}".format(i))
341
+            for i in range(5, 7)
342
+        ]
310 343
 
311 344
         hours = [({"begin": datetime.time(hour=14, minute=0)},),
312 345
                  ({"begin": datetime.time(hour=16, minute=0)},),
@@ -326,10 +359,28 @@ class RoomTestCase(TestCase):
326 359
                 end = begin + datetime.timedelta(hours=rn.get("duration", 2))
327 360
 
328 361
                 course = Course.objects.create(source=self.source,
329
-                                               begin=begin, end=end)
362
+                                               begin=begin, end=end,
363
+                                               celcat_id=0)
330 364
                 course.groups.add(group)
331 365
                 course.rooms.add(room)
332 366
 
367
+    def test_format(self):
368
+        amphis = self.rooms[-2:]
369
+
370
+        self.assertEqual(format_rooms([]), "")
371
+        self.assertEqual(format_rooms(self.rooms[:1]), "Salle 0")
372
+        self.assertEqual(format_rooms(self.rooms[:2]), "Salles 0, 1")
373
+        self.assertEqual(format_rooms([amphis[0]]), "Amphi 5")
374
+        self.assertEqual(format_rooms(amphis), "Amphi 5, Amphi 6")
375
+        self.assertEqual(format_rooms([amphis[0]] + self.rooms[:1]),
376
+                         "Amphi 5, salle 0")
377
+        self.assertEqual(format_rooms([amphis[0]] + self.rooms[:2]),
378
+                         "Amphi 5, salles 0, 1")
379
+        self.assertEqual(format_rooms(amphis + self.rooms[:1]),
380
+                         "Amphi 5, Amphi 6, salle 0")
381
+        self.assertEqual(format_rooms(amphis + self.rooms[:2]),
382
+                         "Amphi 5, Amphi 6, salles 0, 1")
383
+
333 384
     def test_qsjps(self):
334 385
         begin = timezone.make_aware(datetime.datetime.combine(
335 386
             self.day, datetime.time(hour=15, minute=0)))
@@ -345,3 +396,239 @@ class RoomTestCase(TestCase):
345 396
         self.assertNotIn(self.rooms[4], rooms)
346 397
         self.assertIn(self.rooms[5], rooms)
347 398
         self.assertIn(self.rooms[6], rooms)
399
+
400
+
401
+class UPS2018ParserTestCase(TestCase):
402
+    @mock.patch("requests.get", side_effect=mock_requests_get)
403
+    def setUp(self, *args, **kwargs):
404
+        source = Source.objects.create(url="https://example.org/2018")
405
+        self.room = Room.objects.create(name="Salle quelconque")
406
+        self.room2 = Room.objects.create(name="Salle quelconque 2")
407
+
408
+        self.group = Group(celcat_name="L3 Info s1 CMA")
409
+        self.group.source = source
410
+        self.group.save()
411
+
412
+        self.parser = UPS2018Parser(source)
413
+
414
+    def test_get_event(self):
415
+        get_event = self.parser._Parser__get_event
416
+        count = Course.objects.count()
417
+
418
+        event = get_event(
419
+            {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
420
+             "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque;AAA"
421
+             "<br>L3 Info s1 CMA;L3 Info s1 TDA2<br>"
422
+             "Salle quelconque;Salle quelconque 2<br>Commentaire", "id": "0",
423
+             "tag": []},
424
+            timezone.make_aware(datetime.datetime(2018, 9, 21)),
425
+            timezone.make_aware(datetime.datetime(2018, 9, 1)),
426
+            timezone.make_aware(datetime.datetime(2018, 10, 1)),
427
+            2018, 38)
428
+
429
+        ngroup = Group.objects.filter(name="L3 Info s1 TDA2").first()
430
+        self.assertIsNotNone(ngroup)
431
+
432
+        self.assertEqual(event.name, "Cours quelconque, AAA")
433
+        self.assertEqual(event.type, "COURS/TD")
434
+        self.assertIn(self.group, event.groups.all())
435
+        self.assertIn(ngroup, event.groups.all())
436
+        self.assertEqual(event.groups.count(), 2)
437
+        self.assertIn(self.room, event.rooms.all())
438
+        self.assertIn(self.room2, event.rooms.all())
439
+        self.assertEqual(event.rooms.count(), 2)
440
+        self.assertEqual(event.notes, "Commentaire")
441
+        self.assertEqual(event.begin, timezone.make_aware(
442
+            datetime.datetime(2018, 9, 21, 10, 0, 0)))
443
+        self.assertEqual(event.end, timezone.make_aware(
444
+            datetime.datetime(2018, 9, 21, 12, 0, 0)))
445
+
446
+        self.assertEqual(count, Course.objects.count() - 1)
447
+        count += 1
448
+
449
+        events = [
450
+            {
451
+                "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque"
452
+                "<br>L3 Info s1 CMA<br>Salle quelconque",
453
+                "name": "Cours quelconque", "type": "COURS/TD",
454
+                "group": self.group,
455
+                "room": self.room,
456
+            },
457
+            {
458
+                "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque"
459
+                "<br>L3 Info s1 TDA2<br>Salle quelconque 3",
460
+                "name": "Cours quelconque",
461
+                "type": "COURS/TD",
462
+                "group": ngroup,
463
+                "notes": "Salle quelconque 3"
464
+            },
465
+            {
466
+                "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque"
467
+                "<br>L3 Info s1 CMA<br>Salle quelconque 3<br>Commentaire",
468
+                "name": "Cours quelconque",
469
+                "type": "COURS/TD",
470
+                "group": self.group,
471
+                "notes": "Salle quelconque 3\nCommentaire",
472
+            },
473
+            {
474
+                "text": "(10:00-12:00)<br>COURS/TD"
475
+                "<br>L3 Info s1 CMA<br>Salle quelconque 3",
476
+                "name": "COURS/TD",
477
+                "group": self.group,
478
+                "notes": "Salle quelconque 3"
479
+            },
480
+            {
481
+                "text": "COURS/TD<br>L3 Info s1 CMA<br>Salle quelconque 3",
482
+                "name": "COURS/TD",
483
+                "group": self.group,
484
+                "notes": "Salle quelconque 3"
485
+            },
486
+            {
487
+                "text": "L3 Info s1 CMA<br>Salle quelconque",
488
+                "name": "Sans nom",
489
+                "group": self.group,
490
+                "room": self.room
491
+            },
492
+            {
493
+                "text": "L3 Info s1 CMA<br>Salle quelconque 3",
494
+                "name": "Sans nom",
495
+                "group": self.group,
496
+                "notes": "Salle quelconque 3"
497
+            },
498
+            {
499
+                "text": "(10:00-12:00)<br>L3 Info s1 CMA<br>Salle quelconque",
500
+                "name": "Sans nom",
501
+                "group": self.group,
502
+                "room": self.room
503
+            }
504
+        ]
505
+
506
+        for e in events:
507
+            event = get_event(
508
+                {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
509
+                 "text": e["text"]},
510
+                timezone.make_aware(datetime.datetime(2018, 9, 21)),
511
+                timezone.make_aware(datetime.datetime(2018, 9, 1)),
512
+                timezone.make_aware(datetime.datetime(2018, 10, 1)),
513
+                2018, 38)
514
+
515
+            self.assertEqual(event.name, e["name"])
516
+            self.assertIn(e["group"], event.groups.all())
517
+            self.assertEqual(event.groups.count(), 1)
518
+
519
+            if "type" in e:
520
+                self.assertEqual(event.type, e["type"])
521
+            else:
522
+                self.assertIsNone(event.type)
523
+
524
+            if "room" in e:
525
+                self.assertIn(e["room"], event.rooms.all())
526
+                self.assertEqual(event.rooms.count(), 1)
527
+            else:
528
+                self.assertEqual(event.rooms.count(), 0)
529
+
530
+            if "notes" in e:
531
+                self.assertEqual(event.notes, e["notes"])
532
+            else:
533
+                self.assertIsNone(event.notes)
534
+
535
+            self.assertEqual(count, Course.objects.count() - 1)
536
+            count += 1
537
+
538
+        event = get_event(
539
+            {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
540
+             "text": "Global Event"},
541
+            timezone.make_aware(datetime.datetime(2018, 9, 21)),
542
+            timezone.make_aware(datetime.datetime(2018, 9, 1)),
543
+            timezone.make_aware(datetime.datetime(2018, 10, 1)),
544
+            2018, 38)
545
+        self.assertIsNone(event)
546
+        self.assertEqual(count, Course.objects.count())
547
+
548
+        event = get_event(
549
+            {"start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
550
+             "text": "L3 Info s1 CMA<br>Salle quelconque 2"},
551
+            timezone.make_aware(datetime.datetime(2018, 9, 21)),
552
+            timezone.make_aware(datetime.datetime(2018, 9, 1)),
553
+            timezone.make_aware(datetime.datetime(2018, 10, 1)),
554
+            2018, 39)
555
+        self.assertIsNone(event)
556
+        self.assertEqual(count, Course.objects.count())
557
+
558
+    @mock.patch("requests.get", side_effect=mock_requests_get)
559
+    def test_get_events(self, *args, **kwargs):
560
+        self.parser.get_source()
561
+        courses = [
562
+            {"begin":
563
+             timezone.make_aware(datetime.datetime(2018, 9, 21, 10, 00, 00)),
564
+             "end":
565
+             timezone.make_aware(datetime.datetime(2018, 9, 21, 12, 00, 00))},
566
+            {"begin":
567
+             timezone.make_aware(datetime.datetime(2018, 10, 22, 10, 00, 00)),
568
+             "end":
569
+             timezone.make_aware(datetime.datetime(2018, 10, 22, 12, 00, 00))}
570
+        ]
571
+
572
+        for i, course in enumerate(self.parser.get_events(
573
+                timezone.make_aware(datetime.datetime(2018, 9, 21)))):
574
+            self.assertEqual(course.name, "Cours quelconque")
575
+            self.assertEqual(course.type, "COURS/TD")
576
+            self.assertIn(self.group, course.groups.all())
577
+            self.assertEqual(course.groups.count(), 1)
578
+            self.assertIn(self.room, course.rooms.all())
579
+            self.assertEqual(course.rooms.count(), 1)
580
+            self.assertIsNone(course.notes)
581
+            self.assertEqual(course.begin, courses[i]["begin"])
582
+            self.assertEqual(course.end, courses[i]["end"])
583
+
584
+        self.assertEqual(i, len(courses) - 1)
585
+
586
+    @mock.patch("requests.get", side_effect=mock_requests_get)
587
+    def test_get_source(self, *args, **kwargs):
588
+        events = self.parser.get_source()
589
+        self.assertEquals(events, [
590
+            [{
591
+                "start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
592
+                "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque"
593
+                "<br>L3 Info s1 CMA<br>Salle quelconque",
594
+                "id": "0",
595
+            }], [{
596
+                "start": "2018-09-21T10:00:00", "end": "2018-09-21T12:00:00",
597
+                "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque"
598
+                "<br>L3 Info s1 CMA<br>Salle quelconque",
599
+                "id": "1",
600
+            }, {
601
+                "start": "2018-10-22T10:00:00", "end": "2018-10-22T12:00:00",
602
+                "text": "(10:00-12:00)<br>COURS/TD<br>Cours quelconque"
603
+                "<br>L3 Info s1 CMA<br>Salle quelconque",
604
+                "id": "2",
605
+            }], [], [], [], [], [], [], [], [], []])
606
+
607
+    def test_get_update_date(self):
608
+        # Pas de date de mise à jour dans ce format
609
+        self.assertIsNone(self.parser.get_update_date())
610
+
611
+
612
+class UPS2018BrokenSourceTestCase(TestCase):
613
+    @mock.patch("requests.get")
614
+    def test_broken_source(self, mock_get):
615
+        mock_get.return_value = mock_requests_get("")
616
+
617
+        source = Source.objects.create(url="https://example.org/2018")
618
+        with self.assertRaises(ParserError):
619
+            UPS2018Parser(source)
620
+
621
+    @mock.patch("requests.get")
622
+    def test_half_broken_source(self, mock_get):
623
+        source = Source.objects.create(url="https://example.org/2018")
624
+        mock_get.side_effect = [
625
+            mock_requests_get(""),
626
+            mock_requests_get(source.url)
627
+        ]
628
+
629
+        parser = UPS2018Parser(source)
630
+        self.assertEqual(parser.months, [
631
+            "September, 2018", "October, 2018", "November, 2018",
632
+            "December, 2018", "January, 2019", "February, 2019", "March, 2019",
633
+            "April, 2019", "May, 2019", "June, 2019", "July, 2019"
634
+        ])

+ 50
- 0
tests/data/2018/empty.html View File

@@ -0,0 +1,50 @@
1
+<script>
2
+function do_something() {
3
+alert("something");
4
+}
5
+</script>
6
+
7
+			<option value="August, 2017">August, 2017</option>
8
+			<option value="September, 2017">September, 2017</option>
9
+			<option value="October, 2017">October, 2017</option>
10
+			<option value="November, 2017">November, 2017</option>
11
+			<option value="December, 2017">December, 2017</option>
12
+			<option value="January, 2018">January, 2018</option>
13
+			<option value="February, 2018">February, 2018</option>
14
+			<option value="March, 2018">March, 2018</option>
15
+			<option value="April, 2018">April, 2018</option>
16
+			<option value="May, 2018">May, 2018</option>
17
+			<option value="June, 2018">June, 2018</option>
18
+			<option value="July, 2018">July, 2018</option>
19
+			<option value="August, 2018">August, 2018</option>
20
+			<option value="September, 2018">September, 2018</option>
21
+			<option value="October, 2018">October, 2018</option>
22
+			<option value="November, 2018">November, 2018</option>
23
+			<option value="December, 2018">December, 2018</option>
24
+			<option value="January, 2019">January, 2019</option>
25
+			<option value="February, 2019">February, 2019</option>
26
+			<option value="March, 2019">March, 2019</option>
27
+			<option value="April, 2019">April, 2019</option>
28
+			<option value="May, 2019">May, 2019</option>
29
+			<option value="June, 2019">June, 2019</option>
30
+			<option value="July, 2019">July, 2019</option>
31
+
32
+<script>
33
+function do_something_else() {
34
+var v = "a variable";
35
+var vv = "another_variable";
36
+do_something();
37
+}
38
+</script>
39
+
40
+<script>
41
+function courses() {
42
+var v = {};
43
+v.events.list = [];;
44
+}
45
+</script>
46
+
47
+<script>
48
+courses();
49
+do_something_else();
50
+</script>

+ 50
- 0
tests/data/2018/october.html View File

@@ -0,0 +1,50 @@
1
+<script>
2
+function do_something() {
3
+alert("something");
4
+}
5
+</script>
6
+
7
+			<option value="August, 2017">August, 2017</option>
8
+			<option value="September, 2017">September, 2017</option>
9
+			<option value="October, 2017">October, 2017</option>
10
+			<option value="November, 2017">November, 2017</option>
11
+			<option value="December, 2017">December, 2017</option>
12
+			<option value="January, 2018">January, 2018</option>
13
+			<option value="February, 2018">February, 2018</option>
14
+			<option value="March, 2018">March, 2018</option>
15
+			<option value="April, 2018">April, 2018</option>
16
+			<option value="May, 2018">May, 2018</option>
17
+			<option value="June, 2018">June, 2018</option>
18
+			<option value="July, 2018">July, 2018</option>
19
+			<option value="August, 2018">August, 2018</option>
20
+			<option value="September, 2018">September, 2018</option>
21
+			<option selected="selected" value="October, 2018">October, 2018</option>
22
+			<option value="November, 2018">November, 2018</option>
23
+			<option value="December, 2018">December, 2018</option>
24
+			<option value="January, 2019">January, 2019</option>
25
+			<option value="February, 2019">February, 2019</option>
26
+			<option value="March, 2019">March, 2019</option>
27
+			<option value="April, 2019">April, 2019</option>
28
+			<option value="May, 2019">May, 2019</option>
29
+			<option value="June, 2019">June, 2019</option>
30
+			<option value="July, 2019">July, 2019</option>
31
+
32
+<script>
33
+function do_something_else() {
34
+var v = "a variable";
35
+var vv = "another_variable";
36
+do_something();
37
+}
38
+</script>
39
+
40
+<script>
41
+function courses() {
42
+var v = {};
43
+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", "id": "1"}, {"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"}];;
44
+}
45
+</script>
46
+
47
+<script>
48
+courses();
49
+do_something_else();
50
+</script>

+ 51
- 0
tests/data/2018/september.html View File

@@ -0,0 +1,51 @@
1
+<script>
2
+function do_something() {
3
+alert("something");
4
+}
5
+</script>
6
+
7
+			<option value="August, 2017">August, 2017</option>
8
+			<option value="September, 2017">September, 2017</option>
9
+			<option value="October, 2017">October, 2017</option>
10
+			<option value="November, 2017">November, 2017</option>
11
+			<option value="December, 2017">December, 2017</option>
12
+			<option value="January, 2018">January, 2018</option>
13
+			<option value="February, 2018">February, 2018</option>
14
+			<option value="March, 2018">March, 2018</option>
15
+			<option value="April, 2018">April, 2018</option>
16
+			<option value="May, 2018">May, 2018</option>
17
+			<option value="June, 2018">June, 2018</option>
18
+			<option value="July, 2018">July, 2018</option>
19
+			<option value="August, 2018">August, 2018</option>
20
+			<option selected="selected" value="September, 2018">September, 2018</option>
21
+			<option value="October, 2018">October, 2018</option>
22
+			<option value="November, 2018">November, 2018</option>
23
+			<option value="December, 2018">December, 2018</option>
24
+			<option value="January, 2019">January, 2019</option>
25
+			<option value="February, 2019">February, 2019</option>
26
+			<option value="March, 2019">March, 2019</option>
27
+			<option value="April, 2019">April, 2019</option>
28
+			<option value="May, 2019">May, 2019</option>
29
+			<option value="June, 2019">June, 2019</option>
30
+			<option value="July, 2019">July, 2019</option>
31
+
32
+<script>
33
+  function do_something_else() {
34
+      var v = "a variable";
35
+      var vv = "another_variable";
36
+
37
+      do_something();
38
+  }
39
+</script>
40
+
41
+<script>
42
+function courses() {
43
+var v = {};
44
+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", "id": "0"}];;
45
+}
46
+</script>
47
+
48
+<script>
49
+courses();
50
+do_something_else();
51
+</script>