class TestDecoder: def setup_class(self): self.dcwdc = JsonModel(DataclassWithDataclass) self.dcxs = JsonModel(DataclassXs) def test_nested_dataclass(self): actual = self.dcwdc.load(json.dumps({"dc_with_list": {"xs": [1]}})) expected = DataclassWithDataclass(DataclassWithList([1])) assert actual == expected def test_nested_list_of_dataclasses(self): actual = self.dcxs.load(json.dumps({"xs": [{"x": 0}, {"x": 1}]})) expected = DataclassXs([DataclassX(0), DataclassX(1)]) assert actual == expected
def fetch(name: str) -> PypiPackage: response = requests.get(f'https://pypi.org/pypi/{name}/json') try: response.raise_for_status() except requests.exceptions.HTTPError as e: raise PypiError(name) from e package_json = response.text model = JsonModel(PypiPackage, allow_any=True, camel_case=False) return model.load(package_json)
class TestTime: def setup_class(self): self.model = JsonModel(Alarm) self.json = f'{{"at": "07:00:00", "enabled": true}}' self.dataclass = Alarm(time(7, 0, 0), enabled=True) def test_load(self): assert self.model.load(self.json) == self.dataclass def test_dump(self): assert self.model.dump(self.dataclass) == self.json
class TestDate: def setup_class(self): self.model = JsonModel(Event) self.json = f'{{"what": "Nobel Prize", "when": "1922-09-09"}}' self.dataclass = Event("Nobel Prize", date(1922, 9, 9)) def test_load(self): assert self.model.load(self.json) == self.dataclass def test_dump(self): assert self.model.dump(self.dataclass) == self.json
class TestTimestamp: def setup_class(self): self.model = JsonModel(TimestampDatetime) dt = datetime(2018, 11, 17, 16, 55, 28, 456753, tzinfo=timezone.utc) ts = dt.timestamp() self.json = f'{{"value": {ts}}}' self.dataclass = TimestampDatetime(Timestamp(dt)) def test_load(self): assert self.model.load(self.json) == self.dataclass def test_dump(self): assert self.model.dump(self.dataclass) == self.json
class TestDefaultDatetime: def setup_class(self): self.model = JsonModel(Post) dt = datetime(2018, 11, 17, 16, 55, 28, 456753, tzinfo=timezone.utc) iso = dt.isoformat() self.json = f'{{"createdAt": "{iso}"}}' self.dataclass = Post(datetime.fromisoformat(iso)) def test_load(self): assert self.model.load(self.json) == self.dataclass def test_dump(self): assert self.model.dump(self.dataclass) == self.json
def fetch_package(name: str, warehouse='https://pypi.org/pypi') -> Package: """Fetch a package from pypi.org by name. Provide a URL for other warehouse to use it instead of PyPI. """ response = requests.get(f'{warehouse}/{name}/json') try: response.raise_for_status() except requests.exceptions.HTTPError as e: raise PackageIndexError(name) from e package_json = response.text model = JsonModel(Package, camel_case=False) return model.load(package_json)
class TestTypes: def setup_class(self): self.uuid_s = 'd1d61dd7-c036-47d3-a6ed-91cc2e885fc8' self.dc_uuid_json = f'{{"id": "{self.uuid_s}"}}' self.uuid_model = JsonModel(DataclassWithUuid) def test_uuid_decode(self): actual = self.uuid_model.load(self.dc_uuid_json) assert actual == DataclassWithUuid(UUID(self.uuid_s)) def test_uuid_encode(self): actual = self.uuid_model.dump(DataclassWithUuid(UUID(self.uuid_s))) assert actual == self.dc_uuid_json
class TestSerializer: def setup_class(self): serializers = field_serializers([UserIdSerializer]) self.model = JsonModel(User, serializers=serializers) def test_dump(self): actual = self.model.dump( User(id=UserId(0), username='******', password='******', age=None)) expected = '{"id": 0, "username": "******", "password": "******", "age": null}' assert actual == expected def test_load(self): actual = self.model.load( '{"id": 0, "username": "******", "password": "******", "age": null}') expected = User(id=UserId(0), username='******', password='******', age=None) assert actual == expected
class TestDefaults: def setup_class(self): self.model = JsonModel(User) def test_invalid_class(self): with pytest.raises(AssertionError): JsonModel(dict, serializers=[]) def test_load(self): user = self.model.load( '{"id": {"value": 0}, "username": "******", "password": "******", "age": null}' ) assert user == User(id=UserId(0), username='******', password='******', age=None) def test_load_many(self): expected = [ User(id=UserId(0), username='******', password='******', age=None), User(id=UserId(1), username='******', password='******', age=23) ] data = '[{"id": {"value": 0}, "username": "******", "password": "******", "age": null},' \ '{"id": {"value": 1}, "username": "******", "password": "******", "age": 23}]' actual = self.model.load_many(data) assert actual == expected def test_dump(self): user = User(id=UserId(0), username='******', password='******', age=None) d = self.model.dump(user) assert d == '{"id": {"value": 0}, "username": "******", "password": "******", "age": null}' def test_dump_many(self): user1 = User(id=UserId(0), username='******', password='******', age=None) user2 = User(id=UserId(1), username='******', password='******', age=23) expected = '[{"id": {"value": 0}, "username": "******", "password": "******", "age": null}, ' \ '{"id": {"value": 1}, "username": "******", "password": "******", "age": 23}]' actual = self.model.dump_many([user1, user2]) assert actual == expected