aboutsummaryrefslogtreecommitdiff
path: root/tests.py
blob: 24141c1f3a77908a44f9186ea78091f24ffe9de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#    Copyright (C) 2017  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
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    celcatsanitizer is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    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 django.test import TestCase
from .models import Course, Group, Timetable, Year
from .utils import tz_now


class CourseTestCase(TestCase):
    def setUp(self):
        dt = tz_now()

        self.year = Year(name="L2", slug="l2")
        self.year.save()

        self.timetable = Timetable(year=self.year, name="Test timetable 2", url="http://example.org/", slug="test-timetable2")
        self.timetable.save()

        cma = Group.objects.create(celcat_name="L1 info s2 CMA", timetable=self.timetable)
        tda2 = Group.objects.create(celcat_name="L1 info s2 TDA2", timetable=self.timetable)
        self.tpa21 = Group.objects.create(celcat_name="L1 info s2 TPA21", timetable=self.timetable)

        cmb = Group.objects.create(celcat_name="L1 info s2 CMB", timetable=self.timetable)
        tdb2 = Group.objects.create(celcat_name="L1 info s2 TDB2", timetable=self.timetable)
        self.tpb21 = Group.objects.create(celcat_name="L1 info s2 TPB21", timetable=self.timetable)

        for group in (cma, tda2, self.tpa21, cmb, tdb2, self.tpb21,):
            course = Course.objects.create(name="{0} course".format(group.name), type="cours", timetable=self.timetable, begin=dt, end=dt)
            course.groups.add(group)

    def test_get_courses_for_group(self):
        tpa21_courses = Course.objects.get_courses_for_group(self.tpa21)
        tpb21_courses = Course.objects.get_courses_for_group(self.tpb21)

        tpa21_course_names = ["L1 info s2 CMA course", "L1 info s2 TDA2 course", "L1 info s2 TPA21 course"]
        tpb21_course_names = ["L1 info s2 CMB course", "L1 info s2 TDB2 course", "L1 info s2 TPB21 course"]

        for courses, names in ((tpa21_courses, tpa21_course_names,), (tpb21_courses, tpb21_course_names,),):
            for course in courses:
                self.assertIn(course.name, names)
                names.remove(course.name)


class GroupTestCase(TestCase):
    def setUp(self):
        self.year = Year(name="L1", slug="l1")
        self.year.save()

        self.timetable = Timetable(year=self.year, name="Test timetable", url="http://example.com/", slug="test-timetable")
        self.timetable.save()

        Group.objects.create(celcat_name="L1 info s2 CMA", timetable=self.timetable)
        Group.objects.create(celcat_name="L1 info s2 TDA2", timetable=self.timetable)
        Group.objects.create(celcat_name="L1 info s2 TPA21", timetable=self.timetable)

        Group.objects.create(celcat_name="L1 info s2 CMB", timetable=self.timetable)
        Group.objects.create(celcat_name="L1 info s2 TDB2", timetable=self.timetable)
        Group.objects.create(celcat_name="L1 info s2 TPB21", timetable=self.timetable)

        Group.objects.create(celcat_name="L1 info (toutes sections et semestres confondus)", timetable=self.timetable)

        # Cas spéciaux de groupes sans semestre. Normalement un groupe
        # sans semestre ne possède pas de sous-groupe non plus, mais
        # certains cas font foirer la regex. Voici un exemple trouvé
        # dans la base de données de production.
        Group.objects.create(celcat_name="M1 GC (toutes sections et semestres "
                             "confondus)", timetable=self.timetable)

        # Doit appartenir au groupe au-dessus.
        Group.objects.create(celcat_name="M1 GC s2 GA111", timetable=self.timetable)

        # Cas spécial avec les parenthèses
        Group.objects.create(celcat_name="M1 CHI-TCCM (EM) (toutes sections et"
                             " semestres confondus)", timetable=self.timetable)
        Group.objects.create(celcat_name="M1 CHI-TCCM (EM) s2 TPA12",
                             timetable=self.timetable)

    def test_corresponds(self):
        cma = Group.objects.get(celcat_name="L1 info s2 CMA", timetable=self.timetable)
        tda2 = Group.objects.get(celcat_name="L1 info s2 TDA2", timetable=self.timetable)
        tpa21 = Group.objects.get(celcat_name="L1 info s2 TPA21", timetable=self.timetable)

        cmb = Group.objects.get(celcat_name="L1 info s2 CMB", timetable=self.timetable)
        tdb2 = Group.objects.get(celcat_name="L1 info s2 TDB2", timetable=self.timetable)
        tpb21 = Group.objects.get(celcat_name="L1 info s2 TPB21", timetable=self.timetable)

        general = Group.objects.get(celcat_name="L1 info (toutes sections et semestres confondus)", timetable=self.timetable)

        self.assertFalse(cma.corresponds_to(*tda2.group_info))
        self.assertFalse(cma.corresponds_to(*tpa21.group_info))
        self.assertFalse(tda2.corresponds_to(*tpa21.group_info))

        self.assertFalse(cmb.corresponds_to(*tdb2.group_info))
        self.assertFalse(cmb.corresponds_to(*tpb21.group_info))
        self.assertFalse(tdb2.corresponds_to(*tpb21.group_info))

        self.assertFalse(cmb.corresponds_to(*tda2.group_info))
        self.assertFalse(cmb.corresponds_to(*tpa21.group_info))
        self.assertFalse(tdb2.corresponds_to(*tpa21.group_info))

        self.assertTrue(tda2.corresponds_to(*cma.group_info))
        self.assertTrue(tpa21.corresponds_to(*cma.group_info))
        self.assertTrue(tpa21.corresponds_to(*tda2.group_info))

        self.assertTrue(tdb2.corresponds_to(*cmb.group_info))
        self.assertTrue(tpb21.corresponds_to(*cmb.group_info))
        self.assertTrue(tpb21.corresponds_to(*tdb2.group_info))

        self.assertFalse(tda2.corresponds_to(*cmb.group_info))
        self.assertFalse(tpa21.corresponds_to(*cmb.group_info))
        self.assertFalse(tpa21.corresponds_to(*tdb2.group_info))

        self.assertFalse(general.corresponds_to(*cma.group_info))
        self.assertFalse(general.corresponds_to(*cmb.group_info))
        self.assertFalse(general.corresponds_to(*tda2.group_info))
        self.assertFalse(general.corresponds_to(*tdb2.group_info))
        self.assertFalse(general.corresponds_to(*tpa21.group_info))
        self.assertFalse(general.corresponds_to(*tpb21.group_info))

        self.assertTrue(cma.corresponds_to(*general.group_info))
        self.assertTrue(cmb.corresponds_to(*general.group_info))
        self.assertTrue(tda2.corresponds_to(*general.group_info))
        self.assertTrue(tdb2.corresponds_to(*general.group_info))
        self.assertTrue(tpa21.corresponds_to(*general.group_info))
        self.assertTrue(tpb21.corresponds_to(*general.group_info))

    def test_corresponds_no_semester(self):
        general = Group.objects.get(celcat_name="M1 GC (toutes sections et "
                                    "semestres confondus)", timetable=self.timetable)
        ga111 = Group.objects.get(celcat_name="M1 GC s2 GA111",
                                  timetable=self.timetable)

        self.assertTrue(ga111.corresponds_to(*general.group_info))
        self.assertFalse(general.corresponds_to(*ga111.group_info))

    def test_correspond_parenthesis(self):
        general = Group.objects.get(celcat_name="M1 CHI-TCCM (EM) (toutes"
                                    " sections et semestres confondus)")
        a12 = Group.objects.get(celcat_name="M1 CHI-TCCM (EM) s2 TPA12")

        self.assertTrue(a12.corresponds_to(*general.group_info))
        self.assertFalse(general.corresponds_to(*a12.group_info))

    def test_get(self):
        cma = Group.objects.get(name="L1 info s2 CMA", timetable=self.timetable)
        tda2 = Group.objects.get(name="L1 info s2 TDA2", timetable=self.timetable)
        tpa21 = Group.objects.get(name="L1 info s2 TPA21", timetable=self.timetable)

        cmb = Group.objects.get(name="L1 info s2 CMB", timetable=self.timetable)
        tdb2 = Group.objects.get(name="L1 info s2 TDB2", timetable=self.timetable)
        tpb21 = Group.objects.get(name="L1 info s2 TPB21", timetable=self.timetable)

        general = Group.objects.get(celcat_name="L1 info (toutes sections et semestres confondus)", timetable=self.timetable)

        self.assertEqual(cma.celcat_name, "L1 info s2 CMA")
        self.assertEqual(tda2.celcat_name, "L1 info s2 TDA2")
        self.assertEqual(tpa21.celcat_name, "L1 info s2 TPA21")

        self.assertEqual(cmb.celcat_name, "L1 info s2 CMB")
        self.assertEqual(tdb2.celcat_name, "L1 info s2 TDB2")
        self.assertEqual(tpb21.celcat_name, "L1 info s2 TPB21")

        self.assertEqual(general.celcat_name, "L1 info (toutes sections et semestres confondus)")

    def test_parse(self):
        cma = Group.objects.get(celcat_name="L1 info s2 CMA", timetable=self.timetable)
        tda2 = Group.objects.get(celcat_name="L1 info s2 TDA2", timetable=self.timetable)
        tpa21 = Group.objects.get(celcat_name="L1 info s2 TPA21", timetable=self.timetable)

        cmb = Group.objects.get(celcat_name="L1 info s2 CMB", timetable=self.timetable)
        tdb2 = Group.objects.get(celcat_name="L1 info s2 TDB2", timetable=self.timetable)
        tpb21 = Group.objects.get(celcat_name="L1 info s2 TPB21", timetable=self.timetable)

        general = Group.objects.get(celcat_name="L1 info (toutes sections et semestres confondus)", timetable=self.timetable)

        self.assertEqual(cma.group_info, ("L1 info", 2, "A"))
        self.assertEqual(tda2.group_info, ("L1 info", 2, "A2"))
        self.assertEqual(tpa21.group_info, ("L1 info", 2, "A21"))

        self.assertEqual(cmb.group_info, ("L1 info", 2, "B"))
        self.assertEqual(tdb2.group_info, ("L1 info", 2, "B2"))
        self.assertEqual(tpb21.group_info, ("L1 info", 2, "B21"))

        self.assertEqual(general.group_info, ("L1 info", None, ""))

    def test_parse_no_semester(self):
        general = Group.objects.get(celcat_name="M1 GC (toutes sections et "
                                    "semestres confondus)", timetable=self.timetable)
        ga111 = Group.objects.get(celcat_name="M1 GC s2 GA111",
                                  timetable=self.timetable)

        self.assertEqual(general.group_info, ("M1 GC", None, ""))
        self.assertEqual(ga111.group_info, ("M1 GC", 2, "A111"))

    def test_parse_parenthesis(self):
        general = Group.objects.get(celcat_name="M1 CHI-TCCM (EM) (toutes"
                                    " sections et semestres confondus)")
        a12 = Group.objects.get(celcat_name="M1 CHI-TCCM (EM) s2 TPA12")

        self.assertEqual(general.group_info, ("M1 CHI-TCCM (EM)", None, ""))
        self.assertEqual(a12.group_info, ("M1 CHI-TCCM (EM)", 2, "A12"))