-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtests.py
More file actions
184 lines (150 loc) · 5.67 KB
/
tests.py
File metadata and controls
184 lines (150 loc) · 5.67 KB
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
# ruff: noqa: S101 # Asserts are ok in tests
from datetime import date, datetime
from time import struct_time
from edtf import convert
from edtf.parser.edtf_exceptions import EDTFParseException
from edtf.parser.grammar import parse_edtf
from edtf.util import remapparams
def test_dt_to_struct_time_for_datetime():
now = datetime.now()
st = convert.dt_to_struct_time(now)
assert st[:6] == now.timetuple()[:6]
assert st[6:] == (0, 0, -1)
def test_dt_to_struct_time_for_date():
today = date.today()
st = convert.dt_to_struct_time(today)
assert st[:3] == today.timetuple()[:3]
assert st[3:6] == (0, 0, 0)
assert st[6:] == (0, 0, -1)
def test_struct_time_to_date():
st = struct_time(
[2018, 4, 19] + convert.TIME_EMPTY_TIME + convert.TIME_EMPTY_EXTRAS
)
d = date(*st[:3])
assert d == convert.struct_time_to_date(st)
def test_struct_time_to_datetime():
st = struct_time([2018, 4, 19] + [10, 13, 54] + convert.TIME_EMPTY_EXTRAS)
dt = datetime(*st[:6])
converted_dt = convert.struct_time_to_datetime(st)
assert dt == converted_dt
assert converted_dt.timetuple()[6:] == (3, 109, -1)
def test_trim_struct_time():
now = datetime.now()
st = now.timetuple()
trimmed_st = convert.trim_struct_time(st)
assert trimmed_st[:6] == (
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second,
)
assert trimmed_st[6:] == (0, 0, -1)
assert st[6:] != (0, 0, -1)
def test_struct_time_to_jd():
st_ad = struct_time([2018, 4, 19] + [10, 13, 54] + convert.TIME_EMPTY_EXTRAS)
jd_ad = 2458227.9263194446
assert jd_ad == convert.struct_time_to_jd(st_ad)
st_bc = struct_time([-2018, 4, 19] + [10, 13, 54] + convert.TIME_EMPTY_EXTRAS)
jd_bc = 984091.9263194444
assert jd_bc == convert.struct_time_to_jd(st_bc)
def test_jd_to_struct_time():
jd_ad = 2458227.9263194446
st_ad = struct_time([2018, 4, 19] + [10, 13, 54] + convert.TIME_EMPTY_EXTRAS)
assert st_ad == convert.jd_to_struct_time(jd_ad)
jd_bc = 984091.9263194444
st_bc = struct_time([-2018, 4, 19] + [10, 13, 54 - 1] + convert.TIME_EMPTY_EXTRAS)
assert st_bc == convert.jd_to_struct_time(jd_bc)
def test_jd_round_trip_for_extreme_future():
original_st = struct_time([999999, 8, 4] + [21, 15, 3] + convert.TIME_EMPTY_EXTRAS)
jd = convert.struct_time_to_jd(original_st)
converted_st = convert.jd_to_struct_time(jd)
assert original_st[:5] == converted_st[:5]
assert converted_st[5] == 3 - 1
def test_jd_round_trip_for_extreme_past():
original_st = struct_time([-999999, 8, 4] + [21, 15, 3] + convert.TIME_EMPTY_EXTRAS)
converted_st = convert.jd_to_struct_time(convert.struct_time_to_jd(original_st))
assert tuple(converted_st) == (-999999 + 1, 8, 4, 21, 15, 3, 0, 0, -1)
def test_jd_round_trip_for_zero_year_aka_1_bc():
original_st = struct_time([0, 9, 5] + [4, 58, 59] + convert.TIME_EMPTY_EXTRAS)
converted_st = convert.jd_to_struct_time(convert.struct_time_to_jd(original_st))
assert tuple(converted_st) == (0, 9, 5, 4, 58, 59, 0, 0, -1)
def test_jd_round_trip_for_2_bc():
original_st = struct_time([-1, 12, 5] + [4, 58, 59] + convert.TIME_EMPTY_EXTRAS)
converted_st = convert.jd_to_struct_time(convert.struct_time_to_jd(original_st))
assert tuple(converted_st) == (-1, 12, 5, 4, 58, 59, 0, 0, -1)
def test_roll_negative_time_fields():
year = -100
month = -17
day = -34
hour = -25
minute = -74
second = -253
assert convert._roll_negative_time_fields(
year, month, day, hour, minute, second
) == (-102, 5, 24, 21, 41, 47)
def test_remapparams():
@remapparams(parseAll="parse_all")
def parser(s, parse_all=True):
pass
assert parser.__name__ == "parser" # noqa: S101
parser("foo")
# this should not warn
parser("foo", parse_all=False)
# this should warn, but only once
for _ in 1, 2:
parser("foo", parseAll=False)
try:
parser("foo", parseAll=False, parse_all=True)
except ValueError:
pass
else:
raise AssertionError("expected ValueError because of duplicated parameters")
try:
@remapparams()
def no_remappings():
pass
except ValueError:
pass
else:
raise AssertionError(
"expected ValueError from @remapparams() because no remappings"
)
try:
@remapparams(p1="p2", p2="p3")
def no_remappings():
pass
except ValueError:
pass
else:
raise AssertionError(
"expected ValueError from @remapparams() because p1 remaps to another remapped parameter"
)
def test_remapparams_parse_edtf():
edtf_s = "2005-09-24T10:00:00" # ISO8601 example from the EDTF spec
dat = parse_edtf(edtf_s) # implicit parse_all=True
assert dat.isoformat() == edtf_s
assert parse_edtf(edtf_s, parse_all=True).isoformat() == edtf_s
assert parse_edtf(edtf_s, parseAll=True).isoformat() == edtf_s
assert parse_edtf(f"{edtf_s} SNORT", parse_all=False).isoformat() == edtf_s
assert parse_edtf(f"{edtf_s} SNORT", parseAll=False).isoformat() == edtf_s
# make sure parse_all=True fails the SNORT parse
try:
parse_edtf(f"{edtf_s} SNORT")
except EDTFParseException:
pass
else:
raise AssertionError("expected EDTFParseException")
try:
parse_edtf(f"{edtf_s} SNORT", parse_all=True)
except EDTFParseException:
pass
else:
raise AssertionError("expected EDTFParseException")
try:
parse_edtf(f"{edtf_s} SNORT", parseAll=True)
except EDTFParseException:
pass
else:
raise AssertionError("expected EDTFParseException")