From 93eb65316e52459a897f20c8f0ba10128cbbf3de Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 13 Jun 2025 12:17:41 +0930 Subject: [PATCH] refactor(itim): Update Test Suite for Cluster model ref: #824 #825 --- app/fixtures/fresh_db.sql | 14 ++- app/itim/tests/unit/cluster/conftest.py | 19 +++ .../tests/unit/cluster/test_cluster_api_v2.py | 3 +- .../test_cluster_item_ticket_api_v2.py | 3 + .../unit/cluster/test_unit_cluster_model.py | 109 ++++++++++++++++-- .../unit/cluster/test_unit_cluster_viewset.py | 6 +- app/itim/viewsets/index.py | 2 +- app/tests/fixtures/__init__.py | 5 + app/tests/fixtures/model_cluster.py | 26 +++++ 9 files changed, 169 insertions(+), 18 deletions(-) create mode 100644 app/itim/tests/unit/cluster/conftest.py create mode 100644 app/tests/fixtures/model_cluster.py diff --git a/app/fixtures/fresh_db.sql b/app/fixtures/fresh_db.sql index f28db715..e4fec274 100644 --- a/app/fixtures/fresh_db.sql +++ b/app/fixtures/fresh_db.sql @@ -18,7 +18,6 @@ CREATE TABLE IF NOT EXISTS "itim_cluster_devices" ("id" integer NOT NULL PRIMARY CREATE TABLE IF NOT EXISTS "itim_cluster_nodes" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "cluster_id" integer NOT NULL REFERENCES "itim_cluster" ("id") DEFERRABLE INITIALLY DEFERRED, "device_id" integer NOT NULL REFERENCES "itam_device" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "itim_service_dependent_service" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "from_service_id" integer NOT NULL REFERENCES "itim_service" ("id") DEFERRABLE INITIALLY DEFERRED, "to_service_id" integer NOT NULL REFERENCES "itim_service" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "itim_service_port" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "service_id" integer NOT NULL REFERENCES "itim_service" ("id") DEFERRABLE INITIALLY DEFERRED, "port_id" integer NOT NULL REFERENCES "itim_port" ("id") DEFERRABLE INITIALLY DEFERRED); -CREATE TABLE IF NOT EXISTS "itim_cluster" ("is_global" bool NOT NULL, "model_notes" text NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(50) NOT NULL, "slug" varchar(50) NOT NULL, "config" text NULL CHECK ((JSON_VALID("config") OR "config" IS NULL)), "created" datetime NOT NULL, "modified" datetime NOT NULL, "cluster_type_id" integer NULL REFERENCES "itim_clustertype" ("id") DEFERRABLE INITIALLY DEFERRED, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "parent_cluster_id" integer NULL REFERENCES "itim_cluster" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "itim_clustertype" ("is_global" bool NOT NULL, "model_notes" text NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(50) NOT NULL, "slug" varchar(50) NOT NULL, "created" datetime NOT NULL, "modified" datetime NOT NULL, "config" text NULL CHECK ((JSON_VALID("config") OR "config" IS NULL)), "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "itim_port" ("is_global" bool NOT NULL, "model_notes" text NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "number" integer NOT NULL, "description" varchar(80) NULL, "created" datetime NOT NULL, "modified" datetime NOT NULL, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "protocol" varchar(3) NOT NULL); CREATE TABLE IF NOT EXISTS "itim_service" ("is_global" bool NOT NULL, "model_notes" text NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "is_template" bool NOT NULL, "name" varchar(50) NOT NULL, "config" text NULL CHECK ((JSON_VALID("config") OR "config" IS NULL)), "config_key_variable" varchar(50) NULL, "created" datetime NOT NULL, "modified" datetime NOT NULL, "cluster_id" integer NULL REFERENCES "itim_cluster" ("id") DEFERRABLE INITIALLY DEFERRED, "device_id" integer NULL REFERENCES "itam_device" ("id") DEFERRABLE INITIALLY DEFERRED, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "template_id" integer NULL REFERENCES "itim_service" ("id") DEFERRABLE INITIALLY DEFERRED); @@ -112,8 +111,8 @@ 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 "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_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 "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 "access_company" ("entity_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "access_entity" ("id") DEFERRABLE INITIALLY DEFERRED, "name" varchar(80) NOT NULL); @@ -203,6 +202,9 @@ CREATE TABLE IF NOT EXISTS "itam_softwarecategory_centurionmodelnote" ("centurio CREATE TABLE IF NOT EXISTS "itam_softwareversion" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "name" varchar(50) NOT NULL, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "software_id" integer NOT NULL REFERENCES "itam_software" ("id") DEFERRABLE INITIALLY DEFERRED, "model_notes" text NULL); CREATE TABLE IF NOT EXISTS "itam_softwareversion_audithistory" ("centurionaudit_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_audithistory" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "itam_softwareversion" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "itam_softwareversion_centurionmodelnote" ("centurionmodelnote_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_centurionmodelnote" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "itam_softwareversion" ("id") DEFERRABLE INITIALLY DEFERRED); +CREATE TABLE IF NOT EXISTS "itim_cluster" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(50) NOT NULL, "config" text NULL CHECK ((JSON_VALID("config") OR "config" IS NULL)), "created" datetime NOT NULL, "modified" datetime NOT NULL, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "cluster_type_id" integer NULL REFERENCES "itim_clustertype" ("id") DEFERRABLE INITIALLY DEFERRED, "model_notes" text NULL, "parent_cluster_id" integer NULL REFERENCES "itim_cluster" ("id") DEFERRABLE INITIALLY DEFERRED); +CREATE TABLE IF NOT EXISTS "itim_cluster_audithistory" ("centurionaudit_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_audithistory" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "itim_cluster" ("id") DEFERRABLE INITIALLY DEFERRED); +CREATE TABLE IF NOT EXISTS "itim_cluster_centurionmodelnote" ("centurionmodelnote_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_centurionmodelnote" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "itim_cluster" ("id") DEFERRABLE INITIALLY DEFERRED); CREATE TABLE IF NOT EXISTS "django_session" ("session_key" varchar(40) NOT NULL PRIMARY KEY, "session_data" text NOT NULL, "expire_date" datetime NOT NULL); CREATE TABLE IF NOT EXISTS "social_auth_association" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "server_url" varchar(255) NOT NULL, "handle" varchar(255) NOT NULL, "secret" varchar(255) NOT NULL, "issued" integer NOT NULL, "lifetime" integer NOT NULL, "assoc_type" varchar(64) NOT NULL); CREATE TABLE IF NOT EXISTS "social_auth_code" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "email" varchar(254) NOT NULL, "code" varchar(32) NOT NULL, "verified" bool NOT NULL, "timestamp" datetime NOT NULL); @@ -210,16 +212,15 @@ CREATE TABLE IF NOT EXISTS "social_auth_nonce" ("id" integer NOT NULL PRIMARY KE CREATE TABLE IF NOT EXISTS "social_auth_usersocialauth" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "provider" varchar(32) NOT NULL, "uid" varchar(255) NOT NULL, "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "created" datetime NOT NULL, "modified" datetime NOT NULL, "extra_data" text NOT NULL CHECK ((JSON_VALID("extra_data") OR "extra_data" IS NULL))); CREATE TABLE IF NOT EXISTS "social_auth_partial" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "token" varchar(32) NOT NULL, "next_step" smallint unsigned NOT NULL CHECK ("next_step" >= 0), "backend" varchar(32) NOT NULL, "timestamp" datetime NOT NULL, "data" text NOT NULL CHECK ((JSON_VALID("data") OR "data" IS NULL))); DELETE FROM sqlite_sequence; -INSERT INTO sqlite_sequence VALUES('django_migrations',206); -INSERT INTO sqlite_sequence VALUES('django_content_type',189); -INSERT INTO sqlite_sequence VALUES('auth_permission',801); +INSERT INTO sqlite_sequence VALUES('django_migrations',207); +INSERT INTO sqlite_sequence VALUES('django_content_type',191); +INSERT INTO sqlite_sequence VALUES('auth_permission',809); INSERT INTO sqlite_sequence VALUES('auth_group',0); INSERT INTO sqlite_sequence VALUES('auth_user',0); INSERT INTO sqlite_sequence VALUES('project_management_project',0); INSERT INTO sqlite_sequence VALUES('project_management_projectmilestone',0); INSERT INTO sqlite_sequence VALUES('project_management_projectstate',0); INSERT INTO sqlite_sequence VALUES('project_management_projecttype',0); -INSERT INTO sqlite_sequence VALUES('itim_cluster',0); INSERT INTO sqlite_sequence VALUES('itim_clustertype',0); INSERT INTO sqlite_sequence VALUES('itim_port',0); INSERT INTO sqlite_sequence VALUES('itim_service',0); @@ -264,6 +265,7 @@ INSERT INTO sqlite_sequence VALUES('itam_operatingsystemversion',0); INSERT INTO sqlite_sequence VALUES('itam_software',0); INSERT INTO sqlite_sequence VALUES('itam_softwarecategory',0); INSERT INTO sqlite_sequence VALUES('itam_softwareversion',0); +INSERT INTO sqlite_sequence VALUES('itim_cluster',0); INSERT INTO sqlite_sequence VALUES('social_auth_association',0); INSERT INTO sqlite_sequence VALUES('social_auth_code',0); INSERT INTO sqlite_sequence VALUES('social_auth_nonce',0); diff --git a/app/itim/tests/unit/cluster/conftest.py b/app/itim/tests/unit/cluster/conftest.py new file mode 100644 index 00000000..09e96c4d --- /dev/null +++ b/app/itim/tests/unit/cluster/conftest.py @@ -0,0 +1,19 @@ +import pytest + + + +@pytest.fixture( scope = 'class') +def model(model_cluster): + + yield model_cluster + + +@pytest.fixture( scope = 'class', autouse = True) +def model_kwargs(request, kwargs_cluster): + + request.cls.kwargs_create_item = kwargs_cluster.copy() + + yield kwargs_cluster.copy() + + if hasattr(request.cls, 'kwargs_create_item'): + del request.cls.kwargs_create_item diff --git a/app/itim/tests/unit/cluster/test_cluster_api_v2.py b/app/itim/tests/unit/cluster/test_cluster_api_v2.py index b94a8f95..b2b5a35c 100644 --- a/app/itim/tests/unit/cluster/test_cluster_api_v2.py +++ b/app/itim/tests/unit/cluster/test_cluster_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 @@ -20,6 +19,8 @@ from itim.models.clusters import Cluster, ClusterType +@pytest.mark.model_cluster +@pytest.mark.module_itim class ClusterAPI( TestCase, APITenancyObject diff --git a/app/itim/tests/unit/cluster/test_cluster_item_ticket_api_v2.py b/app/itim/tests/unit/cluster/test_cluster_item_ticket_api_v2.py index 2b1adb6c..957a7c13 100644 --- a/app/itim/tests/unit/cluster/test_cluster_item_ticket_api_v2.py +++ b/app/itim/tests/unit/cluster/test_cluster_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.clusters import Cluster +@pytest.mark.model_cluster +@pytest.mark.module_itim class ClusterItemTicketAPI( ItemTicketAPI, TestCase, 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 b98d6bb5..d0313a2c 100644 --- a/app/itim/tests/unit/cluster/test_unit_cluster_model.py +++ b/app/itim/tests/unit/cluster/test_unit_cluster_model.py @@ -1,16 +1,107 @@ -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.clusters import Cluster - -class ClusterModel( - TenancyObjectInheritedCases, - TestCase, +@pytest.mark.model_cluster +class ClusterModelTestCases( + CenturionAbstractModelInheritedCases ): - model = Cluster + + @property + def parameterized_class_attributes(self): + + return { + 'model_tag': { + 'type': str, + 'value': 'cluster' + }, + } + + + @property + def parameterized_model_fields(self): + + return { + 'parent_cluster': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ForeignKey, + 'null': True, + 'unique': False, + }, + 'cluster_type': { + '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.IntegerField, + 'length': 50, + 'null': False, + 'unique': False, + }, + 'config': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.JSONField, + 'null': True, + 'unique': False, + }, + 'nodes': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ManyToManyField, + 'null': False, + 'unique': False, + }, + 'devices': { + 'blank': True, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.ManyToManyField, + 'null': False, + 'unique': False, + }, + 'modified': { + 'blank': False, + 'default': models.fields.NOT_PROVIDED, + 'field_type': models.DateTimeField, + 'null': False, + 'unique': False, + }, + } + + + @pytest.mark.skip(reason="to be written") + def test_cluster_move_organization(user): + """Move Organization test + + When a cluster moves organization, clustersoftware and clustersoftware table data + must also move organizations + """ + pass + + + +class ClusterModelInheritedCases( + ClusterModelTestCases, +): + pass + + +@pytest.mark.module_itim +class ClusterModelPyTest( + ClusterModelTestCases, +): + pass diff --git a/app/itim/tests/unit/cluster/test_unit_cluster_viewset.py b/app/itim/tests/unit/cluster/test_unit_cluster_viewset.py index 09e206d9..6953df90 100644 --- a/app/itim/tests/unit/cluster/test_unit_cluster_viewset.py +++ b/app/itim/tests/unit/cluster/test_unit_cluster_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.cluster import ViewSet +@pytest.mark.model_cluster +@pytest.mark.module_itim class ClusterViewsetList( ModelViewSetInheritedCases, TestCase, @@ -29,7 +33,7 @@ class ClusterViewsetList( client = Client() - + url = reverse( self.route_name + '-list', kwargs = self.kwargs diff --git a/app/itim/viewsets/index.py b/app/itim/viewsets/index.py index 58e33fdc..ba461fb7 100644 --- a/app/itim/viewsets/index.py +++ b/app/itim/viewsets/index.py @@ -26,7 +26,7 @@ class Index(IndexViewset): return Response( { "change": reverse('v2:_api_v2_ticket_change-list', request=request), - "cluster": reverse('v2:_api_v2_cluster-list', request=request), + "cluster": reverse('v2:_api_cluster-list', request=request), "incident": reverse('v2:_api_v2_ticket_incident-list', request=request), "problem": reverse('v2:_api_v2_ticket_problem-list', request=request), "service": reverse('v2:_api_v2_service-list', request=request), diff --git a/app/tests/fixtures/__init__.py b/app/tests/fixtures/__init__.py index ed7d1970..c1b529c8 100644 --- a/app/tests/fixtures/__init__.py +++ b/app/tests/fixtures/__init__.py @@ -43,6 +43,11 @@ from .model_checkin import ( model_checkin, ) +from .model_cluster import ( + kwargs_cluster, + model_cluster, +) + from .model_configgroup import ( kwargs_configgroups, model_configgroups, diff --git a/app/tests/fixtures/model_cluster.py b/app/tests/fixtures/model_cluster.py new file mode 100644 index 00000000..61a869bc --- /dev/null +++ b/app/tests/fixtures/model_cluster.py @@ -0,0 +1,26 @@ +import datetime +import pytest + +from itim.models.clusters import Cluster + + + +@pytest.fixture( scope = 'class') +def model_cluster(): + + yield Cluster + + +@pytest.fixture( scope = 'class') +def kwargs_cluster(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': 'cluster_' + random_str, + } + + yield kwargs.copy()