From ea1d6f4a201064276c4606969297bcdda9b42469 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 16 Jun 2025 18:04:49 +0930 Subject: [PATCH] chore(api): Add support to API Permission to include additional tests ref: #833 --- .../test_functional_meta_permissions_api.py | 16 ++++++++++++++- .../centurion_erp/development/testing.md | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/api/tests/functional/test_functional_meta_permissions_api.py b/app/api/tests/functional/test_functional_meta_permissions_api.py index 73370a3e..aca7141a 100644 --- a/app/api/tests/functional/test_functional_meta_permissions_api.py +++ b/app/api/tests/functional/test_functional_meta_permissions_api.py @@ -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, diff --git a/docs/projects/centurion_erp/development/testing.md b/docs/projects/centurion_erp/development/testing.md index 0f52868b..73f4b24d 100644 --- a/docs/projects/centurion_erp/development/testing.md +++ b/docs/projects/centurion_erp/development/testing.md @@ -232,6 +232,26 @@ Due to how pytest and pytest-django works, there is no method available for clas +### 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 `/tests/functional/additional__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_`. Variable `` is the data key that you will specify within the test method.