Skip to content

Commit

Permalink
Add tests for only and exclude with fields/additional
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Deckard committed Jun 1, 2018
1 parent 45bd98c commit 84e9a84
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions tests/test_schema.py
Expand Up @@ -654,7 +654,7 @@ def test_only_in_init(SchemaClass, user):
assert 'age' in s

def test_invalid_only_param(user):
with pytest.raises(AttributeError):
with pytest.raises(ValueError):
UserSchema(only=("_invalid", "name")).dump(user)

def test_can_serialize_uuid(serialized_user, user):
Expand Down Expand Up @@ -1274,12 +1274,68 @@ class MySchema(Schema):
assert 'bar' not in result


def test_only_and_exclude_with_fields():
class MySchema(Schema):
foo = fields.Field()

class Meta:
fields = ('bar', 'baz')
sch = MySchema(only=('bar', 'baz'), exclude=('bar', ))
data = dict(foo=42, bar=24, baz=242)
result = sch.dump(data)
assert 'baz' in result
assert 'bar' not in result


def test_invalid_only_and_exclude_with_fields():
class MySchema(Schema):
foo = fields.Field()

class Meta:
fields = ('bar', 'baz')

with pytest.raises(ValueError) as excinfo:
MySchema(only=('foo', 'par'), exclude=('ban', ))

assert 'foo' in str(excinfo)
assert 'par' in str(excinfo)
assert 'ban' in str(excinfo)


def test_only_and_exclude_with_additional():
class MySchema(Schema):
foo = fields.Field()

class Meta:
additional = ('bar', 'baz')
sch = MySchema(only=('foo', 'bar'), exclude=('bar', ))
data = dict(foo=42, bar=24, baz=242)
result = sch.dump(data)
assert 'foo' in result
assert 'bar' not in result


def test_invalid_only_and_exclude_with_additional():
class MySchema(Schema):
foo = fields.Field()

class Meta:
additional = ('bar', 'baz')

with pytest.raises(ValueError) as excinfo:
MySchema(only=('foop', 'par'), exclude=('ban', ))

assert 'foop' in str(excinfo)
assert 'par' in str(excinfo)
assert 'ban' in str(excinfo)


def test_exclude_invalid_attribute():

class MySchema(Schema):
foo = fields.Field()

with pytest.raises(AttributeError):
with pytest.raises(ValueError):
MySchema(exclude=('bar', ))


Expand All @@ -1289,7 +1345,7 @@ class MySchema(Schema):
class Meta:
fields = ('foo', )

with pytest.raises(AttributeError):
with pytest.raises(ValueError):
MySchema(only=('baz', ))


Expand All @@ -1299,7 +1355,7 @@ class MySchema(Schema):
class Meta:
additional = ('b', )

with pytest.raises(AttributeError):
with pytest.raises(ValueError):
MySchema(only=('c', )).dump({'c': 3})

def test_only_empty():
Expand Down

0 comments on commit 84e9a84

Please sign in to comment.