From f377ad102e3556f59e3fe1fb5218c7b501d86289 Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 13 Jun 2025 15:11:42 +0930 Subject: [PATCH] refactor(itim): Update Test Suite for Service model ref: #824 #828 --- app/fixtures/fresh_db.sql | 6 +- .../unit/cluster/test_unit_cluster_model.py | 2 +- app/itim/tests/unit/service/conftest.py | 19 +++ .../tests/unit/service/test_service_api_v2.py | 3 +- .../test_service_item_ticket_api_v2.py | 3 + .../unit/service/test_unit_service_model.py | 122 ++++++++++++++++-- .../unit/service/test_unit_service_viewset.py | 4 + app/tests/fixtures/__init__.py | 5 + app/tests/fixtures/model_service.py | 26 ++++ 9 files changed, 176 insertions(+), 14 deletions(-) create mode 100644 app/itim/tests/unit/service/conftest.py create mode 100644 app/tests/fixtures/model_service.py diff --git a/app/fixtures/fresh_db.sql b/app/fixtures/fresh_db.sql index 4716b06a..db7a4bca 100644 --- a/app/fixtures/fresh_db.sql +++ b/app/fixtures/fresh_db.sql @@ -105,10 +105,10 @@ CREATE TABLE IF NOT EXISTS "devops_git_group_history" ("modelhistory_ptr_id" int CREATE TABLE IF NOT EXISTS "devops_github_repository_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "devops_githubrepository" ("gitrepository_ptr_id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "devops_gitlab_repository_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "devops_gitlabrepository" ("gitrepository_ptr_id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "devops_git_group_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "devops_gitgroup" ("id") DEFERRABLE INITIALLY DEFERRED); -CREATE TABLE IF NOT EXISTS "access_organization_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "access_organization_history" ("modelhistory_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_history" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED); -CREATE TABLE IF NOT EXISTS "settings_usersettings" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "default_organization_id" integer NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "timezone" varchar(32) NOT NULL, "browser_mode" integer NOT NULL); +CREATE TABLE IF NOT EXISTS "access_organization_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "settings_appsettings" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "device_model_is_global" bool NOT NULL, "device_type_is_global" bool NOT NULL, "manufacturer_is_global" bool NOT NULL, "software_is_global" bool NOT NULL, "software_categories_is_global" bool NOT NULL, "global_organization_id" integer NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "owner_organization_id" integer NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED); +CREATE TABLE IF NOT EXISTS "settings_usersettings" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "default_organization_id" integer NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "timezone" varchar(32) NOT NULL, "browser_mode" integer NOT NULL); CREATE TABLE IF NOT EXISTS "access_company" ("entity_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "access_entity" ("id") DEFERRABLE INITIALLY DEFERRED, "name" varchar(80) NOT NULL); CREATE TABLE IF NOT EXISTS "access_entity" ("is_global" bool NOT NULL, "model_notes" text NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "entity_type" varchar(30) NOT NULL); CREATE TABLE IF NOT EXISTS "access_person" ("entity_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "access_entity" ("id") DEFERRABLE INITIALLY DEFERRED, "f_name" varchar(64) NOT NULL, "l_name" varchar(64) NOT NULL, "dob" date NULL, "m_name" varchar(100) NULL); @@ -233,8 +233,8 @@ INSERT INTO sqlite_sequence VALUES('core_ticket',0); INSERT INTO sqlite_sequence VALUES('core_ticketcomment',0); INSERT INTO sqlite_sequence VALUES('core_ticketlinkeditem',0); INSERT INTO sqlite_sequence VALUES('settings_externallink',0); -INSERT INTO sqlite_sequence VALUES('settings_usersettings',0); INSERT INTO sqlite_sequence VALUES('settings_appsettings',1); +INSERT INTO sqlite_sequence VALUES('settings_usersettings',0); INSERT INTO sqlite_sequence VALUES('access_entity',0); INSERT INTO sqlite_sequence VALUES('core_ticketbase',0); INSERT INTO sqlite_sequence VALUES('core_ticketcommentbase',0); diff --git a/app/itim/tests/unit/cluster/test_unit_cluster_model.py b/app/itim/tests/unit/cluster/test_unit_cluster_model.py index 55ce7a4c..1b3cfce5 100644 --- a/app/itim/tests/unit/cluster/test_unit_cluster_model.py +++ b/app/itim/tests/unit/cluster/test_unit_cluster_model.py @@ -47,7 +47,7 @@ class ClusterModelTestCases( 'name': { 'blank': False, 'default': models.fields.NOT_PROVIDED, - 'field_type': models.IntegerField, + 'field_type': models.CharField, 'length': 50, 'null': False, 'unique': False, diff --git a/app/itim/tests/unit/service/conftest.py b/app/itim/tests/unit/service/conftest.py new file mode 100644 index 00000000..300981d7 --- /dev/null +++ b/app/itim/tests/unit/service/conftest.py @@ -0,0 +1,19 @@ +import pytest + + + +@pytest.fixture( scope = 'class') +def model(model_service): + + yield model_service + + +@pytest.fixture( scope = 'class', autouse = True) +def model_kwargs(request, kwargs_service): + + request.cls.kwargs_create_item = kwargs_service.copy() + + yield kwargs_service.copy() + + if hasattr(request.cls, 'kwargs_create_item'): + del request.cls.kwargs_create_item diff --git a/app/itim/tests/unit/service/test_service_api_v2.py b/app/itim/tests/unit/service/test_service_api_v2.py index 6dbbeed6..3c3a3721 100644 --- a/app/itim/tests/unit/service/test_service_api_v2.py +++ b/app/itim/tests/unit/service/test_service_api_v2.py @@ -1,5 +1,4 @@ import pytest -import unittest from django.contrib.auth.models import Permission, User from django.contrib.contenttypes.models import ContentType @@ -21,6 +20,8 @@ from itim.models.services import Service, Port +@pytest.mark.model_service +@pytest.mark.module_itim class ServiceAPI( TestCase, APITenancyObject diff --git a/app/itim/tests/unit/service/test_service_item_ticket_api_v2.py b/app/itim/tests/unit/service/test_service_item_ticket_api_v2.py index e372a14e..759df084 100644 --- a/app/itim/tests/unit/service/test_service_item_ticket_api_v2.py +++ b/app/itim/tests/unit/service/test_service_item_ticket_api_v2.py @@ -1,3 +1,4 @@ +import pytest from django.shortcuts import reverse from django.test import Client, TestCase @@ -10,6 +11,8 @@ from itim.models.services import Service +@pytest.mark.model_service +@pytest.mark.module_itim class ServiceItemTicketAPI( ItemTicketAPI, TestCase, diff --git a/app/itim/tests/unit/service/test_unit_service_model.py b/app/itim/tests/unit/service/test_unit_service_model.py index aee1fab3..a5e9047d 100644 --- a/app/itim/tests/unit/service/test_unit_service_model.py +++ b/app/itim/tests/unit/service/test_unit_service_model.py @@ -1,16 +1,120 @@ -from django.test import TestCase +import pytest -from centurion.tests.unit.test_unit_models import ( - TenancyObjectInheritedCases +from django.db import models + + +from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import ( + CenturionAbstractModelInheritedCases ) -from itim.models.services import Service - -class ServiceModel( - TenancyObjectInheritedCases, - TestCase, +@pytest.mark.model_service +class ServiceModelTestCases( + CenturionAbstractModelInheritedCases ): - model = Service + + @property + def parameterized_class_attributes(self): + + return { + 'model_tag': { + 'type': str, + 'value': 'service' + }, + } + + + @property + def parameterized_model_fields(self): + + return { + 'is_template': { + 'blank': False, + 'default': False, + 'field_type': models.BooleanField, + 'null': False, + 'unique': False, + }, + 'template': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ForeignKey, + 'null': True, + 'unique': False, + }, + 'name': { + 'blank': False, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.CharField, + 'length': 50, + 'null': False, + 'unique': False, + }, + 'device': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ForeignKey, + 'null': True, + 'unique': False, + }, + 'cluster': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ForeignKey, + 'null': True, + 'unique': False, + }, + 'config': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.JSONField, + 'null': True, + 'unique': False, + }, + 'config_key_variable': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.CharField, + 'null': True, + 'unique': False, + }, + 'port': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ManyToManyField, + 'null': False, + 'unique': False, + }, + 'dependent_service': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ManyToManyField, + 'null': False, + 'symmetrical': False, + 'unique': False, + }, + 'modified': { + 'blank': False, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.DateTimeField, + 'null': False, + 'unique': False, + }, + } + + + +class ServiceModelInheritedCases( + ServiceModelTestCases, +): + pass + + + +@pytest.mark.module_itim +class ServiceModelPyTest( + ServiceModelTestCases, +): + pass diff --git a/app/itim/tests/unit/service/test_unit_service_viewset.py b/app/itim/tests/unit/service/test_unit_service_viewset.py index 8329593a..7138f3db 100644 --- a/app/itim/tests/unit/service/test_unit_service_viewset.py +++ b/app/itim/tests/unit/service/test_unit_service_viewset.py @@ -1,3 +1,5 @@ +import pytest + from django.test import Client, TestCase from rest_framework.reverse import reverse @@ -8,6 +10,8 @@ from itim.viewsets.service import ViewSet +@pytest.mark.model_service +@pytest.mark.module_itim class ServiceViewsetList( ModelViewSetInheritedCases, TestCase, diff --git a/app/tests/fixtures/__init__.py b/app/tests/fixtures/__init__.py index fea371fa..573b406d 100644 --- a/app/tests/fixtures/__init__.py +++ b/app/tests/fixtures/__init__.py @@ -168,6 +168,11 @@ from .model_port import ( model_port, ) +from .model_service import ( + kwargs_service, + model_service, +) + from .model_software import ( kwargs_software, model_software, diff --git a/app/tests/fixtures/model_service.py b/app/tests/fixtures/model_service.py new file mode 100644 index 00000000..17b13304 --- /dev/null +++ b/app/tests/fixtures/model_service.py @@ -0,0 +1,26 @@ +import datetime +import pytest + +from itim.models.services import Service + + + +@pytest.fixture( scope = 'class') +def model_service(): + + yield Service + + +@pytest.fixture( scope = 'class') +def kwargs_service(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(), + 'name': 'service_' + random_str, + } + + yield kwargs.copy()