Skip to content

Commit 557530b

Browse files
Improving jsonutil tests (#929)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 19dbf1b commit 557530b

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

tests/test_jsonutil.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ def __float__(self):
3232
numbers.Real.register(MyFloat)
3333

3434

35+
def test_parse_date_invalid():
36+
assert jsonutil.parse_date(None) is None
37+
assert jsonutil.parse_date("") == ""
38+
assert jsonutil.parse_date("invalid-date") == "invalid-date"
39+
40+
41+
def test_parse_date_valid():
42+
ref = REFERENCE_DATETIME
43+
timestamp = "2013-07-03T16:34:52.249482Z"
44+
parsed = jsonutil.parse_date(timestamp)
45+
assert isinstance(parsed, datetime.datetime)
46+
47+
48+
def test_parse_date_from_naive():
49+
ref = REFERENCE_DATETIME
50+
timestamp = "2013-07-03T16:34:52.249482"
51+
52+
with pytest.deprecated_call(match="Interpreting naive datetime as local"):
53+
parsed = jsonutil.parse_date(timestamp)
54+
55+
assert isinstance(parsed, datetime.datetime)
56+
assert parsed.tzinfo is not None
57+
assert parsed.tzinfo.utcoffset(ref) == tzlocal().utcoffset(ref)
58+
assert parsed == ref
59+
60+
3561
def test_extract_date_from_naive():
3662
ref = REFERENCE_DATETIME
3763
timestamp = "2013-07-03T16:34:52.249482"
@@ -45,7 +71,17 @@ def test_extract_date_from_naive():
4571
assert extracted == ref
4672

4773

48-
def test_extract_dates():
74+
def test_extract_dates_from_str():
75+
ref = REFERENCE_DATETIME
76+
timestamp = "2013-07-03T16:34:52.249482Z"
77+
extracted = jsonutil.extract_dates(timestamp)
78+
79+
assert isinstance(extracted, datetime.datetime)
80+
assert extracted.tzinfo is not None
81+
assert extracted.tzinfo.utcoffset(ref) == timedelta(0)
82+
83+
84+
def test_extract_dates_from_list():
4985
ref = REFERENCE_DATETIME
5086
timestamps = [
5187
"2013-07-03T16:34:52.249482Z",
@@ -66,6 +102,28 @@ def test_extract_dates():
66102
assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8)
67103

68104

105+
def test_extract_dates_from_dict():
106+
ref = REFERENCE_DATETIME
107+
timestamps = {
108+
0: "2013-07-03T16:34:52.249482Z",
109+
1: "2013-07-03T16:34:52.249482-0800",
110+
2: "2013-07-03T16:34:52.249482+0800",
111+
3: "2013-07-03T16:34:52.249482-08:00",
112+
4: "2013-07-03T16:34:52.249482+08:00",
113+
}
114+
extracted = jsonutil.extract_dates(timestamps)
115+
for k in extracted:
116+
dt = extracted[k]
117+
assert isinstance(dt, datetime.datetime)
118+
assert dt.tzinfo is not None
119+
120+
assert extracted[0].tzinfo.utcoffset(ref) == timedelta(0)
121+
assert extracted[1].tzinfo.utcoffset(ref) == timedelta(hours=-8)
122+
assert extracted[2].tzinfo.utcoffset(ref) == timedelta(hours=8)
123+
assert extracted[3].tzinfo.utcoffset(ref) == timedelta(hours=-8)
124+
assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8)
125+
126+
69127
def test_parse_ms_precision():
70128
base = "2013-07-03T16:34:52"
71129
digits = "1234567890"

0 commit comments

Comments
 (0)