refactor: Unify all json.(loads|dumps) usage to utils.json (#28702)

Co-authored-by: Eyal Ezer <eyal.ezer@ge.com>
This commit is contained in:
Eyal Ezer
2024-05-28 16:17:41 -05:00
committed by GitHub
parent 87110ebce4
commit 07b2449bd7
234 changed files with 530 additions and 480 deletions

View File

@@ -15,30 +15,64 @@
# specific language governing permissions and limitations
# under the License.
import datetime
import json
import math
from unittest.mock import MagicMock
import pytest
from superset.exceptions import SupersetException
from superset.utils.json import (
dumps,
json_iso_dttm_ser,
pessimistic_json_iso_dttm_ser,
validate_json,
)
from superset.utils import json
def test_json_loads():
serialized_data = (
'{"str": "Hello World", "int": 123456789, "float": 0.12345, "bool": true}'
)
data = json.loads(serialized_data)
assert data["str"] == "Hello World"
assert data["int"] == 123456789
assert data["float"] == 0.12345
assert data["bool"] is True
def test_json_loads_exception():
invalid = '{"a": 5, "b": [1, 5, ["g", "h]]}'
with pytest.raises(json.JSONDecodeError) as excinfo:
json.loads(invalid)
assert (
str(excinfo.value)
== "Unterminated string starting at: line 1 column 28 (char 27)"
)
def test_json_loads_encoding():
unicode_data = b'{"a": "\u0073\u0074\u0072"}'
data = json.loads(unicode_data)
assert data["a"] == "str"
utf16_data = b'\xff\xfe{\x00"\x00a\x00"\x00:\x00 \x00"\x00s\x00t\x00r\x00"\x00}\x00'
data = json.loads(utf16_data, encoding="utf-16")
assert data["a"] == "str"
def test_json_loads_allow_nan():
serialized_data = '{"float": NaN}'
with pytest.raises(json.JSONDecodeError) as excinfo:
json.loads(serialized_data)
assert str(excinfo.value) == "Expecting value: line 1 column 11 (char 10)"
data = json.loads(serialized_data, allow_nan=True)
assert isinstance(data, object)
assert math.isnan(data["float"]) is True
def test_json_dumps():
data = {
"str": "some string",
"str": "Hello World",
"int": 123456789,
"float": 0.12345,
"bool": True,
}
json_str = dumps(data, default=pessimistic_json_iso_dttm_ser)
json_str = json.dumps(data, default=json.pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["str"] == "some string"
assert reloaded_data["str"] == "Hello World"
assert reloaded_data["int"] == 123456789
assert reloaded_data["float"] == 0.12345
assert reloaded_data["bool"] is True
@@ -50,7 +84,7 @@ def test_json_dumps_encoding():
"utf16": b"\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00W\x00o\x00r\x00l\x00d\x00",
"bytes": b"\xff",
}
json_str = dumps(data, default=pessimistic_json_iso_dttm_ser)
json_str = json.dumps(data, default=json.pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["utf8"] == "Hello World"
assert reloaded_data["utf16"] == "Hello World"
@@ -62,7 +96,7 @@ def test_json_iso_dttm_ser():
"datetime": datetime.datetime(2021, 1, 1, 0, 0, 0),
"date": datetime.date(2021, 1, 1),
}
json_str = json.dumps(data, default=json_iso_dttm_ser)
json_str = json.dumps(data, default=json.json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["datetime"] == "2021-01-01T00:00:00"
assert reloaded_data["date"] == "2021-01-01"
@@ -72,16 +106,14 @@ def test_pessimistic_json_iso_dttm_ser():
data = {
"datetime": datetime.datetime(2021, 1, 1, 0, 0, 0),
"date": datetime.date(2021, 1, 1),
"UNSERIALIZABLE": MagicMock(),
}
json_str = json.dumps(data, default=pessimistic_json_iso_dttm_ser)
json_str = json.dumps(data, default=json.pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["datetime"] == "2021-01-01T00:00:00"
assert reloaded_data["date"] == "2021-01-01"
assert (
reloaded_data["UNSERIALIZABLE"]
== "Unserializable [<class 'unittest.mock.MagicMock'>]"
)
with pytest.raises(TypeError) as excinfo:
json.dumps({"UNSERIALIZABLE": MagicMock()})
assert str(excinfo.value) == "_asdict() must return a dict, not MagicMock"
def test_pessimistic_json_iso_dttm_ser_nonutf8():
@@ -89,7 +121,7 @@ def test_pessimistic_json_iso_dttm_ser_nonutf8():
"INVALID_UTF8_BYTES": b"\xff",
}
assert isinstance(data["INVALID_UTF8_BYTES"], bytes)
json_str = json.dumps(data, default=pessimistic_json_iso_dttm_ser)
json_str = json.dumps(data, default=json.pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["INVALID_UTF8_BYTES"] == "[bytes]"
@@ -99,16 +131,18 @@ def test_pessimistic_json_iso_dttm_ser_utf16():
"VALID_UTF16_BYTES": b"\xff\xfeS0\x930k0a0o0\x16NLu",
}
assert isinstance(data["VALID_UTF16_BYTES"], bytes)
json_str = json.dumps(data, default=pessimistic_json_iso_dttm_ser)
json_str = json.dumps(data, default=json.pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["VALID_UTF16_BYTES"] == "こんにちは世界"
def test_validate_json():
valid = '{"a": 5, "b": [1, 5, ["g", "h"]]}'
assert validate_json(valid) is None
assert json.validate_json(valid) is None
invalid = '{"a": 5, "b": [1, 5, ["g", "h]]}'
with pytest.raises(SupersetException) as excinfo:
validate_json(invalid)
assert str(excinfo.value) == "JSON is not valid"
with pytest.raises(json.JSONDecodeError) as excinfo:
json.validate_json(invalid)
assert (
str(excinfo.value)
== "Unterminated string starting at: line 1 column 28 (char 27)"
)