chore(api): Add support to API Permission to include additional tests

ref: #833
This commit is contained in:
2025-06-16 18:04:49 +09:30
parent 0a9a26a43b
commit ea1d6f4a20
2 changed files with 35 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import pytest
from django.apps import apps
from django.conf import settings
from django.utils.module_loading import import_string
from api.tests.functional.test_functional_permissions_api import (
APIPermissionsInheritedCases
@ -144,9 +145,22 @@ for centurion_model in get_models(
model_name = centurion_model._meta.model_name
cls_name: str = f"{centurion_model._meta.object_name}APIPermissionsPyTest"
inc_classes = (APIPermissionsTestCases,)
try:
additional_testcases = import_string(
centurion_model._meta.app_label + '.tests.functional.additional_' +
centurion_model._meta.model_name + '_permissions_api.AdditionalTestCases'
)
inc_classes += (additional_testcases,)
except Exception as ex:
additional_testcases = None
dynamic_class = type(
cls_name,
(APIPermissionsTestCases,),
inc_classes,
{
'__module__': 'api.tests.functional.test_functional_meta_permissions_api',
'__qualname__': cls_name,

View File

@ -232,6 +232,26 @@ Due to how pytest and pytest-django works, there is no method available for clas
<!-- markdownlint-restore -->
### API Permissions Tests
API Permissions tests are automagically created when `pytest collect` runs. Normally there will be nothing that needs to be done for this test suite. However if you find there is a requirement for adding additional API Permission Test Cases add an additional tests file. This file must be placed in path `<app name>/tests/functional/additional_<model name>_permissions_api.py`. The contents of this file is as follows:
``` py
class AdditionalTestCases: # You must use this class name
def test_my_test_case(self, fixture_name):
# your test case logic.
```
Once this file is detected during `collect` the test cases in class `AdditionalTestCases`, will be included in the API Permission Test Suit for the model in question.
## Parameterizing Tests
To be able to paramertize any test case, the test must be setup to use PyTest. Within the test class the test data is required to be stored in a dictionary prefixed with string `paramaterized_<data_name>`. Variable `<data_name>` is the data key that you will specify within the test method.