/
test_timestamp.py
289 lines (236 loc) · 9.96 KB
/
test_timestamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
##############################################################################
#
# Copyright (c) 2011 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import unittest
class Test__UTC(unittest.TestCase):
def _getTargetClass(self):
from persistent.timestamp import _UTC
return _UTC
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_tzname(self):
utc = self._makeOne()
self.assertEqual(utc.tzname(), 'UTC')
def test_utcoffset(self):
from datetime import timedelta
utc = self._makeOne()
self.assertEqual(utc.utcoffset(object()), timedelta(0))
def test_dst(self):
utc = self._makeOne()
self.assertEqual(utc.dst(), 0)
def test_fromutc(self):
source = object()
utc = self._makeOne()
self.assertTrue(utc.fromutc(source) is source)
class pyTimeStampTests(unittest.TestCase):
def _getTargetClass(self):
from persistent.timestamp import pyTimeStamp
return pyTimeStamp
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_invalid_arglist(self):
BAD_ARGS = [(),
(1,),
(1, 2),
(1, 2, 3),
(1, 2, 3, 4),
(1, 2, 3, 4, 5),
('1', '2', '3', '4', '5', '6'),
(1, 2, 3, 4, 5, 6, 7),
]
for args in BAD_ARGS:
self.assertRaises((TypeError, ValueError), self._makeOne, *args)
def test_ctor_from_invalid_strings(self):
BAD_ARGS = [''
'\x00',
'\x00' * 2,
'\x00' * 3,
'\x00' * 4,
'\x00' * 5,
'\x00' * 7,
]
for args in BAD_ARGS:
self.assertRaises((TypeError, ValueError), self._makeOne, *args)
def test_ctor_from_string(self):
from persistent.timestamp import _makeOctets
from persistent.timestamp import _makeUTC
ZERO = _makeUTC(1900, 1, 1, 0, 0, 0)
EPOCH = _makeUTC(1970, 1, 1, 0, 0, 0)
DELTA = ZERO - EPOCH
DELTA_SECS = DELTA.days * 86400 + DELTA.seconds
SERIAL = _makeOctets('\x00' * 8)
ts = self._makeOne(SERIAL)
self.assertEqual(ts.raw(), SERIAL)
self.assertEqual(ts.year(), 1900)
self.assertEqual(ts.month(), 1)
self.assertEqual(ts.day(), 1)
self.assertEqual(ts.hour(), 0)
self.assertEqual(ts.minute(), 0)
self.assertEqual(ts.second(), 0.0)
self.assertEqual(ts.timeTime(), DELTA_SECS)
def test_ctor_from_string_non_zero(self):
before = self._makeOne(2011, 2, 16, 14, 37, 22.0)
after = self._makeOne(before.raw())
self.assertEqual(before.raw(), after.raw())
def test_ctor_from_elements(self):
from persistent.timestamp import _makeOctets
from persistent.timestamp import _makeUTC
ZERO = _makeUTC(1900, 1, 1, 0, 0, 0)
EPOCH = _makeUTC(1970, 1, 1, 0, 0, 0)
DELTA = ZERO - EPOCH
DELTA_SECS = DELTA.days * 86400 + DELTA.seconds
SERIAL = _makeOctets('\x00' * 8)
ts = self._makeOne(1900, 1, 1, 0, 0, 0.0)
self.assertEqual(ts.raw(), SERIAL)
self.assertEqual(ts.year(), 1900)
self.assertEqual(ts.month(), 1)
self.assertEqual(ts.day(), 1)
self.assertEqual(ts.hour(), 0)
self.assertEqual(ts.minute(), 0)
self.assertEqual(ts.second(), 0.0)
self.assertEqual(ts.timeTime(), DELTA_SECS)
def test_laterThan_invalid(self):
from persistent.timestamp import _makeOctets
ERRORS = (ValueError, TypeError)
SERIAL = _makeOctets('\x01' * 8)
ts = self._makeOne(SERIAL)
self.assertRaises(ERRORS, ts.laterThan, None)
self.assertRaises(ERRORS, ts.laterThan, '')
self.assertRaises(ERRORS, ts.laterThan, ())
self.assertRaises(ERRORS, ts.laterThan, [])
self.assertRaises(ERRORS, ts.laterThan, {})
self.assertRaises(ERRORS, ts.laterThan, object())
def test_laterThan_self_is_earlier(self):
from persistent.timestamp import _makeOctets
SERIAL1 = _makeOctets('\x01' * 8)
SERIAL2 = _makeOctets('\x02' * 8)
ts1 = self._makeOne(SERIAL1)
ts2 = self._makeOne(SERIAL2)
later = ts1.laterThan(ts2)
self.assertEqual(later.raw(), _makeOctets('\x02' * 7 + '\x03'))
def test_laterThan_self_is_later(self):
from persistent.timestamp import _makeOctets
SERIAL1 = _makeOctets('\x01' * 8)
SERIAL2 = _makeOctets('\x02' * 8)
ts1 = self._makeOne(SERIAL1)
ts2 = self._makeOne(SERIAL2)
later = ts2.laterThan(ts1)
self.assertTrue(later is ts2)
def test_repr(self):
from persistent.timestamp import _makeOctets
SERIAL = _makeOctets('\x01' * 8)
ts = self._makeOne(SERIAL)
self.assertEqual(repr(ts), repr(SERIAL))
class TimeStampTests(pyTimeStampTests):
def _getTargetClass(self):
from persistent.timestamp import TimeStamp
return TimeStamp
class PyAndCComparisonTests(unittest.TestCase):
"""
Compares C and Python implementations.
"""
# A particular instant in time
now = 1229959248.3
# That instant in time split as the result of this expression:
# (time.gmtime(now)[:5] + (now % 60,))
now_ts_args = (2008, 12, 22, 15, 20, 48.299999952316284)
def _make_many_instants(self):
# Given the above data, return many slight variations on
# it to test matching
yield self.now_ts_args
for i in range(2000):
yield self.now_ts_args[:-1] + (self.now_ts_args[-1] + (i % 60.0)/100.0 , )
def _makeC(self, *args, **kwargs):
from persistent.timestamp import TimeStamp
return TimeStamp(*args, **kwargs)
def _makePy(self, *args, **kwargs):
from persistent.timestamp import pyTimeStamp
return pyTimeStamp(*args, **kwargs)
def _make_C_and_Py(self, *args, **kwargs):
return self._makeC(*args, **kwargs), self._makePy(*args, **kwargs)
def test_reprs_equal(self):
for args in self._make_many_instants():
c, py = self._make_C_and_Py(*args)
self.assertEqual(repr(c), repr(py))
def test_raw_equal(self):
c, py = self._make_C_and_Py(*self.now_ts_args)
self.assertEqual(c.raw(), py.raw())
def test_equal(self):
c, py = self._make_C_and_Py(*self.now_ts_args)
self.assertEqual(c, py)
def test_hash_equal(self):
c, py = self._make_C_and_Py(*self.now_ts_args)
self.assertEqual(hash(c), hash(py))
def test_hash_equal_constants(self):
# The simple constants make it easier to diagnose
# a difference in algorithms
c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(hash(c), 8)
self.assertEqual(hash(c), hash(py))
c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x00\x00\x00\x01')
self.assertEqual(hash(c), 9)
self.assertEqual(hash(c), hash(py))
c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x00\x00\x01\x00')
self.assertEqual(hash(c), 1000011)
self.assertEqual(hash(c), hash(py))
c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x00\x01\x00\x00')
self.assertEqual(hash(c), 1000006000001)
self.assertEqual(hash(c), hash(py))
c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x01\x00\x00\x00')
self.assertEqual(hash(c), 1000009000027000019)
self.assertEqual(hash(c), hash(py))
# Overflow kicks in at this point
c, py = self._make_C_and_Py(b'\x00\x00\x00\x01\x00\x00\x00\x00')
self.assertEqual(hash(c), -4442925868394654887)
self.assertEqual(hash(c), hash(py))
c, py = self._make_C_and_Py(b'\x00\x00\x01\x00\x00\x00\x00\x00')
self.assertEqual(hash(c), -3993531167153147845)
self.assertEqual(hash(c), hash(py))
c, py = self._make_C_and_Py(b'\x01\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(hash(c), -3099646879006235965)
self.assertEqual(hash(c), hash(py))
def test_ordering(self):
small_c = self._makeC(b'\x00\x00\x00\x00\x00\x00\x00\x01')
big_c = self._makeC(b'\x01\x00\x00\x00\x00\x00\x00\x00')
small_py = self._makePy(b'\x00\x00\x00\x00\x00\x00\x00\x01')
big_py = self._makePy(b'\x01\x00\x00\x00\x00\x00\x00\x00')
self.assertTrue(small_py < big_py)
self.assertTrue(small_py <= big_py)
self.assertTrue(small_py < big_c)
self.assertTrue(small_py <= big_c)
self.assertTrue(small_py <= small_c)
self.assertTrue(small_c < big_c)
self.assertTrue(small_c <= big_c)
self.assertTrue(small_c <= big_py)
self.assertTrue(big_c > small_py)
self.assertTrue(big_c >= big_py)
self.assertFalse(big_c == small_py)
self.assertFalse(small_py == big_c)
self.assertTrue(big_c != small_py)
self.assertTrue(small_py != big_c)
def test_suite():
suite = [
unittest.makeSuite(Test__UTC),
unittest.makeSuite(pyTimeStampTests),
unittest.makeSuite(TimeStampTests),
]
try:
from persistent.timestamp import pyTimeStamp
from persistent.timestamp import TimeStamp
except ImportError:
pass
else:
if pyTimeStamp != TimeStamp:
# We have both implementations available
suite.append(PyAndCComparisonTests)
return unittest.TestSuite(suite)