drf-api-action elevates DRF testing by simplifying REST endpoint testing to a seamless, function-like experience.
Project description
The drf-api-action Python package is designed to elevate your testing experience for Django Rest Framework (DRF) REST endpoints. With the api-action fixture, this package empowers you to effortlessly test your REST endpoints as if they were conventional functions.
Features:
-
Simplified Testing: Testing DRF REST endpoints using the api-action decorator, treating them like regular functions.
-
Seamless Integration: Replacing DRF's action decorator with api-action in your WebViewSet seamlessly.
-
Clear Traceback: Instead of getting a response with error code, get the real traceback that led to the error.
-
Pagination Support: Paginating easily through pages by a single kwarg.
Installation
You can install drf-api-action
using pip:
pip install drf-api-action
Usage
To use drf-api-action
as a Pytest fixture, you need to follow these steps:
Step 1: Import the Required Classes and our fixture
import pytest
from tests.test_server.test_app.models import DummyModel
from tests.test_server.test_app.views import DummyViewSetFixture
Step 2: use the following action_api mark decorator:
@pytest.mark.action_api(view_set_class={YOUR VIEW_SET})
e.g:
our ViewSet is called DummyViewSetFixture
import pytest
from tests.test_server.test_app.views import DummyViewSetFixture
@pytest.mark.action_api(view_set_class=DummyViewSetFixture)
def test_call_as_api_fixture(db, action_api):
pass
Now you can use all DummyViewSetFixture
functionality!
Step 3: write your tests
e.g:
our ViewSet is called DummyViewSetFixture
import pytest
from tests.test_server.test_app.models import DummyModel
from tests.test_server.test_app.views import DummyViewSetFixture
@pytest.mark.action_api(view_set_class=DummyViewSetFixture)
def test_call_as_api_fixture(db, action_api):
dummy_model = DummyModel()
dummy_model.dummy_int = 1
dummy_model.save()
res = action_api.api_dummy(pk=1)
assert res["dummy_int"] == 1
import pytest
from tests.test_server.test_app.views import DummyViewSetFixture
@pytest.mark.action_api(view_set_class=DummyViewSetFixture)
def test_dummy(db, action_api):
result = action_api.dummy(pk='bbb')
assert result['dummy_int'] == 1
tests/functionality_tests/test_as_api.py:11 (test_call_as_api)
self = <django.db.models.fields.BigAutoField: id>, value = 'bb'
def get_prep_value(self, value):
value = super().get_prep_value(value)
if value is None:
return None
try:
> return int(value)
E ValueError: invalid literal for int() with base 10: 'bb'
../venv/lib/python3.9/site-packages/django/db/models/fields/__init__.py:2053: ValueError
The above exception was the direct cause of the following exception:
queryset = <QuerySet [<DummyModel: DummyModel object (1)>]>, filter_args = ()
filter_kwargs = {'pk': 'bb'}
def get_object_or_404(queryset, *filter_args, **filter_kwargs):
"""
Same as Django's standard shortcut, but make sure to also raise 404
if the filter_kwargs don't match the required types.
"""
try:
> return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
../venv/lib/python3.9/site-packages/rest_framework/generics.py:19:
and so on....
Package Testing
The drf-api-action
library includes tests to ensure the functionality works as expected. To run the tests run pytest
:
pytest
The tests will be executed, and the results will be displayed in the console.
Example
Example of using drf-api-action in a DRF project
Support & Contribution
For guidance on support & contribution, see the contributing guidelines.
Bug Report
For guidance on how to open a bug, see the bug report template.
Open an Issue
For guidance on how to open an issue, see the issue template.
Code Of Conduct
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for drf_api_action-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16698360f63549d1c3acfdbbd8f1abafafad6312e51115befbd13fa079c89750 |
|
MD5 | 22a8f5218e7542234417fa80e194cdc6 |
|
BLAKE2b-256 | 9e4ee304999337c03ec4b6ae20aa36ba280a0b952277c4d69c6f329784963a20 |