Lab as a Service 2.0
[laas.git] / src / booking / tests / test_models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
4 #
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 ##############################################################################
10
11
12 from datetime import timedelta
13
14 from django.contrib.auth.models import Permission
15 from django.test import TestCase
16 from django.utils import timezone
17
18 from booking.models import *
19 from resource_inventory.models import ResourceBundle, GenericResourceBundle, ConfigBundle
20
21 class BookingModelTestCase(TestCase):
22     count = 0
23     def setUp(self):
24         self.owner = User.objects.create(username='owner')
25
26         self.res1 = ResourceBundle.objects.create(
27                 template=GenericResourceBundle.objects.create(name="gbundle" + str(self.count))
28                 )
29         self.count += 1
30         self.res2 = ResourceBundle.objects.create(
31                 template=GenericResourceBundle.objects.create(name="gbundle2" + str(self.count))
32                 )
33         self.count += 1
34         self.user1 = User.objects.create(username='user1')
35
36         self.add_booking_perm = Permission.objects.get(codename='add_booking')
37         self.user1.user_permissions.add(self.add_booking_perm)
38
39         self.user1 = User.objects.get(pk=self.user1.id)
40         self.config_bundle = ConfigBundle.objects.create(owner=self.user1, name="test config")
41
42     def test_start_end(self):
43         """
44         if the start of a booking is greater or equal then the end, saving should raise a
45         ValueException
46         """
47         start = timezone.now()
48         end = start - timedelta(weeks=1)
49         self.assertRaises(
50             ValueError,
51             Booking.objects.create,
52             start=start,
53             end=end,
54             resource=self.res1,
55             owner=self.user1,
56             config_bundle=self.config_bundle
57             )
58         end = start
59         self.assertRaises(
60             ValueError,
61             Booking.objects.create,
62             start=start,
63             end=end,
64             resource=self.res1,
65             owner=self.user1,
66             config_bundle=self.config_bundle
67             )
68
69     def test_conflicts(self):
70         """
71         saving an overlapping booking on the same resource should raise a ValueException
72         saving for different resources should succeed
73         """
74         start = timezone.now()
75         end = start + timedelta(weeks=1)
76         self.assertTrue(
77             Booking.objects.create(
78                 start=start,
79                 end=end,
80                 owner=self.user1,
81                 resource=self.res1,
82                 config_bundle=self.config_bundle
83                 )
84             )
85
86         self.assertRaises(
87             ValueError,
88             Booking.objects.create,
89             start=start,
90             end=end,
91             resource=self.res1,
92             owner=self.user1,
93             config_bundle=self.config_bundle
94             )
95         self.assertRaises(
96             ValueError,
97             Booking.objects.create,
98             start=start + timedelta(days=1),
99             end=end - timedelta(days=1),
100             resource=self.res1,
101             owner=self.user1,
102             config_bundle=self.config_bundle
103             )
104
105         self.assertRaises(
106             ValueError,
107             Booking.objects.create,
108             start=start - timedelta(days=1),
109             end=end,
110             resource=self.res1,
111             owner=self.user1,
112             config_bundle=self.config_bundle
113             )
114
115         self.assertRaises(
116             ValueError,
117             Booking.objects.create,
118             start=start - timedelta(days=1),
119             end=end - timedelta(days=1),
120             resource=self.res1,
121             owner=self.user1,
122             config_bundle=self.config_bundle
123             )
124
125         self.assertRaises(
126             ValueError,
127             Booking.objects.create,
128             start=start,
129             end=end + timedelta(days=1),
130             resource=self.res1,
131             owner=self.user1,
132             config_bundle=self.config_bundle
133             )
134
135         self.assertRaises(
136             ValueError,
137             Booking.objects.create,
138             start=start + timedelta(days=1),
139             end=end + timedelta(days=1),
140             resource=self.res1,
141             owner=self.user1,
142             config_bundle=self.config_bundle
143             )
144
145         self.assertTrue(
146             Booking.objects.create(
147                 start=start - timedelta(days=1),
148                 end=start,
149                 owner=self.user1,
150                 resource=self.res1,
151                 config_bundle=self.config_bundle
152                 )
153             )
154         self.assertTrue(
155             Booking.objects.create(
156                 start=end,
157                 end=end + timedelta(days=1),
158                 owner=self.user1,
159                 resource=self.res1,
160                 config_bundle=self.config_bundle
161                 )
162             )
163
164         self.assertTrue(
165             Booking.objects.create(
166                 start=start - timedelta(days=2),
167                 end=start - timedelta(days=1),
168                 owner=self.user1,
169                 resource=self.res1,
170                 config_bundle=self.config_bundle
171                 )
172             )
173
174         self.assertTrue(
175             Booking.objects.create(
176                 start=end + timedelta(days=1),
177                 end=end + timedelta(days=2),
178                 owner=self.user1,
179                 resource=self.res1,
180                 config_bundle=self.config_bundle
181                 )
182             )
183
184         self.assertTrue(
185             Booking.objects.create(
186                 start=start,
187                 end=end,
188                 owner=self.user1,
189                 resource=self.res2,
190                 config_bundle=self.config_bundle
191                 )
192             )
193
194     def test_extensions(self):
195         """
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
198         """
199         start = timezone.now()
200         end = start + timedelta(weeks=1)
201         self.assertTrue(
202             Booking.objects.create(
203                 start=start,
204                 end=end,
205                 owner=self.user1,
206                 resource=self.res1,
207                 config_bundle=self.config_bundle
208             )
209         )
210
211         booking = Booking.objects.all().first()  # should be only thing in db
212
213         self.assertEquals(booking.ext_count, 2)
214         booking.end = booking.end + timedelta(days=3)
215         try:
216             booking.save()
217         except Exception:
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)
222         try:
223             self.assertTrue(booking.save())
224         except Exception:
225             self.fail("save() threw an exception")
226