test(core): Remaining Unit Model Test Cases for CenturionAuditMeta Model
ref: #772 #759
This commit is contained in:
@ -200,8 +200,8 @@ class CenturionAudit(
|
||||
|
||||
|
||||
class AuditMetaModel(
|
||||
CenturionSubModel,
|
||||
CenturionAudit,
|
||||
CenturionSubModel,
|
||||
):
|
||||
|
||||
|
||||
@ -213,7 +213,7 @@ class AuditMetaModel(
|
||||
|
||||
def clean_fields(self, exclude = None):
|
||||
|
||||
if hasattr(self, 'model'):
|
||||
if getattr(self, 'model', None):
|
||||
|
||||
if not self.get_model_history(self.model):
|
||||
|
||||
@ -222,4 +222,12 @@ class AuditMetaModel(
|
||||
message = 'Unable to process the history.'
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
raise ValidationError(
|
||||
code = 'no_model_supplied',
|
||||
message = 'Unable to process the history, no model was supplied.'
|
||||
)
|
||||
|
||||
|
||||
super().clean_fields(exclude = exclude)
|
||||
|
@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from core.tests.unit.centurion_sub_abstract.test_unit_centurion_sub_abstract_model import (
|
||||
CenturionSubAbstractModelInheritedCases,
|
||||
)
|
||||
@ -14,29 +16,20 @@ class MetaAbstractModelTestCases(
|
||||
CenturionSubAbstractModelInheritedCases,
|
||||
CenturionAuditModelInheritedCases
|
||||
):
|
||||
pass
|
||||
# parameterized_class_attributes = {
|
||||
# '_audit_enabled': {
|
||||
# 'value': False,
|
||||
# },
|
||||
# '_notes_enabled': {
|
||||
# 'value': False,
|
||||
# }
|
||||
# }
|
||||
|
||||
def test_method_centurionauditsub_clean_fields_called(self, mocker, model_instance):
|
||||
"""Test Class Method
|
||||
|
||||
Ensure method `CenturionSubAbstractModel.clean_fields` is called.
|
||||
"""
|
||||
|
||||
# check models with model._audit_enabled=True have a model created
|
||||
clean_fields = mocker.patch('core.models.audit.AuditMetaModel.clean_fields', return_value = None)
|
||||
|
||||
# check models with model._audit_enabled=False DONT have a model created
|
||||
model_instance.clean_fields()
|
||||
|
||||
# check the Meta class has the correct attributes
|
||||
clean_fields.assert_called_once()
|
||||
|
||||
|
||||
# confirm it exists in sys.modules
|
||||
|
||||
# check they inherit form audithistory parent class
|
||||
|
||||
|
||||
|
||||
class MetaAbstractModelInheritedCases(
|
||||
@ -66,3 +59,74 @@ class MetaAbstractModelPyTest(
|
||||
is an abstract model this test is not required.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def test_method_clean_fields_calls_super_clean_fields(self, mocker, model_instance):
|
||||
"""Test Class Method
|
||||
|
||||
Ensure method `clean_fields` calls `super.clean_fields` when auditing
|
||||
is enabled.
|
||||
"""
|
||||
|
||||
model_instance.model = model_instance
|
||||
|
||||
mocker.patch('core.models.audit.CenturionAudit.get_model_history', return_value = True)
|
||||
|
||||
super_clean_fields = mocker.patch('core.models.audit.CenturionAudit.clean_fields', return_value = None)
|
||||
|
||||
model_instance.clean_fields()
|
||||
|
||||
|
||||
super_clean_fields.assert_called_with(
|
||||
exclude = None
|
||||
)
|
||||
|
||||
|
||||
def test_method_clean_fields_exception_no_model_history(self, mocker, model_instance):
|
||||
"""Test Class Method
|
||||
|
||||
Ensure method `clean_fields` calls `super.clean_fields` when auditing
|
||||
is enabled.
|
||||
"""
|
||||
|
||||
model_instance.model = model_instance
|
||||
|
||||
mocker.patch('core.models.audit.CenturionAudit.get_model_history', return_value = False)
|
||||
|
||||
# super_clean_fields = mocker.patch('core.models.audit.CenturionAudit.clean_fields', return_value = None)
|
||||
|
||||
|
||||
with pytest.raises( ValidationError ) as e:
|
||||
|
||||
model_instance.clean_fields()
|
||||
|
||||
|
||||
assert e.value.code == 'did_not_process_history'
|
||||
# super_clean_fields.assert_called_with(
|
||||
# exclude = None
|
||||
# )
|
||||
|
||||
|
||||
def test_method_clean_fields_exception_no_model(self, mocker, model_instance):
|
||||
"""Test Class Method
|
||||
|
||||
Ensure method `clean_fields` calls `super.clean_fields` when auditing
|
||||
is enabled.
|
||||
"""
|
||||
|
||||
model_instance.model = None
|
||||
|
||||
# mocker.patch('core.models.audit.CenturionAudit.get_model_history', return_value = False)
|
||||
|
||||
# super_clean_fields = mocker.patch('core.models.audit.CenturionAudit.clean_fields', return_value = None)
|
||||
|
||||
|
||||
with pytest.raises( ValidationError ) as e:
|
||||
|
||||
model_instance.clean_fields()
|
||||
|
||||
|
||||
assert e.value.code == 'no_model_supplied'
|
||||
# super_clean_fields.assert_called_with(
|
||||
# exclude = None
|
||||
# )
|
||||
|
@ -47,6 +47,8 @@ When creating models they must meet the following requirements:
|
||||
|
||||
- No `queryset` is to return data that the user has not got access to.
|
||||
|
||||
- Model Exceptions must be from `django.core.exceptions`
|
||||
|
||||
!!! tip
|
||||
It's a good idea to create the initial model class, then create and add the model tests for that class. This way you can run the tests to ensure that the requirements are met. Of Note, the tests may not cover ALL of the requirements section, due diligence will need to be exercised.
|
||||
|
||||
|
Reference in New Issue
Block a user