Hello,
I wrote an GetArchivedMixin that filters queryset based on value of GET parameter. I would like to test it without rellying on other parts of codebase - so with isolated test-only models, templates, urls and views. My ideal scenario is something like tests/generic_views in Django codebase.
How can I configure tests in this way? I would greatly appreciate if someone could point me to correct place in documentation for this or to some guide.
I tried creating tests/ directory inside app directory with test_GetArchivedMixin.py, models.py and empty __init__.py file. Running python manage.py test successfully found this test, but logically failed with django.db.utils.OperationalError: no such table: core_simplemodel.
test_GetArchivedMixin.py:
from typing import Any
from core.views.mixins import GetArchivedMixin
from django.http import HttpRequest, HttpResponse
from django.test import RequestFactory, TestCase
from django.views.generic import ListView
from .models import SimpleModel
class GetArchivedListView(ListView, GetArchivedMixin):
queryset = SimpleModel.objects.all()
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
return HttpResponse(status=200, content=self.get_queryset().all())
class GetArchivedViewTests(TestCase):
def setUp(self) -> None:
self.factory = RequestFactory()
@classmethod
def setUpTestData(cls) -> None:
cls.naziv1: SimpleModel = SimpleModel.objects.create(naziv="Naziv1")
cls.naziv1.is_active = False
cls.naziv1.save()
def test_all(self):
request = self.factory.get("")
response = GetArchivedListView.as_view()(request)
self.assertEqual(response.status_code, 200)
models.py
from core.models.generic import ArchiveableModel
from django.db import models
class SimpleModel(ArchiveableModel):
naziv = models.CharField(max_length=100)
Thank you and kind regards!