Skip to content

Commit 8aa27e6

Browse files
author
Ryan P Kilby
committed
Simplify 'Post' filtersets
1 parent a8d3574 commit 8aa27e6

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

tests/filters.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,10 @@ class Meta:
5757
model = Note
5858

5959

60-
class PostFilterWithRelated(FilterSet):
61-
note = RelatedFilter(NoteFilterWithRelatedAll, name='note')
62-
63-
class Meta:
64-
model = Post
65-
66-
67-
class PostFilterWithMethod(FilterSet):
60+
class PostFilter(FilterSet):
61+
# Used for Related filter and MethodFilter tests
6862
note = RelatedFilter(NoteFilterWithRelatedAll, name='note')
63+
date_published = filters.AllLookupsFilter()
6964
is_published = filters.MethodFilter()
7065

7166
class Meta:
@@ -92,23 +87,23 @@ def filter_is_published(self, name, qs, value):
9287

9388
class CoverFilterWithRelatedMethodFilter(FilterSet):
9489
comment = filters.CharFilter(name='comment')
95-
post = RelatedFilter(PostFilterWithMethod, name='post')
90+
post = RelatedFilter(PostFilter, name='post')
9691

9792
class Meta:
9893
model = Cover
9994

10095

10196
class CoverFilterWithRelated(FilterSet):
10297
comment = filters.CharFilter(name='comment')
103-
post = RelatedFilter(PostFilterWithRelated, name='post')
98+
post = RelatedFilter(PostFilter, name='post')
10499

105100
class Meta:
106101
model = Cover
107102

108103

109104
class PageFilterWithRelated(FilterSet):
110105
title = filters.CharFilter(name='title')
111-
previous_page = RelatedFilter(PostFilterWithRelated, name='previous_page')
106+
previous_page = RelatedFilter(PostFilter, name='previous_page')
112107

113108
class Meta:
114109
model = Page

tests/test_filterset.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
NoteFilterWithRelated,
2121
NoteFilterWithRelatedAll,
2222
NoteFilterWithRelatedAllDifferentFilterName,
23-
PostFilterWithRelated,
24-
PostFilterWithMethod,
23+
PostFilter,
2524
CoverFilterWithRelatedMethodFilter,
2625
CoverFilterWithRelated,
2726
# PageFilterWithRelated,
@@ -256,7 +255,7 @@ def test_double_relation_filter(self):
256255
GET = {
257256
'note__author__username__endswith': 'user2'
258257
}
259-
f = PostFilterWithRelated(GET, queryset=Post.objects.all())
258+
f = PostFilter(GET, queryset=Post.objects.all())
260259
self.assertEqual(len(list(f)), 1)
261260
post = list(f)[0]
262261
self.assertEqual(post.content, "Test content in post 3")
@@ -320,7 +319,7 @@ def test_get_filterset_subset(self):
320319
class MethodFilterTests(TestCase):
321320

322321
@classmethod
323-
def generateTestData(cls):
322+
def setUpTestData(cls):
324323
user = User.objects.create(username="user1", email="user1@example.org")
325324

326325
note1 = Note.objects.create(title="Test 1", content="Test content 1", author=user)
@@ -336,7 +335,7 @@ def test_method_filter(self):
336335
GET = {
337336
'is_published': 'true'
338337
}
339-
filterset = PostFilterWithMethod(GET, queryset=Post.objects.all())
338+
filterset = PostFilter(GET, queryset=Post.objects.all())
340339
results = list(filterset)
341340
self.assertEqual(len(results), 1)
342341
self.assertEqual(results[0].content, "Test content in post 2")

0 commit comments

Comments
 (0)