From 114b2e1ae3413575f7682a13187927f8f555d1e1 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 26 May 2025 16:19:40 +0930 Subject: [PATCH] test(core): Ensure model that has audit enableed has audit model ref: #773 #515 --- ...est_functional_centurion_abstract_model.py | 31 +++++- .../test_functional_centurion_audit_model.py | 101 ++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/app/core/tests/functional/centurion_abstract/test_functional_centurion_abstract_model.py b/app/core/tests/functional/centurion_abstract/test_functional_centurion_abstract_model.py index 127a144a..50cd377e 100644 --- a/app/core/tests/functional/centurion_abstract/test_functional_centurion_abstract_model.py +++ b/app/core/tests/functional/centurion_abstract/test_functional_centurion_abstract_model.py @@ -21,6 +21,31 @@ class CenturionAbstractModelTestCases( + def test_model_has_history_model(self, model): + """Audit History Table check + + Check if the model has a corresponding audit history table that should be + called `__audithistory` + """ + + if model._meta.abstract: + + pytest.xfail( reason = 'Model is an Abstract Model and can not be created.' ) + + elif not getattr(model, '_audit_enabled', False): + + pytest.xfail( reason = 'Model has audit history disabled.' ) + + + history_model = apps.get_model( + app_label = model._meta.app_label, + model_name = model().get_history_model_name() + ) + + assert history_model.__name__ == model().get_history_model_name() + + + def test_model_create_has_history_entry(self, content_type, created_model, model): """Model Created @@ -28,10 +53,14 @@ class CenturionAbstractModelTestCases( entry. """ - if model._meta.abstract: + if created_model._meta.abstract: pytest.xfail( reason = 'Model is an Abstract Model and can not be created.' ) + elif not getattr(created_model, '_audit_enabled', False): + + pytest.xfail( reason = 'Model has audit history disabled.' ) + history_model = apps.get_model( created_model._meta.app_label, created_model.get_history_model_name() ) diff --git a/app/core/tests/functional/centurion_audit/test_functional_centurion_audit_model.py b/app/core/tests/functional/centurion_audit/test_functional_centurion_audit_model.py index 3f030c8c..204ce728 100644 --- a/app/core/tests/functional/centurion_audit/test_functional_centurion_audit_model.py +++ b/app/core/tests/functional/centurion_audit/test_functional_centurion_audit_model.py @@ -59,3 +59,104 @@ class CenturionAuditModelInheritedCases( ): pass + + + +class CenturionAuditModelPyTest( + CenturionAuditModelTestCases, +): + + + + @staticmethod + def get_models( excludes: list[ str ] ) -> list[ tuple ]: + """Fetch models from Centurion Apps + + Args: + excludes (list[ str ]): Words that may be in a models name to exclude + + Returns: + list[ tuple ]: Centurion ERP Only models + """ + + models: list = [] + + model_apps: list = [] + + exclude_model_apps = [ + 'django', + 'django_celery_results', + 'django_filters', + 'drf_spectacular', + 'drf_spectacular_sidecar', + 'coresheaders', + 'corsheaders', + 'rest_framework', + 'rest_framework_json_api', + 'social_django', + ] + + for app in settings.INSTALLED_APPS: + + app = app.split('.')[0] + + if app in exclude_model_apps: + continue + + model_apps += [ app ] + + + for model in apps.get_models(): + + if model._meta.app_label not in model_apps: + continue + + skip = False + + for exclude in excludes: + + if exclude in str(model._meta.model_name): + skip = True + break + + if skip: + continue + + models += [ (model,) ] + + return models + + + + history_models = get_models( [ 'audithistory', 'base', 'history', 'note', 'ticket' ] ) + + + @pytest.mark.parametrize( + argnames = [ + 'test_model' + ], + argvalues = history_models, + ids = [ model[0]._meta.app_label + '_' + model[0]._meta.model_name for model in history_models ] + ) + def test_model_has_history_model(self, test_model, model): + """Audit History Table check + + Check if the model has a corresponding audit history table that should be + called `__audithistory` + """ + + if test_model._meta.abstract: + + pytest.xfail( reason = 'Model is an Abstract Model and can not be created.' ) + + elif not getattr(test_model, '_audit_enabled', False): + + pytest.xfail( reason = 'Model has audit history disabled.' ) + + + history_model = apps.get_model( + app_label = test_model._meta.app_label, + model_name = test_model().get_history_model_name() + ) + + assert history_model.__name__ == test_model().get_history_model_name()