Skip to content

Uprev speedate #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ strum_macros = "0.26.1"
serde_json = {version = "1.0.114", features = ["arbitrary_precision", "preserve_order"]}
enum_dispatch = "0.3.8"
serde = { version = "1.0.196", features = ["derive"] }
speedate = "0.13.0"
speedate = "0.14.0"
smallvec = "1.13.1"
ahash = "0.8.10"
url = "2.5.0"
Expand Down
6 changes: 3 additions & 3 deletions tests/serializers/test_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_set_member_db(any_serializer):
(datetime(2032, 1, 1, 1, 1), b'"2032-01-01T01:01:00"'),
(date(2022, 12, 3), b'"2022-12-03"'),
(time(12, 30, 45), b'"12:30:45"'),
(timedelta(hours=2), b'"PT7200S"'),
(timedelta(hours=2), b'"PT2H"'),
(MyDataclass(1, 'foo', 2), b'{"a":1,"b":"foo"}'),
(MyModel(a=1, b='foo'), b'{"a":1,"b":"foo"}'),
([MyDataclass(1, 'a', 2), MyModel(a=2, b='b')], b'[{"a":1,"b":"a"},{"a":2,"b":"b"}]'),
Expand Down Expand Up @@ -164,8 +164,8 @@ def test_any_with_date_serializer():
def test_any_with_timedelta_serializer():
s = SchemaSerializer(core_schema.any_schema(serialization={'type': 'timedelta'}))
assert s.to_python(timedelta(hours=2)) == timedelta(hours=2)
assert s.to_python(timedelta(hours=2), mode='json') == 'PT7200S'
assert s.to_json(timedelta(hours=2)) == b'"PT7200S"'
assert s.to_python(timedelta(hours=2), mode='json') == 'PT2H'
assert s.to_json(timedelta(hours=2)) == b'"PT2H"'

with pytest.warns(UserWarning) as warning_info:
assert s.to_python(b'bang', mode='json') == 'bang'
Expand Down
12 changes: 6 additions & 6 deletions tests/serializers/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def test_timedelta():
v = SchemaSerializer(core_schema.timedelta_schema())
assert v.to_python(timedelta(days=2, hours=3, minutes=4)) == timedelta(days=2, hours=3, minutes=4)

assert v.to_python(timedelta(days=2, hours=3, minutes=4), mode='json') == 'P2DT11040S'
assert v.to_json(timedelta(days=2, hours=3, minutes=4)) == b'"P2DT11040S"'
assert v.to_python(timedelta(days=2, hours=3, minutes=4), mode='json') == 'P2DT3H4M'
assert v.to_json(timedelta(days=2, hours=3, minutes=4)) == b'"P2DT3H4M"'

with pytest.warns(
UserWarning, match='Expected `timedelta` but got `int` - serialized value may not be as expected'
Expand All @@ -39,14 +39,14 @@ def test_timedelta_float():
def test_timedelta_key():
v = SchemaSerializer(core_schema.dict_schema(core_schema.timedelta_schema(), core_schema.int_schema()))
assert v.to_python({timedelta(days=2, hours=3, minutes=4): 1}) == {timedelta(days=2, hours=3, minutes=4): 1}
assert v.to_python({timedelta(days=2, hours=3, minutes=4): 1}, mode='json') == {'P2DT11040S': 1}
assert v.to_json({timedelta(days=2, hours=3, minutes=4): 1}) == b'{"P2DT11040S":1}'
assert v.to_python({timedelta(days=2, hours=3, minutes=4): 1}, mode='json') == {'P2DT3H4M': 1}
assert v.to_json({timedelta(days=2, hours=3, minutes=4): 1}) == b'{"P2DT3H4M":1}'


@pytest.mark.skipif(not pandas, reason='pandas not installed')
def test_pandas():
v = SchemaSerializer(core_schema.timedelta_schema())
d = pandas.Timestamp('2023-01-01T02:00:00Z') - pandas.Timestamp('2023-01-01T00:00:00Z')
assert v.to_python(d) == d
assert v.to_python(d, mode='json') == 'PT7200S'
assert v.to_json(d) == b'"PT7200S"'
assert v.to_python(d, mode='json') == 'PT2H'
assert v.to_json(d) == b'"PT2H"'