refactor(assistance): ViewSet Unit Test Suite re-written to pytest for model KnowledgeBaseCategory

ref: #904 #901
This commit is contained in:
2025-07-29 12:21:07 +09:30
parent 4fc58c5ea1
commit 09c1acbb1f
5 changed files with 94 additions and 523 deletions

View File

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

View File

@ -1,481 +0,0 @@
import django
import pytest
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 rest_framework.relations import Hyperlink
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 assistance.models.knowledge_base_category import KnowledgeBaseCategory
User = django.contrib.auth.get_user_model()
class KnowledgeBaseCategoryAPI(
TestCase,
APITenancyObject
):
model = KnowledgeBaseCategory
app_namespace = 'v2'
url_name = '_api_v2_knowledge_base_category'
@classmethod
def setUpTestData(self):
"""Setup Test
1. Create the object
2. create view user
4. make api request
"""
organization = Organization.objects.create(name='test_org')
self.organization = organization
different_organization = Organization.objects.create(name='test_different_organization')
self.view_team = Team.objects.create(
organization=organization,
team_name = 'teamone',
model_notes = 'random note'
)
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,
)
)
self.view_team.permissions.set([view_permissions])
self.view_user = User.objects.create_user(username="test_user_view", password="password")
parent_category = self.model.objects.create(
name='cat parent',
target_user = self.view_user,
organization=organization,
)
self.item = self.model.objects.create(
name='cat',
model_notes='dsa',
target_user = self.view_user,
organization=organization,
parent_category = parent_category,
)
# self.item.target_teams.set([self.view_team])
self.url_view_kwargs = {'pk': self.item.id}
teamuser = TeamUsers.objects.create(
team = self.view_team,
user = self.view_user
)
organization.manager = self.view_user
organization.save()
client = Client()
url = reverse(self.app_namespace + ':' + self.url_name + '-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_model_notes(self):
# """ Test for existance of API Field
# model_notes field does not exist for KB articles
# """
# assert 'model_notes' not in self.api_data
# def test_api_field_type_model_notes(self):
# """ Test for type for API Field
# model_notes does not exist for KB articles
# """
# pass
def test_api_field_exists_parent_category(self):
""" Test for existance of API Field
parent_category field must exist
"""
assert 'parent_category' in self.api_data
def test_api_field_type_parent_category(self):
""" Test for type for API Field
parent_category field must be dict
"""
assert type(self.api_data['parent_category']) is dict
def test_api_field_exists_parent_category_id(self):
""" Test for existance of API Field
parent_category.id field must exist
"""
assert 'id' in self.api_data['parent_category']
def test_api_field_type_parent_category_id(self):
""" Test for type for API Field
parent_category.id field must be int
"""
assert type(self.api_data['parent_category']['id']) is int
def test_api_field_exists_parent_category_display_name(self):
""" Test for existance of API Field
parent_category.display_name field must exist
"""
assert 'display_name' in self.api_data['parent_category']
def test_api_field_type_parent_category_display_name(self):
""" Test for type for API Field
parent_category.display_name field must be int
"""
assert type(self.api_data['parent_category']['display_name']) is str
def test_api_field_exists_parent_category_url(self):
""" Test for existance of API Field
parent_category.url field must exist
"""
assert 'url' in self.api_data['parent_category']
def test_api_field_type_parent_category_url(self):
""" Test for type for API Field
parent_category.url field must be int
"""
assert type(self.api_data['parent_category']['url']) is str
# def test_api_field_exists_summary(self):
# """ Test for existance of API Field
# summary field must exist
# """
# assert 'summary' in self.api_data
# def test_api_field_type_summary(self):
# """ Test for type for API Field
# summary field must be str
# """
# assert type(self.api_data['summary']) is str
# def test_api_field_exists_content(self):
# """ Test for existance of API Field
# content field must exist
# """
# assert 'content' in self.api_data
# def test_api_field_type_summary(self):
# """ Test for type for API Field
# content field must be str
# """
# assert type(self.api_data['content']) is str
# def test_api_field_exists_release_date(self):
# """ Test for existance of API Field
# release_date field must exist
# """
# assert 'release_date' in self.api_data
# def test_api_field_type_release_date(self):
# """ Test for type for API Field
# release_date field must be str
# """
# assert type(self.api_data['release_date']) is str
# def test_api_field_exists_expiry_date(self):
# """ Test for existance of API Field
# expiry_date field must exist
# """
# assert 'expiry_date' in self.api_data
# def test_api_field_type_expiry_date(self):
# """ Test for type for API Field
# expiry_date field must be str
# """
# assert type(self.api_data['expiry_date']) is str
# def test_api_field_exists_public(self):
# """ Test for existance of API Field
# public field must exist
# """
# assert 'public' in self.api_data
# def test_api_field_type_public(self):
# """ Test for type for API Field
# public field must be bool
# """
# assert type(self.api_data['public']) is bool
def test_api_field_type_target_user(self):
""" Test for type for API Field
target_user field must be dict
"""
assert type(self.api_data['target_user']) is dict
def test_api_field_exists_target_user_id(self):
""" Test for existance of API Field
target_user.id field must exist
"""
assert 'id' in self.api_data['target_user']
def test_api_field_type_target_user_id(self):
""" Test for type for API Field
target_user.id field must be int
"""
assert type(self.api_data['target_user']['id']) is int
def test_api_field_exists_target_user_display_name(self):
""" Test for existance of API Field
target_user.display_name field must exist
"""
assert 'display_name' in self.api_data['target_user']
def test_api_field_type_target_user_display_name(self):
""" Test for type for API Field
target_user.display_name field must be int
"""
assert type(self.api_data['target_user']['display_name']) is str
def test_api_field_exists_target_user_url(self):
""" Test for existance of API Field
target_user.url field must exist
"""
assert 'url' in self.api_data['target_user']
def test_api_field_type_target_user_url(self):
""" Test for type for API Field
target_user.url field must be int
"""
assert type(self.api_data['target_user']['url']) is Hyperlink
# def test_api_field_type_responsible_user(self):
# """ Test for type for API Field
# responsible_user field must be dict
# """
# assert type(self.api_data['responsible_user']) is dict
# def test_api_field_exists_responsible_user_id(self):
# """ Test for existance of API Field
# responsible_user.id field must exist
# """
# assert 'id' in self.api_data['responsible_user']
# def test_api_field_type_responsible_user_id(self):
# """ Test for type for API Field
# responsible_user.id field must be int
# """
# assert type(self.api_data['responsible_user']['id']) is int
# def test_api_field_exists_responsible_user_display_name(self):
# """ Test for existance of API Field
# responsible_user.display_name field must exist
# """
# assert 'display_name' in self.api_data['responsible_user']
# def test_api_field_type_responsible_user_display_name(self):
# """ Test for type for API Field
# responsible_user.display_name field must be int
# """
# assert type(self.api_data['responsible_user']['display_name']) is str
# def test_api_field_exists_responsible_user_url(self):
# """ Test for existance of API Field
# responsible_user.url field must exist
# """
# assert 'url' in self.api_data['responsible_user']
# def test_api_field_type_responsible_user_url(self):
# """ Test for type for API Field
# responsible_user.url field must be Hyperlink
# """
# assert type(self.api_data['responsible_user']['url']) is Hyperlink
# def test_api_field_type_responsible_teams(self):
# """ Test for type for API Field
# responsible_teams field must be list
# """
# assert type(self.api_data['responsible_teams']) is list
# def test_api_field_exists_responsible_teams_id(self):
# """ Test for existance of API Field
# responsible_teams.id field must exist
# """
# assert 'id' in self.api_data['responsible_teams'][0]
# def test_api_field_type_responsible_teams_id(self):
# """ Test for type for API Field
# responsible_teams.id field must be int
# """
# assert type(self.api_data['responsible_teams'][0]['id']) is int
# def test_api_field_exists_responsible_teams_display_name(self):
# """ Test for existance of API Field
# responsible_teams.display_name field must exist
# """
# assert 'display_name' in self.api_data['responsible_teams'][0]
# def test_api_field_type_responsible_teams_display_name(self):
# """ Test for type for API Field
# responsible_teams.display_name field must be int
# """
# assert type(self.api_data['responsible_teams'][0]['display_name']) is str
# def test_api_field_exists_responsible_teams_url(self):
# """ Test for existance of API Field
# responsible_teams.url field must exist
# """
# assert 'url' in self.api_data['responsible_teams'][0]
# def test_api_field_type_responsible_teams_url(self):
# """ Test for type for API Field
# responsible_teams.url field must be str
# """
# assert type(self.api_data['responsible_teams'][0]['url']) is str

View File

@ -77,6 +77,7 @@ class KnowledgeBaseCategoryModelInheritedCases(
@pytest.mark.module_module_assistance
class KnowledgeBaseCategoryModelPyTest(
KnowledgeBaseCategoryModelTestCases,
):

View File

@ -0,0 +1,87 @@
import pytest
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
from assistance.viewsets.knowledge_base_category import (
KnowledgeBaseCategory,
ViewSet,
)
@pytest.mark.model_knowledgebasecategory
class ViewsetTestCases(
ModelViewSetInheritedCases,
):
@pytest.fixture( scope = 'function' )
def viewset(self):
return ViewSet
@property
def parameterized_class_attributes(self):
return {
'_model_documentation': {
'type': type(None),
},
'back_url': {
'type': type(None),
},
'documentation': {
'type': type(None),
'value': None
},
'filterset_fields': {
'value': [
'name',
'organization',
'parent_category',
'target_user',
'target_team'
]
},
'model': {
'value': KnowledgeBaseCategory
},
'model_documentation': {
'type': type(None),
},
'queryset': {
'type': type(None),
},
'serializer_class': {
'type': type(None),
},
'search_fields': {
'value': [
'name'
]
},
'view_description': {
'value': 'Settings, Knowledge Base Categories'
},
'view_name': {
'type': type(None),
},
'view_serializer_name': {
'type': type(None),
}
}
class knowledgebaseCategoryViewsetInheritedCases(
ViewsetTestCases,
):
pass
@pytest.mark.module_assistance
class knowledgebaseCategoryViewsetPyTest(
ViewsetTestCases,
):
pass

View File

@ -1,42 +0,0 @@
from django.test import Client, TestCase
from rest_framework.reverse import reverse
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
from assistance.viewsets.knowledge_base_category import ViewSet
@pytest.mark.skip(reason = 'see #895, tests being refactored')
class KnowledgeBaseViewsetList(
ModelViewSetInheritedCases,
TestCase,
):
viewset = ViewSet
route_name = 'v2:_api_v2_knowledge_base_category'
@classmethod
def setUpTestData(self):
"""Setup Test
1. make list request
"""
super().setUpTestData()
client = Client()
url = reverse(
self.route_name + '-list',
kwargs = self.kwargs
)
client.force_login(self.view_user)
self.http_options_response_list = client.options(url)