Skip to content

Commit 6917e2a

Browse files
committed
Fix legacy test compatibility and achieve 100% test coverage
Fixes for Python 3.8/Django 3.2 compatibility: - Replace assertQuerySetEqual with assertCountEqual in test_admin_filters.py (assertQuerySetEqual doesn't exist in older Django versions) Coverage improvements in test_dynamic_first.py: - Remove redundant conditional check in _parse_accept_language() - Add test_empty_accept_language() to cover empty Accept-Language header - Simplify test_unsupported_language_falls_back() to focus on zh-CN case Result: 100% test coverage across all test files, all environments pass.
1 parent 3b47112 commit 6917e2a

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

django_countries/tests/test_admin_filters.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,9 @@ def test_filter_country(self):
245245
cl.get_results(request)
246246
# Should return records containing NZ (excluding the one with only FR,AU)
247247
expected = models.MultiCountry.objects.filter(countries__contains="NZ")
248-
self.assertQuerySetEqual(
249-
cl.result_list,
250-
expected,
251-
ordered=False,
248+
self.assertCountEqual(
249+
list(cl.result_list),
250+
list(expected),
252251
)
253252

254253
def test_filter_different_country(self):
@@ -260,10 +259,9 @@ def test_filter_different_country(self):
260259
cl = ChangeList(request, **self.get_changelist_kwargs())
261260
cl.get_results(request)
262261
expected = models.MultiCountry.objects.filter(countries__contains="FR")
263-
self.assertQuerySetEqual(
264-
cl.result_list,
265-
expected,
266-
ordered=False,
262+
self.assertCountEqual(
263+
list(cl.result_list),
264+
list(expected),
267265
)
268266

269267
def _test_choices(self, selected_country_code="NZ"):

django_countries/tests/test_dynamic_first.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def _parse_accept_language(self, accept_language):
560560

561561
# Get the first (highest priority) language
562562
# Format: [(lang, priority), ...] sorted by priority
563-
return parsed[0][0].lower() if parsed else None
563+
return parsed[0][0].lower()
564564

565565
def test_australian_browser_auto_detect(self):
566566
"""Test that Australian browser (en-AU) gets Australia first."""
@@ -667,28 +667,39 @@ def test_base_language_without_country(self):
667667
self.assertEqual(country_list[0].code, "US")
668668
self.assertEqual(country_list[1].code, "GB")
669669

670+
def test_empty_accept_language(self):
671+
"""Test browser with empty Accept-Language header."""
672+
with self.settings(
673+
COUNTRIES_FIRST_AUTO_DETECT=True,
674+
COUNTRIES_FIRST=["US", "GB"],
675+
):
676+
# Simulate browser with no Accept-Language header
677+
language = self._parse_accept_language("")
678+
679+
# Should return None for empty header
680+
self.assertIsNone(language)
681+
682+
# Without a language, auto-detect can't work, falls back to default
683+
country_list = list(countries)
684+
self.assertEqual(country_list[0].code, "US")
685+
self.assertEqual(country_list[1].code, "GB")
686+
670687
def test_unsupported_language_falls_back(self):
671-
"""Test browser with unsupported language falls back to default."""
688+
"""Test browser with unsupported language still works."""
672689
with self.settings(
673690
COUNTRIES_FIRST_AUTO_DETECT=True,
674691
COUNTRIES_FIRST=["US"],
675-
LANGUAGE_CODE="en-us",
676692
):
677-
# Simulate browser sending unsupported language
693+
# Simulate browser sending zh-CN (which IS in Django's default LANGUAGES)
678694
language = self._parse_accept_language("zh-CN")
695+
# Django supports zh-hans, so zh-cn will be normalized
696+
self.assertIsNotNone(language)
679697

680-
# Should fall back to LANGUAGE_CODE since zh is not in LANGUAGES
681-
self.assertIn(language, ["en-us", "zh-cn"])
682-
683-
# Activate the language like Django's LocaleMiddleware would
698+
# Activate the language
684699
with translation.override(language):
685700
country_list = list(countries)
686-
687-
# If zh-cn is supported, CN should be first; otherwise US
688-
if language == "zh-cn":
689-
self.assertEqual(country_list[0].code, "CN")
690-
else:
691-
self.assertEqual(country_list[0].code, "US")
701+
# CN should be auto-detected from the locale
702+
self.assertEqual(country_list[0].code, "CN")
692703

693704

694705
# Define a view for middleware tests

0 commit comments

Comments
 (0)