xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for time_converter unittests.""" |
| 6 | |
| 7 | import datetime |
| 8 | import unittest |
| 9 | |
| 10 | import mock |
| 11 | import pytz |
| 12 | |
| 13 | import time_converter |
| 14 | |
| 15 | |
| 16 | # datetime.datetime cannot be patched directly since it's a built-in |
| 17 | # type that is immutable. NewDatetime is created here for patching |
| 18 | # datetime.datetime.now. |
| 19 | class NewDatetime(datetime.datetime): |
| 20 | |
| 21 | @classmethod |
| 22 | def now(cls): |
| 23 | pass |
| 24 | |
| 25 | |
| 26 | class TimeConverterTestCase(unittest.TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | old_datetime = datetime.datetime |
| 30 | datetime.datetime = NewDatetime |
| 31 | mock_now = mock.patch('datetime.datetime.now') |
| 32 | self._mock_now = mock_now.start() |
| 33 | self.addCleanup(mock_now.stop) |
| 34 | self.addCleanup(setattr, datetime, 'datetime', old_datetime) |
| 35 | |
| 36 | def testConvertPSTToUTC(self): |
| 37 | """Convert a task on Saturday 9:00 on at day PST (2017, 8, 1). |
| 38 | |
| 39 | PST (2017, 8, 1, 9) should be converted to UTC (2017, 8, 1, 16). |
| 40 | The task should be run at Saturday 16:00 in UTC. |
| 41 | """ |
| 42 | self._mock_now.return_value = datetime.datetime(2017, 8, 1) |
| 43 | pst_time = time_converter.TimeInfo(5, 9) |
Xixuan Wu | 008ee83 | 2017-10-12 16:59:34 -0700 | [diff] [blame] | 44 | utc_time = time_converter.convert_time_info(pst_time) |
xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 45 | self.assertEqual(utc_time.weekday, 5) |
| 46 | self.assertEqual(utc_time.hour, 16) |
| 47 | |
| 48 | def testConvertPSTToUTCNextDay(self): |
| 49 | """Convert a task on Saturday 19:00 at day PST (201# 7, 8, 1). |
| 50 | |
| 51 | PST (2017, 8, 1, 19) should be converted to UTC (20# 17, 8, 2, 2). |
| 52 | The task should be run at Sunday 2:00 in UTC. |
| 53 | """ |
| 54 | self._mock_now.return_value = datetime.datetime(2017, 8, 1) |
| 55 | pst_time = time_converter.TimeInfo(5, 19) |
Xixuan Wu | 008ee83 | 2017-10-12 16:59:34 -0700 | [diff] [blame] | 56 | utc_time = time_converter.convert_time_info(pst_time) |
xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 57 | self.assertEqual(utc_time.weekday, 6) |
| 58 | self.assertEqual(utc_time.hour, 2) |
| 59 | |
| 60 | def testConvertPSTToUTCNextMonday(self): |
| 61 | """Convert a task on Sunday 19:00 at day PST (2017,# 8, 1). |
| 62 | |
| 63 | PST (2017, 8, 1, 9) should be converted to UTC (201# 7, 8, 1, 16). |
| 64 | The task should be run at Monday 2:00 in UTC. |
| 65 | """ |
| 66 | self._mock_now.return_value = datetime.datetime(2017, 8, 1) |
| 67 | pst_time = time_converter.TimeInfo(6, 19) |
Xixuan Wu | 008ee83 | 2017-10-12 16:59:34 -0700 | [diff] [blame] | 68 | utc_time = time_converter.convert_time_info(pst_time) |
xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 69 | self.assertEqual(utc_time.weekday, 0) |
| 70 | self.assertEqual(utc_time.hour, 2) |
| 71 | |
| 72 | def testConvertPSTToUTCWinterTime(self): |
| 73 | """Convert a task on Saturday 9:00 at day PST (2017# , 2, 1). |
| 74 | |
| 75 | PST (2017, 2, 1, 9) should be converted to UTC (201# 7, 2, 1, 17). |
| 76 | The task should be run at Saturday 17:00 in UTC. |
| 77 | """ |
| 78 | self._mock_now.return_value = datetime.datetime(2017, 2, 1) |
| 79 | pst_time = time_converter.TimeInfo(5, 9) |
Xixuan Wu | 008ee83 | 2017-10-12 16:59:34 -0700 | [diff] [blame] | 80 | utc_time = time_converter.convert_time_info(pst_time) |
xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 81 | self.assertEqual(utc_time.weekday, 5) |
| 82 | self.assertEqual(utc_time.hour, 17) |
| 83 | |
| 84 | def testConvertHKToUTC(self): |
| 85 | """Convert a task on Saturday 9:00 at day Asia/HK (# 2017, 8, 1). |
| 86 | |
| 87 | Asia/HK (2017, 8, 1, 9) should be converted to UTC (201# 7, 8, 1, 1). |
| 88 | The task should be run at Saturday 1:00 in UTC. |
| 89 | """ |
| 90 | self._mock_now.return_value = datetime.datetime(2017, 8, 1) |
| 91 | hk_time = time_converter.TimeInfo(5, 9) |
Xixuan Wu | 008ee83 | 2017-10-12 16:59:34 -0700 | [diff] [blame] | 92 | utc_time = time_converter.convert_time_info( |
xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 93 | hk_time, source_tz=pytz.timezone('Asia/Hong_Kong')) |
| 94 | self.assertEqual(utc_time.weekday, 5) |
| 95 | self.assertEqual(utc_time.hour, 1) |
| 96 | |
| 97 | def testConvertHKToUTCPreviousDay(self): |
| 98 | """Convert a task on Saturday 2:00 at day Asia/HK (# 2017, 8, 1). |
| 99 | |
| 100 | Asia/HK (2017, 8, 1, 2) should be converted to UTC (201# 7, 7, 31, 18). |
| 101 | The task should be run at Friday 18:00 in UTC. |
| 102 | """ |
| 103 | self._mock_now.return_value = datetime.datetime(2017, 8, 1) |
| 104 | hk_time = time_converter.TimeInfo(5, 2) |
Xixuan Wu | 008ee83 | 2017-10-12 16:59:34 -0700 | [diff] [blame] | 105 | utc_time = time_converter.convert_time_info( |
xixuan | 9a8b74a | 2017-08-08 11:24:16 -0700 | [diff] [blame] | 106 | hk_time, source_tz=pytz.timezone('Asia/Hong_Kong')) |
| 107 | self.assertEqual(utc_time.weekday, 4) |
| 108 | self.assertEqual(utc_time.hour, 18) |
| 109 | |
| 110 | |
| 111 | if __name__ == '__main__': |
| 112 | unittest.main() |