Skip to content

Commit 9c54ba9

Browse files
PiDelportmichaeljohnbarr
authored andcommitted
Fix choices attribute Django check for empty values (#11)
* Add tests for choices attribute check * Fix handling of empty choices values This was accidentally returning the warning without paramaters filled in for empty values, instead of actually skipping them.
1 parent 9954f47 commit 9c54ba9

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

tests/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ class LocationTimeZoneChoices(models.Model):
8484
)
8585

8686

87+
class LocationTimeZoneChoicesWithEmpty(models.Model):
88+
timezone = TimeZoneField(
89+
verbose_name=_('timezone'),
90+
max_length=64,
91+
null=True,
92+
blank=True,
93+
choices=[('', 'No time zone')] + list(PRETTY_ALL_TIMEZONES_CHOICES),
94+
)
95+
96+
97+
class LocationTimeZoneBadChoices(models.Model):
98+
timezone = TimeZoneField(
99+
verbose_name=_('timezone'),
100+
max_length=64,
101+
null=True,
102+
blank=True,
103+
choices=[('Bad/Worse', 'Bad Choice')],
104+
)
105+
106+
87107
class TZWithGoodStringDefault(models.Model):
88108
"""Test should validate that"""
89109
timezone = TimeZoneField(

tests/test_choices.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import re
99

1010
# Django
11+
from django.core import checks
1112
from django.test import TestCase
1213

1314
# App
15+
from tests import models
1416
from timezone_utils.choices import (ALL_TIMEZONES_CHOICES,
1517
COMMON_TIMEZONES_CHOICES,
1618
GROUPED_ALL_TIMEZONES_CHOICES,
@@ -170,3 +172,21 @@ def test_get_choices_values_ungrouped(self):
170172
values = map(itemgetter(0), choices)
171173
for value in values:
172174
self.assertIn(value, pytz.all_timezones)
175+
176+
def test_check_choices_attribute_good(self):
177+
self.assertEqual(models.LocationTimeZoneChoices.check(), [])
178+
# Don't warn for empty values:
179+
self.assertEqual(models.LocationTimeZoneChoicesWithEmpty.check(), [])
180+
181+
def test_check_choices_attribute_bad(self):
182+
self.assertEqual(models.LocationTimeZoneBadChoices.check(), [
183+
checks.Warning(
184+
msg=(
185+
"'choices' contains an invalid time zone value 'Bad/Worse' "
186+
"which was not found as a supported time zone by pytz "
187+
"{version}.".format(version=pytz.__version__)
188+
),
189+
hint='Values must be found in pytz.all_timezones.',
190+
obj=models.LocationTimeZoneBadChoices._meta.get_field('timezone'),
191+
),
192+
])

timezone_utils/fields.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ def _check_choices_attribute(self): # pragma: no cover
195195
)
196196
})
197197

198-
# Return the warning
199-
return [
200-
checks.Warning(**warning_params)
201-
]
198+
# Return the warning
199+
return [
200+
checks.Warning(**warning_params)
201+
]
202+
202203
elif option_key not in pytz.all_timezones:
203204
# Make sure we don't raise this error on empty
204205
# values
@@ -211,10 +212,10 @@ def _check_choices_attribute(self): # pragma: no cover
211212
)
212213
})
213214

214-
# Return the warning
215-
return [
216-
checks.Warning(**warning_params)
217-
]
215+
# Return the warning
216+
return [
217+
checks.Warning(**warning_params)
218+
]
218219

219220
# When no error, return an empty list
220221
return []

0 commit comments

Comments
 (0)