1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
12 from datetime import timedelta
14 from django.contrib.auth.models import User
15 from django.test import TestCase
16 from django.utils import timezone
18 from booking.models import Booking
19 from dashboard.testing_utils import make_resource_template, make_user
22 class BookingModelTestCase(TestCase):
24 Test the Booking model.
26 Creates all the scafolding needed and tests the Booking model
31 Prepare for Booking model tests.
33 Creates all the needed models, such as users, resources, and configurations
35 self.owner = User.objects.create(username='owner')
36 self.res1 = make_resource_template(name="Test template 1")
37 self.res2 = make_resource_template(name="Test template 2")
38 self.user1 = make_user(username='user1')
40 def test_start_end(self):
42 Verify the start and end fields.
44 if the start of a booking is greater or equal then the end,
45 saving should raise a ValueException
47 start = timezone.now()
48 end = start - timedelta(weeks=1)
51 Booking.objects.create,
60 Booking.objects.create,
67 def test_conflicts(self):
69 Verify conflicting dates are dealt with.
71 saving an overlapping booking on the same resource
72 should raise a ValueException
73 saving for different resources should succeed
75 start = timezone.now()
76 end = start + timedelta(weeks=1)
78 Booking.objects.create(
88 Booking.objects.create,
97 Booking.objects.create,
98 start=start + timedelta(days=1),
99 end=end - timedelta(days=1),
106 Booking.objects.create,
107 start=start - timedelta(days=1),
115 Booking.objects.create,
116 start=start - timedelta(days=1),
117 end=end - timedelta(days=1),
124 Booking.objects.create,
126 end=end + timedelta(days=1),
133 Booking.objects.create,
134 start=start + timedelta(days=1),
135 end=end + timedelta(days=1),
141 Booking.objects.create(
142 start=start - timedelta(days=1),
150 Booking.objects.create(
152 end=end + timedelta(days=1),
159 Booking.objects.create(
160 start=start - timedelta(days=2),
161 end=start - timedelta(days=1),
168 Booking.objects.create(
169 start=end + timedelta(days=1),
170 end=end + timedelta(days=2),
177 Booking.objects.create(
185 def test_extensions(self):
187 Test booking extensions.
189 saving a booking with an extended end time is allows to happen twice,
190 and each extension must be a maximum of one week long
192 start = timezone.now()
193 end = start + timedelta(weeks=1)
195 Booking.objects.create(
203 booking = Booking.objects.all().first() # should be only thing in db
205 self.assertEquals(booking.ext_count, 2)
206 booking.end = booking.end + timedelta(days=3)
210 self.fail("save() threw an exception")