refactor(config_management): Model Unit Test Suite re-written to pytest for model ConfigGroup

ref: #908 closes #905
This commit is contained in:
2025-07-30 08:27:50 +09:30
parent 6f58f2985c
commit e3b9cba23c
3 changed files with 57 additions and 388 deletions

View File

@ -1,103 +0,0 @@
from django.shortcuts import reverse
from django.test import Client, TestCase
from core.tests.abstract.test_item_ticket_api_v2 import ItemTicketAPI
from core.models.ticket.ticket_linked_items import TicketLinkedItem
from config_management.models.groups import ConfigGroups
class ConfigGroupsTicketAPI(
ItemTicketAPI,
TestCase,
):
"""Test Cases for Item Tickets
Args:
APITenancyObject (class): Base class for ALL field checks
"""
item_type = TicketLinkedItem.Modules.CONFIG_GROUP
item_class = 'config_group'
item_model = ConfigGroups
@classmethod
def setUpTestData(self):
"""Setup Test
1. Create an organization for user and item
2. Create an item
"""
super().setUpTestData()
self.linked_item = self.item_model.objects.create(
organization = self.organization,
name = 'dev'
)
self.item = self.model.objects.create(
organization = self.organization,
ticket = self.ticket,
# Item attributes
item = self.linked_item.id,
item_type = self.item_type,
)
self.url_view_kwargs = {
'item_class': self.item_class,
'item_id': self.item.id,
'pk': self.item.id,
}
client = Client()
url = reverse('v2:_api_v2_item_tickets-detail', kwargs=self.url_view_kwargs)
client.force_login(self.view_user)
response = client.get(url)
self.api_data = response.data
def test_api_field_value_item_id(self):
""" Test for existance of API Field
item.id field must exist
"""
assert self.api_data['item']['id'] == self.linked_item.id
def test_api_field_value_item_type(self):
""" Test for type for API Field
item_type field must be int
"""
assert self.api_data['item_type'] == self.item_type
def test_api_field_type_item_url(self):
""" Test for type for API Field
item.url field must be str
"""
assert type(self.api_data['item']['url']) is str

View File

@ -1,196 +0,0 @@
import django
import pytest
import unittest
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import reverse
from django.test import Client, TestCase
from access.models.tenant import Tenant as Organization
from access.models.team import Team
from access.models.team_user import TeamUsers
from api.tests.abstract.api_fields import APITenancyObject
from config_management.models.groups import ConfigGroups
User = django.contrib.auth.get_user_model()
class ConfigGroupsAPI(
TestCase,
APITenancyObject
):
model = ConfigGroups
@classmethod
def setUpTestData(self):
"""Setup Test
1. Create an organization for user and item
2. Create an item
"""
self.organization = Organization.objects.create(name='test_org')
self.item = self.model.objects.create(
organization = self.organization,
name = 'one',
config = dict({"key": "one", "existing": "dont_over_write"})
)
self.second_item = self.model.objects.create(
organization = self.organization,
name = 'one_two',
model_notes = 'stuff',
config = dict({"key": "two"}),
parent = self.item
)
self.url_view_kwargs = {'pk': self.second_item.id}
view_permissions = Permission.objects.get(
codename = 'view_' + self.model._meta.model_name,
content_type = ContentType.objects.get(
app_label = self.model._meta.app_label,
model = self.model._meta.model_name,
)
)
view_team = Team.objects.create(
team_name = 'view_team',
organization = self.organization,
)
view_team.permissions.set([view_permissions])
self.view_user = User.objects.create_user(username="test_user_view", password="password")
teamuser = TeamUsers.objects.create(
team = view_team,
user = self.view_user
)
client = Client()
url = reverse('v2:_api_configgroups-detail', kwargs=self.url_view_kwargs)
client.force_login(self.view_user)
response = client.get(url)
self.api_data = response.data
def test_api_field_exists_config(self):
""" Test for existance of API Field
config field must exist
"""
assert 'config' in self.api_data
def test_api_field_type_config(self):
""" Test for type for API Field
config field must be dict
"""
assert type(self.api_data['config']) is dict
def test_api_field_exists_parent(self):
""" Test for existance of API Field
parent field must exist
"""
assert 'parent' in self.api_data
def test_api_field_type_parent(self):
""" Test for type for API Field
parent field must be dict
"""
assert type(self.api_data['parent']) is dict
def test_api_field_exists_parent_id(self):
""" Test for existance of API Field
parent.id field must exist
"""
assert 'id' in self.api_data['parent']
def test_api_field_type_parent_id(self):
""" Test for type for API Field
parent.id field must be int
"""
assert type(self.api_data['parent']['id']) is int
def test_api_field_exists_parent_display_name(self):
""" Test for existance of API Field
parent.display_name field must exist
"""
assert 'display_name' in self.api_data['parent']
def test_api_field_type_parent_display_name(self):
""" Test for type for API Field
parent.display_name field must be str
"""
assert type(self.api_data['parent']['display_name']) is str
def test_api_field_exists_parent_url(self):
""" Test for existance of API Field
parent.url field must exist
"""
assert 'url' in self.api_data['parent']
def test_api_field_type_parent_url(self):
""" Test for type for API Field
parent.url field must be str
"""
assert type(self.api_data['parent']['url']) is str
def test_api_field_exists_urls_tickets(self):
""" Test for existance of API Field
_urls.tickets field must exist
"""
assert 'tickets' in self.api_data['_urls']
def test_api_field_type_urls_tickets(self):
""" Test for type for API Field
_urls.tickets field must be str
"""
assert type(self.api_data['_urls']['tickets']) is str

View File

@ -2,42 +2,71 @@ import pytest
from django.db import models
from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import (
CenturionAbstractModelInheritedCases
)
# class Old:
# kwargs_item_create = {
# 'name': 'one',
# 'config': dict({"key": "one", "existing": "dont_over_write"})
# }
# model = ConfigGroups
# @classmethod
# def setUpTestData(self):
# """Setup Test
# 1. Create an organization for user and item
# 2. Create an item
# """
# self.organization = Organization.objects.create(name='test_org')
# super().setUpTestData()
@pytest.mark.model_configgroups
class ConfigGroupModelTestCases(
CenturionAbstractModelInheritedCases
):
# self.second_item = self.model.objects.create(
# organization = self.organization,
# name = 'one_two',
# config = dict({"key": "two"}),
# parent = self.item
# )
@property
def parameterized_class_attributes(self):
return {
'model_tag': {
'type': str,
'value': 'config_group'
},
}
@property
def parameterized_model_fields(self):
return {
'parent': {
'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,
'max_length': 50,
'null': False,
'unique': False,
},
'config': {
'blank': True,
'default': models.fields.NOT_PROVIDED,
'field_type': models.JSONField,
'null': True,
'unique': False,
},
'hosts': {
'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,
},
}
# def test_config_groups_count_child_groups(self):
# """ Test function count_children """
@ -86,67 +115,6 @@ from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model impor
@pytest.mark.model_configgroups
class ConfigGroupModelTestCases(
CenturionAbstractModelInheritedCases
):
@property
def parameterized_class_attributes(self):
return {
'model_tag': {
'type': str,
'value': 'config_group'
},
}
@property
def parameterized_model_fields(self):
return {
'parent': {
'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,
'max_length': 50,
'null': False,
'unique': False,
},
'config': {
'blank': True,
'default': models.fields.NOT_PROVIDED,
'field_type': models.JSONField,
'null': True,
'unique': False,
},
'hosts': {
'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,
},
}
class ConfigGroupModelInheritedCases(
ConfigGroupModelTestCases,
):