Skip to content

Commit

Permalink
Test CountryFilter.choices() with empty selection
Browse files Browse the repository at this point in the history
Add a regression test for the case where no country has been specified
in the filter parameters. There was already a test where a country was
given, but the issue in question only occurred when none was selected.
  • Loading branch information
foutrelis committed Apr 1, 2024
1 parent bae099f commit 7780268
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions django_countries/tests/test_admin_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.test import TestCase
from django.test.client import RequestFactory

from django_countries import filters
from django_countries import countries, filters
from django_countries.tests import models

test_site = admin.AdminSite(name="test-admin")
Expand Down Expand Up @@ -51,13 +51,26 @@ def test_filter_country(self):
list(cl.result_list), list(models.Person.objects.exclude(country="AU"))
)

def test_choices(self):
request = RequestFactory().get("/person/", data={"country": "NZ"})
def _test_choices(self, selected_country_code="NZ"):
request_params = {}
selected_country = "All"

if selected_country_code:
request_params["country"] = selected_country_code
selected_country = countries.name(selected_country_code)

request = RequestFactory().get("/person/", data=request_params)
request.user = AnonymousUser()
cl = ChangeList(request, **self.get_changelist_kwargs())
choices = list(cl.filter_specs[0].choices(cl))
self.assertEqual(
[c["display"] for c in choices], ["All", "Australia", "New Zealand"]
)
for choice in choices:
self.assertEqual(choice["selected"], choice["display"] == "New Zealand")
self.assertEqual(choice["selected"], choice["display"] == selected_country)

def test_choices(self):
return self._test_choices()

def test_choices_empty_selection(self):
return self._test_choices(selected_country_code=None)

0 comments on commit 7780268

Please sign in to comment.