Provides a declarative and powerful way to test ListModelView
ListModelViewTest
class ListModelViewTest(AbstractModelViewTest)
Provides a declarative and powerful way to test the list model view.
This class executes a matrix of test cases to validate the functionality of the ListModelView
,
assessing its behavior under various conditions using combinations of path parameters, headers,
and query parameters.
Each test method within this class is automatically attached to the test case when instantiated
as a class attribute on a ModelViewSetTestCase
subclass. The test method names are dynamically
generated based on the class attribute name. For example, if the ListModelViewTest
is used
to test a ListModelView
named list_departments_view
, the test methods will be named
test_list_departments_view__test_list_models_ok
,
test_list_departments_view__test_list_models_headers_unauthorized
, etc.
This naming convention ensures clear and consistent identification of test cases.
Attributes:
model_view
ListModelView - The list model view to be tested.model_viewset_test_case
ModelViewSetTestCase - The test case to which this test belongs.
Example:
- Let's say you defined a
ListModelView
like this:
# examples/views/department_views.py
from ninja_crud import views, viewsets
from examples.models import Department
from examples.schemas import DepartmentOut
class DepartmentViewSet(viewsets.ModelViewSet):
model = Department
list_departments_view = views.ListModelView(
output_schema=DepartmentOut
)
- You can test the
list_departments_view
like this:
# examples/tests/test_department_views.py
from ninja_crud import testing
from examples.views.department_views import DepartmentViewSet
class TestDepartmentViewSet(testing.viewsets.ModelViewSetTestCase):
model_viewset_class = DepartmentViewSet
base_path = "api/departments"
def setUpTestData(cls):
cls.department_1 = Department.objects.create(title="department-1")
cls.department_2 = Department.objects.create(title="department-2")
cls.department_3 = Department.objects.create(title="department-3")
test_list_departments_view = testing.views.ListModelViewTest(
query_parameters=testing.components.QueryParameters(
ok={"limit": 10, "offset": 0},
bad_request={"limit": -1, "offset": -1}
)
)
Notes:
The class attribute ListModelViewTest
should be named after the view being tested.
For example, if you are testing the list_departments_view
attribute of the
DepartmentViewSet
class, the class attribute should be named
test_list_departments_view
.
__init__
def __init__(
path_parameters: Optional[ArgOrCallable[PathParameters,
TestCaseType]] = None,
query_parameters: Optional[ArgOrCallable[QueryParameters,
TestCaseType]] = None,
headers: Optional[ArgOrCallable[Headers,
TestCaseType]] = None) -> None
Initializes the ListModelViewTest with path parameters, query parameters, and optional headers.
Arguments:
path_parameters
Optional[ArgOrCallable[PathParameters, TestCaseType]], optional - Path parameters for
the request. Can be a static object, a callable, or a property on the test case. Defaults to None.query_parameters
Optional[ArgOrCallable[QueryParameters, TestCaseType]], optional - Query parameters for
the request. Can be a static object, a callable, or a property on the test case. Defaults to None.headers
Optional[ArgOrCallable[Headers, TestCaseType]], optional - Headers for the
request. Can be a static object, a callable, or a property on the test case. Defaults to None.
on_successful_request
def on_successful_request(response: django.http.HttpResponse,
path_parameters: dict, query_parameters: dict,
headers: dict, payload: dict)
Callback method to handle the response for a successful request.
This method is called when the view returns a successful HTTP response. It verifies that
the response content matches the expected output, ensuring the correctness of the view's output.
The expected output is derived from the model instance specified in the path parameters.
Arguments:
response
django.http.HttpResponse - The HttpResponse object from the view.path_parameters
dict - The path parameters used in the request.query_parameters
dict - The query parameters used in the request.headers
dict - The headers used in the request.payload
dict - The payload sent with the request.
on_failed_request
def on_failed_request(response: django.http.HttpResponse,
path_parameters: dict, query_parameters: dict,
headers: dict, payload: dict)
Callback method to handle the response for a failed request.
This method is called when the view returns a failed HTTP response. It only verifies that
the response status code matches the expected status code, ensuring the correctness of the
view's error handling.
Arguments:
response
django.http.HttpResponse - The HttpResponse object from the view.path_parameters
dict - The path parameters used in the request.query_parameters
dict - The query parameters used in the request.headers
dict - The headers used in the request.payload
dict - The payload sent with the request.
test_list_models_ok
@django.test.tag("list")
def test_list_models_ok()
Tests the successful scenarios.
Executes subtests combining various ok
path parameters, ok
query parameters, and ok
headers to verify
the correct handling and response output under valid conditions. Each combination is tested as a subtest.
test_list_models_query_parameters_bad_request
@django.test.tag("list")
def test_list_models_query_parameters_bad_request()
Tests the bad request query parameter scenarios.
Executes subtests combining various ok
path parameters, bad_request
query parameters, and ok
headers to
verify the correct handling and response output under bad request conditions. Each combination of
parameters is tested.
test_list_models_headers_unauthorized
@django.test.tag("list")
def test_list_models_headers_unauthorized()
Tests the unauthorized headers scenarios.
Executes subtests combining various ok
path parameters, ok
query parameters, and unauthorized
headers to
verify the correct handling and response output under unauthorized conditions. Each combination of
parameters is tested.
test_list_models_headers_forbidden
@django.test.tag("list")
def test_list_models_headers_forbidden()
Tests the forbidden headers scenarios.
Executes subtests combining various ok
path parameters, ok
query parameters, and forbidden
headers to
verify the correct handling and response output under forbidden conditions. Each combination of
parameters is tested.
test_list_models_path_parameters_not_found
@django.test.tag("list")
def test_list_models_path_parameters_not_found()
Tests the not found path parameter scenarios.
Executes subtests combining various not_found
path parameters, ok
query parameters, and ok
headers to
verify the correct handling and response output under not found conditions. Each combination of
parameters is tested.