refactor(access): Update Test Suite for Entity model

ref: #839 #840
This commit is contained in:
2025-06-19 21:53:47 +09:30
parent 847e4f93ea
commit b6dae72a07
6 changed files with 98 additions and 216 deletions

View File

@ -1,14 +1,18 @@
import pytest
from access.models.entity import Entity
@pytest.fixture( scope = 'class')
def model(request):
def model(model_entity):
request.cls.model = Entity
yield model_entity
yield request.cls.model
@pytest.fixture( scope = 'class')
def model_kwargs(request, kwargs_entity):
del request.cls.model
request.cls.kwargs_create_item = kwargs_entity.copy()
yield kwargs_entity.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -8,6 +8,7 @@ from api.tests.functional.test_functional_api_fields import (
@pytest.mark.model_entity
class EntityAPITestCases(
APIFieldsInheritedCases,
):
@ -71,6 +72,7 @@ class EntityAPIInheritedCases(
@pytest.mark.module_access
class EntityAPIPyTest(
EntityAPITestCases,
):

View File

@ -4,271 +4,112 @@ from django.db import models
from access.models.entity import Entity
from centurion.tests.unit.test_unit_models import (
PyTestTenancyObjectInheritedCases,
from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import (
CenturionAbstractModelInheritedCases
)
@pytest.mark.model_entity
class EntityModelTestCases(
PyTestTenancyObjectInheritedCases,
CenturionAbstractModelInheritedCases
):
base_model = Entity
kwargs_create_item: dict = {}
@property
def parameterized_class_attributes(self):
sub_model_type = 'entity'
"""Sub Model Type
sub-models must have this attribute defined in `ModelName.Meta.sub_model_type`
"""
parameterized_fields: dict = {
"entity_type": {
'field_type': models.fields.CharField,
'field_parameter_default_exists': False,
# 'field_parameter_default_value': 'entity',
'field_parameter_verbose_name_type': str
return {
'_is_submodel': {
'value': False
},
# "asset_number": {
# 'field_type': models.fields.CharField,
# 'field_parameter_default_exists': False,
# 'field_parameter_verbose_name_type': str,
# },
# "serial_number": {
# 'field_type': models.fields.CharField,
# 'field_parameter_default_exists': False,
# 'field_parameter_verbose_name_type': str,
# }
'model_tag': {
'type': str,
'value': 'entity'
},
'url_model_name': {
'type': str,
'value': 'entity'
}
}
@property
def parameterized_fields(self):
@pytest.fixture( scope = 'class')
def setup_model(self,
request,
model,
django_db_blocker,
organization_one,
organization_two
):
with django_db_blocker.unblock():
request.cls.organization = organization_one
request.cls.different_organization = organization_two
kwargs_create_item = {}
for base in reversed(request.cls.__mro__):
if hasattr(base, 'kwargs_create_item'):
if base.kwargs_create_item is None:
continue
kwargs_create_item.update(**base.kwargs_create_item)
return {
'entity_type': {
'blank': False,
'default': models.fields.NOT_PROVIDED,
'field_type': models.CharField,
'length': 50,
'null': False,
'unique': True,
},
'modified': {
'blank': False,
'default': models.fields.NOT_PROVIDED,
'field_type': models.DateTimeField,
'null': False,
'unique': False,
},
}
if len(kwargs_create_item) > 0:
request.cls.kwargs_create_item = kwargs_create_item
if 'organization' not in request.cls.kwargs_create_item:
request.cls.kwargs_create_item.update({
'organization': request.cls.organization
})
yield
@pytest.fixture( scope = 'class', autouse = True)
def class_setup(self,
setup_model,
create_model,
):
pass
def test_class_inherits_entity(self):
def test_class_inherits_entity(self, model):
""" Class inheritence
TenancyObject must inherit SaveHistory
"""
assert issubclass(self.model, Entity)
assert issubclass(model, Entity)
def test_attribute_type_history_app_label(self):
"""Attribute Type
def test_function_value_get_related_model(self, model, model_instance):
"""Function test
history_app_label is of type str
Confirm function `get_related_model` is of the sub-model type
"""
assert type(self.model.history_app_label) is str
assert type(model_instance.get_related_model()) == model
def test_attribute_value_history_app_label(self):
"""Attribute Type
history_app_label has been set, override this test case with the value
of attribute `history_app_label`
"""
assert self.model.history_app_label == 'access'
def test_attribute_type_history_model_name(self):
"""Attribute Type
history_model_name is of type str
"""
assert type(self.model.history_model_name) is str
def test_attribute_value_history_model_name(self):
"""Attribute Type
history_model_name has been set, override this test case with the value
of attribute `history_model_name`
"""
assert self.model.history_model_name == 'entity'
def test_attribute_type_kb_model_name(self):
def test_attribute_type_kb_model_name(self, model):
"""Attribute Type
kb_model_name is of type str
"""
assert type(self.model.kb_model_name) is str
assert type(model.kb_model_name) is str
def test_attribute_value_kb_model_name(self):
def test_attribute_value_kb_model_name(self, model):
"""Attribute Type
kb_model_name has been set, override this test case with the value
of attribute `kb_model_name`
"""
assert self.model.kb_model_name == 'entity'
def test_attribute_type_note_basename(self):
"""Attribute Type
note_basename is of type str
"""
assert type(self.model.note_basename) is str
def test_attribute_value_note_basename(self):
"""Attribute Type
note_basename has been set, override this test case with the value
of attribute `note_basename`
"""
assert self.model.note_basename == '_api_v2_entity_note'
# def test_function_is_property_get_model_type(self):
# """Function test
# Confirm function `get_model_type` is a property
# """
# assert type(self.model.get_model_type) is property
# def test_function_value_get_model_type(self):
# """Function test
# Confirm function `get_model_type` does not have a value of None
# value should be equaul to Meta.sub_model_type
# """
# assert self.item.get_model_type == self.item._meta.sub_model_type
def test_function_value_get_related_model(self):
"""Function test
Confirm function `get_related_model` is of the sub-model type
"""
assert type(self.item.get_related_model()) == self.model
def test_function_value_get_url(self):
assert self.item.get_url() == '/api/v2/access/entity/' + str(self.item.id)
assert model.kb_model_name == 'entity'
class EntityModelInheritedCases(
EntityModelTestCases,
):
"""Sub-Ticket Test Cases
Test Cases for Ticket models that inherit from model Entity
"""
kwargs_create_item: dict = {}
model = None
sub_model_type = None
"""Ticket Sub Model Type
Ticket sub-models must have this attribute defined in `ModelNam.Meta.sub_model_type`
"""
# def test_function_value_get_model_type(self):
# """Function test
# Confirm function `get_model_type` does not have a value of None
# value should be equaul to Meta.sub_model_type
# """
# assert self.item.get_model_type == self.item._meta.sub_model_type
pass
@pytest.mark.module_access
class EntityModelPyTest(
EntityModelTestCases,
):
def test_function_value_get_related_model(self):
def test_function_value_get_related_model(self, model_instance):
"""Function test
Confirm function `get_related_model` is None for base model
"""
assert self.item.get_related_model() is None
assert model_instance.get_related_model() is None

View File

@ -1,3 +1,5 @@
import pytest
from django.test import Client, TestCase
from rest_framework.reverse import reverse
@ -12,6 +14,7 @@ from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
@pytest.mark.model_entity
class ViewsetTestCases(
ModelViewSetInheritedCases,
):
@ -70,6 +73,7 @@ class EntityViewsetInheritedCases(
@pytest.mark.module_access
class EntityViewsetTest(
ViewsetTestCases,
TestCase,

View File

@ -102,6 +102,11 @@ from .model_devicetype import (
model_devicetype,
)
from .model_entity import (
kwargs_entity,
model_entity,
)
from .model_externallink import (
kwargs_externallink,
model_externallink,

26
app/tests/fixtures/model_entity.py vendored Normal file
View File

@ -0,0 +1,26 @@
import datetime
import pytest
from access.models.entity import Entity
@pytest.fixture( scope = 'class')
def model_entity():
yield Entity
@pytest.fixture( scope = 'class')
def kwargs_entity( kwargs_centurionmodel ):
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
random_str = str(random_str).replace(
' ', '').replace(':', '').replace('+', '').replace('.', '')
kwargs = {
**kwargs_centurionmodel.copy(),
'entity_type': 'entity',
}
yield kwargs.copy()