Development Override Compose File
[laas.git] / src / dashboard / tests / test_models.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from datetime import timedelta
12 from math import ceil, floor
13
14 from django.test import TestCase
15 from django.utils import timezone
16
17 from booking.models import *
18 from dashboard.models import Resource
19 from jenkins.models import JenkinsSlave
20
21
22 class ResourceModelTestCase(TestCase):
23     def setUp(self):
24         self.slave = JenkinsSlave.objects.create(name='test', url='test')
25         self.owner = User.objects.create(username='owner')
26
27         self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x',
28                                             url='x', owner=self.owner)
29
30     def test_booking_utilization(self):
31         utilization = self.res1.get_booking_utilization(1)
32         self.assertTrue(utilization['booked_seconds'] == 0)
33         self.assertTrue(utilization['available_seconds'] == timedelta(weeks=1).total_seconds())
34
35         start = timezone.now() + timedelta(days=1)
36         end = start + timedelta(days=1)
37         booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1,
38                                user=self.owner)
39
40         utilization = self.res1.get_booking_utilization(1)
41         booked_seconds = timedelta(days=1).total_seconds()
42         self.assertEqual(utilization['booked_seconds'], booked_seconds)
43
44         utilization = self.res1.get_booking_utilization(-1)
45         self.assertEqual(utilization['booked_seconds'], 0)
46
47         booking.delete()
48         start = timezone.now() - timedelta(days=1)
49         end = start + timedelta(days=2)
50         booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1,
51                                user=self.owner)
52         booked_seconds = self.res1.get_booking_utilization(1)['booked_seconds']
53         # use ceil because a fraction of the booked time has already passed now
54         booked_seconds = ceil(booked_seconds)
55         self.assertEqual(booked_seconds, timedelta(days=1).total_seconds())
56
57         booking.delete()
58         start = timezone.now() + timedelta(days=6)
59         end = start + timedelta(days=2)
60         booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1,
61                                user=self.owner)
62         booked_seconds = self.res1.get_booking_utilization(1)['booked_seconds']
63         booked_seconds = floor(booked_seconds)
64         self.assertEqual(booked_seconds, timedelta(days=1).total_seconds())
65
66
67
68
69