Skip to content

Commit

Permalink
feat: update error messages (#30)
Browse files Browse the repository at this point in the history
* feat: update error messages 

* feat: use json.dumps whenever we have json content types

* feat: support OAS 3.1 list of types

---------

Co-authored-by: danut.turta1 <danut.turta@sendcloud.com>
  • Loading branch information
danut-t and danut.turta1 committed Apr 4, 2024
1 parent 02743d1 commit f00ae02
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 131 deletions.
114 changes: 114 additions & 0 deletions openapi_tester/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import json
from typing import TYPE_CHECKING

from rest_framework.test import APIClient
Expand Down Expand Up @@ -33,6 +34,111 @@ def request(self, **kwargs) -> Response: # type: ignore[override]
self.schema_tester.validate_response(response)
return response

# pylint: disable=W0622
def post(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().post(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def put(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().put(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def patch(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().patch(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def delete(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().delete(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def options(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().options(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

@staticmethod
def _is_successful_response(response: Response) -> bool:
return response.status_code < 400
Expand All @@ -41,3 +147,11 @@ def _is_successful_response(response: Response) -> bool:
def _schema_tester_factory() -> SchemaTester:
"""Factory of default ``SchemaTester`` instances."""
return SchemaTester()

@staticmethod
def _serialize(data):
try:
return json.dumps(data)
except (TypeError, OverflowError):
# Data is already serialized
return data
2 changes: 1 addition & 1 deletion openapi_tester/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class OpenAPITestConfig:
case_tester: Optional[Callable[[str], None]] = None
ignore_case: Optional[List[str]] = None
validators: Any = None
reference: str = "init"
reference: str = "root"
http_message: str = "response"
10 changes: 7 additions & 3 deletions openapi_tester/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@
VALIDATE_MINIMUM_NUMBER_OF_PROPERTIES_ERROR = "The number of properties in {data} is fewer than the specified minimum number of properties of {min_length}"
VALIDATE_MAXIMUM_NUMBER_OF_PROPERTIES_ERROR = "The number of properties in {data} exceeds the specified maximum number of properties of {max_length}"
VALIDATE_UNIQUE_ITEMS_ERROR = "The array {data} must contain unique items only"
VALIDATE_NONE_ERROR = "Received a null value for a non-nullable schema object"
VALIDATE_NONE_ERROR = "A property received a null value in the {http_message} data, but is a non-nullable object in the schema definition"
VALIDATE_MISSING_KEY_ERROR = (
'The following property is missing in the {http_message} data: "{missing_key}"'
"The following property was found in the schema definition, "
'but is missing from the {http_message} data: "{missing_key}"'
)
VALIDATE_EXCESS_KEY_ERROR = (
"The following property was found in the {http_message} data, "
'but is missing from the schema definition: "{excess_key}"'
)
VALIDATE_EXCESS_KEY_ERROR = 'The following property was found in the {http_message}, but is missing from the schema definition: "{excess_key}"'
VALIDATE_WRITE_ONLY_RESPONSE_KEY_ERROR = 'The following property was found in the response, but is documented as being "writeOnly": "{write_only_key}"'
VALIDATE_ONE_OF_ERROR = "Expected data to match one and only one of the oneOf schema types; found {matches} matches"
VALIDATE_ANY_OF_ERROR = "Expected data to match one or more of the documented anyOf schema types, but found no matches"
Expand Down
Loading

0 comments on commit f00ae02

Please sign in to comment.