forked from snok/drf-openapi-tester
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from maticardenas/14-add-support-for-django-ni…
…nja-test-client Django ninja support
- Loading branch information
Showing
15 changed files
with
966 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Configuration module for schema section test. | ||
""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Any, Callable, List, Optional | ||
|
||
|
||
@dataclass | ||
class OpenAPITestConfig: | ||
"""Configuration dataclass for schema section test.""" | ||
|
||
case_tester: Optional[Callable[[str], None]] = None | ||
ignore_case: Optional[List[str]] = None | ||
validators: Any = None | ||
reference: str = "init" | ||
http_message: str = "response" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
This module contains the concrete response handlers for both DRF and Django Ninja responses. | ||
""" | ||
import json | ||
from typing import TYPE_CHECKING, Optional, Union | ||
|
||
if TYPE_CHECKING: | ||
from django.http.response import HttpResponse | ||
from rest_framework.response import Response | ||
|
||
|
||
class ResponseHandler: | ||
""" | ||
This class is used to handle the response and request data | ||
from both DRF and Django HTTP (Django Ninja) responses. | ||
""" | ||
|
||
def __init__(self, response: Union["Response", "HttpResponse"]) -> None: | ||
self._response = response | ||
|
||
@property | ||
def response(self) -> Union["Response", "HttpResponse"]: | ||
return self._response | ||
|
||
@property | ||
def data(self) -> Optional[dict]: | ||
... | ||
|
||
@property | ||
def request_data(self) -> Optional[dict]: | ||
... | ||
|
||
|
||
class DRFResponseHandler(ResponseHandler): | ||
""" | ||
Handles the response and request data from DRF responses. | ||
""" | ||
|
||
def __init__(self, response: "Response") -> None: | ||
super().__init__(response) | ||
|
||
@property | ||
def data(self) -> Optional[dict]: | ||
return self.response.json() if self.response.data is not None else None # type: ignore[attr-defined] | ||
|
||
@property | ||
def request_data(self) -> Optional[dict]: | ||
return self.response.renderer_context["request"].data # type: ignore[attr-defined] | ||
|
||
|
||
class DjangoNinjaResponseHandler(ResponseHandler): | ||
""" | ||
Handles the response and request data from Django Ninja responses. | ||
""" | ||
|
||
def __init__(self, response: "HttpResponse") -> None: | ||
super().__init__(response) | ||
|
||
@property | ||
def data(self) -> Optional[dict]: | ||
return self.response.json() if self.response.content else None # type: ignore[attr-defined] | ||
|
||
@property | ||
def request_data(self) -> Optional[dict]: | ||
response_body = self.response.wsgi_request.body # type: ignore[attr-defined] | ||
return json.loads(response_body) if response_body else None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# pylint: disable=R0903 | ||
""" | ||
Module that contains the factory to create response handlers. | ||
""" | ||
|
||
from typing import TYPE_CHECKING, Union | ||
|
||
from rest_framework.response import Response | ||
|
||
from openapi_tester.response_handler import DjangoNinjaResponseHandler, DRFResponseHandler | ||
|
||
if TYPE_CHECKING: | ||
from django.http.response import HttpResponse | ||
|
||
from openapi_tester.response_handler import ResponseHandler | ||
|
||
|
||
class ResponseHandlerFactory: | ||
""" | ||
Response Handler Factory: this class is used to create a response handler | ||
instance for both DRF and Django HTTP (Django Ninja) responses. | ||
""" | ||
|
||
@staticmethod | ||
def create(response: Union[Response, "HttpResponse"]) -> "ResponseHandler": | ||
if isinstance(response, Response): | ||
return DRFResponseHandler(response) | ||
return DjangoNinjaResponseHandler(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.