Skip to content

Commit

Permalink
Update filtration for LazySchema
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Tkachenko authored and Stranger6667 committed Sep 30, 2019
1 parent ae2284e commit b158599
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/schemathesis/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from .models import Endpoint
from .schemas import BaseSchema
from .types import Filter
from .utils import NOT_SET


@attr.s(slots=True)
class LazySchema:
fixture_name: str = attr.ib()

def parametrize(self, method: Optional[Filter] = None, endpoint: Optional[Filter] = None) -> Callable:
def parametrize(self, method: Optional[Filter] = NOT_SET, endpoint: Optional[Filter] = NOT_SET) -> Callable:
def wrapper(func: Callable) -> Callable:
def test(request: FixtureRequest, subtests: SubTests) -> None:
"""The actual test, which is executed by pytest."""
Expand Down Expand Up @@ -54,9 +55,11 @@ def get_schema(
schema = request.getfixturevalue(name)
if not isinstance(schema, BaseSchema):
raise ValueError(f"The given schema must be an instance of BaseSchema, got: {type(schema)}")
schema.method = method
schema.endpoint = endpoint
return schema
if method is NOT_SET:
method = schema.method
if method is not NOT_SET:
endpoint = schema.endpoint
return schema.__class__(schema.raw_schema, method=method, endpoint=endpoint)


def get_fixtures(func: Callable, request: FixtureRequest) -> Dict[str, Any]:
Expand Down
40 changes: 40 additions & 0 deletions test/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,46 @@ def test_b(request, case):
result.stdout.re_match_lines([r"Hypothesis calls: 2$"])


def test_with_filters_override(testdir):
# When the test uses method / endpoint filter
testdir.make_test(
"""
@pytest.fixture
def simple_schema():
return schema
lazy_schema = schemathesis.from_pytest_fixture("simple_schema")
@lazy_schema.parametrize(endpoint=None, method="GET")
def test_a(request, case):
request.config.HYPOTHESIS_CASES += 1
assert case.path == "/v1/first"
assert case.method == "GET"
@lazy_schema.parametrize(endpoint="/second", method=None)
def test_b(request, case):
request.config.HYPOTHESIS_CASES += 1
assert case.path == "/v1/second"
assert case.method == "POST"
""",
paths={"/first": {"post": {}, "get": {}}, "/second": {"post": {}, "get": {}}},
method="/first",
endpoint="POST",
)
result = testdir.runpytest("-v")
# Then the filters should be applied to the generated tests
result.assert_outcomes(passed=2)
result.stdout.re_match_lines(
[
r"test_with_filters_override.py::test_a PASSED",
r"test_with_filters_override.py::test_b PASSED",
r".*2 passed",
]
)
result.stdout.re_match_lines([r"Hypothesis calls: 2$"])


def test_invalid_fixture(testdir):
# When the test uses a schema fixture that doesn't return a BaseSchema subtype
testdir.make_test(
Expand Down

0 comments on commit b158599

Please sign in to comment.