2016-06-04 22:20:07 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2014-11-26 21:11:28 +03:00
|
|
|
import unittest
|
|
|
|
import mozunit
|
|
|
|
from datetime import datetime
|
2016-06-04 22:20:07 +03:00
|
|
|
from taskgraph.util.time import (
|
2014-11-26 21:11:28 +03:00
|
|
|
InvalidString,
|
|
|
|
UnknownTimeMeasurement,
|
|
|
|
value_of,
|
|
|
|
json_time_from_now
|
|
|
|
)
|
|
|
|
|
2016-06-21 04:06:55 +03:00
|
|
|
|
2014-11-26 21:11:28 +03:00
|
|
|
class FromNowTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_invalid_str(self):
|
|
|
|
with self.assertRaises(InvalidString):
|
|
|
|
value_of('wtfs')
|
|
|
|
|
|
|
|
def test_missing_unit(self):
|
|
|
|
with self.assertRaises(InvalidString):
|
|
|
|
value_of('1')
|
|
|
|
|
|
|
|
def test_missing_unknown_unit(self):
|
|
|
|
with self.assertRaises(UnknownTimeMeasurement):
|
|
|
|
value_of('1z')
|
|
|
|
|
|
|
|
def test_value_of(self):
|
|
|
|
self.assertEqual(value_of('1s').total_seconds(), 1)
|
|
|
|
self.assertEqual(value_of('1 second').total_seconds(), 1)
|
2016-01-27 18:42:52 +03:00
|
|
|
self.assertEqual(value_of('1min').total_seconds(), 60)
|
2014-11-26 21:11:28 +03:00
|
|
|
self.assertEqual(value_of('1h').total_seconds(), 3600)
|
|
|
|
self.assertEqual(value_of('1d').total_seconds(), 86400)
|
2016-01-27 18:42:52 +03:00
|
|
|
self.assertEqual(value_of('1mo').total_seconds(), 2592000)
|
|
|
|
self.assertEqual(value_of('1 month').total_seconds(), 2592000)
|
2014-11-26 21:11:28 +03:00
|
|
|
self.assertEqual(value_of('1y').total_seconds(), 31536000)
|
|
|
|
|
2016-01-27 18:42:52 +03:00
|
|
|
with self.assertRaises(UnknownTimeMeasurement):
|
|
|
|
value_of('1m').total_seconds() # ambiguous between minute and month
|
|
|
|
|
2014-11-26 21:11:28 +03:00
|
|
|
def test_json_from_now_utc_now(self):
|
|
|
|
# Just here to ensure we don't raise.
|
2016-06-21 04:06:55 +03:00
|
|
|
json_time_from_now('1 years')
|
2014-11-26 21:11:28 +03:00
|
|
|
|
|
|
|
def test_json_from_now(self):
|
|
|
|
now = datetime(2014, 1, 1)
|
|
|
|
self.assertEqual(json_time_from_now('1 years', now),
|
2016-06-21 04:06:55 +03:00
|
|
|
'2015-01-01T00:00:00Z')
|
2014-11-26 21:11:28 +03:00
|
|
|
self.assertEqual(json_time_from_now('6 days', now),
|
2016-06-21 04:06:55 +03:00
|
|
|
'2014-01-07T00:00:00Z')
|
2014-11-26 21:11:28 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
mozunit.main()
|