refactor(itim): Serializer Unit Test Suite re-written to Pytest for model ClusterType

ref: #932 #929
This commit is contained in:
2025-08-03 11:54:44 +09:30
parent 173a47d942
commit a3f4299299
5 changed files with 130 additions and 53 deletions

View File

@ -1,53 +0,0 @@
import pytest
from django.test import TestCase
from rest_framework.exceptions import ValidationError
from access.models.tenant import Tenant as Organization
from itim.serializers.cluster_type import ClusterType, ClusterTypeModelSerializer
class ClusterTypeValidationAPI(
TestCase,
):
model = ClusterType
@classmethod
def setUpTestData(self):
"""Setup Test
1. Create an org
2. Create an item
"""
organization = Organization.objects.create(name='test_org')
self.organization = organization
self.item = self.model.objects.create(
organization=organization,
name = 'os name',
)
def test_serializer_validation_no_name(self):
"""Serializer Validation Check
Ensure that if creating and no name is provided a validation error occurs
"""
with pytest.raises(ValidationError) as err:
serializer = ClusterTypeModelSerializer(data={
"organization": self.organization.id,
})
serializer.is_valid(raise_exception = True)
assert err.value.get_codes()['name'][0] == 'required'

View File

@ -17,3 +17,9 @@ def model_kwargs(request, kwargs_clustertype):
if hasattr(request.cls, 'kwargs_create_item'):
del request.cls.kwargs_create_item
@pytest.fixture( scope = 'class')
def model_serializer(serializer_clustertype):
yield serializer_clustertype

View File

@ -0,0 +1,107 @@
import pytest
from django.db import models
from rest_framework.exceptions import ValidationError
from api.tests.unit.test_unit_serializer import (
SerializerTestCases
)
from centurion.tests.abstract.mock_view import MockView
@pytest.mark.model_clustertype
class ClusterTypeSerializerTestCases(
SerializerTestCases
):
@pytest.fixture( scope = 'function' )
def created_model(self, django_db_blocker, model, model_kwargs):
with django_db_blocker.unblock():
kwargs_many_to_many = {}
kwargs = {}
for key, value in model_kwargs.items():
field = model._meta.get_field(key)
if isinstance(field, models.ManyToManyField):
kwargs_many_to_many.update({
key: value
})
else:
kwargs.update({
key: value
})
item = model.objects.create( **kwargs )
for key, value in kwargs_many_to_many.items():
field = getattr(item, key)
for entry in value:
field.add(entry)
yield item
item.delete()
def test_serializer_validation_no_name(self,
kwargs_api_create, model, model_serializer, request_user
):
"""Serializer Validation Check
Ensure that if creating and no name is provided a validation error occurs
"""
mock_view = MockView(
user = request_user,
model = model,
action = 'create',
)
kwargs = kwargs_api_create.copy()
del kwargs['name']
with pytest.raises(ValidationError) as err:
serializer = model_serializer['model'](
context = {
'request': mock_view.request,
'view': mock_view,
},
data = kwargs,
)
serializer.is_valid(raise_exception = True)
assert err.value.get_codes()['name'][0] == 'required'
class ClusterTypeSerializerInheritedCases(
ClusterTypeSerializerTestCases
):
pass
@pytest.mark.module_itim
class ClusterTypeSerializerPyTest(
ClusterTypeSerializerTestCases
):
pass

View File

@ -67,6 +67,7 @@ from .model_cluster import (
from .model_clustertype import (
kwargs_clustertype,
model_clustertype,
serializer_clustertype,
)
from .model_company import (

View File

@ -2,6 +2,11 @@ import datetime
import pytest
from itim.models.clusters import ClusterType
from itim.serializers.cluster_type import (
ClusterTypeBaseSerializer,
ClusterTypeModelSerializer,
ClusterTypeViewSerializer
)
@ -21,6 +26,17 @@ def kwargs_clustertype(kwargs_centurionmodel):
kwargs = {
**kwargs_centurionmodel.copy(),
'name': 'clustertype_' + random_str,
'config': { 'config_key_1': 'config_value_1' }
}
yield kwargs.copy()
@pytest.fixture( scope = 'class')
def serializer_clustertype():
yield {
'base': ClusterTypeBaseSerializer,
'model': ClusterTypeModelSerializer,
'view': ClusterTypeViewSerializer
}