Skip to content

Commit

Permalink
Stricter tests & refactoring
Browse files Browse the repository at this point in the history
To fix cases discovered by mutmut
  • Loading branch information
Dmitry Dygalo authored and Stranger6667 committed Sep 13, 2019
1 parent 0f5b065 commit 83843cf
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
7 changes: 1 addition & 6 deletions src/schemathesis/extra/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ def collect(self) -> List[Function]: # type: ignore
"""Generate different test items for all endpoints available in the given schema."""
try:
return [
item
for endpoint in self.schemathesis_case.schema.get_all_endpoints(
filter_method=self.schemathesis_case.filter_method,
filter_endpoint=self.schemathesis_case.filter_endpoint,
)
for item in self._gen_items(endpoint)
item for endpoint in self.schemathesis_case.get_all_endpoints() for item in self._gen_items(endpoint)
]
except Exception:
pytest.fail("Error during collection")
Expand Down
2 changes: 0 additions & 2 deletions src/schemathesis/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from .schemas import Endpoint
from .types import Body, Headers, PathParameters, Query

# TODO. Better naming


@attr.s(slots=True)
class Case:
Expand Down
5 changes: 4 additions & 1 deletion src/schemathesis/parametrizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, Optional, Union
from typing import Any, Callable, Dict, Generator, Optional, Union

import attr

Expand Down Expand Up @@ -89,6 +89,9 @@ def schema(self) -> schemas.BaseSchema:
self._schema = schemas.wrap_schema(schema)
return self._schema

def get_all_endpoints(self) -> Generator[schemas.Endpoint, None, None]:
return self.schema.get_all_endpoints(filter_method=self.filter_method, filter_endpoint=self.filter_endpoint)


def is_schemathesis_test(func: Callable) -> bool:
"""Check whether test is parametrized with schemathesis."""
Expand Down
10 changes: 3 additions & 7 deletions src/schemathesis/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PreparedParameters:
body: Body = attr.ib(init=False, factory=empty_object)


@attr.s(hash=False)
@attr.s(slots=True)
class BaseSchema:
raw_schema: Dict[str, Any] = attr.ib()

Expand Down Expand Up @@ -127,11 +127,7 @@ def process_query(self, result: PreparedParameters, parameter: Dict[str, Any]) -

def process_body(self, result: PreparedParameters, parameter: Dict[str, Any]) -> None:
# "schema" is a required field
body = self.resolve(parameter["schema"])
if body["type"] == "object":
# Objects could contain references inside
body = self.resolve(body)
result.body = body
result.body = self.resolve(parameter["schema"])

def add_parameter(self, container: Dict[str, Any], parameter: Dict[str, Any]) -> None:
"""Add parameter object to a container."""
Expand All @@ -146,7 +142,7 @@ def parameter_to_json_schema(self, data: Dict[str, Any]) -> Dict[str, Any]:
key: value
for key, value in data.items()
# Do not include keys not supported by JSON schema
if key not in ("name", "in") and (key != "required" or isinstance(value, list))
if not (key == "required" and not isinstance(value, list))
}

@overload
Expand Down
25 changes: 23 additions & 2 deletions test/schemas/test_parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,27 @@ def test_(request, case):
result.stdout.re_match_lines([r"Hypothesis calls: 1"])


def test_without_required(testdir):
# When "required" field is not present in the parameter
testdir.make_test(
"""
@schema.parametrize()
@settings(max_examples=1)
def test_(request, case):
assert case.path == "/v1/users"
assert case.method == "GET"
if not case.query:
request.config.HYPOTHESIS_CASES += 1
""",
**as_param({"in": "query", "name": "key", "type": "string"}),
)
# then the parameter is optional
# NOTE. could be flaky
result = testdir.runpytest("-v", "-s")
result.assert_outcomes(passed=1)
result.stdout.re_match_lines([r"Hypothesis calls: 1"])


def test_invalid_schema(testdir):
# When the given schema is not valid
testdir.makepyfile(
Expand All @@ -419,7 +440,7 @@ def test_(request, case):
result = testdir.runpytest()
# Then collection phase should fail with error
result.assert_outcomes(error=1)
result.stdout.re_match_lines([r".*Error during collection"])
result.stdout.re_match_lines([r".*Error during collection$"])


def test_exception_during_test(testdir):
Expand All @@ -435,7 +456,7 @@ def test_(request, case):
result = testdir.runpytest("-v", "-rf")
# Then the tests should fail with the relevant error message
result.assert_outcomes(failed=1)
result.stdout.re_match_lines([r".*Cannot have max_size=6 < min_size=10"])
result.stdout.re_match_lines([r".*Cannot have max_size=6 < min_size=10", ".*Failed: Cannot"])


def test_no_base_path(testdir):
Expand Down
3 changes: 0 additions & 3 deletions test/test_parametrizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,3 @@ def test_b():

# Then this callable should be evaluated only once and reused
assert counter == 1


# TODO. respect hypothesis-profile
2 changes: 1 addition & 1 deletion test/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def test_base_path_suffix(swagger_20, base_path):


def test_wrap_schema_unsupported_type():
with pytest.raises(ValueError, match="Unsupported schema type"):
with pytest.raises(ValueError, match="^Unsupported schema type$"):
wrap_schema({})

0 comments on commit 83843cf

Please sign in to comment.