1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
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 ##############################################################################
11 from datetime import timedelta
12 from math import ceil, floor
14 from django.test import TestCase
15 from django.utils import timezone
17 from booking.models import *
18 from dashboard.models import Resource
19 from jenkins.models import JenkinsSlave
22 class ResourceModelTestCase(TestCase):
24 self.slave = JenkinsSlave.objects.create(name='test', url='test')
25 self.owner = User.objects.create(username='owner')
27 self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x',
28 url='x', owner=self.owner)
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())
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,
40 utilization = self.res1.get_booking_utilization(1)
41 booked_seconds = timedelta(days=1).total_seconds()
42 self.assertEqual(utilization['booked_seconds'], booked_seconds)
44 utilization = self.res1.get_booking_utilization(-1)
45 self.assertEqual(utilization['booked_seconds'], 0)
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,
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())
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,
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())