diff --git a/docs/changelog.rst b/docs/changelog.rst index fd48f0e653..446320fd44 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 ---------------------- @@ -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 diff --git a/src/schemathesis/schemas.py b/src/schemathesis/schemas.py index 134108ccce..bc14264f83 100644 --- a/src/schemathesis/schemas.py +++ b/src/schemathesis/schemas.py @@ -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) diff --git a/test/conftest.py b/test/conftest.py index af42734b00..f2af25177c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -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 diff --git a/test/test_parametrization.py b/test/test_parametrization.py index a815119374..18050a7671 100644 --- a/test/test_parametrization.py +++ b/test/test_parametrization.py @@ -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 @@ -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(