-
-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathtest_admin.py
More file actions
66 lines (54 loc) · 2.59 KB
/
test_admin.py
File metadata and controls
66 lines (54 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from unittest.mock import Mock
from django.contrib.admin.views.main import ChangeList
from model_bakery import baker
from django.test import TestCase, RequestFactory
from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin
from sponsors.models import Sponsorship
class TestCustomSponsorshipStatusListFilter(TestCase):
def setUp(self):
self.request = RequestFactory().get("/")
self.model_admin = SponsorshipAdmin
self.filter = SponsorshipStatusListFilter(
request=self.request,
params={},
model=Sponsorship,
model_admin=self.model_admin
)
def test_basic_configuration(self):
self.assertEqual("status", self.filter.title)
self.assertEqual("status", self.filter.parameter_name)
self.assertIn(SponsorshipStatusListFilter, SponsorshipAdmin.list_filter)
def test_lookups(self):
expected = [
("applied", "Applied"),
("rejected", "Rejected"),
("approved", "Approved"),
("finalized", "Finalized"),
]
self.assertEqual(expected, self.filter.lookups(self.request, self.model_admin))
def test_filter_queryset(self):
sponsor = baker.make("sponsors.Sponsor")
sponsorships = [
baker.make(Sponsorship, status=Sponsorship.REJECTED, sponsor=sponsor),
baker.make(Sponsorship, status=Sponsorship.APPLIED, sponsor=sponsor),
baker.make(Sponsorship, status=Sponsorship.APPROVED, sponsor=sponsor),
baker.make(Sponsorship, status=Sponsorship.FINALIZED, sponsor=sponsor),
]
# filter by applied, approved and finalized status by default
qs = self.filter.queryset(self.request, Sponsorship.objects.all())
self.assertEqual(3, qs.count())
self.assertNotIn(sponsorships[0], qs)
for sp in sponsorships:
self.filter.used_parameters[self.filter.parameter_name] = sp.status
qs = self.filter.queryset(self.request, Sponsorship.objects.all())
self.assertEqual(1, qs.count())
self.assertIn(sp, qs)
def test_choices_with_custom_text_for_all(self):
lookups = self.filter.lookups(self.request, self.model_admin)
changelist = Mock(ChangeList, autospec=True)
changelist.add_facets = False
choices = self.filter.choices(changelist)
self.assertEqual(len(choices), len(lookups) + 1)
self.assertEqual(choices[0]["display"], "Applied / Approved / Finalized")
for i, choice in enumerate(choices[1:]):
self.assertEqual(choice["display"], lookups[i][1])