refactor(tests): Create global model fixtures

ref: #780 #729
This commit is contained in:
2025-05-31 10:44:17 +09:30
parent 66d14ea5bb
commit 1d31a259df
47 changed files with 820 additions and 358 deletions

View File

@ -1,10 +1,18 @@
import pytest
from access.models.tenancy_abstract import TenancyAbstractModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_tenancyabstract):
yield TenancyAbstractModel
yield model_tenancyabstract
@pytest.fixture( scope = 'class')
def model_kwargs(request, kwargs_tenancyabstract):
request.cls.kwargs_create_item = kwargs_tenancyabstract.copy()
yield kwargs_tenancyabstract.copy()
del request.cls.kwargs_create_item

View File

@ -11,13 +11,8 @@ class TenancyAbstractModelTestCases(
kwargs_create_item = {
'organization': 'set by fixture - setup_organization'
}
@pytest.fixture( scope = 'class', autouse = True)
def setup_organization(cls, request, model, organization_one):
def setup_organization(cls, request, model, organization_one, model_kwargs):
request.cls.organization = organization_one

View File

@ -1,10 +1,18 @@
import pytest
from access.models.tenancy_abstract import TenancyAbstractModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_tenancyabstract):
yield TenancyAbstractModel
yield model_tenancyabstract
@pytest.fixture( scope = 'class')
def model_kwargs(request, kwargs_tenancyabstract):
request.cls.kwargs_create_item = kwargs_tenancyabstract.copy()
yield kwargs_tenancyabstract.copy()
del request.cls.kwargs_create_item

View File

@ -15,10 +15,6 @@ class TenancyAbstractModelTestCases(
):
kwargs_create_item = {}
parameterized_class_attributes = {
'context': {
'type': dict,
@ -42,7 +38,7 @@ class TenancyAbstractModelTestCases(
@pytest.fixture( scope = 'class', autouse = True)
def setup_organization(cls, request, model, organization_one):
def setup_organization(cls, request, model, organization_one, model_kwargs):
request.cls.organization = organization_one

View File

@ -98,7 +98,7 @@ class ModelTestCases(
@pytest.fixture( scope = 'class', autouse = True)
def setup_class(cls, request, model):
def setup_class(cls, request, model, model_kwargs):
pass

View File

@ -11,9 +11,9 @@ from django.test import (
TestCase
)
from access.models.tenant import Tenant
from tests.fixtures import *
from settings.models.app_settings import AppSettings
from access.models.tenant import Tenant
User = django.contrib.auth.get_user_model()
@ -270,15 +270,6 @@ def pytest_generate_tests(metafunc):
@pytest.fixture( scope = 'class')
def content_type():
from django.contrib.contenttypes.models import ContentType
yield ContentType
@pytest.fixture( scope = 'class')
def create_model(request, django_db_blocker):
@ -591,17 +582,6 @@ def organization_three(django_db_blocker):
@pytest.fixture( scope = 'class')
def permission():
from django.contrib.auth.models import (
Permission,
)
yield Permission
@pytest.fixture
def recursearray() -> dict[dict, str, any]:
"""Recursive dict lookup
@ -776,15 +756,12 @@ def fake_view():
@pytest.fixture(scope = 'class')
def user(django_db_blocker):
def user(django_db_blocker, model_user, kwargs_user):
with django_db_blocker.unblock():
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
user = User.objects.create(
username="test_user-" + random_str,
password="password"
user = model_user.objects.create(
**kwargs_user
)
yield user
@ -795,176 +772,4 @@ def user(django_db_blocker):
@pytest.fixture( scope = 'class')
def api_user_permissions( django_db_blocker,
content_type,
model,
organization_one,
organization_two,
organization_three,
permission,
):
with django_db_blocker.unblock():
random_str = datetime.datetime.now(tz=datetime.timezone.utc)
app_settings = AppSettings.objects.get(
owner_organization = None
)
app_settings.global_organization = organization_three
app_settings.save()
add_permissions = permission.objects.get(
codename = 'add_' + model._meta.model_name,
content_type = content_type.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
add_team = Team.objects.create(
team_name = 'add_team' + str(random_str),
organization = organization_one,
)
add_team.permissions.set([add_permissions])
add_user = User.objects.create_user(username="test_user_add" + str(random_str), password="password")
TeamUsers.objects.create(
team = add_team,
user = add_user
)
change_permissions = permission.objects.get(
codename = 'change_' + model._meta.model_name,
content_type = content_type.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
change_team = Team.objects.create(
team_name = 'change_team' + str(random_str),
organization = organization_one,
)
change_team.permissions.set([change_permissions])
change_user = User.objects.create_user(username="test_user_change" + str(random_str), password="password")
TeamUsers.objects.create(
team = change_team,
user = change_user
)
delete_permissions = permission.objects.get(
codename = 'delete_' + model._meta.model_name,
content_type = content_type.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
delete_team = Team.objects.create(
team_name = 'delete_team' + str(random_str),
organization = organization_one,
)
delete_team.permissions.set([delete_permissions])
delete_user = User.objects.create_user(username="test_user_delete" + str(random_str), password="password")
TeamUsers.objects.create(
team = delete_team,
user = delete_user
)
view_permissions = permission.objects.get(
codename = 'view_' + model._meta.model_name,
content_type = content_type.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
view_team = Team.objects.create(
team_name = 'view_team' + str(random_str),
organization = organization_one,
)
view_team.permissions.set([view_permissions])
view_user = User.objects.create_user(username="test_user_view" + str(random_str), password="password")
TeamUsers.objects.create(
team = view_team,
user = view_user
)
different_organization_user = User.objects.create_user(username="test_diff_org_user" + str(random_str), password="password")
different_organization_team = Team.objects.create(
team_name = 'diff_org_team' + str(random_str),
organization = organization_two,
)
different_organization_team.permissions.set([
view_permissions,
add_permissions,
change_permissions,
delete_permissions,
])
TeamUsers.objects.create(
team = different_organization_team,
user = different_organization_user
)
yield {
'app_settings': app_settings,
'tenancy': {
'different': organization_two,
'global': organization_three,
'user': organization_one
},
'user': {
'add': add_user,
'change': change_user,
'delete': delete_user,
'different_tenancy': different_organization_user,
'view': view_user,
}
}
add_team.delete()
add_user.delete()
change_team.delete()
change_user.delete()
delete_team.delete()
delete_user.delete()
view_team.delete()
view_user.delete()
different_organization_team.delete()
different_organization_user.delete()

View File

@ -1,14 +1,19 @@
import pytest
from core.models.centurion import CenturionModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionmodel):
request.cls.model = CenturionModel
yield model_centurionmodel
yield request.cls.model
del request.cls.model
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionmodel):
request.cls.kwargs_create_item = kwargs_centurionmodel.copy()
yield kwargs_centurionmodel.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -14,12 +14,6 @@ class CenturionAbstractModelTestCases(
):
kwargs_create_item = {
'model_notes': 'model notes txt',
'created': '2025-05-23T00:00',
}
def test_model_has_history_model(self, model):
"""Audit History Table check
@ -46,7 +40,7 @@ class CenturionAbstractModelTestCases(
def test_model_create_has_history_entry(self, content_type, created_model, model):
def test_model_create_has_history_entry(self, model_contenttype, created_model, model):
"""Model Created
Ensure that the model when created, added a `create` Audit History
@ -66,7 +60,7 @@ class CenturionAbstractModelTestCases(
entry = history_model.objects.get(
model = created_model,
content_type = content_type.objects.get(
content_type = model_contenttype.objects.get(
app_label = created_model._meta.app_label,
model = created_model._meta.model_name
)

View File

@ -1,10 +1,19 @@
import pytest
from core.models.audit import CenturionAudit
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionaudit):
yield CenturionAudit
yield model_centurionaudit
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionaudit):
request.cls.kwargs_create_item = kwargs_centurionaudit.copy()
yield kwargs_centurionaudit.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -16,25 +16,15 @@ class CenturionAuditModelTestCases(
):
kwargs_create_item = {
'before': {},
'after': {
'after_key': 'after_value'
},
'action': CenturionAudit.Actions.ADD,
'user': 'fixture sets value',
'content_type': 'fixture sets value',
}
@pytest.fixture( scope = 'class', autouse = True )
def setup_vars(self, content_type, django_db_blocker, user, model):
def setup_vars(self, model_contenttype, django_db_blocker, model):
with django_db_blocker.unblock():
try:
content_type = content_type.objects.get(
content_type = model_contenttype.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
@ -42,14 +32,13 @@ class CenturionAuditModelTestCases(
except content_type.DoesNotExist:
# Enable Abstract models to be tested
content_type = content_type.objects.get(
content_type = model_contenttype.objects.get(
pk = 1,
)
self.kwargs_create_item.update({
'content_type': content_type,
'user': user,
})

View File

@ -1,10 +1,19 @@
import pytest
from core.models.centurion_notes import CenturionModelNote
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionmodelnote):
yield CenturionModelNote
yield model_centurionmodelnote
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionmodelnote):
request.cls.kwargs_create_item = kwargs_centurionmodelnote.copy()
yield kwargs_centurionmodelnote.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -14,21 +14,15 @@ class CenturionNoteModelTestCases(
):
kwargs_create_item = {
'body': 'a random note',
'created_by': 'fixture sets value',
'content_type': 'fixture sets value',
}
@pytest.fixture( scope = 'class', autouse = True )
def setup_vars(self, content_type, django_db_blocker, user, model):
def setup_vars(self, model_contenttype, django_db_blocker, model):
with django_db_blocker.unblock():
try:
content_type = content_type.objects.get(
content_type = model_contenttype.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
@ -36,14 +30,13 @@ class CenturionNoteModelTestCases(
except content_type.DoesNotExist:
# Enable Abstract models to be tested
content_type = content_type.objects.get(
content_type = model_contenttype.objects.get(
pk = 1,
)
self.kwargs_create_item.update({
'content_type': content_type,
'created_by': user,
})

View File

@ -1,14 +1,19 @@
import pytest
from core.models.centurion import CenturionModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionmodel):
request.cls.model = CenturionModel
yield model_centurionmodel
yield request.cls.model
del request.cls.model
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionmodel):
request.cls.kwargs_create_item = kwargs_centurionmodel.copy()
yield kwargs_centurionmodel.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -22,14 +22,6 @@ class CenturionAbstractModelTestCases(
):
kwargs_create_item = {
'model_notes': 'model notes txt',
'created': '2025-05-23T00:00',
}
@property
def parameterized_class_attributes(self):

View File

@ -1,10 +1,19 @@
import pytest
from core.models.audit import CenturionAudit
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionaudit):
yield CenturionAudit
yield model_centurionaudit
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionaudit):
request.cls.kwargs_create_item = kwargs_centurionaudit.copy()
yield kwargs_centurionaudit.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -1,14 +1,14 @@
import inspect
import pytest
from django.apps import apps
# from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import (
ValidationError
)
from django.db import models
from core.models.audit import CenturionAudit
# from core.models.audit import CenturionAudit
from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import (
CenturionAbstractModelInheritedCases
@ -21,17 +21,6 @@ class CenturionAuditModelTestCases(
):
kwargs_create_item = {
'before': {},
'after': {
'after_key': 'after_value'
},
'action': CenturionAudit.Actions.ADD,
'user': 'fixture sets value',
'content_type': 'fixture sets value',
}
parameterized_class_attributes = {
'_audit_enabled': {
'value': False,

View File

@ -5,6 +5,17 @@ from core.models.audit import AuditMetaModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionauditmeta):
yield AuditMetaModel
yield model_centurionauditmeta
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionauditmeta):
request.cls.kwargs_create_item = kwargs_centurionauditmeta.copy()
yield kwargs_centurionauditmeta.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -1,10 +1,19 @@
import pytest
from core.models.centurion_notes import CenturionModelNote
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionmodelnote):
yield CenturionModelNote
yield model_centurionmodelnote
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionmodelnote):
request.cls.kwargs_create_item = kwargs_centurionmodelnote.copy()
yield kwargs_centurionmodelnote.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -15,13 +15,6 @@ class CenturionNoteModelTestCases(
):
kwargs_create_item = {
'body': 'a random note',
'created_by': 'fixture sets value',
'content_type': 'fixture sets value',
}
parameterized_class_attributes = {
'_audit_enabled': {
'value': False,

View File

@ -1,10 +1,19 @@
import pytest
from core.models.centurion_notes import NoteMetaModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionmodelnotemeta):
yield NoteMetaModel
yield model_centurionmodelnotemeta
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionmodelnotemeta):
request.cls.kwargs_create_item = kwargs_centurionmodelnotemeta.copy()
yield kwargs_centurionmodelnotemeta.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -5,6 +5,17 @@ from core.models.centurion import CenturionSubModel
@pytest.fixture( scope = 'class')
def model(request):
def model(model_centurionsubmodel):
yield CenturionSubModel
yield model_centurionsubmodel
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_centurionsubmodel):
request.cls.kwargs_create_item = kwargs_centurionsubmodel.copy()
yield kwargs_centurionsubmodel.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -1,3 +1,5 @@
import pytest
from django.test import Client, TestCase
from rest_framework.reverse import reverse
@ -13,6 +15,7 @@ from devops.models.check_ins import CheckIn
@pytest.mark.skip( reason = "Audit history requires context['user']")
class Checkin(
TestCase,
):

View File

@ -1,10 +1,19 @@
import pytest
from devops.models.git_group import GitGroup
@pytest.fixture( scope = 'class')
def model(request):
def model(model_gitgroup):
yield GitGroup
yield model_gitgroup
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_gitgroup):
request.cls.kwargs_create_item = kwargs_gitgroup.copy()
yield kwargs_gitgroup.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -12,7 +12,7 @@ from devops.serializers.public_feature_flag import FeatureFlag, ViewSerializer
from itam.models.software import Software
@pytest.mark.skip( reason = "Audit history requires context['user']")
class Serializer(
TestCase,
):

View File

@ -1,3 +1,4 @@
import pytest
from datetime import datetime
from dateutil import tz
@ -15,6 +16,7 @@ from itam.models.software import Software
@pytest.mark.skip( reason = "Audit history requires context['user']")
class ViewSetBase:
model = FeatureFlag

View File

@ -1,10 +1,20 @@
import pytest
from devops.models.feature_flag import FeatureFlag
@pytest.fixture( scope = 'class')
def model(request):
def model(model_featureflag):
yield FeatureFlag
yield model_featureflag
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_featureflag):
request.cls.kwargs_create_item = kwargs_featureflag.copy()
yield kwargs_featureflag.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -1,10 +1,19 @@
import pytest
from devops.models.git_group import GitGroup
@pytest.fixture( scope = 'class')
def model(request):
def model(model_gitgroup):
yield GitGroup
yield model_gitgroup
@pytest.fixture( scope = 'class', autouse = True)
def model_kwargs(request, kwargs_gitgroup):
request.cls.kwargs_create_item = kwargs_gitgroup.copy()
yield kwargs_gitgroup.copy()
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item

View File

@ -7,8 +7,6 @@ from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model impor
CenturionAbstractModelInheritedCases
)
from devops.models.git_group import GitGroup
@pytest.mark.model_gitgroup
@ -17,18 +15,6 @@ class GitGroupModelTestCases(
):
kwargs_create_item = {
'parent_group': None,
'provider': GitGroup.GitProvider.GITHUB,
'provider_pk': 1,
'name': 'a name',
'path': 'a_path',
'description': 'a random bit of text.'
}
@property
def parameterized_class_attributes(self):

View File

@ -1,3 +1,4 @@
import pytest
from datetime import datetime
from django.shortcuts import reverse
@ -13,7 +14,7 @@ from devops.models.software_enable_feature_flag import SoftwareEnableFeatureFlag
from itam.models.software import Software
@pytest.mark.skip( reason = "Audit history requires context['user']")
class API(
TestCase,
):

View File

@ -1,3 +1,4 @@
import pytest
from django.test import Client, TestCase
from rest_framework.reverse import reverse
@ -10,7 +11,7 @@ from devops.viewsets.public_feature_flag import ViewSet
from itam.models.software import Software
@pytest.mark.skip( reason = "Audit history requires context['user']")
class ViewsetList(
PublicReadOnlyViewSetInheritedCases,
TestCase,

0
app/tests/__init__.py Normal file
View File

64
app/tests/fixtures/__init__.py vendored Normal file
View File

@ -0,0 +1,64 @@
# pylint: disable=W0611:unused-import
from app.tests.fixtures.model_centurionsubmodel import model_centurionsubmodel
from .api_request_permissions import (
api_request_permissions,
)
from .model_centurionaudit import (
kwargs_centurionaudit,
model_centurionaudit,
)
from .model_centurionauditmeta import (
kwargs_centurionauditmeta,
model_centurionauditmeta,
)
from .model_centurionmodel import (
kwargs_centurionmodel,
model_centurionmodel,
)
from .model_centurionmodelnote import (
kwargs_centurionmodelnote,
model_centurionmodelnote,
)
from .model_centurionmodelnotemeta import (
kwargs_centurionmodelnotemeta,
model_centurionmodelnotemeta,
)
from .model_centurionsubmodel import (
kwargs_centurionsubmodel,
model_centurionsubmodel,
)
from .model_contenttype import (
model_contenttype,
)
from .model_featureflag import (
kwargs_featureflag,
model_featureflag,
)
from .model_gitgroup import (
kwargs_gitgroup,
model_gitgroup,
)
from .model_permission import (
model_permission,
)
from .model_tenancyabstract import (
kwargs_tenancyabstract,
model_tenancyabstract,
)
from .model_user import (
kwargs_user,
model_user,
)

View File

@ -0,0 +1,192 @@
import django
import pytest
from settings.models.app_settings import AppSettings
User = django.contrib.auth.get_user_model()
@pytest.fixture( scope = 'class')
def api_request_permissions( django_db_blocker,
model_contenttype,
model_permission,
model,
organization_one,
organization_two,
organization_three,
):
with django_db_blocker.unblock():
random_str = datetime.datetime.now(tz=datetime.timezone.utc)
app_settings = AppSettings.objects.get(
owner_organization = None
)
app_settings.global_organization = organization_three
app_settings.save()
add_permissions = model_permission.objects.get(
codename = 'add_' + model._meta.model_name,
content_type = model_contenttype.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
add_team = Team.objects.create(
team_name = 'add_team' + str(random_str),
organization = organization_one,
)
add_team.permissions.set([add_permissions])
add_user = User.objects.create_user(
username="test_user_add" + str(random_str), password="password"
)
TeamUsers.objects.create(
team = add_team,
user = add_user
)
change_permissions = model_permission.objects.get(
codename = 'change_' + model._meta.model_name,
content_type = model_contenttype.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
change_team = Team.objects.create(
team_name = 'change_team' + str(random_str),
organization = organization_one,
)
change_team.permissions.set([change_permissions])
change_user = User.objects.create_user(
username="test_user_change" + str(random_str), password="password"
)
TeamUsers.objects.create(
team = change_team,
user = change_user
)
delete_permissions = model_permission.objects.get(
codename = 'delete_' + model._meta.model_name,
content_type = model_contenttype.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
delete_team = Team.objects.create(
team_name = 'delete_team' + str(random_str),
organization = organization_one,
)
delete_team.permissions.set([delete_permissions])
delete_user = User.objects.create_user(
username="test_user_delete" + str(random_str), password="password"
)
TeamUsers.objects.create(
team = delete_team,
user = delete_user
)
view_permissions = model_permission.objects.get(
codename = 'view_' + model._meta.model_name,
content_type = model_contenttype.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
)
)
view_team = Team.objects.create(
team_name = 'view_team' + str(random_str),
organization = organization_one,
)
view_team.permissions.set([view_permissions])
view_user = User.objects.create_user(
username="test_user_view" + str(random_str), password="password"
)
TeamUsers.objects.create(
team = view_team,
user = view_user
)
different_organization_user = User.objects.create_user(
username="test_diff_org_user" + str(random_str), password="password"
)
different_organization_team = Team.objects.create(
team_name = 'diff_org_team' + str(random_str),
organization = organization_two,
)
different_organization_team.permissions.set([
view_permissions,
add_permissions,
change_permissions,
delete_permissions,
])
TeamUsers.objects.create(
team = different_organization_team,
user = different_organization_user
)
yield {
'app_settings': app_settings,
'tenancy': {
'different': organization_two,
'global': organization_three,
'user': organization_one
},
'user': {
'add': add_user,
'change': change_user,
'delete': delete_user,
'different_tenancy': different_organization_user,
'no_permissions': '',
'view': view_user,
}
}
add_team.delete()
add_user.delete()
change_team.delete()
change_user.delete()
delete_team.delete()
delete_user.delete()
view_team.delete()
view_user.delete()
different_organization_team.delete()
different_organization_user.delete()

View File

@ -0,0 +1,49 @@
import datetime
import pytest
from core.models.audit import CenturionAudit
@pytest.fixture( scope = 'class')
def model_centurionaudit():
yield CenturionAudit
@pytest.fixture( scope = 'class')
def kwargs_centurionaudit(django_db_blocker, kwargs_centurionmodel, kwargs_user, model_user):
kwargs = kwargs_centurionmodel.copy()
del kwargs['model_notes']
with django_db_blocker.unblock():
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
user_kwargs = kwargs_user.copy()
user_kwargs.update({
'username': 'audit_user' + str(random_str)
})
user = model_user.objects.create(
**user_kwargs,
)
kwargs = {
**kwargs,
'before': {},
'after': {
'after_key': 'after_value'
},
'action': CenturionAudit.Actions.ADD,
'user': user,
'content_type': 'fixture sets value',
}
yield kwargs.copy()
with django_db_blocker.unblock():
user.delete()

View File

@ -0,0 +1,20 @@
import pytest
from core.models.audit import AuditMetaModel
@pytest.fixture( scope = 'class')
def model_centurionauditmeta(request):
yield AuditMetaModel
@pytest.fixture( scope = 'class')
def kwargs_centurionauditmeta(request, kwargs_centurionaudit):
kwargs = {
**kwargs_centurionaudit.copy(),
}
yield kwargs.copy()

View File

@ -0,0 +1,22 @@
import pytest
from core.models.centurion import CenturionModel
@pytest.fixture( scope = 'class')
def model_centurionmodel():
yield CenturionModel
@pytest.fixture( scope = 'class')
def kwargs_centurionmodel(kwargs_tenancyabstract):
kwargs = {
**kwargs_tenancyabstract,
'model_notes': 'model notes txt',
'created': '2025-05-23T00:00',
}
yield kwargs.copy()

View File

@ -0,0 +1,43 @@
import datetime
import pytest
from core.models.centurion_notes import CenturionModelNote
@pytest.fixture( scope = 'class')
def model_centurionmodelnote():
yield CenturionModelNote
@pytest.fixture( scope = 'class')
def kwargs_centurionmodelnote(django_db_blocker, kwargs_centurionmodel, kwargs_user, model_user):
kwargs = kwargs_centurionmodel.copy()
del kwargs['model_notes']
with django_db_blocker.unblock():
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
user_kwargs = kwargs_user.copy()
user_kwargs.update({
'username': 'note_user' + str(random_str)
})
user = model_user.objects.create(
**user_kwargs,
)
kwargs = {
**kwargs,
'body': 'a random note',
'created_by': user,
'content_type': 'fixture sets value',
}
yield kwargs.copy()
with django_db_blocker.unblock():
user.delete()

View File

@ -0,0 +1,20 @@
import pytest
from core.models.centurion_notes import NoteMetaModel
@pytest.fixture( scope = 'class')
def model_centurionmodelnotemeta(request):
yield NoteMetaModel
@pytest.fixture( scope = 'class')
def kwargs_centurionmodelnotemeta(request, kwargs_centurionmodelnote):
kwargs = {
**kwargs_centurionmodelnote.copy(),
}
yield kwargs

View File

@ -0,0 +1,20 @@
import pytest
from core.models.centurion import CenturionSubModel
@pytest.fixture( scope = 'class')
def model_centurionsubmodel(request):
yield CenturionSubModel
@pytest.fixture( scope = 'class')
def kwargs_centurionsubmodel(request, kwargs_centurionmodel):
kwargs = {
**kwargs_centurionmodel.copy(),
}
yield kwargs.copy()

16
app/tests/fixtures/model_contenttype.py vendored Normal file
View File

@ -0,0 +1,16 @@
import pytest
from django.contrib.contenttypes.models import ContentType
@pytest.fixture( scope = 'class')
def model_contenttype():
yield ContentType
@pytest.fixture( scope = 'class')
def content_type():
yield ContentType

27
app/tests/fixtures/model_featureflag.py vendored Normal file
View File

@ -0,0 +1,27 @@
import pytest
from devops.models.feature_flag import FeatureFlag
@pytest.fixture( scope = 'class')
def model_featureflag(request):
yield FeatureFlag
@pytest.fixture( scope = 'class')
def kwargs_featureflag(kwargs_centurionmodel):
# kwargs = kwargs_centurionmodel.copy()
# del kwargs['model_notes']
kwargs = {
**kwargs_centurionmodel.copy(),
'software': None,
'name': 'a name',
'description': ' a description',
'enabled': True,
}
yield kwargs.copy()

29
app/tests/fixtures/model_gitgroup.py vendored Normal file
View File

@ -0,0 +1,29 @@
import pytest
from devops.models.git_group import GitGroup
@pytest.fixture( scope = 'class')
def model_gitgroup(request):
yield GitGroup
@pytest.fixture( scope = 'class')
def kwargs_gitgroup(kwargs_centurionmodel):
# kwargs = kwargs_centurionmodel.copy()
# del kwargs['model_notes']
kwargs = {
**kwargs_centurionmodel.copy(),
'parent_group': None,
'provider': GitGroup.GitProvider.GITHUB,
'provider_pk': 1,
'name': 'a name',
'path': 'a_path',
'description': 'a random bit of text.'
}
yield kwargs.copy()

17
app/tests/fixtures/model_permission.py vendored Normal file
View File

@ -0,0 +1,17 @@
import datetime
import django
import pytest
from django.contrib.auth.models import (
Permission,
)
User = django.contrib.auth.get_user_model()
@pytest.fixture( scope = 'class')
def model_permission():
yield Permission

View File

@ -0,0 +1,18 @@
import pytest
from access.models.tenancy_abstract import TenancyAbstractModel
@pytest.fixture( scope = 'class')
def model_tenancyabstract(request):
yield TenancyAbstractModel
@pytest.fixture( scope = 'class')
def kwargs_tenancyabstract():
kwargs = {}
yield kwargs.copy()

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

@ -0,0 +1,26 @@
import datetime
import django
import pytest
User = django.contrib.auth.get_user_model()
@pytest.fixture( scope = 'class')
def model_user():
yield User
@pytest.fixture( scope = 'class')
def kwargs_user():
kwargs = {}
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
kwargs = {
'username': "test_user-" + random_str,
'password': "password"
}
yield kwargs.copy()

View File

@ -10,12 +10,6 @@ Unit and functional tests are written to aid in application stability and to ass
We use PyTest as the testing framework. As such, All available features of pytest are available. We have slightly deviated from the standard naming convention wherein test class must be suffixed with `PyTest`. Please [see below](#writing-tests) for more details.
!!! note
As of release v1.3, the UI has moved to it's [own project](https://github.com/nofusscomputing/centurion_erp_ui) with the current Django UI feature locked and depreciated.
!!! note
These docs are currently undergoing a rewrite as how we write and expect tests to be written has changed.
## Directory Structure
@ -149,9 +143,43 @@ Fixtures are used to setup the test and to pass objects to test should they requ
There may also be a requirement that you add additional fixtures, they are:
- `model` This fixture should be defined in `conftest.py` in the test suite files directory. _Only required if the model is required to be worked with._
- Global Model Fixtures
``` py filename="conftest.py"
Locatation for the global fixtures is `app/tests/fixtures/`. Each model is to have a global fixture file added with name `model_<model name>` within this file the following fixtures are to be created:
``` py title="tests/fixtures/model_centurionmodel.py"
import pytest
from core.models.centurion import CenturionModel
@pytest.fixture( scope = 'class')
def model_centurionmodel():
yield CenturionModel
@pytest.fixture( scope = 'class')
def kwargs_centurionmodel(kwargs_tenancyabstract):
kwargs = {
**kwargs_tenancyabstract,
'model_notes': 'model notes txt',
'created': '2025-05-23T00:00',
}
yield kwargs.copy()
```
- `model` is to return the model class un-instantiated
- `kwargs` the Kwargs required to create the model.
- `model` and `model_kwargs` These fixtures should be defined in `conftest.py` in the test suite files directory. _Only required if the model is required to be worked with._
``` py title="conftest.py"
import pytest
@ -162,11 +190,18 @@ There may also be a requirement that you add additional fixtures, they are:
@pytest.fixture( scope = 'class')
def model(request):
request.cls.model = RequestTicket
yield RequestTicket
yield request.cls.model
@pytest.fixture( scope = 'class')
def model_kwargs(request, kwargs_<model_name>):
request.cls.kwargs_create_item = kwargs_<model_name>.copy()
yield kwargs_<model_name>.copy()
del request.cls.kwargs_create_item
del request.cls.model # Don't forget to clean-up any objects created.
```

View File

@ -6,10 +6,12 @@ ACTIVATE_VENV :=. ${PATH_VENV}/bin/activate
.PHONY: clean prepare docs ansible-lint lint test
prepare:
prepare-git-submodule:
git submodule update --init;
git submodule foreach git submodule update --init;
prepare-python: prepare-git-submodule
python3 -m venv ${PATH_VENV};
${ACTIVATE_VENV};
pip install -r website-template/gitlab-ci/mkdocs/requirements.txt;
@ -17,13 +19,15 @@ prepare:
pip install -r gitlab-ci/mkdocs/requirements.txt;
pip install -r requirements.txt;
pip install -r requirements_test.txt;
prepare-docs: prepare-git-submodule
npm install markdownlint-cli2;
npm install markdownlint-cli2-formatter-junit;
cp -f "website-template/.markdownlint.json" ".markdownlint.json";
cp -f "gitlab-ci/lint/.markdownlint-cli2.jsonc" ".markdownlint-cli2.jsonc";
markdown-mkdocs-lint:
markdown-mkdocs-lint: prepare-docs
PATH=${PATH}:node_modules/.bin markdownlint-cli2 docs/*.md docs/**/*.md docs/**/**/*.md docs/**/**/**/*.md docs/**/**/**/**/**/*.md !docs/pull_request_template.md !CHANGELOG.md !gitlab-ci !website-template || true