add nick
[laas.git] / src / booking / tests / test_stats.py
1 #############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, Sean Smith, 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 import pytz
10 from datetime import timedelta, datetime
11
12 from django.test import TestCase
13
14 from booking.models import Booking
15 from booking.stats import StatisticsManager as sm
16 from dashboard.testing_utils import make_user
17
18
19 class StatsTestCases(TestCase):
20
21     def test_no_booking_outside_span(self):
22         now = datetime.now(pytz.utc)
23
24         bad_date = now + timedelta(days=1200)
25         Booking.objects.create(start=now, end=bad_date, owner=make_user(username='jj'))
26
27         actual = sm.getContinuousBookingTimeSeries()
28         dates = actual['booking'][0]
29
30         for date in dates:
31             self.assertNotEqual(date, bad_date)
32
33     def check_booking_and_user_counts(self):
34         now = datetime.now(pytz.utc)
35
36         for i in range(20):
37             Booking.objects.create(
38                 start=now,
39                 end=now + timedelta(weeks=3),
40                 owner=make_user(username='a'))
41
42         for i in range(30):
43             Booking.objects.create(
44                 start=now + timedelta(days=5),
45                 end=now + timedelta(weeks=3, days=5),
46                 owner=make_user(username='a'))
47
48         for i in range(120):
49             Booking.objects.create(
50                 start=now + timedelta(weeks=1),
51                 end=now + timedelta(weeks=4),
52                 owner=make_user(username='a'))
53
54         dates = [[now, 20], [now + timedelta(days=5), 30], [now + timedelta(weeks=1), 120]]
55         actual = sm.getContinuousBookingTimeSeries()
56
57         for date in dates:
58             self.assertEqual(date[1], actual['booking'][date[0]])
59             self.assertEqual(date[1], actual['booking'][date[1]])