Skip to content

Commit

Permalink
refactor: Move endpoints implementation from BaseOpenAPISchema to…
Browse files Browse the repository at this point in the history
… `BaseSchema`
  • Loading branch information
Stranger6667 committed Sep 25, 2020
1 parent e9be0f8 commit ab90551
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
14 changes: 13 additions & 1 deletion src/schemathesis/schemas.py
Expand Up @@ -88,7 +88,11 @@ def get_base_url(self) -> str:

@property
def endpoints(self) -> Dict[str, CaseInsensitiveDict]:
raise NotImplementedError
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 endpoints_count(self) -> int:
Expand Down Expand Up @@ -225,3 +229,11 @@ def get_case_strategy(
self, endpoint: Endpoint, hooks: Optional[HookDispatcher] = None, feedback: Optional[Feedback] = None
) -> SearchStrategy:
raise NotImplementedError


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: 0 additions & 17 deletions src/schemathesis/specs/openapi/schemas.py
Expand Up @@ -6,7 +6,6 @@

import jsonschema
from hypothesis.strategies import SearchStrategy
from requests.structures import CaseInsensitiveDict

from ...exceptions import InvalidSchema
from ...hooks import HookContext, HookDispatcher
Expand Down Expand Up @@ -46,14 +45,6 @@ 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

def get_all_endpoints(self) -> Generator[Endpoint, None, None]:
try:
paths = self.raw_schema["paths"] # pylint: disable=unsubscriptable-object
Expand Down Expand Up @@ -465,14 +456,6 @@ def get_common_parameters(methods: Dict[str, Any]) -> List[Dict[str, Any]]:
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


def get_schema_from_parameter(data: Dict[str, Any]) -> Dict[str, Any]:
# In Open API 3.0 there could be "schema" or "content" field. They are mutually exclusive.
if "schema" in data:
Expand Down
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.specs.openapi.schemas import endpoints_to_dict
from schemathesis.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.specs.openapi.schemas.endpoints_to_dict", wraps=endpoints_to_dict)
mocked = mocker.patch("schemathesis.schemas.endpoints_to_dict", wraps=endpoints_to_dict)
assert "_endpoints" not in swagger.__dict__
assert isinstance(swagger["/users"], CaseInsensitiveDict)
assert mocked.call_count == 1
Expand Down

0 comments on commit ab90551

Please sign in to comment.