refactor(project_management): ViewSet Unit Test Suite re-written to Pytest for model ProjectMilestone

ref: #938 #935
This commit is contained in:
2025-08-04 14:58:34 +09:30
parent bb7cf9d462
commit 2d720032f6

View File

@ -1,54 +1,130 @@
import pytest
from django.test import Client, TestCase
from rest_framework.reverse import reverse
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
from project_management.models.projects import Project
from project_management.viewsets.project_milestone import ViewSet
from project_management.viewsets.project_milestone import (
ProjectMilestone,
ViewSet,
)
@pytest.mark.skip(reason = 'see #895, tests being refactored')
@pytest.mark.model_projectmilestone
@pytest.mark.module_project_management
class ProjectMilestoneViewsetList(
class ViewsetTestCases(
ModelViewSetInheritedCases,
TestCase,
):
viewset = ViewSet
route_name = 'v2:_api_projectmilestone'
@pytest.fixture( scope = 'function' )
def viewset(self):
return ViewSet
@classmethod
def setUpTestData(self):
"""Setup Test
1. make list request
"""
super().setUpTestData()
self.kwargs = {
'project_id': Project.objects.create(
organization = self.organization,
name = 'proj'
).id
@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': []
},
'model': {
'value': ProjectMilestone
},
'model_documentation': {
'type': type(None),
},
'queryset': {
'type': type(None),
},
'serializer_class': {
'type': type(None),
},
'search_fields': {
'value': [
'name',
'description'
]
},
'view_description': {
'value': 'Physical Devices'
},
'view_name': {
'type': type(None),
},
'view_serializer_name': {
'type': type(None),
}
}
client = Client()
url = reverse(
self.route_name + '-list',
kwargs = self.kwargs
)
def test_view_func_get_queryset_cache_result(self, viewset_mock_request,
model_kwargs
):
"""Viewset Test
client.force_login(self.view_user)
Ensure that the `get_queryset` function caches the result under
attribute `<viewset>.queryset`
"""
self.http_options_response_list = client.options(url)
view_set = viewset_mock_request
view_set.kwargs = { 'project_id': model_kwargs['project'].id }
assert view_set.queryset is None # Must be empty before init
q = view_set.get_queryset()
assert view_set.queryset is not None # Must not be empty after init
assert q == view_set.queryset
def test_view_func_get_queryset_cache_result_used(self, mocker, viewset, viewset_mock_request,
model_kwargs
):
"""Viewset Test
Ensure that the `get_queryset` function caches the result under
attribute `<viewset>.queryset`
"""
qs = mocker.spy(viewset_mock_request.model, 'objects')
view_set = viewset_mock_request
view_set.kwargs = { 'project_id': model_kwargs['project'].id }
view_set.get_queryset() # Initial QuerySet fetch/filter and cache
assert len(qs.method_calls) == 1 # one call to .all()
assert len(qs.mock_calls) == 3 # calls = .all(), all().filter()
view_set.get_queryset() # Use Cached results, dont re-fetch QuerySet
assert len(qs.method_calls) == 1
assert len(qs.mock_calls) == 3
class ProjectMilestoneViewsetInheritedCases(
ViewsetTestCases,
):
pass
@pytest.mark.module_project_management
class ProjectMilestoneViewsetPyTest(
ViewsetTestCases,
):
pass