37eb65505ae016e35e25aeddb1379887514084c7
[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 User
15 from django.test import TestCase
16 from django.utils import timezone
17
18 from booking.models import Booking
19 from dashboard.testing_utils import make_resource_template, make_user
20
21
22 class BookingModelTestCase(TestCase):
23     """
24     Test the Booking model.
25
26     Creates all the scafolding needed and tests the Booking model
27     """
28
29     def setUp(self):
30         """
31         Prepare for Booking model tests.
32
33         Creates all the needed models, such as users, resources, and configurations
34         """
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')
39
40     def test_start_end(self):
41         """
42         Verify the start and end fields.
43
44         if the start of a booking is greater or equal then the end,
45         saving should raise a 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         )
57         end = start
58         self.assertRaises(
59             ValueError,
60             Booking.objects.create,
61             start=start,
62             end=end,
63             resource=self.res1,
64             owner=self.user1,
65         )
66
67     def test_conflicts(self):
68         """
69         Verify conflicting dates are dealt with.
70
71         saving an overlapping booking on the same resource
72         should raise a ValueException
73         saving for different resources should succeed
74         """
75         start = timezone.now()
76         end = start + timedelta(weeks=1)
77         self.assertTrue(
78             Booking.objects.create(
79                 start=start,
80                 end=end,
81                 owner=self.user1,
82                 resource=self.res1,
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         )
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         )
103
104         self.assertRaises(
105             ValueError,
106             Booking.objects.create,
107             start=start - timedelta(days=1),
108             end=end,
109             resource=self.res1,
110             owner=self.user1,
111         )
112
113         self.assertRaises(
114             ValueError,
115             Booking.objects.create,
116             start=start - timedelta(days=1),
117             end=end - timedelta(days=1),
118             resource=self.res1,
119             owner=self.user1,
120         )
121
122         self.assertRaises(
123             ValueError,
124             Booking.objects.create,
125             start=start,
126             end=end + timedelta(days=1),
127             resource=self.res1,
128             owner=self.user1,
129         )
130
131         self.assertRaises(
132             ValueError,
133             Booking.objects.create,
134             start=start + timedelta(days=1),
135             end=end + timedelta(days=1),
136             resource=self.res1,
137             owner=self.user1,
138         )
139
140         self.assertTrue(
141             Booking.objects.create(
142                 start=start - timedelta(days=1),
143                 end=start,
144                 owner=self.user1,
145                 resource=self.res1,
146             )
147         )
148
149         self.assertTrue(
150             Booking.objects.create(
151                 start=end,
152                 end=end + timedelta(days=1),
153                 owner=self.user1,
154                 resource=self.res1,
155             )
156         )
157
158         self.assertTrue(
159             Booking.objects.create(
160                 start=start - timedelta(days=2),
161                 end=start - timedelta(days=1),
162                 owner=self.user1,
163                 resource=self.res1,
164             )
165         )
166
167         self.assertTrue(
168             Booking.objects.create(
169                 start=end + timedelta(days=1),
170                 end=end + timedelta(days=2),
171                 owner=self.user1,
172                 resource=self.res1,
173             )
174         )
175
176         self.assertTrue(
177             Booking.objects.create(
178                 start=start,
179                 end=end,
180                 owner=self.user1,
181                 resource=self.res2,
182             )
183         )
184
185     def test_extensions(self):
186         """
187         Test booking extensions.
188
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
191         """
192         start = timezone.now()
193         end = start + timedelta(weeks=1)
194         self.assertTrue(
195             Booking.objects.create(
196                 start=start,
197                 end=end,
198                 owner=self.user1,
199                 resource=self.res1,
200             )
201         )
202
203         booking = Booking.objects.all().first()  # should be only thing in db
204
205         self.assertEquals(booking.ext_count, 2)
206         booking.end = booking.end + timedelta(days=3)
207         try:
208             booking.save()
209         except Exception:
210             self.fail("save() threw an exception")