forked from datopian/flexidate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flexidate.py
272 lines (217 loc) · 7.6 KB
/
test_flexidate.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
import datetime
from flexidate import parse, FlexiDate, PythonDateParser, DateutilDateParser
class TestPythonStringOrdering(object):
# It is impossible to find a string format such that +ve and -ve numbers
# sort correctly as strings:
# if (in string ordering) X < Y => -X < -Y (False!)
def test_ordering(self):
assert '0' < '1'
assert '-10' < '10'
assert '-' < '@'
assert '-' < '0'
assert '-100' < '-X10'
assert '10' < '1000'
assert '02000' < '10000'
assert ' 2000' < '10000'
def test_bad_ordering(self):
assert ' ' < '0'
assert ' ' < '-'
assert not '-' < '+'
assert '-100' > '-10'
assert not '-100' < '-010'
assert not '-100' < '- 10'
assert not '-100' < ' -10'
assert '10000' < '2000'
assert not '-10' < ' 1'
class TestFlexiDate(object):
def test_init(self):
fd = FlexiDate()
assert fd.year == '', fd
assert fd.month == '', fd
fd = FlexiDate(2000, 1, 1)
assert fd.month == '01', fd
assert fd.day == '01', fd
fd = FlexiDate(2004, 3, 2, 10)
assert fd.month == '03', fd
assert fd.day == '02', fd
assert fd.hour == '10', fd
fd = FlexiDate(2004, 3, 2, 10, 11)
assert fd.month == '03', fd
assert fd.day == '02', fd
assert fd.hour == '10', fd
assert fd.minute == '11', fd
fd = FlexiDate(2004, 3, 2, 10, 11, 12)
assert fd.month == '03', fd
assert fd.day == '02', fd
assert fd.hour == '10', fd
assert fd.minute == '11', fd
assert fd.second == '12', fd
fd = FlexiDate(2004, 3, 2, 10, 11, 12, 123456)
assert fd.month == '03', fd
assert fd.day == '02', fd
assert fd.hour == '10', fd
assert fd.minute == '11', fd
assert fd.second == '12', fd
assert fd.microsecond == '123456', fd
def test_str(self):
fd = FlexiDate(2000, 1, 23)
assert str(fd) == '2000-01-23', '"%s"' % fd
fd = FlexiDate(-2000, 1, 23)
assert str(fd) == '-2000-01-23'
fd = FlexiDate(2000)
assert str(fd) == '2000'
fd = FlexiDate(1760, qualifier='fl.')
assert str(fd) == '1760 [fl.]', fd
fd = FlexiDate(qualifier='anything')
assert str(fd) == ' [anything]'
def test_repr(self):
fd = FlexiDate(2016, 3, 15)
assert repr(fd) == "<class 'flexidate.FlexiDate'> 2016-03-15"
def test_cvt(self):
fd = FlexiDate(2016, 3, 16)
assert fd._cvt(None, 4, True) == '!!!!'
def test_isoformat(self):
fd = FlexiDate(2000, 1, 24)
assert str(fd.isoformat()) == '2000-01-24'
def test_from_str(self):
def dotest(fd):
out = FlexiDate.from_str(str(fd))
assert str(out) == str(fd)
def dotest2(fd):
out = FlexiDate.from_str("Not a date")
assert str(out) == 'None'
fd = FlexiDate(2000, 1, 23)
dotest(fd)
dotest2(fd)
fd = FlexiDate(1760, qualifier='fl.')
dotest(fd)
dotest2(fd)
fd = FlexiDate(-1760, 1, 3, qualifier='fl.')
dotest(fd)
dotest2(fd)
def test_as_float(self):
fd = FlexiDate(2000)
assert fd.as_float() == float(2000), fd.as_float()
fd = FlexiDate(1760, 1, 2)
exp = 1760 + 1/12.0 + 2/365.0
assert fd.as_float() == exp, fd.as_float()
fd = FlexiDate(-1000)
assert fd.as_float() == float(-1000)
fd = FlexiDate()
assert fd.as_float() == None
def test_as_datetime(self):
fd = FlexiDate(2000)
out = fd.as_datetime()
assert out == datetime.datetime(2000, 1, 1), out
fd = FlexiDate(1760, 1, 2)
out = fd.as_datetime()
assert out == datetime.datetime(1760, 1, 2), out
class TestDateParsers(object):
def test_using_datetime(self):
parser = PythonDateParser()
d1 = datetime.date(2000, 1, 23)
fd = parser.parse(d1)
assert fd.year == '2000'
d1 = datetime.datetime(2000, 1, 23)
fd = parser.parse(d1)
# assert str(fd) == '2000-01-23T00:00:00', fd
assert str(fd) == '2000-01-23 00:00:00.00', fd
def test_using_dateutil(self):
parser = DateutilDateParser()
in1 = '2016-06-03 10'
fd = parser.parse(in1)
assert str(fd) == in1, fd
in1 = '86'
fd = parser.parse(in1)
assert str(fd) == '0086'
in1 = '2001-02'
fd = parser.parse(in1)
assert str(fd) == in1, fd
in1 = 'March 1762'
fd = parser.parse(in1)
assert str(fd) == '1762-03'
in1 = 'March 1762'
fd = parser.parse(in1)
assert str(fd) == '1762-03'
in1 = '1768 AD'
fd = parser.parse(in1)
assert str(fd) == '1768', fd
in1 = '1768 A.D.'
fd = parser.parse(in1)
assert str(fd) == '1768', fd
in1 = '-1850'
fd = parser.parse(in1)
assert str(fd) == '-1850', fd
in1 = '1762 BC'
fd = parser.parse(in1)
assert str(fd) == '-1762', fd
in1 = '4 BC'
fd = parser.parse(in1)
assert str(fd) == '-0004', fd
in1 = '4 B.C.'
fd = parser.parse(in1)
assert str(fd) == '-0004', fd
in1 = 'Wed, 06 Jan 2010 09:30:00 GMT'
fd = parser.parse(in1)
assert str(fd) == '2010-01-06 09:30:00.00', fd
in1 = 'Tue, 07 Dec 2010 10:00:00 GMT'
fd = parser.parse(in1)
assert str(fd) == '2010-12-07 10:00:00.00', fd
in1 = '2015.03.01'
fd = parser.parse(in1)
assert str(fd) == '2015-03-01', fd
def test_parse(self):
d1 = datetime.date(2000, 1, 23)
fd = parse(d1)
assert fd.year == '2000'
d1 = datetime.datetime(2002, 1, 23)
fd = parse(d1)
assert fd.year == '2002'
fd = parse('March 1762')
assert str(fd) == '1762-03'
fd = parse(1966)
assert str(fd) == '1966'
fd = parse('22/07/2010')
assert fd.month == '07', fd.month
d2 = FlexiDate(1760, 1, 2)
fd = parse(d2)
assert fd.year == '1760'
def test_parse_ambiguous_day_month(self):
fd = parse('05/07/2010')
assert fd.month == '07', fd.month
assert fd.day == '05', fd.month
def test_parse_with_none(self):
d1 = parse(None)
assert d1 is None
def test_parse_wildcards(self):
fd = parse('198?')
assert fd.year == '', fd.year # expect this to not parse
# TODO but we should have a float if possible
# assert fd.as_float() == u'1980', fd.as_float()
def test_parse_with_qualifiers(self):
fd = parse('1985?')
assert fd.year == u'1985', fd
assert fd.qualifier == u'Uncertainty : 1985?', fd.qualifier
fd = parse('c.1780')
assert fd.year == u'1780', fd
assert fd.qualifier == u"Note 'circa' : c.1780", fd
fd = parse('c. 1780')
assert fd.year == u'1780', fd
assert fd.qualifier.startswith(u"Note 'circa'"), fd
def test_ambiguous(self):
# TODO: have to be careful here ...
fd = parse('1068/1069')
def test_small_years(self):
in1 = '23'
fd = parse(in1)
assert str(fd) == '0023', fd
assert fd.as_float() == 23, fd.as_float()
def test_small_years_with_zeros(self):
in1 = '0023'
fd = parse(in1)
assert str(fd) == '0023', fd
assert fd.as_float() == 23, fd.as_float()
def test_years_with_alpha_prefix(self):
in1 = "p1980"
fd = parse(in1)
assert str(fd) == "1980", fd