| 1 |
"""Test suite for our JSON utilities.""" |
| 2 |
|
| 3 |
# Copyright (c) Jupyter Development Team. |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
import datetime |
| 6 |
import json |
| 7 |
import numbers |
| 8 |
from datetime import timedelta |
| 9 |
from unittest import mock |
| 10 |
|
| 11 |
import pytest |
| 12 |
from dateutil.tz import tzlocal, tzoffset |
| 13 |
|
| 14 |
from jupyter_client import jsonutil |
| 15 |
from jupyter_client.session import utcnow |
| 16 |
|
| 17 |
REFERENCE_DATETIME = datetime.datetime(2013, 7, 3, 16, 34, 52, 249482, tzlocal()) |
| 18 |
|
| 19 |
|
| 20 |
class MyInt: |
| 21 |
def __int__(self): |
| 22 |
return 389 |
| 23 |
|
| 24 |
|
| 25 |
numbers.Integral.register(MyInt) |
| 26 |
|
| 27 |
|
| 28 |
class MyFloat: |
| 29 |
def __float__(self): |
| 30 |
return 3.14 |
| 31 |
|
| 32 |
|
| 33 |
numbers.Real.register(MyFloat) |
| 34 |
|
| 35 |
|
| 36 |
def test_parse_date_invalid(): |
| 37 |
assert jsonutil.parse_date(None) is None |
| 38 |
assert jsonutil.parse_date("") == "" |
| 39 |
assert jsonutil.parse_date("invalid-date") == "invalid-date" |
| 40 |
|
| 41 |
|
| 42 |
def test_parse_date_valid(): |
| 43 |
ref = REFERENCE_DATETIME |
| 44 |
timestamp = "2013-07-03T16:34:52.249482Z" |
| 45 |
parsed = jsonutil.parse_date(timestamp) |
| 46 |
assert isinstance(parsed, datetime.datetime) |
| 47 |
|
| 48 |
|
| 49 |
def test_parse_date_from_naive(): |
| 50 |
ref = REFERENCE_DATETIME |
| 51 |
timestamp = "2013-07-03T16:34:52.249482" |
| 52 |
|
| 53 |
with pytest.deprecated_call(match="Interpreting naive datetime as local"): |
| 54 |
parsed = jsonutil.parse_date(timestamp) |
| 55 |
|
| 56 |
assert isinstance(parsed, datetime.datetime) |
| 57 |
assert parsed.tzinfo is not None |
| 58 |
assert parsed.tzinfo.utcoffset(ref) == tzlocal().utcoffset(ref) |
| 59 |
assert parsed == ref |
| 60 |
|
| 61 |
|
| 62 |
def test_extract_date_from_naive(): |
| 63 |
ref = REFERENCE_DATETIME |
| 64 |
timestamp = "2013-07-03T16:34:52.249482" |
| 65 |
|
| 66 |
with pytest.deprecated_call(match="Interpreting naive datetime as local"): |
| 67 |
extracted = jsonutil.extract_dates(timestamp) |
| 68 |
|
| 69 |
assert isinstance(extracted, datetime.datetime) |
| 70 |
assert extracted.tzinfo is not None |
| 71 |
assert extracted.tzinfo.utcoffset(ref) == tzlocal().utcoffset(ref) |
| 72 |
assert extracted == ref |
| 73 |
|
| 74 |
|
| 75 |
def test_extract_dates_from_str(): |
| 76 |
ref = REFERENCE_DATETIME |
| 77 |
timestamp = "2013-07-03T16:34:52.249482Z" |
| 78 |
extracted = jsonutil.extract_dates(timestamp) |
| 79 |
|
| 80 |
assert isinstance(extracted, datetime.datetime) |
| 81 |
assert extracted.tzinfo is not None |
| 82 |
assert extracted.tzinfo.utcoffset(ref) == timedelta(0) |
| 83 |
|
| 84 |
|
| 85 |
def test_extract_dates_from_list(): |
| 86 |
ref = REFERENCE_DATETIME |
| 87 |
timestamps = [ |
| 88 |
"2013-07-03T16:34:52.249482Z", |
| 89 |
"2013-07-03T16:34:52.249482-0800", |
| 90 |
"2013-07-03T16:34:52.249482+0800", |
| 91 |
"2013-07-03T16:34:52.249482-08:00", |
| 92 |
"2013-07-03T16:34:52.249482+08:00", |
| 93 |
] |
| 94 |
extracted = jsonutil.extract_dates(timestamps) |
| 95 |
for dt in extracted: |
| 96 |
assert isinstance(dt, datetime.datetime) |
| 97 |
assert dt.tzinfo is not None |
| 98 |
|
| 99 |
assert extracted[0].tzinfo.utcoffset(ref) == timedelta(0) |
| 100 |
assert extracted[1].tzinfo.utcoffset(ref) == timedelta(hours=-8) |
| 101 |
assert extracted[2].tzinfo.utcoffset(ref) == timedelta(hours=8) |
| 102 |
assert extracted[3].tzinfo.utcoffset(ref) == timedelta(hours=-8) |
| 103 |
assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8) |
| 104 |
|
| 105 |
|
| 106 |
def test_extract_dates_from_dict(): |
| 107 |
ref = REFERENCE_DATETIME |
| 108 |
timestamps = { |
| 109 |
0: "2013-07-03T16:34:52.249482Z", |
| 110 |
1: "2013-07-03T16:34:52.249482-0800", |
| 111 |
2: "2013-07-03T16:34:52.249482+0800", |
| 112 |
3: "2013-07-03T16:34:52.249482-08:00", |
| 113 |
4: "2013-07-03T16:34:52.249482+08:00", |
| 114 |
} |
| 115 |
extracted = jsonutil.extract_dates(timestamps) |
| 116 |
for k in extracted: |
| 117 |
dt = extracted[k] |
| 118 |
assert isinstance(dt, datetime.datetime) |
| 119 |
assert dt.tzinfo is not None |
| 120 |
|
| 121 |
assert extracted[0].tzinfo.utcoffset(ref) == timedelta(0) |
| 122 |
assert extracted[1].tzinfo.utcoffset(ref) == timedelta(hours=-8) |
| 123 |
assert extracted[2].tzinfo.utcoffset(ref) == timedelta(hours=8) |
| 124 |
assert extracted[3].tzinfo.utcoffset(ref) == timedelta(hours=-8) |
| 125 |
assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8) |
| 126 |
|
| 127 |
|
| 128 |
def test_parse_ms_precision(): |
| 129 |
base = "2013-07-03T16:34:52" |
| 130 |
digits = "1234567890" |
| 131 |
|
| 132 |
parsed = jsonutil.parse_date(base + "Z") |
| 133 |
assert isinstance(parsed, datetime.datetime) |
| 134 |
for i in range(len(digits)): |
| 135 |
ts = base + "." + digits[:i] |
| 136 |
parsed = jsonutil.parse_date(ts + "Z") |
| 137 |
if i >= 1 and i <= 6: |
| 138 |
assert isinstance(parsed, datetime.datetime) |
| 139 |
else: |
| 140 |
assert isinstance(parsed, str) |
| 141 |
|
| 142 |
|
| 143 |
def test_json_default_datetime(): |
| 144 |
naive = datetime.datetime.now() # noqa |
| 145 |
local = tzoffset("Local", -8 * 3600) |
| 146 |
other = tzoffset("Other", 2 * 3600) |
| 147 |
data = dict(naive=naive, utc=utcnow(), withtz=naive.replace(tzinfo=other)) |
| 148 |
with mock.patch.object(jsonutil, "tzlocal", lambda: local): # noqa |
| 149 |
with pytest.deprecated_call(match="Please add timezone info"): |
| 150 |
jsondata = json.dumps(data, default=jsonutil.json_default) |
| 151 |
assert "Z" in jsondata |
| 152 |
assert jsondata.count("Z") == 1 |
| 153 |
extracted = jsonutil.extract_dates(json.loads(jsondata)) |
| 154 |
for dt in extracted.values(): |
| 155 |
assert isinstance(dt, datetime.datetime) |
| 156 |
assert dt.tzinfo is not None |
| 157 |
|
| 158 |
|
| 159 |
def test_json_default(): |
| 160 |
# list of input/expected output. Use None for the expected output if it |
| 161 |
# can be the same as the input. |
| 162 |
pairs: list = [ |
| 163 |
(1, None), # start with scalars |
| 164 |
(1.123, None), |
| 165 |
(1.0, None), |
| 166 |
("a", None), |
| 167 |
(True, None), |
| 168 |
(False, None), |
| 169 |
(None, None), |
| 170 |
({"key": b"\xff"}, {"key": "/w=="}), |
| 171 |
# Containers |
| 172 |
([1, 2], None), |
| 173 |
((1, 2), [1, 2]), |
| 174 |
({1, 2}, [1, 2]), |
| 175 |
(dict(x=1), None), |
| 176 |
({"x": 1, "y": [1, 2, 3], "1": "int"}, None), |
| 177 |
# More exotic objects |
| 178 |
((x for x in range(3)), [0, 1, 2]), |
| 179 |
(iter([1, 2]), [1, 2]), |
| 180 |
(MyFloat(), 3.14), |
| 181 |
(MyInt(), 389), |
| 182 |
(datetime.date(2025, 4, 8), "2025-04-08"), |
| 183 |
] |
| 184 |
|
| 185 |
for val, jval in pairs: |
| 186 |
if jval is None: |
| 187 |
jval = val # noqa |
| 188 |
out = json.loads(json.dumps(val, default=jsonutil.json_default)) |
| 189 |
# validate our cleanup |
| 190 |
assert out == jval |