class TestAny: def setup_class(self): self.model = DictModel(User, allow_any=True) now = datetime.now() height = Decimal('1.76') self.keith = User('Keith', height, now, {}) self.keith_dict = dict(name='Keith', height=height, registered=now, meta={}) self.meta = {'app1': [{'age': 13}], 'contact': [self.keith]} self.albert_meta = User('Albert', Decimal('2'), now, self.meta) self.albert_dict = dict(name='Albert', height=Decimal('2'), registered=now, meta=self.meta) def test_load(self): actual = self.model.load(self.keith_dict) assert actual == self.keith def test_dump(self): actual = self.model.dump(self.keith) assert actual == self.keith_dict def test_nested_implicit_any_load(self): actual = self.model.load(self.albert_dict) assert actual.meta == self.meta def test_nested_implicit_any_dump(self): actual = self.model.dump(self.albert_meta) assert actual['meta'] == self.meta
class TestComplexGeneric: def setup_class(self): self.model = DictModel(EnvelopeEnvelope) uuid_str = 'd1d61dd7-c036-47d3-a6ed-91cc2e885fc8' self.o = EnvelopeEnvelope(UUID(uuid_str), Message(Envelope(3, Message('hello')))) self.d = { 'id': uuid_str, 'message': { 'content': { 'id': 3, 'message': { 'content': 'hello' } } } } def test_load(self): actual = self.model.load(self.d) assert actual == self.o def test_dump(self): actual = self.model.dump(self.o) assert actual == self.d
class TestDefaults: def setup_class(self): self.model = DictModel(User) def test_invalid_class(self): with pytest.raises(AssertionError): DictModel(dict, serializers=[]) def test_load(self): user = self.model.load({'id': {'value': 0}, 'username': '******', 'password': '******', 'age': None}) 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': None}, {'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': None} 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': None}, {'id': {'value': 1}, 'username': '******', 'password': '******', 'age': 23}] actual = self.model.dump_many([user1, user2]) assert actual == expected
class TestDecimal: def setup_class(self): self.model = DictModel(Person) def test_load(self): actual = self.model.load(keith_dict) assert actual == keith def test_dump(self): actual = self.model.dump(keith) assert actual == keith_dict
class TestNestedGeneric: def setup_class(self): self.model = DictModel(Envelope[int, Message[str]]) self.o = Envelope(2, Message('test')) self.d = {'id': 2, 'message': {'content': 'test'}} def test_load(self): actual = self.model.load(self.d) assert actual == self.o def test_dump(self): actual = self.model.dump(self.o) assert actual == self.d
class TestSimpleGeneric: def setup_class(self): self.model = DictModel(Envelope[int, str]) self.o = Envelope(1, 'test') self.d = {'id': 1, 'message': 'test'} def test_load(self): actual = self.model.load(self.d) assert actual == self.o def test_dump(self): actual = self.model.dump(self.o) assert actual == self.d
class TestTypes: def setup_class(self): uuid_s = 'd1d61dd7-c036-47d3-a6ed-91cc2e885fc8' self.uuid = UUID(uuid_s) self.dc_uuid_json = {"id": uuid_s} self.model = DictModel(DataclassWithUuid) def test_uuid_encode(self): actual = self.model.dump(DataclassWithUuid(self.uuid)) assert actual == self.dc_uuid_json def test_uuid_decode(self): actual = self.model.load(self.dc_uuid_json) assert actual == DataclassWithUuid(self.uuid)
class TestSerializer: def setup_class(self): serializers = field_serializers([UserIdSerializer]) self.model = DictModel(User, serializers=serializers) def test_load(self): actual = self.model.load({'id': 0, 'username': '******', 'password': '******', 'age': None}) expected = User(id=UserId(0), username='******', password='******', age=None) assert actual == expected def test_dump(self): actual = self.model.dump(User(id=UserId(0), username='******', password='******', age=None)) expected = {'id': 0, 'username': '******', 'password': '******', 'age': None} assert actual == expected
class TestCollectionGeneric: def setup_class(self): self.model = DictModel(Recipe) self.o = Recipe([Author('harry'), Author('hermione')], ['magic', 'shrooms']) self.d = { 'authors': [{ 'name': 'harry' }, { 'name': 'hermione' }], 'tags': ['magic', 'shrooms'] } def test_load(self): actual = self.model.load(self.d) assert actual == self.o def test_dump(self): actual = self.model.dump(self.o) assert actual == self.d