Skip to content

Commit

Permalink
* made plugin import implicitly
Browse files Browse the repository at this point in the history
* removed unnecessary code
  • Loading branch information
Ori-Roza committed Apr 27, 2024
1 parent e332719 commit 7ab0632
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 142 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ coverage.xml
.pytest_cache/
.DS_Store
*.sqlite3
/tests/test_server/test_app/migrations/
notes
20 changes: 0 additions & 20 deletions LICENSE.txt

This file was deleted.

44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pip install drf-api-action

```python
import pytest
from tests.test_app.models import DummyModel
from tests.test_app.views import DummyViewSetFixture
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:
Expand All @@ -49,43 +49,48 @@ from tests.test_app.views import DummyViewSetFixture

e.g:
our ViewSet is called `DummyViewSetFixture`

```python
import pytest
from tests.test_app.views import DummyViewSetFixture
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
pass
```
Now you can use all `DummyViewSetFixture` functionality!

#### Step 3: write your tests

e.g:
our ViewSet is called `DummyViewSetFixture`

```python
import pytest
from tests.test_app.models import DummyModel
from tests.test_app.views import DummyViewSetFixture
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
dummy_model = DummyModel()
dummy_model.dummy_int = 1
dummy_model.save()
res = action_api.api_dummy(pk=1)
assert res["dummy_int"] == 1

```

```python
import pytest
from tests.test_app.views import DummyViewSetFixture
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
result = action_api.dummy(pk='bbb')
assert result['dummy_int'] == 1
```

```shell
Expand Down Expand Up @@ -123,17 +128,10 @@ and so on....
## Package Testing
The `drf-api-action` library includes tests to ensure the functionality works as expected. To run the tests, follow these steps:
1. Navigate to the root directory of the `drf-api-action/` project.
```shell
cd tests/
```
2. Run the tests using `pytest`
The `drf-api-action` library includes tests to ensure the functionality works as expected. To run the tests run `pytest`:
```shell
python -m pytest -vv
PYTHONPATH=`pwd` pytest
```
The tests will be executed, and the results will be displayed in the console.
Expand Down
35 changes: 0 additions & 35 deletions drf_api_action/__init__.py
Original file line number Diff line number Diff line change
@@ -1,35 +0,0 @@
import pytest

from drf_api_action.utils import run_as_api
from drf_api_action.exceptions import ActionsAPIException
from drf_api_action.mixins import APIRestMixin


def run_function(self, func):
def api_item(*args, **kwargs):
serializer_class = func.kwargs['serializer_class']
return run_as_api(self, func, serializer_class, *args, **kwargs)
return api_item


@pytest.fixture(scope="session")
def action_api(request):
"""
Make Dango WebView endpoints accessible
"""
if request.keywords['action_api'].kwargs.get("view_set_class") is None:
raise ActionsAPIException('using action_api fixture must require a view_set_class kwarg')

view_set_class = request.keywords['action_api'].kwargs["view_set_class"]

class WrapperApiClass(APIRestMixin, view_set_class):
def __getattribute__(self, item):
class_attribute = super().__getattribute__(item)

if callable(class_attribute) and hasattr(class_attribute, 'detail'):
return run_function(self, class_attribute)

return class_attribute

api = WrapperApiClass()
return api
37 changes: 37 additions & 0 deletions drf_api_action/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest

from drf_api_action.utils import run_as_api
from drf_api_action.exceptions import ActionsAPIException


def run_function(self, func):
def api_item(*args, **kwargs):
serializer_class = func.kwargs['serializer_class']
return run_as_api(self, func, serializer_class, *args, **kwargs)

return api_item


@pytest.fixture
def action_api(request):
"""
Make Dango WebView endpoints accessible
"""
from drf_api_action.mixins import APIRestMixin

if request.keywords['action_api'].kwargs.get("view_set_class") is None:
raise ActionsAPIException('using action_api fixture must require a view_set_class kwarg')

view_set_class = request.keywords['action_api'].kwargs["view_set_class"]

class WrapperApiClass(APIRestMixin, view_set_class):
def __getattribute__(self, item):
class_attribute = super().__getattribute__(item)

if callable(class_attribute) and hasattr(class_attribute, 'detail'):
return run_function(self, class_attribute)

return class_attribute

api = WrapperApiClass()
return api
76 changes: 45 additions & 31 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
[project]
[build-system]
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "drf_api_action"
version = "1.2.0"
description = "drf-api-action elevates DRF testing by simplifying REST endpoint testing to a seamless, function-like experience."
version = "1.1.0"
readme = "README.md"
license = {file = "LICENSE.txt"}
dependencies = ["Django>=4.2.8", "djangorestframework>=3.14.0", "pytest-django>=4.5.2", "pytest-cov>=4.1.0"]
maintainers = [
{ name="Ori Roza", email="ori75660@gmail.com" },
]
authors = [
{ name="Ori Roza", email="ori75660@gmail.com" },
]
requires-python = ">=3.8"

[tool.poetry.plugins.pytest11]
drf_api_action = "drf_api_action.plugin"

classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
authors = ["Ori Roza <ori75660@gmail.com>"]
license = "MIT"
repository = "https://github.com/Ori-Roza/drf-api-action"
homepage = "https://github.com/Ori-Roza/drf-api-action"

keywords = [
"pytest",
"mocks",
"testing",
"fixtures",
"tests"
"tests",
"django"
]

# Pypi classifiers: https://pypi.org/classifiers/
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Software Development :: Testing",
'Framework :: Pytest',
"License :: OSI Approved :: MIT License",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
]

[tool.setuptools]
py-modules = []
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "tests.test_server.settings"
python_files = "tests.py test_*.py *_tests.py"
markers = [
"action_api: Viewset"
]
testpaths = ["tests"]

[tool.poetry.plugins.pytest11]
api_action = "drf_api_action.plugin"

[project.urls]
Home-page = "https://github.com/Ori-Roza/drf-api-action"
Documentation = "https://github.com/Ori-Roza/drf-api-action"
"Source Code" = "https://github.com/Ori-Roza/drf-api-action"
"Issue Tracker" = "https://github.com/Ori-Roza/drf-api-action/issues/"

[tool.poetry.dependencies]
python = "^3.7"
importlib_metadata = {version = "^4.5.0", python = "<3.8"}
Django = ">=4.2.8"
djangorestframework = ">=3.14.0"
pytest-django = ">=4.5.2"
pytest-cov = "4.1.0"

[build-system]
build-backend = "flit_core.buildapi"
requires = ["flit_core >=3.8.0,<4"]

[tool.pylint.master]
fail-under = 10.0
Expand Down
3 changes: 0 additions & 3 deletions pytest.ini

This file was deleted.

4 changes: 0 additions & 4 deletions setup.cfg

This file was deleted.

Binary file removed tests/db.sqlite3
Binary file not shown.
File renamed without changes.
21 changes: 0 additions & 21 deletions tests/test_app/migrations/0001_initial.py

This file was deleted.

Empty file.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/test_app/settings.py → tests/test_server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
ROOT_URLCONF = "tests.test_app.urls"
ROOT_URLCONF = "tests.test_server.urls"

INSTALLED_APPS = [ # Default Django apps:
"django.contrib.auth",
Expand All @@ -23,7 +23,7 @@
# third party apps
"rest_framework",
# model apps
"tests.test_app",
"tests.test_server.test_app",
]

MIDDLEWARE = ['django.middleware.security.SecurityMiddleware',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/test_app/urls.py → tests/test_server/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Testing URL routing definition
"""
from .views import DummyAPIViewSet, DummyViewSet
from tests.test_server.test_app.views import DummyAPIViewSet, DummyViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
Expand Down
4 changes: 2 additions & 2 deletions tests/functionality_tests/tests.py → tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from rest_framework.exceptions import ValidationError

from drf_api_action.utils import extract_page_number
from tests.test_app.models import DummyModel
from tests.test_app.views import DummyViewSetFixture, DummyAPIViewSet
from tests.test_server.test_app.models import DummyModel
from tests.test_server.test_app.views import DummyViewSetFixture, DummyAPIViewSet


@pytest.mark.action_api(view_set_class=DummyViewSetFixture)
Expand Down

0 comments on commit 7ab0632

Please sign in to comment.