From 557530b9f9ade569a22f651e36ac54b939b96ec3 Mon Sep 17 00:00:00 2001 From: Andre Hora Date: Wed, 8 Feb 2023 15:25:28 -0300 Subject: [PATCH] Improving jsonutil tests (#929) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- tests/test_jsonutil.py | 60 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/test_jsonutil.py b/tests/test_jsonutil.py index 6ca5d9a2..a3074b5c 100644 --- a/tests/test_jsonutil.py +++ b/tests/test_jsonutil.py @@ -32,6 +32,32 @@ def __float__(self): numbers.Real.register(MyFloat) +def test_parse_date_invalid(): + assert jsonutil.parse_date(None) is None + assert jsonutil.parse_date("") == "" + assert jsonutil.parse_date("invalid-date") == "invalid-date" + + +def test_parse_date_valid(): + ref = REFERENCE_DATETIME + timestamp = "2013-07-03T16:34:52.249482Z" + parsed = jsonutil.parse_date(timestamp) + assert isinstance(parsed, datetime.datetime) + + +def test_parse_date_from_naive(): + ref = REFERENCE_DATETIME + timestamp = "2013-07-03T16:34:52.249482" + + with pytest.deprecated_call(match="Interpreting naive datetime as local"): + parsed = jsonutil.parse_date(timestamp) + + assert isinstance(parsed, datetime.datetime) + assert parsed.tzinfo is not None + assert parsed.tzinfo.utcoffset(ref) == tzlocal().utcoffset(ref) + assert parsed == ref + + def test_extract_date_from_naive(): ref = REFERENCE_DATETIME timestamp = "2013-07-03T16:34:52.249482" @@ -45,7 +71,17 @@ def test_extract_date_from_naive(): assert extracted == ref -def test_extract_dates(): +def test_extract_dates_from_str(): + ref = REFERENCE_DATETIME + timestamp = "2013-07-03T16:34:52.249482Z" + extracted = jsonutil.extract_dates(timestamp) + + assert isinstance(extracted, datetime.datetime) + assert extracted.tzinfo is not None + assert extracted.tzinfo.utcoffset(ref) == timedelta(0) + + +def test_extract_dates_from_list(): ref = REFERENCE_DATETIME timestamps = [ "2013-07-03T16:34:52.249482Z", @@ -66,6 +102,28 @@ def test_extract_dates(): assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8) +def test_extract_dates_from_dict(): + ref = REFERENCE_DATETIME + timestamps = { + 0: "2013-07-03T16:34:52.249482Z", + 1: "2013-07-03T16:34:52.249482-0800", + 2: "2013-07-03T16:34:52.249482+0800", + 3: "2013-07-03T16:34:52.249482-08:00", + 4: "2013-07-03T16:34:52.249482+08:00", + } + extracted = jsonutil.extract_dates(timestamps) + for k in extracted: + dt = extracted[k] + assert isinstance(dt, datetime.datetime) + assert dt.tzinfo is not None + + assert extracted[0].tzinfo.utcoffset(ref) == timedelta(0) + assert extracted[1].tzinfo.utcoffset(ref) == timedelta(hours=-8) + assert extracted[2].tzinfo.utcoffset(ref) == timedelta(hours=8) + assert extracted[3].tzinfo.utcoffset(ref) == timedelta(hours=-8) + assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8) + + def test_parse_ms_precision(): base = "2013-07-03T16:34:52" digits = "1234567890"