Skip to content

Commit

Permalink
tests: add tests for filterset_factory
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jan 24, 2024
1 parent b590408 commit e8a5d1f
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
NumberFilter,
UUIDFilter,
)
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS, FilterSet
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS, FilterSet, filterset_factory
from django_filters.widgets import BooleanWidget

from .models import (
Expand Down Expand Up @@ -52,10 +52,6 @@ def test_get_declared_filters(self):
def test_filters_for_model(self):
pass

@unittest.skip("todo")
def test_filterset_factory(self):
pass


class DbFieldDefaultFiltersTests(TestCase):
def test_expected_db_fields_get_filters(self):
Expand Down Expand Up @@ -715,6 +711,55 @@ class F(A, B):
"f5": CharFilter,
}

def test_filterset_factory(self):
filterset = filterset_factory(Article)
self.assertEqual(list(filterset.base_filters), ["name", "published", "author"])

def test_filterset_factory_fields(self):
filterset = filterset_factory(Article, fields=["name"])
self.assertEqual(list(filterset.base_filters), ["name"])

def test_filterset_factory_base_filter(self):
class FilterSetBase(FilterSet):
f1 = CharFilter()
f2 = CharFilter()

filterset = filterset_factory(Article, filterset=FilterSetBase)
self.assertEqual(list(filterset.base_filters), ["name", "published", "author", "f1", "f2"])

def test_filterset_factory_base_filter_meta_fields(self):
class FilterSetBase(FilterSet):
class Meta:
fields = ["name"]
f1 = CharFilter()
f2 = CharFilter()

filterset = filterset_factory(Article, filterset=FilterSetBase)
self.assertEqual(list(filterset.base_filters), ["name", "published", "author", "f1", "f2"])

def test_filtesret_factory_base_filter_meta_inheritance_filter_overrides(self):
class FilterSetBase(FilterSet):
class Meta:
filter_overrides = {
models.CharField: {
"filter_class": BooleanFilter,
},
}

filterset = filterset_factory(Article, FilterSetBase)

f = Article._meta.get_field("author")
result, params = filterset.filter_for_lookup(f, "isnull")
self.assertEqual(result, BooleanFilter)

def test_filtesret_factory_base_filter_meta_inheritance_exclude(self):
class FilterSetBase(FilterSet):
class Meta:
exclude = ["published"]

filterset = filterset_factory(Article, FilterSetBase)
self.assertEqual(list(filterset.base_filters), ["name", "author"])


class FilterSetInstantiationTests(TestCase):
class F(FilterSet):
Expand Down

0 comments on commit e8a5d1f

Please sign in to comment.