1#!/usr/bin/python2 2 3# Copyright 2015 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7from __future__ import absolute_import 8from __future__ import division 9from __future__ import print_function 10 11from datetime import timedelta, datetime 12import mock 13import unittest 14 15import common 16from autotest_lib.frontend import setup_django_readonly_environment 17from autotest_lib.frontend import setup_test_environment 18from autotest_lib.frontend.afe import models 19from autotest_lib.site_utils import count_jobs 20from django import test 21from six.moves import range 22 23 24class TestCountJobs(test.TestCase): 25 """Tests the count_jobs script's functionality. 26 """ 27 28 def setUp(self): 29 super(TestCountJobs, self).setUp() 30 setup_test_environment.set_up() 31 32 33 def tearDown(self): 34 super(TestCountJobs, self).tearDown() 35 setup_test_environment.tear_down() 36 37 38 def test_no_jobs(self): 39 """Initially, there should be no jobs.""" 40 self.assertEqual( 41 0, 42 count_jobs.number_of_jobs_since(timedelta(days=999))) 43 44 45 def test_count_jobs(self): 46 """When n jobs are inserted, n jobs should be counted within a day range. 47 48 Furthermore, 0 jobs should be counted within 0 or (-1) days. 49 """ 50 some_day = datetime.fromtimestamp(1450211914) # a time grabbed from time.time() 51 class FakeDatetime(datetime): 52 """Always returns the same 'now' value""" 53 @classmethod 54 def now(self): 55 """Return a fake 'now', rather than rely on the system's clock.""" 56 return some_day 57 with mock.patch.object(count_jobs, 'datetime', FakeDatetime): 58 for i in range(1, 24): 59 models.Job(created_on=some_day - timedelta(hours=i)).save() 60 for count, days in ((i, 1), (0, 0), (0, -1)): 61 self.assertEqual( 62 count, 63 count_jobs.number_of_jobs_since(timedelta(days=days))) 64 65 66if __name__ == '__main__': 67 unittest.main() 68