Provides a declarative and powerful way to test CreateModelView

CreateModelViewTest

class CreateModelViewTest(AbstractModelViewTest)

Provides a declarative and powerful way to test the create model view.

This class executes a matrix of test cases to validate the functionality of the CreateModelView,
assessing its behavior under various conditions using combinations of path parameters, headers,
and payloads.

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 CreateModelViewTest is used
to test a CreateModelView named create_department_view, the test methods will be named
test_create_department_view__test_create_model_ok,
test_create_department_view__test_create_model_headers_unauthorized, etc.

This naming convention ensures clear and consistent identification of test cases.

Attributes:

  • model_view CreateModelView - The create model view to be tested.
  • model_viewset_test_case ModelViewSetTestCase - The test case to which this test belongs.

Example:

  1. Let's say you defined a CreateModelView like this:
# examples/views/department_views.py
from ninja_crud import views, viewsets

from examples.models import Department
from examples.schemas import DepartmentIn, DepartmentOut

class DepartmentViewSet(viewsets.ModelViewSet):
    model = Department

    create_department_view = views.CreateModelView(
        input_schema=DepartmentIn,
        output_schema=DepartmentOut
    )
  1. You can test the create_department_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")

    test_create_department_view = testing.views.CreateModelViewTest(
        payloads=lambda test_case: testing.components.Payloads(
            ok={"title": "department"},
            bad_request={"title": ""},
            conflict={"title": test_case.department_1.title}
        )
    )

Notes:

The class attribute CreateModelViewTest should be named after the view being tested.
For example, if you are testing the create_department_view attribute of the
DepartmentViewSet class, the class attribute should be named
test_create_department_view.

__init__

def __init__(
        payloads: ArgOrCallable[Payloads, TestCaseType],
        path_parameters: Optional[ArgOrCallable[PathParameters,
                                                TestCaseType]] = None,
        headers: Optional[ArgOrCallable[Headers,
                                        TestCaseType]] = None) -> None

Initializes the CreateModelViewTest with payloads, optional path parameters, and optional headers.

Arguments:

  • payloads ArgOrCallable[Payloads, TestCaseType] - Payloads for the request. Can be a
    static object, a callable, or a property on the test case.
  • 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.
  • 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_create_model_ok

@django.test.tag("create")
def test_create_model_ok()

Tests the successful scenarios.

Executes subtests combining various ok payloads, ok path parameters, and ok headers to verify
the correct handling and response output under valid conditions. Each combination is tested as a subtest.

test_create_model_payloads_bad_request

@django.test.tag("create")
def test_create_model_payloads_bad_request()

Tests the bad request payload scenarios.

Executes subtests combining various ok path parameters, bad_request payloads, and ok headers to
verify the correct handling and response output under bad request conditions. Each combination of
parameters is tested.

test_create_model_payloads_conflict

@django.test.tag("create")
def test_create_model_payloads_conflict()

Tests the conflict payload scenarios.

Executes subtests combining various ok path parameters, conflict payloads, and ok headers to
verify the correct handling and response output under conflict conditions. Each combination of
parameters is tested.

test_create_model_headers_unauthorized

@django.test.tag("create")
def test_create_model_headers_unauthorized()

Tests the unauthorized headers scenarios.

Executes subtests combining various ok path parameters, ok payloads, and unauthorized headers to
verify the correct handling and response output under unauthorized conditions. Each combination of
parameters is tested.

test_create_model_headers_forbidden

@django.test.tag("create")
def test_create_model_headers_forbidden()

Tests the forbidden headers scenarios.

Executes subtests combining various ok path parameters, ok payloads, and forbidden headers to
verify the correct handling and response output under forbidden conditions. Each combination of
parameters is tested.

test_create_model_path_parameters_not_found

@django.test.tag("create")
def test_create_model_path_parameters_not_found()

Tests the not found path parameter scenarios.

Executes subtests combining various not_found path parameters, ok payloads, and ok headers to
verify the correct handling and response output under not found conditions. Each combination of
parameters is tested.