42 lines
953 B
Python
42 lines
953 B
Python
# coding=utf-8
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
import datetime
|
|
import pytest
|
|
from tsgrain_controller import util
|
|
|
|
|
|
@dataclass
|
|
class _TestCls:
|
|
name: str
|
|
money: int
|
|
|
|
|
|
class _TestClsSerializable(_TestCls):
|
|
|
|
def serialize(self):
|
|
return {'name': 'Little' + self.name, 'money': self.money}
|
|
|
|
|
|
class _TestEnum(Enum):
|
|
OPT1 = 1
|
|
OPT2 = 2
|
|
|
|
|
|
@pytest.mark.parametrize('o,expected', [
|
|
('hello', '"hello"'),
|
|
(42, '42'),
|
|
({
|
|
'k1': 'v1',
|
|
'k2': 'v2'
|
|
}, '{"k1": "v1", "k2": "v2"}'),
|
|
(_TestEnum.OPT2, '2'),
|
|
(_TestCls("Mary", 121), '{"name": "Mary", "money": 121}'),
|
|
(_TestClsSerializable("Mary",
|
|
121), '{"name": "LittleMary", "money": 121}'),
|
|
(datetime.datetime(2022, 1, 2, 15, 30, 11), '"2022-01-02T15:30:11"'),
|
|
(datetime.date(2022, 1, 2), '"2022-01-02"'),
|
|
])
|
|
def test_to_json(o, expected):
|
|
res = util.to_json(o)
|
|
assert res == expected
|