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

feat: add custom django testing client #262

Merged
merged 6 commits into from
Apr 19, 2022
Merged

Conversation

skarzi
Copy link
Contributor

@skarzi skarzi commented Apr 14, 2022

Introduce OpenAPIClient that extends rest_framework.test.APIClient
with responses validation against OpenAPI schema by using SchemaTester.

Closes #261

Introduce `OpenAPIClient` that extends `rest_framework.test.APIClient`
with responses validation against OpenAPI schema by using `SchemaTester`.
Copy link
Member

@sondrelg sondrelg left a 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 Show resolved Hide resolved
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):
Copy link
Member

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 ...

Copy link
Contributor Author

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 👍

README.md Outdated Show resolved Hide resolved
Co-authored-by: Sondre Lillebø Gundersen <sondrelg@live.no>
@codecov
Copy link

codecov bot commented Apr 15, 2022

Codecov Report

Merging #262 (6491cde) into master (7949267) will not change coverage.
The diff coverage is 100.0%.

@@           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     
Impacted Files Coverage Δ
openapi_tester/__init__.py 100.0% <100.0%> (ø)
openapi_tester/clients.py 100.0% <100.0%> (ø)

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

def __init__(self, *args, schema_tester: SchemaTester, **kwargs) -> None:
Copy link
Contributor Author

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.

Copy link
Member

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
Comment on lines 239 to 249
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)
Copy link
Contributor Author

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:

Suggested change
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

Copy link
Member

@sondrelg sondrelg Apr 17, 2022

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):
    ...

Copy link
Contributor Author

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.
@sondrelg
Copy link
Member

Please ping me when this is finalised and I'll take another look 👍 Guessing it might already be, but not sure 🙂

@skarzi
Copy link
Contributor Author

skarzi commented Apr 19, 2022

@sondrelg it's ready to review ;)

@sondrelg
Copy link
Member

This looks really good @skarzi! Thanks for the effort. I'll release a new minor version shortly 👍

@sondrelg sondrelg merged commit 2e37b9c into snok:master Apr 19, 2022
@skarzi
Copy link
Contributor Author

skarzi commented Apr 19, 2022

@sondrelg thanks 👍 If you need any help with this project let me know, I am eager to contribute more!

@sondrelg
Copy link
Member

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.! 👏

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

custom testing API client implementation
2 participants