From f4a94b09a547397b7ed6f232f69548f5c3fbbb48 Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Fri, 8 May 2020 20:31:29 +0200 Subject: [PATCH] refactor: Move OpenAPI-specifi `endpoints` implementation to the `BaseOpenAPISchema` class --- src/schemathesis/schemas.py | 14 +------------- src/schemathesis/specs/openapi/schemas.py | 17 +++++++++++++++++ test/test_direct_access.py | 4 ++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/schemathesis/schemas.py b/src/schemathesis/schemas.py index d7949ed7ef..2776747fc8 100644 --- a/src/schemathesis/schemas.py +++ b/src/schemathesis/schemas.py @@ -51,11 +51,7 @@ def verbose_name(self) -> str: @property def endpoints(self) -> Dict[str, CaseInsensitiveDict]: - if not hasattr(self, "_endpoints"): - # pylint: disable=attribute-defined-outside-init - endpoints = self.get_all_endpoints() - self._endpoints = endpoints_to_dict(endpoints) - return self._endpoints + raise NotImplementedError @property def endpoints_count(self) -> int: @@ -154,11 +150,3 @@ def dispatch_hook(self, name: str, context: HookContext, *args: Any, **kwargs: A local_dispatcher = self.get_local_hook_dispatcher() if local_dispatcher is not None: local_dispatcher.dispatch(name, context, *args, **kwargs) - - -def endpoints_to_dict(endpoints: Generator[Endpoint, None, None]) -> Dict[str, CaseInsensitiveDict]: - output: Dict[str, CaseInsensitiveDict] = {} - for endpoint in endpoints: - output.setdefault(endpoint.path, CaseInsensitiveDict()) - output[endpoint.path][endpoint.method] = endpoint - return output diff --git a/src/schemathesis/specs/openapi/schemas.py b/src/schemathesis/specs/openapi/schemas.py index ea735079f2..b9a6db86d4 100644 --- a/src/schemathesis/specs/openapi/schemas.py +++ b/src/schemathesis/specs/openapi/schemas.py @@ -5,6 +5,7 @@ from urllib.parse import urljoin, urlsplit import jsonschema +from requests.structures import CaseInsensitiveDict from ...exceptions import InvalidSchema from ...filters import should_skip_by_operation_id, should_skip_by_tag, should_skip_endpoint, should_skip_method @@ -28,6 +29,14 @@ def __repr__(self) -> str: info = self.raw_schema["info"] return f"{self.__class__.__name__} for {info['title']} ({info['version']})" + @property + def endpoints(self) -> Dict[str, CaseInsensitiveDict]: + if not hasattr(self, "_endpoints"): + # pylint: disable=attribute-defined-outside-init + endpoints = self.get_all_endpoints() + self._endpoints = endpoints_to_dict(endpoints) + return self._endpoints + @property def resolver(self) -> ConvertingResolver: if not hasattr(self, "_resolver"): @@ -310,3 +319,11 @@ def get_common_parameters(methods: Dict[str, Any]) -> List[Dict[str, Any]]: if common_parameters is not None: return deepcopy(common_parameters) return [] + + +def endpoints_to_dict(endpoints: Generator[Endpoint, None, None]) -> Dict[str, CaseInsensitiveDict]: + output: Dict[str, CaseInsensitiveDict] = {} + for endpoint in endpoints: + output.setdefault(endpoint.path, CaseInsensitiveDict()) + output[endpoint.path][endpoint.method] = endpoint + return output diff --git a/test/test_direct_access.py b/test/test_direct_access.py index bb9abf2290..bc980e3cba 100644 --- a/test/test_direct_access.py +++ b/test/test_direct_access.py @@ -4,7 +4,7 @@ import schemathesis from schemathesis.models import Case, Endpoint -from schemathesis.schemas import endpoints_to_dict +from schemathesis.specs.openapi.schemas import endpoints_to_dict def test_contains(swagger_20): @@ -13,7 +13,7 @@ def test_contains(swagger_20): def test_getitem(simple_schema, mocker): swagger = schemathesis.from_dict(simple_schema) - mocked = mocker.patch("schemathesis.schemas.endpoints_to_dict", wraps=endpoints_to_dict) + mocked = mocker.patch("schemathesis.specs.openapi.schemas.endpoints_to_dict", wraps=endpoints_to_dict) assert "_endpoints" not in swagger.__dict__ assert isinstance(swagger["/v1/users"], CaseInsensitiveDict) assert mocked.call_count == 1