test_models.py 13.9 KB
Newer Older
Tom van Bussel's avatar
Tom van Bussel committed
1
2
3
4
5
6
import datetime

from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone

Thom Wiggers's avatar
Thom Wiggers committed
7
from activemembers.models import Committee
Tom van Bussel's avatar
Tom van Bussel committed
8
from events.models import Event, Registration
9
from mailinglists.models import MailingList
Tom van Bussel's avatar
Tom van Bussel committed
10
11
12
13
14
15
from members.models import Member


class EventTest(TestCase):
    """Tests events"""

16
    fixtures = ['members.json']
Tom van Bussel's avatar
Tom van Bussel committed
17

Thom Wiggers's avatar
Thom Wiggers committed
18
19
    @classmethod
    def setUpTestData(cls):
20
21
22
23
24
25
26
27
28
29
        cls.mailinglist = MailingList.objects.create(
            name="testmail"
        )

        cls.committee = Committee.objects.create(
            name_nl="commissie",
            name_en="committee",
            contact_mailinglist=cls.mailinglist
        )

Thom Wiggers's avatar
Thom Wiggers committed
30
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
31
32
            title_nl='testevene',
            title_en='testevent',
33
            organiser=cls.committee,
Tom van Bussel's avatar
Tom van Bussel committed
34
35
36
37
38
39
40
41
            description_en='desc',
            description_nl='besch',
            start=(timezone.now() + datetime.timedelta(hours=1)),
            end=(timezone.now() + datetime.timedelta(hours=2)),
            location_en='test location',
            location_nl='test locatie',
            map_location='test map location',
            price=0.00,
42
            fine=5.00)
Thom Wiggers's avatar
Thom Wiggers committed
43
        cls.member = Member.objects.all()[0]
Tom van Bussel's avatar
Tom van Bussel committed
44

Thom Wiggers's avatar
Thom Wiggers committed
45
    def setUp(self):
46
47
        self.mailinglist.refresh_from_db()
        self.committee.refresh_from_db()
Thom Wiggers's avatar
Thom Wiggers committed
48
49
50
51
        self.event.refresh_from_db()
        self.member.refresh_from_db()

    def test_clean_works(self):
Tom van Bussel's avatar
Tom van Bussel committed
52
53
        self.event.clean()

Thom Wiggers's avatar
Thom Wiggers committed
54
    def test_end_after_start(self):
Tom van Bussel's avatar
Tom van Bussel committed
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
        self.event.start, self.event.end = self.event.end, self.event.start
        with self.assertRaises(ValidationError):
            self.event.clean()

    def test_missing_registration_start(self):
        self.event.cancel_deadline = timezone.now()
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        with self.assertRaises(ValidationError):
            self.event.clean()

        self.event.registration_start = timezone.now()
        self.event.clean()

    def test_missing_registration_end(self):
        self.event.cancel_deadline = timezone.now()
        self.event.registration_start = timezone.now()
        with self.assertRaises(ValidationError):
            self.event.clean()

        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.clean()

    def test_missing_cancel_deadline(self):
        self.event.registration_start = timezone.now()
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        with self.assertRaises(ValidationError):
            self.event.clean()

        self.event.cancel_deadline = timezone.now()
        self.event.clean()

    def test_unnecessary_no_registration_message(self):
        self.event.registration_start = timezone.now()
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = timezone.now()
        self.event.no_registration_message_en = "Not registered"
        self.event.no_registration_message_nl = "Niet geregistreerd"
        with self.assertRaises(ValidationError):
            self.event.clean()

        self.event.no_registration_message_en = ""
        self.event.no_registration_message_nl = ""
        self.event.clean()

    def test_registration_end_after_registration_start(self):
        self.event.registration_start = (timezone.now() +
                                         datetime.timedelta(hours=1))
        self.event.registration_end = timezone.now()
        self.event.cancel_deadline = timezone.now()
        with self.assertRaises(ValidationError):
            self.event.clean()

        self.event.registration_start, self.event.registration_end = \
            self.event.registration_end, self.event.registration_start
        self.event.clean()

115
    def test_cancel_deadline_before_registration_start(self):
116
117
118
        self.event.registration_start = timezone.now()
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
119
120
121
122
123
124
125
126
        self.event.cancel_deadline = (self.event.start +
                                      datetime.timedelta(hours=1))
        with self.assertRaises(ValidationError):
            self.event.clean()

        self.event.cancel_deadline = self.event.start
        self.event.clean()

127
128
129
    def test_reached_participants_limit(self):
        self.event.max_participants = 1
        self.assertFalse(self.event.reached_participants_limit())
Tom van Bussel's avatar
Tom van Bussel committed
130

131
132
133
134
135
136
137
    def test_not_reached_participants_limit(self):
        self.event.max_participants = 1
        Registration.objects.create(event=self.event, member=self.member)
        self.assertTrue(self.event.reached_participants_limit())

    def test_registration_fine_required(self):
        self.event.registration_start = (timezone.now() -
Tom van Bussel's avatar
Tom van Bussel committed
138
139
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
140
141
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() -
Tom van Bussel's avatar
Tom van Bussel committed
142
                                      datetime.timedelta(hours=1))
143
144
        self.event.clean()
        self.event.fine = 0
Tom van Bussel's avatar
Tom van Bussel committed
145

146
147
148
149
150
        with self.assertRaises(ValidationError):
            self.event.clean()

    def test_registration_allowed(self):
        # Open
Tom van Bussel's avatar
Tom van Bussel committed
151
152
153
154
155
156
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
157
        self.assertTrue(self.event.registration_allowed)
Tom van Bussel's avatar
Tom van Bussel committed
158

159
        # No cancel
Tom van Bussel's avatar
Tom van Bussel committed
160
161
162
163
164
165
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() -
                                      datetime.timedelta(hours=1))
166
        self.assertTrue(self.event.registration_allowed)
Tom van Bussel's avatar
Tom van Bussel committed
167

168
169
170
171
172
173
174
175
176
177
        # Not yet open
        self.event.registration_start = (timezone.now() +
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=2))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.assertFalse(self.event.registration_allowed)

        # Cancel only
Tom van Bussel's avatar
Tom van Bussel committed
178
179
180
181
182
183
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() -
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
184
        self.assertFalse(self.event.registration_allowed)
Tom van Bussel's avatar
Tom van Bussel committed
185

186
        # Registration is closed
Tom van Bussel's avatar
Tom van Bussel committed
187
188
189
190
191
192
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() -
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() -
                                      datetime.timedelta(hours=1))
193
        self.assertFalse(self.event.registration_allowed)
Tom van Bussel's avatar
Tom van Bussel committed
194

195
196
197
198
199
        # Registration not needed
        self.event.registration_start = None
        self.event.registration_end = None
        self.event.cancel_deadline = None
        self.assertFalse(self.event.registration_allowed)
Tom van Bussel's avatar
Tom van Bussel committed
200

201
202
203
204
205
206
207
208
    def test_missing_orgination_mailinglist(self):
        self.event.clean()

        self.event.organiser.contact_mailinglist = None

        with self.assertRaises(ValidationError):
            self.event.clean()

209
210
211
212
213
214
215
216
217
    def test_cancellation_allowed(self):
        # Open
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.assertTrue(self.event.cancellation_allowed)
Tom van Bussel's avatar
Tom van Bussel committed
218

219
        # No cancel
220
        self.event.registration_start = (timezone.now() -
221
222
223
224
225
226
227
228
229
230
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() -
                                      datetime.timedelta(hours=1))
        # Allow since cancellation after deadline is possible
        self.assertTrue(self.event.cancellation_allowed)

        # Not yet open
        self.event.registration_start = (timezone.now() +
231
232
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() +
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
                                       datetime.timedelta(hours=2))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.assertFalse(self.event.cancellation_allowed)

        # Cancel only
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() -
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
        self.assertTrue(self.event.cancellation_allowed)

        # Registration is closed
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=2))
        self.event.registration_end = (timezone.now() -
251
252
253
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() -
                                      datetime.timedelta(hours=1))
254
255
        # Allow since cancellation after deadline is possible
        self.assertTrue(self.event.cancellation_allowed)
256

257
258
259
260
261
        # Registration not needed
        self.event.registration_start = None
        self.event.registration_end = None
        self.event.cancel_deadline = None
        self.assertFalse(self.event.cancellation_allowed)
262

Tom van Bussel's avatar
Tom van Bussel committed
263
264
265
266

class RegistrationTest(TestCase):
    """Tests event registrations"""

Thom Wiggers's avatar
Thom Wiggers committed
267
    fixtures = ['members.json', 'committees.json']
Tom van Bussel's avatar
Tom van Bussel committed
268

Thom Wiggers's avatar
Thom Wiggers committed
269
270
271
    @classmethod
    def setUpTestData(cls):
        cls.event = Event.objects.create(
Tom van Bussel's avatar
Tom van Bussel committed
272
273
            title_nl='testevene',
            title_en='testevent',
Thom Wiggers's avatar
Thom Wiggers committed
274
            organiser=Committee.objects.get(pk=1),
Tom van Bussel's avatar
Tom van Bussel committed
275
276
277
278
279
280
281
282
283
            description_en='desc',
            description_nl='besch',
            start=timezone.now(),
            end=(timezone.now() + datetime.timedelta(hours=1)),
            location_en='test location',
            location_nl='test locatie',
            map_location='test map location',
            price=0.00,
            fine=0.00)
Thom Wiggers's avatar
Thom Wiggers committed
284
285
286
        cls.member = Member.objects.all()[0]
        cls.r1 = Registration.objects.create(event=cls.event,
                                             member=cls.member)
287
288
        cls.r2 = Registration.objects.create(event=cls.event,
                                             member=cls.member)
Thom Wiggers's avatar
Thom Wiggers committed
289
290
291

    def setUp(self):
        self.r1.refresh_from_db()
Tom van Bussel's avatar
Tom van Bussel committed
292
293

    def test_is_late_registration(self):
Thom Wiggers's avatar
Thom Wiggers committed
294
        self.assertFalse(self.r1.is_late_cancellation())
Tom van Bussel's avatar
Tom van Bussel committed
295

Thom Wiggers's avatar
Thom Wiggers committed
296
297
        self.r1.date_cancelled = timezone.now()
        self.assertFalse(self.r1.is_late_cancellation())
Tom van Bussel's avatar
Tom van Bussel committed
298
299
300

        self.event.cancel_deadline = (timezone.now() +
                                      datetime.timedelta(hours=1))
Thom Wiggers's avatar
Thom Wiggers committed
301
        self.assertFalse(self.r1.is_late_cancellation())
Tom van Bussel's avatar
Tom van Bussel committed
302
303
304

        self.event.cancel_deadline = (timezone.now() -
                                      datetime.timedelta(hours=1))
Thom Wiggers's avatar
Thom Wiggers committed
305
        self.assertTrue(self.r1.is_late_cancellation())
Tom van Bussel's avatar
Tom van Bussel committed
306
307

    def test_queue_position(self):
308
309
        self.assertEqual(self.r1.queue_position, 0)
        self.assertEqual(self.r2.queue_position, 0)
Tom van Bussel's avatar
Tom van Bussel committed
310
311

        self.event.max_participants = 0
312
313
        self.assertEqual(self.r1.queue_position, 1)
        self.assertEqual(self.r2.queue_position, 2)
Tom van Bussel's avatar
Tom van Bussel committed
314
315

        self.event.max_participants = 1
316
317
        self.assertEqual(self.r1.queue_position, 0)
        self.assertEqual(self.r2.queue_position, 1)
Tom van Bussel's avatar
Tom van Bussel committed
318
319

    def test_registration_either_name_or_member(self):
Thom Wiggers's avatar
Thom Wiggers committed
320
        self.r1.clean()
Tom van Bussel's avatar
Tom van Bussel committed
321
322
323
324
325
326
327
        r2 = Registration.objects.create(event=self.event, name='test name')
        r2.clean()
        with self.assertRaises(ValidationError):
            r3 = Registration.objects.create(event=self.event,
                                             name='test name',
                                             member=self.member)
            r3.clean()
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

    def test_would_cancel_after_deadline(self):
        self.event.registration_start = (timezone.now() -
                                         datetime.timedelta(hours=1))
        self.event.registration_end = (timezone.now() -
                                       datetime.timedelta(hours=1))
        self.event.cancel_deadline = (timezone.now() -
                                      datetime.timedelta(hours=1))

        # Test situation where the event status is REGISTRATION_CLOSED
        self.assertEqual(self.r1.would_cancel_after_deadline(), True)

        self.event.registration_end = (timezone.now() +
                                       datetime.timedelta(hours=2))

        # Test situation where the event status is REGISTRATION_OPEN_NO_CANCEL
        self.assertEqual(self.r1.would_cancel_after_deadline(), True)