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 Permission
15 from django.test import TestCase
16 from django.utils import timezone
18 from booking.models import *
19 from resource_inventory.models import ResourceBundle, GenericResourceBundle, ConfigBundle
21 class BookingModelTestCase(TestCase):
24 self.owner = User.objects.create(username='owner')
26 self.res1 = ResourceBundle.objects.create(
27 template=GenericResourceBundle.objects.create(name="gbundle" + str(self.count))
30 self.res2 = ResourceBundle.objects.create(
31 template=GenericResourceBundle.objects.create(name="gbundle2" + str(self.count))
34 self.user1 = User.objects.create(username='user1')
36 self.add_booking_perm = Permission.objects.get(codename='add_booking')
37 self.user1.user_permissions.add(self.add_booking_perm)
39 self.user1 = User.objects.get(pk=self.user1.id)
40 self.config_bundle = ConfigBundle.objects.create(owner=self.user1, name="test config")
42 def test_start_end(self):
44 if the start of a booking is greater or equal then the end, saving should raise a
47 start = timezone.now()
48 end = start - timedelta(weeks=1)
51 Booking.objects.create,
56 config_bundle=self.config_bundle
61 Booking.objects.create,
66 config_bundle=self.config_bundle
69 def test_conflicts(self):
71 saving an overlapping booking on the same resource should raise a ValueException
72 saving for different resources should succeed
74 start = timezone.now()
75 end = start + timedelta(weeks=1)
77 Booking.objects.create(
82 config_bundle=self.config_bundle
88 Booking.objects.create,
93 config_bundle=self.config_bundle
97 Booking.objects.create,
98 start=start + timedelta(days=1),
99 end=end - timedelta(days=1),
102 config_bundle=self.config_bundle
107 Booking.objects.create,
108 start=start - timedelta(days=1),
112 config_bundle=self.config_bundle
117 Booking.objects.create,
118 start=start - timedelta(days=1),
119 end=end - timedelta(days=1),
122 config_bundle=self.config_bundle
127 Booking.objects.create,
129 end=end + timedelta(days=1),
132 config_bundle=self.config_bundle
137 Booking.objects.create,
138 start=start + timedelta(days=1),
139 end=end + timedelta(days=1),
142 config_bundle=self.config_bundle
146 Booking.objects.create(
147 start=start - timedelta(days=1),
151 config_bundle=self.config_bundle
155 Booking.objects.create(
157 end=end + timedelta(days=1),
160 config_bundle=self.config_bundle
165 Booking.objects.create(
166 start=start - timedelta(days=2),
167 end=start - timedelta(days=1),
170 config_bundle=self.config_bundle
175 Booking.objects.create(
176 start=end + timedelta(days=1),
177 end=end + timedelta(days=2),
180 config_bundle=self.config_bundle
185 Booking.objects.create(
190 config_bundle=self.config_bundle
194 def test_extensions(self):
196 saving a booking with an extended end time is allows to happen twice,
197 and each extension must be a maximum of one week long
199 start = timezone.now()
200 end = start + timedelta(weeks=1)
202 Booking.objects.create(
207 config_bundle=self.config_bundle
211 booking = Booking.objects.all().first() # should be only thing in db
213 self.assertEquals(booking.ext_count, 2)
214 booking.end = booking.end + timedelta(days=3)
218 self.fail("save() threw an exception")
219 booking.end = booking.end + timedelta(weeks=2)
220 self.assertRaises(ValueError, booking.save)
221 booking.end = booking.end - timedelta(days=8)
223 self.assertTrue(booking.save())
225 self.fail("save() threw an exception")