Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Add filterset integraiton tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slavov-v committed Jul 24, 2019
1 parent df7a109 commit 51656f9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django_enum_choices/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def __init__(self, enum_class, choice_builder=value_value, *args, **kwargs):


class EnumChoiceFilterSetMixin:
"""
`django-filter` has specific logic for handling fields with `choices`.
We need to override `filter_for_lookup` to return an `EnumChoiceFilter`
before `django-filter` returns a `ChoiceFilter` as the `filter_class`
for the `EnumChoiceField` instances in the model.
"""

@classmethod
def filter_for_lookup(cls, field, lookup_type):
if isinstance(field, EnumChoiceField):
Expand Down
83 changes: 83 additions & 0 deletions django_enum_choices/tests/test_filterset_integrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from django.test import TestCase

import django_filters as filters

from django_enum_choices.filters import EnumChoiceFilter, EnumChoiceFilterSetMixin

from .testapp.enumerations import CharTestEnum
from .testapp.models import (
StringEnumeratedModel,
CustomChoiceBuilderEnumeratedModel,
custom_choice_builder
)


class FilterSetIntegrationTests(TestCase):
class ExplicitFilterSet(filters.FilterSet):
enumeration = EnumChoiceFilter(CharTestEnum)

class ExplicitChoiceBuilderFilterSet(filters.FilterSet):
enumeration = EnumChoiceFilter(CharTestEnum, choice_builder=custom_choice_builder)

class ImplicitFilterSet(EnumChoiceFilterSetMixin, filters.FilterSet):
class Meta:
model = StringEnumeratedModel
fields = ['enumeration']

class ImplicitChoiceBuilderFilterSet(EnumChoiceFilterSetMixin, filters.FilterSet):
class Meta:
model = CustomChoiceBuilderEnumeratedModel
fields = ['enumeration']

def setUp(self):
for choice in CharTestEnum:
StringEnumeratedModel.objects.create(
enumeration=choice
)

def test_explicitly_declarated_field_filters_correctly(self):
filters = {
'enumeration': 'first'
}
filterset = self.ExplicitFilterSet(filters, StringEnumeratedModel.objects.all())

self.assertEqual(filterset.qs.count(), 1)
self.assertEqual(filterset.qs.first().enumeration, CharTestEnum.FIRST)

def test_explicitly_declarated_field_filters_correctly_with_custom_choice_builder(self):
filters = {
'enumeration': 'Custom_first'
}
filterset = self.ExplicitChoiceBuilderFilterSet(filters, StringEnumeratedModel.objects.all())

self.assertEqual(filterset.qs.count(), 1)
self.assertEqual(filterset.qs.first().enumeration, CharTestEnum.FIRST)

def test_filter_by_non_valid_choice_returns_full_queryset(self):
filters = {
'enumeration': 'invalid'
}
filterset = self.ExplicitChoiceBuilderFilterSet(filters, StringEnumeratedModel.objects.all())

self.assertEqual(filterset.qs.count(), StringEnumeratedModel.objects.count())

def test_implicit_filter_filters_correctly(self):
filters = {
'enumeration': 'first'
}
filterset = self.ImplicitFilterSet(filters)

self.assertEqual(filterset.qs.count(), 1)
self.assertEqual(filterset.qs.first().enumeration, CharTestEnum.FIRST)

def test_implicit_filter_filters_correctly_on_field_with_custom_choice_builder(self):
for choice in CharTestEnum:
CustomChoiceBuilderEnumeratedModel.objects.create(enumeration=choice)

filters = {
'enumeration': 'Custom_first'
}
filterset = self.ImplicitChoiceBuilderFilterSet(filters)

self.assertEqual(filterset.qs.count(), 1)
self.assertEqual(filterset.qs.first().enumeration, CharTestEnum.FIRST)

0 comments on commit 51656f9

Please sign in to comment.