OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from __future__ import absolute_import |
| 16 |
| 17 import datetime |
| 18 |
| 19 import unittest2 |
| 20 from expects import be_below_or_equal, expect, equal, raise_error |
| 21 |
| 22 from google.api.control import timestamp |
| 23 |
| 24 |
| 25 class TestToRfc3339(unittest2.TestCase): |
| 26 A_LONG_TIME_AGO = datetime.datetime(1971, 12, 31, 21, 0, 20, 21000) |
| 27 TESTS = [ |
| 28 (A_LONG_TIME_AGO, '1971-12-31T21:00:20.021Z'), |
| 29 (A_LONG_TIME_AGO - datetime.datetime(1970, 1, 1), |
| 30 '1971-12-31T21:00:20.021Z') |
| 31 ] |
| 32 |
| 33 def test_should_converts_correctly(self): |
| 34 for t in self.TESTS: |
| 35 expect(timestamp.to_rfc3339(t[0])).to(equal(t[1])) |
| 36 |
| 37 def test_should_fail_on_invalid_input(self): |
| 38 testf = lambda: timestamp.to_rfc3339('this will not work') |
| 39 expect(testf).to(raise_error(ValueError)) |
| 40 |
| 41 |
| 42 class TestFromRfc3339(unittest2.TestCase): |
| 43 TOLERANCE = 10000 # 1e-5 * 1e9 |
| 44 TESTS = [ |
| 45 # Simple |
| 46 ('1971-12-31T21:00:20.021Z', |
| 47 datetime.datetime(1971, 12, 31, 21, 0, 20, 21000)), |
| 48 # different timezone |
| 49 ('1996-12-19T16:39:57-08:00', |
| 50 datetime.datetime(1996, 12, 20, 0, 39, 57, 0)), |
| 51 # microseconds |
| 52 ('1996-12-19T16:39:57.123456-08:00', |
| 53 datetime.datetime(1996, 12, 20, 0, 39, 57, 123456)), |
| 54 # Beyond 2038 |
| 55 ('2100-01-01T00:00:00Z', |
| 56 datetime.datetime(2100, 01, 01, 0, 0, 0, 0)) |
| 57 ] |
| 58 |
| 59 NANO_TESTS = [ |
| 60 # Simple |
| 61 ('1971-12-31T21:00:20.021Z', |
| 62 (datetime.datetime(1971, 12, 31, 21, 0, 20, 21000), 21000000)), |
| 63 # different timezone |
| 64 ('1996-12-19T16:39:57-08:00', |
| 65 (datetime.datetime(1996, 12, 20, 0, 39, 57, 0), 0)), |
| 66 # microseconds |
| 67 ('1996-12-19T16:39:57.123456789-08:00', |
| 68 (datetime.datetime(1996, 12, 20, 0, 39, 57, 123457), 123456789)), |
| 69 ] |
| 70 |
| 71 def test_should_convert_correctly_without_nanos(self): |
| 72 for t in self.TESTS: |
| 73 expect(timestamp.from_rfc3339(t[0])).to(equal(t[1])) |
| 74 |
| 75 def test_should_convert_correctly_with_nanos(self): |
| 76 for t in self.NANO_TESTS: |
| 77 dt, nanos = timestamp.from_rfc3339(t[0], with_nanos=True) |
| 78 expect(dt).to(equal(t[1][0])) |
| 79 epsilon = abs(nanos - t[1][1]) |
| 80 # expect(epsilon).to(equal(0)) |
| 81 expect(epsilon).to(be_below_or_equal(self.TOLERANCE)) |
| 82 |
| 83 |
| 84 class TestCompare(unittest2.TestCase): |
| 85 TESTS = [ |
| 86 # Strings |
| 87 ('1971-10-31T21:00:20.021Z', '1971-11-30T21:00:20.021Z', -1), |
| 88 ('1971-11-30T21:00:20.021Z', '1971-10-30T21:00:20.021Z', 1), |
| 89 ('1971-11-30T21:00:20Z', '1971-11-30T21:00:20Z', 0), |
| 90 ('1971-11-30T21:00:20.021Z', '1971-11-30T21:00:20.041Z', -1), |
| 91 ('1971-11-30T21:00:20.021Z', '1971-11-30T21:00:20.001Z', 1), |
| 92 # Datetimes |
| 93 (datetime.datetime(1996, 10, 20, 0, 39, 57, 0), |
| 94 datetime.datetime(1996, 11, 20, 0, 39, 57, 0), |
| 95 -1), |
| 96 (datetime.datetime(1996, 10, 20, 0, 39, 57, 0), |
| 97 datetime.datetime(1996, 10, 20, 0, 39, 57, 0), |
| 98 0), |
| 99 (datetime.datetime(1996, 11, 20, 0, 39, 57, 0), |
| 100 datetime.datetime(1996, 10, 20, 0, 39, 57, 0), |
| 101 1) |
| 102 ] |
| 103 |
| 104 def test_should_compare_correctly(self): |
| 105 for t in self.TESTS: |
| 106 a, b, want = t |
| 107 expect(timestamp.compare(a, b)).to(equal(want)) |
| 108 |
| 109 def test_should_fail_if_inputs_do_not_have_the_same_type(self): |
| 110 testf = lambda: timestamp.compare(self.TESTS[0][0], |
| 111 datetime.datetime.utcnow()) |
| 112 expect(testf).to(raise_error(ValueError)) |
| 113 testf = lambda: timestamp.compare(self.TESTS[0], |
| 114 datetime.datetime.utcnow()) |
| 115 expect(testf).to(raise_error(ValueError)) |
OLD | NEW |