-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add custom django testing client #262
Conversation
Introduce `OpenAPIClient` that extends `rest_framework.test.APIClient` with responses validation against OpenAPI schema by using `SchemaTester`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great 👍 Have a few comments for the docs, but happy to merge this afterwards.
README.md
Outdated
|
||
To use `OpenAPIClient` simply pass `SchemaTester` instance that should be used | ||
to validate responses and then use it like regular Django testing client | ||
(TIP: add custom fixture if you are using `pytest` to avoid code boilerplate): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of the tip here, what do you think about including some example code for both cases? Something like:
If you're using UnitTest or Django tests, do this ...
If you're using pytest do this ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice idea, it will be more useful to show developers how to use this client in both cases 👍
Co-authored-by: Sondre Lillebø Gundersen <[email protected]>
Codecov Report
@@ Coverage Diff @@
## master #262 +/- ##
=======================================
Coverage 100.0% 100.0%
=======================================
Files 8 9 +1
Lines 476 492 +16
Branches 96 97 +1
=======================================
+ Hits 476 492 +16
|
openapi_tester/clients.py
Outdated
class OpenAPIClient(APIClient): | ||
"""``APIClient`` validating responses against OpenAPI schema.""" | ||
|
||
def __init__(self, *args, schema_tester: SchemaTester, **kwargs) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sondrelg I was thinking about making schema_tester
optional and adding some method e.g. _schema_tester_factory
to call when it's None
, just to make the API more developer-friendly. Please let me know what you think about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since implementations of the client will only need a single extra line of code (most likely copy pasted from the docs), I'm not sure it's worth the extra effort, but if you want to add it, I'm not opposed to it 🙂
README.md
Outdated
import functools | ||
|
||
from django.test.testcases import SimpleTestCase | ||
from openapi_tester.schema_tester import SchemaTester | ||
|
||
schema_tester = SchemaTester() | ||
|
||
|
||
class MyTestCase(SimpleTestCase): | ||
client_class = functools.partial(OpenAPIClient, schema_tester=schema_tester) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when an idea from comment #262 will be implemented then this could be simplified to:
import functools | |
from django.test.testcases import SimpleTestCase | |
from openapi_tester.schema_tester import SchemaTester | |
schema_tester = SchemaTester() | |
class MyTestCase(SimpleTestCase): | |
client_class = functools.partial(OpenAPIClient, schema_tester=schema_tester) | |
from django.test.testcases import SimpleTestCase | |
from openapi_tester.clients import OpenAPIClient | |
class MyTestCase(SimpleTestCase): | |
client_class = OpenAPIClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also have
schema_tester = SchemaTester()
class SimpleTestBase(SimpleTestCase):
client_class = functools.partial(OpenAPIClient, schema_tester=schema_tester)
in some test file and just use that instead of SimpleTestCase
right? That way there's no extra code needed for adding it to your test classes.
So for tests, it would just be
class MyTestCase(SimpleTestBase):
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, I will add some info that such subclass should be used instead of the original SimpleTestCase
class
Make `OpenAPIClient` `schema_tester` init argument optional by providing default factory method for `schema_tester` instances.
Please ping me when this is finalised and I'll take another look 👍 Guessing it might already be, but not sure 🙂 |
@sondrelg it's ready to review ;) |
This looks really good @skarzi! Thanks for the effort. I'll release a new minor version shortly 👍 |
@sondrelg thanks 👍 If you need any help with this project let me know, I am eager to contribute more! |
I don't actively use this library at work currently, so having someone help that actively uses it is super helpful 👍 Not sure if there's a lot to be done right now, but feel free to make suggestions, respond to issues, etc.! 👏 |
Introduce
OpenAPIClient
that extendsrest_framework.test.APIClient
with responses validation against OpenAPI schema by using
SchemaTester
.Closes #261