diff options
author | maxbr <maxbr@mi.fu-berlin.de> | 2016-10-17 14:56:11 +0200 |
---|---|---|
committer | maxbr <maxbr@mi.fu-berlin.de> | 2016-10-17 14:56:11 +0200 |
commit | 61f00f4110b1d33827e3bd19ffef34f10be8273a (patch) | |
tree | 2c24ca2a8414eb1f40d5363e03d3c2b21e1fec32 /pharos-dashboard/src/dashboard/tests/test_models.py | |
parent | adafb09cb1f221259970d16886ea0e03169899a5 (diff) |
Add unit tests
JIRA: RELENG-12
This commit increases test statement coverage to 84%. It also fixes the bugs
that emerged while testing.
Change-Id: I696091f1a07f8b7647872c7cb15f4368a4690619
Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'pharos-dashboard/src/dashboard/tests/test_models.py')
-rw-r--r-- | pharos-dashboard/src/dashboard/tests/test_models.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/pharos-dashboard/src/dashboard/tests/test_models.py b/pharos-dashboard/src/dashboard/tests/test_models.py new file mode 100644 index 0000000..3a3aeab --- /dev/null +++ b/pharos-dashboard/src/dashboard/tests/test_models.py @@ -0,0 +1,69 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +from datetime import timedelta +from math import ceil, floor + +from django.test import TestCase +from django.utils import timezone + +from booking.models import * +from dashboard.models import Resource +from jenkins.models import JenkinsSlave + + +class ResourceModelTestCase(TestCase): + def setUp(self): + self.slave = JenkinsSlave.objects.create(name='test', url='test') + self.owner = User.objects.create(username='owner') + + self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x', + url='x', owner=self.owner) + + def test_booking_utilization(self): + utilization = self.res1.get_booking_utilization(1) + self.assertTrue(utilization['booked_seconds'] == 0) + self.assertTrue(utilization['available_seconds'] == timedelta(weeks=1).total_seconds()) + + start = timezone.now() + timedelta(days=1) + end = start + timedelta(days=1) + booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1, + user=self.owner) + + utilization = self.res1.get_booking_utilization(1) + booked_seconds = timedelta(days=1).total_seconds() + self.assertEqual(utilization['booked_seconds'], booked_seconds) + + utilization = self.res1.get_booking_utilization(-1) + self.assertEqual(utilization['booked_seconds'], 0) + + booking.delete() + start = timezone.now() - timedelta(days=1) + end = start + timedelta(days=2) + booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1, + user=self.owner) + booked_seconds = self.res1.get_booking_utilization(1)['booked_seconds'] + # use ceil because a fraction of the booked time has already passed now + booked_seconds = ceil(booked_seconds) + self.assertEqual(booked_seconds, timedelta(days=1).total_seconds()) + + booking.delete() + start = timezone.now() + timedelta(days=6) + end = start + timedelta(days=2) + booking = Booking.objects.create(start=start, end=end, purpose='test', resource=self.res1, + user=self.owner) + booked_seconds = self.res1.get_booking_utilization(1)['booked_seconds'] + booked_seconds = floor(booked_seconds) + self.assertEqual(booked_seconds, timedelta(days=1).total_seconds()) + + + + + |