mirror of
https://github.com/apache/superset.git
synced 2026-04-21 00:54:44 +00:00
fix: add DateOffset to json serializer (#32532)
This commit is contained in:
committed by
GitHub
parent
4c3aae7583
commit
33aa9030bf
@@ -15,13 +15,21 @@
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import copy
|
||||
import datetime
|
||||
import math
|
||||
import uuid
|
||||
from datetime import date, datetime, time, timedelta
|
||||
from decimal import Decimal
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from superset.utils import json
|
||||
from superset.utils.core import (
|
||||
zlib_compress,
|
||||
zlib_decompress,
|
||||
)
|
||||
|
||||
|
||||
def test_json_loads():
|
||||
@@ -94,19 +102,35 @@ def test_json_dumps_encoding():
|
||||
|
||||
def test_json_iso_dttm_ser():
|
||||
data = {
|
||||
"datetime": datetime.datetime(2021, 1, 1, 0, 0, 0),
|
||||
"date": datetime.date(2021, 1, 1),
|
||||
"datetime": datetime(2021, 1, 1, 0, 0, 0),
|
||||
"date": date(2021, 1, 1),
|
||||
"dttm": datetime(2020, 1, 1),
|
||||
"dt": date(2020, 1, 1),
|
||||
"t": time(),
|
||||
}
|
||||
|
||||
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"
|
||||
assert reloaded_data["dttm"] == "2020-01-01T00:00:00"
|
||||
assert reloaded_data["dt"] == "2020-01-01"
|
||||
assert reloaded_data["t"] == "00:00:00"
|
||||
assert json.json_iso_dttm_ser(np.int64(1)) == 1
|
||||
|
||||
assert (
|
||||
json.json_iso_dttm_ser(np.datetime64(), pessimistic=True)
|
||||
== "Unserializable [<class 'numpy.datetime64'>]"
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
json.json_iso_dttm_ser(np.datetime64())
|
||||
|
||||
|
||||
def test_pessimistic_json_iso_dttm_ser():
|
||||
data = {
|
||||
"datetime": datetime.datetime(2021, 1, 1, 0, 0, 0),
|
||||
"date": datetime.date(2021, 1, 1),
|
||||
"datetime": datetime(2021, 1, 1, 0, 0, 0),
|
||||
"date": date(2021, 1, 1),
|
||||
}
|
||||
json_str = json.dumps(data, default=json.pessimistic_json_iso_dttm_ser)
|
||||
reloaded_data = json.loads(json_str)
|
||||
@@ -192,3 +216,57 @@ def test_sensitive_fields() -> None:
|
||||
"user_token": "NEW_TOKEN",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_base_json_conv():
|
||||
assert json.base_json_conv(np.bool_(1)) is True
|
||||
assert json.base_json_conv(np.int64(1)) == 1
|
||||
assert json.base_json_conv(np.array([1, 2, 3])) == [1, 2, 3]
|
||||
assert json.base_json_conv(np.array(None)) is None
|
||||
assert json.base_json_conv({1}) == [1]
|
||||
assert json.base_json_conv(Decimal("1.0")) == 1.0
|
||||
assert isinstance(json.base_json_conv(uuid.uuid4()), str)
|
||||
assert json.base_json_conv(time(12, 0)) == "12:00:00"
|
||||
assert json.base_json_conv(timedelta(0)) == "0:00:00"
|
||||
assert json.base_json_conv(b"") == ""
|
||||
assert isinstance(json.base_json_conv(b"\xff\xfe"), str)
|
||||
assert json.base_json_conv(pd.DateOffset(days=1)) == "DateOffset(days=1)"
|
||||
assert json.base_json_conv(b"\xff") == "[bytes]"
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
json.base_json_conv(np.datetime64())
|
||||
|
||||
|
||||
def test_zlib_compression():
|
||||
json_str = '{"test": 1}'
|
||||
blob = zlib_compress(json_str)
|
||||
got_str = zlib_decompress(blob)
|
||||
assert json_str == got_str
|
||||
|
||||
|
||||
def test_json_int_dttm_ser():
|
||||
dttm = datetime(2020, 1, 1)
|
||||
ts = 1577836800000.0
|
||||
assert json.json_int_dttm_ser(dttm) == ts
|
||||
assert json.json_int_dttm_ser(date(2020, 1, 1)) == ts
|
||||
assert json.json_int_dttm_ser(datetime(1970, 1, 1)) == 0
|
||||
assert json.json_int_dttm_ser(date(1970, 1, 1)) == 0
|
||||
assert json.json_int_dttm_ser(dttm + timedelta(milliseconds=1)) == (ts + 1)
|
||||
assert json.json_int_dttm_ser(np.int64(1)) == 1
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
json.json_int_dttm_ser(np.datetime64())
|
||||
|
||||
|
||||
def test_format_timedelta():
|
||||
assert json.format_timedelta(timedelta(0)) == "0:00:00"
|
||||
assert json.format_timedelta(timedelta(days=1)) == "1 day, 0:00:00"
|
||||
assert json.format_timedelta(timedelta(minutes=-6)) == "-0:06:00"
|
||||
assert (
|
||||
json.format_timedelta(timedelta(0) - timedelta(days=1, hours=5, minutes=6))
|
||||
== "-1 day, 5:06:00"
|
||||
)
|
||||
assert (
|
||||
json.format_timedelta(timedelta(0) - timedelta(days=16, hours=4, minutes=3))
|
||||
== "-16 days, 4:03:00"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user