def blog(user): col1 = User(name="Mick", age=123) col2 = User(name="Keith", age=456) return Blog("Monty's blog", user=user, categories=['humor', 'violence'], collaborators=[col1, col2])
def test_method_field_with_uncallable_attribute(self): class BadSerializer(Schema): foo = 'not callable' bad_field = fields.Method('foo') u = User('Foo') with pytest.raises(ValueError): BadSerializer().dump(u)
def test_default_to_missing(self): u = User('Foo', email=None) marshal = fields.Marshaller() res = marshal(u, {'name': fields.String(), 'email': fields.Email(default=fields.missing)}) assert res['name'] == u.name assert 'email' not in res
def test_method_field_with_method_missing(self): class BadSerializer(Schema): bad_field = fields.Method('invalid') u = User('Foo') with pytest.raises(ValueError): BadSerializer().dump(u)
def test_string_field(self): field = fields.String() user = User(name=b'foo') assert field.serialize('name', user) == 'foo' field = fields.String(allow_none=True) user.name = None assert field.serialize('name', user) is None
def test_string_field(self): field = fields.String() user = User(name=b"foo") assert field.serialize("name", user) == "foo" field = fields.String(allow_none=True) user.name = None assert field.serialize("name", user) is None
def test_serialize_fields_with_load_only_param(self, marshal): u = User('Foo', email='*****@*****.**') fields_dict = { 'name': fields.String(), 'email': fields.Email(load_only=True), } result = marshal(u, fields_dict) assert result['name'] == 'Foo' assert 'email' not in result
def test_method_field_deserialization_is_noop_by_default(self): class MiniUserSchema(Schema): uppername = fields.Method('uppercase_name') def uppercase_name(self, obj): return obj.upper() user = User(name='steve') s = MiniUserSchema(user) assert s.fields['uppername'].deserialize('steve') == 'steve'
def test_serialize_fields_with_data_key_and_prefix_params(self): u = User('Foo', email='*****@*****.**') marshal = Marshaller(prefix='usr_') result = marshal( u, { 'email': fields.Email(data_key='EmAiL'), 'name': fields.String(data_key='NaMe') }) assert result['usr_NaMe'] == u.name assert result['usr_EmAiL'] == u.email
def test_serialize_fields_with_dump_to_and_prefix_params(self): u = User("Foo", email="*****@*****.**") marshal = Marshaller(prefix='usr_') result = marshal( u, { "email": fields.Email(dump_to='EmAiL'), 'name': fields.String(dump_to='NaMe') }) assert result['usr_NaMe'] == u.name assert result['usr_EmAiL'] == u.email
def test__authenticate_user(self): user = User( username='******', password=pbkdf2_sha256.hash("12341234"), email='*****@*****.**', id_partner=1, is_superuser=True, category='partner' ) auth_body = { 'username': user.username, 'password': "******", } _, response = self.client.post('/auth', json=auth_body) token = response.json.get('access_token', None) self.assertIsNotNone(token) user.delete()
def test_formattedstring_field_on_schema(self): class MySchema(Schema): greeting = fields.FormattedString('Hello {name}') user = User(name='Monty') assert MySchema().dump(user).data['greeting'] == 'Hello Monty'
def user(self): return User('Foo', email='*****@*****.**', age=42)
def test_formattedstring_field(self): field = fields.FormattedString('Hello {name}') user = User(name='Monty') assert field.serialize('name', user) == 'Hello Monty'
def user(self): return User("Foo", email="*****@*****.**", age=42)
def test_unordered_dump_returns_dict(self): schema = self.UnorderedSchema() u = User("steve", email="*****@*****.**") result = schema.dump(u) assert not isinstance(result, OrderedDict) assert type(result) is dict
def test_select_field(self, user): field = fields.Select(['male', 'female', 'transexual', 'asexual']) assert field.serialize("sex", user) == "male" invalid = User('foo', sex='alien') with pytest.raises(MarshallingError): field.serialize('sex', invalid)
def test_string_field(self): field = fields.String() user = User(name=b'foo') assert field.serialize('name', user) == 'foo' user.name = None assert field.serialize('name', user) == ''
def test_prefix(self): u = User('Foo', email='*****@*****.**') marshal = Marshaller(prefix='usr_') result = marshal(u, {'email': fields.Email(), 'name': fields.String()}) assert result['usr_name'] == u.name assert result['usr_email'] == u.email
def test_marshalling_generator(self, marshal): gen = (u for u in [User('Foo'), User('Bar')]) res = marshal(gen, {'name': fields.String()}, many=True) assert len(res) == 2
def test_prefix(self): u = User("Foo", email="*****@*****.**") marshal = Marshaller(prefix='usr_') result = marshal(u, {"email": fields.Email(), 'name': fields.String()}) assert result['usr_name'] == u.name assert result['usr_email'] == u.email
def make_object(self, data): return User(**data)
def user(): return User(name="Monty", age=42.3, homepage="http://monty.python.org/")
def user(): return User(name='Monty', age=42.3, homepage='http://monty.python.org/')
def test_marshalling_generator(self, marshal): gen = (u for u in [User("Foo"), User("Bar")]) res = marshal(gen, {"name": fields.String()}, many=True) assert len(res) == 2