Commit 83a88830 authored by Tom van Bussel's avatar Tom van Bussel
Browse files

More tests for events

parent 52866b0c
......@@ -127,6 +127,9 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
def has_fields(self):
return self.registrationinformationfield_set.count() > 0
def num_participants(self):
return self.registration_set.filter(date_cancelled=None).count()
def reached_participants_limit(self):
return (self.max_participants is not None and
self.max_participants <= self.registration_set.filter(
......
import datetime
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone
from events.models import Event, Registration
from members.models import Member
class RegistrationTest(TestCase):
"""Tests event registrations"""
fixtures = ['members.json']
def setUp(self):
self.event = Event.objects.create(
title_nl='testevene',
title_en='testevent',
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)
self.member = Member.objects.all()[0]
def test_registration_either_name_or_member(self):
r1 = Registration.objects.create(event=self.event, member=self.member)
r1.clean()
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()
import datetime
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone
from events.models import Event, Registration
from members.models import Member
class EventTest(TestCase):
"""Tests events"""
fixtures = ['members.json']
def setUp(self):
self.event = Event.objects.create(
title_nl='testevene',
title_en='testevent',
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,
fine=0.00)
self.member = Member.objects.all()[0]
def test_end_after_start(self):
self.event.clean()
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()
def test_status_registration_not_needed(self):
self.assertEqual(self.event.status, Event.REGISTRATION_NOT_NEEDED)
def test_status_registration_not_yet_open(self):
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.assertEqual(self.event.status, Event.REGISTRATION_NOT_YET_OPEN)
def test_status_registration_open(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))
self.assertEqual(self.event.status, Event.REGISTRATION_OPEN)
def test_status_registration_open_no_cancel(self):
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.assertEqual(self.event.status, Event.REGISTRATION_OPEN_NO_CANCEL)
def test_status_registration_closed_cancel_only(self):
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.assertEqual(self.event.status,
Event.REGISTRATION_CLOSED_CANCEL_ONLY)
def test_status_registration_closed(self):
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.assertEqual(self.event.status, Event.REGISTRATION_CLOSED)
def test_reached_participants_limit(self):
self.event.max_participants = 1
self.assertFalse(self.event.reached_participants_limit())
def test_not_reached_participants_limit(self):
self.event.max_participants = 1
r1 = Registration.objects.create(event=self.event, member=self.member)
r1.save()
self.assertTrue(self.event.reached_participants_limit())
class RegistrationTest(TestCase):
"""Tests event registrations"""
fixtures = ['members.json']
def setUp(self):
self.event = Event.objects.create(
title_nl='testevene',
title_en='testevent',
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)
self.member = Member.objects.all()[0]
def test_is_late_registration(self):
r1 = Registration.objects.create(event=self.event, member=self.member)
self.assertFalse(r1.is_late_cancellation())
r1.date_cancelled = timezone.now()
self.assertFalse(r1.is_late_cancellation())
self.event.cancel_deadline = (timezone.now() +
datetime.timedelta(hours=1))
self.assertFalse(r1.is_late_cancellation())
self.event.cancel_deadline = (timezone.now() -
datetime.timedelta(hours=1))
self.assertTrue(r1.is_late_cancellation())
def test_queue_position(self):
r1 = Registration.objects.create(event=self.event, member=self.member)
r1.save()
self.assertEqual(r1.queue_position(), 0)
self.event.max_participants = 0
self.assertEqual(r1.queue_position(), 1)
self.event.max_participants = 1
self.assertEqual(r1.queue_position(), 0)
def test_registration_either_name_or_member(self):
r1 = Registration.objects.create(event=self.event, member=self.member)
r1.clean()
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()
import datetime
from django.test import Client, TestCase
from django.utils import timezone
from events.models import Event, Registration, RegistrationInformationField
from members.models import Member
class RegistrationTest(TestCase):
"""Tests for registration view"""
fixtures = ['members.json']
def setUp(self):
self.event = Event.objects.create(
pk=1,
title_nl='testevene',
title_en='testevent',
description_en='desc',
description_nl='besch',
published=True,
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,
fine=0.00)
self.event.save()
self.member = Member.objects.filter(user__last_name="Wiggers").first()
self.client = Client()
self.client.force_login(self.member.user)
def test_registration_register_not_required(self):
response = self.client.post('/events/1/registration/register/',
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 0)
def test_registration_register(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))
self.event.save()
response = self.client.post('/events/1/registration/register/',
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 1)
self.assertEqual(
self.event.registration_set.first().member, self.member)
def test_registration_register_twice(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))
self.event.save()
response = self.client.post('/events/1/registration/register/',
follow=True)
self.assertEqual(response.status_code, 200)
response = self.client.post('/events/1/registration/register',
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 1)
def test_registration_register_closed(self):
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.event.save()
response = self.client.post('/events/1/registration/register/',
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 0)
def test_registration_cancel(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))
self.event.save()
r1 = Registration.objects.create(event=self.event, member=self.member)
r1.save()
response = self.client.post('/events/1/registration/cancel/',
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 0)
def test_registration_register_fields(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))
self.event.save()
field1 = RegistrationInformationField.objects.create(
pk=1,
event=self.event,
type=RegistrationInformationField.BOOLEAN_FIELD,
name_en="test bool",
name_nl="test bool",
required=False)
field1.save()
field2 = RegistrationInformationField.objects.create(
pk=2,
event=self.event,
type=RegistrationInformationField.INTEGER_FIELD,
name_en="test int",
name_nl="test int",
required=False)
field2.save()
field3 = RegistrationInformationField.objects.create(
pk=3,
event=self.event,
type=RegistrationInformationField.TEXT_FIELD,
name_en="test text",
name_nl="test text",
required=False)
field3.save()
response = self.client.post('/events/1/registration/register/',
{'info_field_1': True,
'info_field_2': 42,
'info_field_3': "text"},
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 1)
registration = self.event.registration_set.first()
self.assertEqual(field1.get_value_for(registration), True)
self.assertEqual(field2.get_value_for(registration), 42)
self.assertEqual(field3.get_value_for(registration), 'text')
def test_registration_register_field_required(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))
self.event.save()
field = RegistrationInformationField.objects.create(
event=self.event,
type=RegistrationInformationField.TEXT_FIELD,
name_en="test",
name_nl="test",
required=True)
field.save()
response = self.client.post('/events/1/registration/register/',
follow=True)
self.assertEqual(response.status_code, 200)
template_names = [template.name for template in response.templates]
self.assertIn('events/event_fields.html', template_names)
self.assertEqual(self.event.num_participants(), 0)
# This is wrong
response = self.client.post('/events/1/registration/register/',
{'test': 'test'},
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 0)
# This is correct
response = self.client.post('/events/1/registration/register/',
{'info_field_1': 'test'},
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.event.num_participants(), 1)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment