Skip to content

Commit

Permalink
fix: Add validate_schema attribute and proper handling to LazySchema
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Feb 10, 2020
1 parent 261c7b4 commit a6a1248
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
21 changes: 11 additions & 10 deletions src/schemathesis/lazy.py
Expand Up @@ -9,7 +9,7 @@
from .exceptions import InvalidSchema
from .models import Endpoint
from .schemas import BaseSchema
from .types import Filter
from .types import Filter, NotSet
from .utils import NOT_SET


Expand All @@ -19,9 +19,14 @@ class LazySchema:
method: Optional[Filter] = attr.ib(default=NOT_SET) # pragma: no mutate
endpoint: Optional[Filter] = attr.ib(default=NOT_SET) # pragma: no mutate
tag: Optional[Filter] = attr.ib(default=NOT_SET) # pragma: no mutate
validate_schema: bool = attr.ib(default=True) # pragma: no mutate

def parametrize(
self, method: Optional[Filter] = NOT_SET, endpoint: Optional[Filter] = NOT_SET, tag: Optional[Filter] = NOT_SET
self,
method: Optional[Filter] = NOT_SET,
endpoint: Optional[Filter] = NOT_SET,
tag: Optional[Filter] = NOT_SET,
validate_schema: Union[bool, NotSet] = NOT_SET,
) -> Callable:
if method is NOT_SET:
method = self.method
Expand All @@ -33,7 +38,7 @@ def parametrize(
def wrapper(func: Callable) -> Callable:
def test(request: FixtureRequest, subtests: SubTests) -> None:
"""The actual test, which is executed by pytest."""
schema = get_schema(request, self.fixture_name, method, endpoint, tag)
schema = get_schema(request, self.fixture_name, method, endpoint, tag, validate_schema)
fixtures = get_fixtures(func, request)
# Changing the node id is required for better reporting - the method and endpoint will appear there
node_id = subtests.item._nodeid
Expand Down Expand Up @@ -81,18 +86,14 @@ def get_schema(
method: Optional[Filter] = None,
endpoint: Optional[Filter] = None,
tag: Optional[Filter] = None,
validate_schema: Union[bool, NotSet] = NOT_SET,
) -> BaseSchema:
"""Loads a schema from the fixture."""
# pylint: disable=too-many-arguments
schema = request.getfixturevalue(name)
if not isinstance(schema, BaseSchema):
raise ValueError(f"The given schema must be an instance of BaseSchema, got: {type(schema)}")
if method is NOT_SET:
method = schema.method
if endpoint is NOT_SET:
endpoint = schema.endpoint
if tag is NOT_SET:
tag = schema.tag
return schema.__class__(schema.raw_schema, method=method, endpoint=endpoint, tag=tag)
return schema.clone(method=method, endpoint=endpoint, tag=tag, validate_schema=validate_schema)


def get_fixtures(func: Callable, request: FixtureRequest) -> Dict[str, Any]:
Expand Down
40 changes: 25 additions & 15 deletions src/schemathesis/schemas.py
Expand Up @@ -105,6 +105,20 @@ def parametrize(
validate_schema: Union[bool, NotSet] = NOT_SET,
) -> Callable:
"""Mark a test function as a parametrized one."""

def wrapper(func: Callable) -> Callable:
func._schemathesis_test = self.clone(method, endpoint, tag, validate_schema) # type: ignore
return func

return wrapper

def clone(
self,
method: Optional[Filter] = NOT_SET,
endpoint: Optional[Filter] = NOT_SET,
tag: Optional[Filter] = NOT_SET,
validate_schema: Union[bool, NotSet] = NOT_SET,
) -> "BaseSchema":
if method is NOT_SET:
method = self.method
if endpoint is NOT_SET:
Expand All @@ -114,21 +128,17 @@ def parametrize(
if validate_schema is NOT_SET:
validate_schema = self.validate_schema

def wrapper(func: Callable) -> Callable:
func._schemathesis_test = self.__class__( # type: ignore
self.raw_schema,
location=self.location,
base_url=self.base_url,
method=method,
endpoint=endpoint,
tag=tag,
app=self.app,
hooks=self.hooks,
validate_schema=validate_schema, # type: ignore
)
return func

return wrapper
return self.__class__(
self.raw_schema,
location=self.location,
base_url=self.base_url,
method=method,
endpoint=endpoint,
tag=tag,
app=self.app,
hooks=self.hooks,
validate_schema=validate_schema, # type: ignore
)

def _get_response_schema(self, definition: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Extract response schema from `responses`."""
Expand Down

0 comments on commit a6a1248

Please sign in to comment.