use the power of restframework also as a library functions
Project description
The drf-api-action Python package is designed to elevate your testing experience for Django Rest Framework (DRF) REST endpoints. With the custom decorator api-action, this package empowers you to effortlessly test your REST endpoints as if they were conventional functions.
Features:
-
Simplified Testing: Easily test DRF REST endpoints using the api-action decorator, treating them like regular functions.
-
Seamless Integration: Replace DRF's action decorator with api-action in your WebViewSet for a smooth transition.
Installation
You can install drf-api-action
using pip:
pip install drf-api-action
Usage
To use drf-api-action
, you need to follow these steps:
Step 1: Import the Required Classes and Decorators
Import the necessary classes and decorators from drf-api-action
and rest_framework
:
from drf_api_action.decorators import action_api
from drf_api_action.mixins import APIRestMixin
from rest_framework.viewsets import ModelViewSet
Step 2: Define Your View Class
Create your view class by inheriting from APIRestMixin
and ModelViewSet
:
class DummyView(APIRestMixin, ModelViewSet):
queryset = DummyModel.objects.all()
serializer_class = DummySerializer
Step 3: Define Your API Actions
Use the action_api
decorator instead of action
decorator to define your API actions as functions inside your view class:
From:
@action(detail=True, methods=["get"], serializer_class=DummySerializer)
def dummy(self, request, **kwargs):
serializer = self.get_serializer(instance=self.get_object())
return Response(data=serializer.data, status=status.HTTP_200_OK)
To:
@action_api(detail=True, methods=["get"], serializer_class=DummySerializer)
def dummy(self, request, **kwargs):
serializer = self.get_serializer(instance=self.get_object())
return Response(data=serializer.data, status=status.HTTP_200_OK)
In the example above, the dummy_func
function is decorated with action_api
. It specifies that the action does not require a detail argument, supports the POST
method, and uses the DummySerializer
for serialization.
Step 4: test REST methods
Create an instance of your view class and call the API actions as regular functions:
def test_dummy():
api = DummyView()
result = api.dummy(**args)
assert result['dummy_int'] == 1
In the example above, we create an instance of DummyAPI
and call the dummy
REST call as if it were a function.
query parameters/post payload are treated as function arguments
Package Testing
The drf-api-action
library includes tests to ensure the functionality works as expected. To run the tests, follow these steps:
- Navigate to the root directory of the
drf-api-action/
project.
cd tests/
- Run the tests using
pytest
python -m pytest -vv
The tests will be executed, and the results will be displayed in the console.
Contribution
Contributions to the drf-api-action
library are welcome. If you would like to contribute, please follow these steps:
-
Fork the
drf-api-action
repository on GitHub. -
Create a new branch for your feature or bug fix.
-
Make the necessary changes and additions.
-
Write tests to cover the new functionality or bug fix.
-
Run the existing tests to ensure all tests pass successfully.
-
Commit your changes and push them to your forked repository.
-
Open a pull request on the original
drf-api-action
repository and provide a detailed description of your changes.
The maintainers will review your pull request, provide feedback, and work with you to merge the changes into the main repository.
Thank you for considering contributing to drf-api-action
!
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.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0a08546048d0355c1ae0bfecafd861b8d198e7f7959f71e80092994286169b8 |
|
MD5 | d1f2844f890c2652348e54a81154bdb6 |
|
BLAKE2b-256 | 4b6a0e495a207f5c6f0f684c92e81b5e6838ae61b59f6184d205de5e55d95623 |