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