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 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 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 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
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 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'] }
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
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_non_primitive(self): model = DictModel(EventComment) serializer = model.serious_model.serializers_by_field['event'] assert type(serializer) is EnumSerializer ctx = Loading(validating=True) with pytest.raises(ValidationError): serializer.load('1932-11-05', ctx) assert serializer.load('1957-10-04', ctx) is HistoricEvent.SPUTNIK
def setup_class(self): self.model = DictModel(Order) self.valid = Order([OrderLine('Nimbus 2000', 1)]) self.valid_d = {'lines': [{'product': 'Nimbus 2000', 'count': 1}]} self.invalid_d = { 'lines': [{ 'product': 'Advanced Potion-Making', 'count': -1 }] }
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
def test_dataclass_load_validation(): model = DictModel(MockDataclass) serializer = model.serious_model.serializers_by_field['child'] assert type(serializer) is OptionalSerializer assert type(serializer._serializer) is DataclassSerializer ctx = Loading(validating=True) with pytest.raises(ValidationError): serializer.load([1, 2], ctx) with pytest.raises(ValidationError): serializer.load('one', ctx) assert serializer.load(None, ctx) is None assert serializer.load({'child': None}, ctx) == MockDataclass(None)
def test_missing_serializer(): with pytest.raises(FieldMissingSerializer): DictModel(User, serializers=[])
def setup_class(self): self.model = DictModel(User)
def setup_class(self): self.model = DictModel(Person)
def test_invalid_class(self): with pytest.raises(AssertionError): DictModel(dict, serializers=[])
def test_union(): with pytest.raises(ModelContainsUnion): DictModel(Something)
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 setup_class(self): serializers = field_serializers([UserIdSerializer]) self.model = DictModel(User, serializers=serializers)
def test_error_when_unexpected_by_default(self): with pytest.raises(LoadError) as exc_info: DictModel(DataclassWithOptional).load({"x": 1, "y": 1}) assert '"y"' in exc_info.value.message
def test_explicit_without_any(self): assert DictModel(Thing, allow_any=True) assert DictModel(Thing, allow_any=False)
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_default(self): assert DictModel(Thing) with pytest.raises(ModelContainsAny): DictModel(Something)
def test_explicit_with_any(self): assert DictModel(Something, allow_any=True) with pytest.raises(ModelContainsAny): assert DictModel(Something, allow_any=False)
def setup_class(self): self.model = DictModel(Envelope[int, str]) self.o = Envelope(1, 'test') self.d = {'id': 1, 'message': 'test'}
def test_explicit_with_collection_of_any(self): assert DictModel(Closet, allow_any=True) with pytest.raises(ModelContainsAny): assert DictModel(Closet, allow_any=False)
def test_allow_unexpected_is_recursive(self): actual = DictModel(DataclassWithOptionalNested, allow_unexpected=True).load( {"x": {"x": None, "y": "test"}}) expected = DataclassWithOptionalNested(DataclassWithOptional(None)) assert actual == expected