Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions tests/test_hypothesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def definition_schema():
[
cs.typed_dict_schema(
{
'name': cs.typed_dict_field(cs.str_schema()),
'sub_branch': cs.typed_dict_field(
cs.with_default_schema(
cs.nullable_schema(cs.definition_reference_schema('Branch')), default=None
Expand All @@ -82,11 +81,10 @@ def definition_schema():


def test_definition_simple(definition_schema):
assert definition_schema.validate_python({'name': 'root'}) == {'name': 'root', 'sub_branch': None}
assert definition_schema.validate_python({}) == {'sub_branch': None}


class BranchModel(TypedDict):
name: str
sub_branch: Optional['BranchModel']


Expand All @@ -97,34 +95,30 @@ def test_recursive(definition_schema, data):
assert definition_schema.validate_python(data) == data


@strategies.composite
def branch_models_with_cycles(draw, existing=None):
if existing is None:
existing = []
model = BranchModel(name=draw(strategies.text()), sub_branch=None)
existing.append(model)
model['sub_branch'] = draw(
strategies.none()
| strategies.builds(BranchModel, name=strategies.text(), sub_branch=branch_models_with_cycles(existing))
| strategies.sampled_from(existing)
)
return model
@given(strategies.integers(min_value=0, max_value=10))
@pytest.mark.thread_unsafe # https://github.com/Quansight-Labs/pytest-run-parallel/issues/20
def test_definition_cycles(definition_schema, depth):
data = BranchModel(sub_branch=None)
model = data

for _ in range(depth):
next_model = BranchModel(sub_branch=None)
model['sub_branch'] = next_model
model = next_model

@given(branch_models_with_cycles())
@pytest.mark.thread_unsafe # https://github.com/Quansight-Labs/pytest-run-parallel/issues/20
def test_definition_cycles(definition_schema, data):
try:
assert definition_schema.validate_python(data) == data
except ValidationError as exc:
assert exc.errors(include_url=False) == [
{
'type': 'recursion_loop',
'loc': IsTuple(length=(1, None)),
'msg': 'Recursion error - cyclic reference detected',
'input': AnyThing(),
}
]
model['sub_branch'] = data

with pytest.raises(ValidationError) as exc_info:
definition_schema.validate_python(data)

assert exc_info.value.errors(include_url=False) == [
{
'type': 'recursion_loop',
'loc': IsTuple(length=(1, None)),
'msg': 'Recursion error - cyclic reference detected',
'input': AnyThing(),
}
]


def test_definition_broken(definition_schema):
Expand Down
Loading