test(core): Ensure model that has audit enableed has audit model

ref: #773 #515
This commit is contained in:
2025-05-26 16:19:40 +09:30
parent 2b6a15cf8f
commit 114b2e1ae3
2 changed files with 131 additions and 1 deletions

View File

@ -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 `<app_label>_<model_name>_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() )

View File

@ -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 `<app_label>_<model_name>_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()