Skip to content

Commit

Permalink
fix: Handling examples of parameters in Open API 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Jan 30, 2020
1 parent 5e7d7b6 commit 2ea8326
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -11,6 +11,11 @@ Added

- ``-x``/``--exitfirst`` CLI option to exit after first failed test. `#378`_

Fixed
~~~~~

- Handling examples of parameters in Open API 3.0. `#381`_

`0.23.6`_ - 2020-01-28
----------------------

Expand Down Expand Up @@ -647,6 +652,7 @@ Fixed
.. _0.3.0: https://github.com/kiwicom/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/kiwicom/schemathesis/compare/v0.1.0...v0.2.0

.. _#381: https://github.com/kiwicom/schemathesis/issues/381
.. _#378: https://github.com/kiwicom/schemathesis/issues/378
.. _#376: https://github.com/kiwicom/schemathesis/issues/376
.. _#374: https://github.com/kiwicom/schemathesis/issues/374
Expand Down
6 changes: 6 additions & 0 deletions src/schemathesis/schemas.py
Expand Up @@ -315,6 +315,12 @@ def process_by_type(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None
else:
super().process_by_type(endpoint, parameter)

def add_parameter(self, container: Optional[Dict[str, Any]], parameter: Dict[str, Any]) -> Dict[str, Any]:
container = super().add_parameter(container, parameter)
if "example" in parameter:
container["example"] = {parameter["name"]: parameter["example"]}
return container

def process_cookie(self, endpoint: Endpoint, parameter: Dict[str, Any]) -> None:
endpoint.cookies = self.add_parameter(endpoint.cookies, parameter)

Expand Down
3 changes: 2 additions & 1 deletion test/conftest.py
Expand Up @@ -127,9 +127,10 @@ def maker(
tag=None,
pytest_plugins=("aiohttp.pytest_plugin",),
validate_schema=True,
schema=None,
**kwargs,
):
schema = make_schema(**kwargs)
schema = schema or make_schema(**kwargs)
preparation = dedent(
"""
import pytest
Expand Down
44 changes: 42 additions & 2 deletions test/test_parametrization.py
Expand Up @@ -138,8 +138,8 @@ def test_(request, case):
result.stdout.re_match_lines([r"Hypothesis calls: 1$"])


def test_specified_example(testdir):
# When the given parameter contains an example
def test_specified_example_body(testdir):
# When the given body parameter contains an example
testdir.make_test(
"""
from hypothesis import Phase
Expand Down Expand Up @@ -181,6 +181,46 @@ def test(request, case):
result.stdout.re_match_lines([r"Hypothesis calls: 1$"])


def test_specified_example_query(testdir):
# When the given query parameter contains an example
testdir.make_test(
"""
from hypothesis import Phase
@schema.parametrize()
@settings(max_examples=1, phases=[Phase.explicit])
def test(request, case):
request.config.HYPOTHESIS_CASES += 1
assert case.query == {"id": "test"}
""",
schema={
"openapi": "3.0.2",
"info": {"title": "Test", "description": "Test", "version": "0.1.0"},
"paths": {
"/query": {
"get": {
"parameters": [
{
"name": "id",
"in": "query",
"required": True,
"example": "test",
"schema": {"type": "string"},
}
],
"responses": {"200": {"description": "OK"}},
}
}
},
},
)

result = testdir.runpytest("-v", "-s")
# Then this example should be used in tests
result.assert_outcomes(passed=1)
result.stdout.re_match_lines([r"Hypothesis calls: 1$"])


def test_deselecting(testdir):
# When pytest selecting is applied via "-k" option
testdir.make_test(
Expand Down

0 comments on commit 2ea8326

Please sign in to comment.