Skip to content

Commit

Permalink
refactor: Move OpenAPI-specifi endpoints implementation to the `Bas…
Browse files Browse the repository at this point in the history
…eOpenAPISchema` class
  • Loading branch information
Stranger6667 committed May 8, 2020
1 parent 80eeed9 commit f4a94b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
14 changes: 1 addition & 13 deletions src/schemathesis/schemas.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions src/schemathesis/specs/openapi/schemas.py
Expand Up @@ -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
Expand All @@ -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"):
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/test_direct_access.py
Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit f4a94b0

Please sign in to comment.