Skip to content

Commit

Permalink
added test for nested context
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostwheel42 committed Nov 16, 2021
1 parent eee1866 commit c04359d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_dump.py
Expand Up @@ -133,6 +133,32 @@ class TestSchema(Schema):
assert nested_def["properties"]["foo"]["type"] == "integer"


def test_nested_context():
class TestNestedSchema(Schema):
def __init__(self, *args, **kwargs):
if kwargs.get("context", {}).get("hide", False):
kwargs["exclude"] = ["foo"]
super().__init__(*args, **kwargs)

foo = fields.Integer(required=True)
bar = fields.Integer(required=True)

class TestSchema(Schema):
bar = fields.Nested(TestNestedSchema)

schema = TestSchema()
dumped_show = validate_and_dump(schema)

schema = TestSchema(context={"hide": True})
dumped_hide = validate_and_dump(schema)

nested_show = dumped_show["definitions"]["TestNestedSchema"]["properties"]
nested_hide = dumped_hide["definitions"]["TestNestedSchema"]["properties"]

assert "bar" in nested_show and "foo" in nested_show
assert "bar" in nested_hide and "foo" not in nested_hide


def test_list():
class ListSchema(Schema):
foo = fields.List(fields.String(), required=True)
Expand Down

0 comments on commit c04359d

Please sign in to comment.