-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_jsn.py
36 lines (28 loc) · 994 Bytes
/
test_jsn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from utz import dataclass, Encoder, json, fromtimestamp
from utz.time import utc
def test_datetimes():
epoch = fromtimestamp(0, tz=utc)
assert json.dumps({ 'epoch': epoch}, cls=Encoder) == '{"epoch": "1970-01-01 00:00:00"}'
assert json.dumps({ 'epoch': epoch}, cls=Encoder, indent=2) == '\n'.join([
'{',
' "epoch": "1970-01-01 00:00:00"',
'}',
])
assert json.dumps({ 'epoch': epoch}, cls=Encoder("%Y-%m-%d")) == '{"epoch": "1970-01-01"}'
assert json.dumps({ 'epoch': epoch}, cls=Encoder("%Y-%m-%d"), indent=2) == '\n'.join([
'{',
' "epoch": "1970-01-01"',
'}',
])
@dataclass
class A:
n: int
@dataclass
class B:
arr: list[A]
def test_dataclasses():
assert json.dumps(A(111), cls=Encoder) == '{"n": 111}'
b = B([A(111), A(222)])
b_str = '{"arr": [{"n": 111}, {"n": 222}]}'
assert json.dumps(b, cls=Encoder) == b_str
assert json.dumps({ 'b': b }, cls=Encoder) == '{"b": %s}' % b_str