Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit 4b161d9

Browse files
committed
feat: make schema_tester optional init argument
Make `OpenAPIClient` `schema_tester` init argument optional by providing default factory method for `schema_tester` instances.
1 parent 4b17e2a commit 4b161d9

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ schema_tester = SchemaTester()
246246

247247

248248
class MySimpleTestCase(SimpleTestCase):
249-
client_class = functools.partial(OpenAPIClient, schema_tester=schema_tester)
249+
client_class = OpenAPIClient
250+
# or use `functools.partial` when you want to provide custom
251+
# ``SchemaTester`` instance:
252+
# client_class = functools.partial(OpenAPIClient, schema_tester=schema_tester)
250253
```
251254

252255
This will ensure you all newly implemented views will be validated against

openapi_tester/clients.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,32 @@
55

66
from rest_framework.test import APIClient
77

8+
from .schema_tester import SchemaTester
9+
810
if TYPE_CHECKING:
911
from rest_framework.response import Response
1012

11-
from .schema_tester import SchemaTester
12-
1313

1414
class OpenAPIClient(APIClient):
1515
"""``APIClient`` validating responses against OpenAPI schema."""
1616

17-
def __init__(self, *args, schema_tester: SchemaTester, **kwargs) -> None:
17+
def __init__(
18+
self,
19+
*args,
20+
schema_tester: SchemaTester | None = None,
21+
**kwargs,
22+
) -> None:
1823
"""Initialize ``OpenAPIClient`` instance."""
1924
super().__init__(*args, **kwargs)
20-
self.schema_tester = schema_tester
25+
self.schema_tester = schema_tester or self._schema_tester_factory()
2126

2227
def request(self, **kwargs) -> Response: # type: ignore[override]
2328
"""Validate fetched response against given OpenAPI schema."""
2429
response = super().request(**kwargs)
2530
self.schema_tester.validate_response(response)
2631
return response
32+
33+
@staticmethod
34+
def _schema_tester_factory() -> SchemaTester:
35+
"""Factory of default ``SchemaTester`` instances."""
36+
return SchemaTester()

tests/test_clients.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ def openapi_client(settings) -> OpenAPIClient:
1515
"""Sample ``OpenAPIClient`` instance to use in tests."""
1616
# use `drf-yasg` schema loader in tests
1717
settings.INSTALLED_APPS = [app for app in settings.INSTALLED_APPS if app != "drf_spectacular"]
18-
return OpenAPIClient(schema_tester=SchemaTester())
18+
return OpenAPIClient()
19+
20+
21+
def test_init_schema_tester_passed():
22+
"""Ensure passed ``SchemaTester`` instance is used."""
23+
schema_tester = SchemaTester()
24+
25+
client = OpenAPIClient(schema_tester=schema_tester)
26+
27+
assert client.schema_tester is schema_tester
1928

2029

2130
@pytest.mark.parametrize(
@@ -77,16 +86,20 @@ def test_request_invalid_response(
7786
openapi_client.generic(**generic_kwargs)
7887

7988

80-
def test_django_testcase_client_class():
89+
@pytest.mark.parametrize(
90+
"openapi_client_class",
91+
[
92+
OpenAPIClient,
93+
functools.partial(OpenAPIClient, schema_tester=SchemaTester()),
94+
],
95+
)
96+
def test_django_testcase_client_class(openapi_client_class):
8197
"""Ensure example from README.md about Django test case works fine."""
8298

8399
class DummyTestCase(SimpleTestCase):
84100
"""Django ``TestCase`` with ``OpenAPIClient`` client."""
85101

86-
client_class = functools.partial(
87-
OpenAPIClient,
88-
schema_tester=SchemaTester(),
89-
)
102+
client_class = openapi_client_class
90103

91104
test_case = DummyTestCase()
92105
test_case._pre_setup()

0 commit comments

Comments
 (0)