| 1 |
"""Test suite for our JSON utilities.""" |
| 2 |
|
| 3 |
# Copyright (c) IPython Development Team. |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
|
| 6 |
import json |
| 7 |
import numbers |
| 8 |
from binascii import a2b_base64 |
| 9 |
from datetime import date, datetime |
| 10 |
|
| 11 |
import pytest |
| 12 |
from jupyter_client._version import version_info as jupyter_client_version |
| 13 |
|
| 14 |
from ipykernel import jsonutil |
| 15 |
from ipykernel.jsonutil import encode_images, json_clean |
| 16 |
|
| 17 |
JUPYTER_CLIENT_MAJOR_VERSION: int = jupyter_client_version[0] # type:ignore |
| 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 |
@pytest.mark.skipif(JUPYTER_CLIENT_MAJOR_VERSION >= 7, reason="json_clean is a no-op") |
| 37 |
def test(): |
| 38 |
# list of input/expected output. Use None for the expected output if it |
| 39 |
# can be the same as the input. |
| 40 |
pairs = [ |
| 41 |
(1, None), # start with scalars |
| 42 |
(1.0, None), |
| 43 |
("a", None), |
| 44 |
(True, None), |
| 45 |
(False, None), |
| 46 |
(None, None), |
| 47 |
# Containers |
| 48 |
([1, 2], None), |
| 49 |
((1, 2), [1, 2]), |
| 50 |
({1, 2}, [1, 2]), |
| 51 |
(dict(x=1), None), |
| 52 |
({"x": 1, "y": [1, 2, 3], "1": "int"}, None), |
| 53 |
# More exotic objects |
| 54 |
((x for x in range(3)), [0, 1, 2]), |
| 55 |
(iter([1, 2]), [1, 2]), |
| 56 |
(datetime(1991, 7, 3, 12, 00), "1991-07-03T12:00:00.000000"), |
| 57 |
(date(1991, 7, 3), "1991-07-03T00:00:00.000000"), |
| 58 |
(MyFloat(), 3.14), |
| 59 |
(MyInt(), 389), |
| 60 |
] |
| 61 |
|
| 62 |
for val, jval in pairs: |
| 63 |
if jval is None: |
| 64 |
jval = val # type:ignore |
| 65 |
out = json_clean(val) |
| 66 |
# validate our cleanup |
| 67 |
assert out == jval |
| 68 |
# and ensure that what we return, indeed encodes cleanly |
| 69 |
json.loads(json.dumps(out)) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.skipif(JUPYTER_CLIENT_MAJOR_VERSION >= 7, reason="json_clean is a no-op") |
| 73 |
def test_encode_images(): |
| 74 |
# invalid data, but the header and footer are from real files |
| 75 |
pngdata = b"\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82" |
| 76 |
jpegdata = b"\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9" |
| 77 |
pdfdata = b"%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>" |
| 78 |
bindata = b"\xff\xff\xff\xff" |
| 79 |
|
| 80 |
fmt = { |
| 81 |
"image/png": pngdata, |
| 82 |
"image/jpeg": jpegdata, |
| 83 |
"application/pdf": pdfdata, |
| 84 |
"application/unrecognized": bindata, |
| 85 |
} |
| 86 |
encoded = json_clean(encode_images(fmt)) |
| 87 |
for key, value in fmt.items(): |
| 88 |
# encoded has unicode, want bytes |
| 89 |
decoded = a2b_base64(encoded[key]) |
| 90 |
assert decoded == value |
| 91 |
encoded2 = json_clean(encode_images(encoded)) |
| 92 |
assert encoded == encoded2 |
| 93 |
|
| 94 |
for key, value in fmt.items(): |
| 95 |
decoded = a2b_base64(encoded[key]) |
| 96 |
assert decoded == value |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.skipif(JUPYTER_CLIENT_MAJOR_VERSION >= 7, reason="json_clean is a no-op") |
| 100 |
def test_lambda(): |
| 101 |
with pytest.raises(ValueError): # noqa: PT011 |
| 102 |
json_clean(lambda: 1) |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.skipif(JUPYTER_CLIENT_MAJOR_VERSION >= 7, reason="json_clean is a no-op") |
| 106 |
def test_exception(): |
| 107 |
bad_dicts = [ |
| 108 |
{1: "number", "1": "string"}, |
| 109 |
{True: "bool", "True": "string"}, |
| 110 |
] |
| 111 |
for d in bad_dicts: |
| 112 |
with pytest.raises(ValueError): # noqa: PT011 |
| 113 |
json_clean(d) |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.skipif(JUPYTER_CLIENT_MAJOR_VERSION >= 7, reason="json_clean is a no-op") |
| 117 |
def test_unicode_dict(): |
| 118 |
data = {"üniço∂e": "üniço∂e"} |
| 119 |
clean = jsonutil.json_clean(data) |
| 120 |
assert data == clean |