| 1 |
import collections.abc |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
from django.db.models import TextChoices |
| 5 |
from django.test import SimpleTestCase |
| 6 |
from django.utils.choices import ( |
| 7 |
BaseChoiceIterator, |
| 8 |
CallableChoiceIterator, |
| 9 |
flatten_choices, |
| 10 |
normalize_choices, |
| 11 |
) |
| 12 |
from django.utils.translation import gettext_lazy as _ |
| 13 |
|
| 14 |
|
| 15 |
class SimpleChoiceIterator(BaseChoiceIterator): |
| 16 |
def __iter__(self): |
| 17 |
return ((i, f"Item #{i}") for i in range(1, 4)) |
| 18 |
|
| 19 |
|
| 20 |
class ChoiceIteratorTests(SimpleTestCase): |
| 21 |
def test_not_implemented_error_on_missing_iter(self): |
| 22 |
class InvalidChoiceIterator(BaseChoiceIterator): |
| 23 |
pass # Not overriding __iter__(). |
| 24 |
|
| 25 |
msg = "BaseChoiceIterator subclasses must implement __iter__()." |
| 26 |
with self.assertRaisesMessage(NotImplementedError, msg): |
| 27 |
iter(InvalidChoiceIterator()) |
| 28 |
|
| 29 |
def test_eq(self): |
| 30 |
unrolled = [(1, "Item #1"), (2, "Item #2"), (3, "Item #3")] |
| 31 |
self.assertEqual(SimpleChoiceIterator(), unrolled) |
| 32 |
self.assertEqual(unrolled, SimpleChoiceIterator()) |
| 33 |
|
| 34 |
def test_eq_instances(self): |
| 35 |
self.assertEqual(SimpleChoiceIterator(), SimpleChoiceIterator()) |
| 36 |
|
| 37 |
def test_not_equal_subset(self): |
| 38 |
self.assertNotEqual(SimpleChoiceIterator(), [(1, "Item #1"), (2, "Item #2")]) |
| 39 |
|
| 40 |
def test_not_equal_superset(self): |
| 41 |
self.assertNotEqual( |
| 42 |
SimpleChoiceIterator(), |
| 43 |
[(1, "Item #1"), (2, "Item #2"), (3, "Item #3"), None], |
| 44 |
) |
| 45 |
|
| 46 |
def test_getitem(self): |
| 47 |
choices = SimpleChoiceIterator() |
| 48 |
for i, expected in [(0, (1, "Item #1")), (-1, (3, "Item #3"))]: |
| 49 |
with self.subTest(index=i): |
| 50 |
self.assertEqual(choices[i], expected) |
| 51 |
|
| 52 |
def test_getitem_indexerror(self): |
| 53 |
choices = SimpleChoiceIterator() |
| 54 |
for i in (4, -4): |
| 55 |
with self.subTest(index=i): |
| 56 |
with self.assertRaises(IndexError) as ctx: |
| 57 |
choices[i] |
| 58 |
self.assertTrue(str(ctx.exception).endswith("index out of range")) |
| 59 |
|
| 60 |
|
| 61 |
class FlattenChoicesTests(SimpleTestCase): |
| 62 |
def test_empty(self): |
| 63 |
def generator(): |
| 64 |
yield from () |
| 65 |
|
| 66 |
for choices in ({}, [], (), set(), frozenset(), generator(), None, ""): |
| 67 |
with self.subTest(choices=choices): |
| 68 |
result = flatten_choices(choices) |
| 69 |
self.assertIsInstance(result, collections.abc.Generator) |
| 70 |
self.assertEqual(list(result), []) |
| 71 |
|
| 72 |
def test_non_empty(self): |
| 73 |
choices = [ |
| 74 |
("C", _("Club")), |
| 75 |
("D", _("Diamond")), |
| 76 |
("H", _("Heart")), |
| 77 |
("S", _("Spade")), |
| 78 |
] |
| 79 |
result = flatten_choices(choices) |
| 80 |
self.assertIsInstance(result, collections.abc.Generator) |
| 81 |
self.assertEqual(list(result), choices) |
| 82 |
|
| 83 |
def test_nested_choices(self): |
| 84 |
choices = [ |
| 85 |
("Audio", [("vinyl", _("Vinyl")), ("cd", _("CD"))]), |
| 86 |
("Video", [("vhs", _("VHS Tape")), ("dvd", _("DVD"))]), |
| 87 |
("unknown", _("Unknown")), |
| 88 |
] |
| 89 |
expected = [ |
| 90 |
("vinyl", _("Vinyl")), |
| 91 |
("cd", _("CD")), |
| 92 |
("vhs", _("VHS Tape")), |
| 93 |
("dvd", _("DVD")), |
| 94 |
("unknown", _("Unknown")), |
| 95 |
] |
| 96 |
result = flatten_choices(choices) |
| 97 |
self.assertIsInstance(result, collections.abc.Generator) |
| 98 |
self.assertEqual(list(result), expected) |
| 99 |
|
| 100 |
|
| 101 |
class NormalizeFieldChoicesTests(SimpleTestCase): |
| 102 |
expected = [ |
| 103 |
("C", _("Club")), |
| 104 |
("D", _("Diamond")), |
| 105 |
("H", _("Heart")), |
| 106 |
("S", _("Spade")), |
| 107 |
] |
| 108 |
expected_nested = [ |
| 109 |
("Audio", [("vinyl", _("Vinyl")), ("cd", _("CD"))]), |
| 110 |
("Video", [("vhs", _("VHS Tape")), ("dvd", _("DVD"))]), |
| 111 |
("unknown", _("Unknown")), |
| 112 |
] |
| 113 |
invalid = [ |
| 114 |
1j, |
| 115 |
123, |
| 116 |
123.45, |
| 117 |
"invalid", |
| 118 |
b"invalid", |
| 119 |
_("invalid"), |
| 120 |
object(), |
| 121 |
None, |
| 122 |
True, |
| 123 |
False, |
| 124 |
] |
| 125 |
invalid_iterable = [ |
| 126 |
# Special cases of a string-likes which would unpack incorrectly. |
| 127 |
["ab"], |
| 128 |
[b"ab"], |
| 129 |
[_("ab")], |
| 130 |
# Non-iterable items or iterable items with incorrect number of |
| 131 |
# elements that cannot be unpacked. |
| 132 |
[123], |
| 133 |
[("value",)], |
| 134 |
[("value", "label", "other")], |
| 135 |
] |
| 136 |
invalid_nested = [ |
| 137 |
# Nested choices can only be two-levels deep, so return callables, |
| 138 |
# mappings, iterables, etc. at deeper levels unmodified. |
| 139 |
[("Group", [("Value", lambda: "Label")])], |
| 140 |
[("Group", [("Value", {"Label 1?": "Label 2?"})])], |
| 141 |
[("Group", [("Value", [("Label 1?", "Label 2?")])])], |
| 142 |
] |
| 143 |
|
| 144 |
def test_empty(self): |
| 145 |
def generator(): |
| 146 |
yield from () |
| 147 |
|
| 148 |
for choices in ({}, [], (), set(), frozenset(), generator()): |
| 149 |
with self.subTest(choices=choices): |
| 150 |
self.assertEqual(normalize_choices(choices), []) |
| 151 |
|
| 152 |
def test_choices(self): |
| 153 |
class Medal(TextChoices): |
| 154 |
GOLD = "GOLD", _("Gold") |
| 155 |
SILVER = "SILVER", _("Silver") |
| 156 |
BRONZE = "BRONZE", _("Bronze") |
| 157 |
|
| 158 |
expected = [ |
| 159 |
("GOLD", _("Gold")), |
| 160 |
("SILVER", _("Silver")), |
| 161 |
("BRONZE", _("Bronze")), |
| 162 |
] |
| 163 |
self.assertEqual(normalize_choices(Medal), expected) |
| 164 |
|
| 165 |
def test_callable(self): |
| 166 |
def get_choices(): |
| 167 |
return { |
| 168 |
"C": _("Club"), |
| 169 |
"D": _("Diamond"), |
| 170 |
"H": _("Heart"), |
| 171 |
"S": _("Spade"), |
| 172 |
} |
| 173 |
|
| 174 |
get_choices_spy = mock.Mock(wraps=get_choices) |
| 175 |
output = normalize_choices(get_choices_spy) |
| 176 |
|
| 177 |
get_choices_spy.assert_not_called() |
| 178 |
self.assertIsInstance(output, CallableChoiceIterator) |
| 179 |
self.assertEqual(output, self.expected) |
| 180 |
get_choices_spy.assert_called_once() |
| 181 |
|
| 182 |
def test_mapping(self): |
| 183 |
choices = { |
| 184 |
"C": _("Club"), |
| 185 |
"D": _("Diamond"), |
| 186 |
"H": _("Heart"), |
| 187 |
"S": _("Spade"), |
| 188 |
} |
| 189 |
self.assertEqual(normalize_choices(choices), self.expected) |
| 190 |
|
| 191 |
def test_iterable(self): |
| 192 |
choices = [ |
| 193 |
("C", _("Club")), |
| 194 |
("D", _("Diamond")), |
| 195 |
("H", _("Heart")), |
| 196 |
("S", _("Spade")), |
| 197 |
] |
| 198 |
self.assertEqual(normalize_choices(choices), self.expected) |
| 199 |
|
| 200 |
def test_iterator(self): |
| 201 |
def generator(): |
| 202 |
yield "C", _("Club") |
| 203 |
yield "D", _("Diamond") |
| 204 |
yield "H", _("Heart") |
| 205 |
yield "S", _("Spade") |
| 206 |
|
| 207 |
choices = generator() |
| 208 |
self.assertEqual(normalize_choices(choices), self.expected) |
| 209 |
|
| 210 |
def test_nested_callable(self): |
| 211 |
def get_audio_choices(): |
| 212 |
return [("vinyl", _("Vinyl")), ("cd", _("CD"))] |
| 213 |
|
| 214 |
def get_video_choices(): |
| 215 |
return [("vhs", _("VHS Tape")), ("dvd", _("DVD"))] |
| 216 |
|
| 217 |
def get_media_choices(): |
| 218 |
return [ |
| 219 |
("Audio", get_audio_choices), |
| 220 |
("Video", get_video_choices), |
| 221 |
("unknown", _("Unknown")), |
| 222 |
] |
| 223 |
|
| 224 |
get_media_choices_spy = mock.Mock(wraps=get_media_choices) |
| 225 |
output = normalize_choices(get_media_choices_spy) |
| 226 |
|
| 227 |
get_media_choices_spy.assert_not_called() |
| 228 |
self.assertIsInstance(output, CallableChoiceIterator) |
| 229 |
self.assertEqual(output, self.expected_nested) |
| 230 |
get_media_choices_spy.assert_called_once() |
| 231 |
|
| 232 |
def test_nested_mapping(self): |
| 233 |
choices = { |
| 234 |
"Audio": {"vinyl": _("Vinyl"), "cd": _("CD")}, |
| 235 |
"Video": {"vhs": _("VHS Tape"), "dvd": _("DVD")}, |
| 236 |
"unknown": _("Unknown"), |
| 237 |
} |
| 238 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 239 |
|
| 240 |
def test_nested_iterable(self): |
| 241 |
choices = [ |
| 242 |
("Audio", [("vinyl", _("Vinyl")), ("cd", _("CD"))]), |
| 243 |
("Video", [("vhs", _("VHS Tape")), ("dvd", _("DVD"))]), |
| 244 |
("unknown", _("Unknown")), |
| 245 |
] |
| 246 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 247 |
|
| 248 |
def test_nested_iterator(self): |
| 249 |
def generate_audio_choices(): |
| 250 |
yield "vinyl", _("Vinyl") |
| 251 |
yield "cd", _("CD") |
| 252 |
|
| 253 |
def generate_video_choices(): |
| 254 |
yield "vhs", _("VHS Tape") |
| 255 |
yield "dvd", _("DVD") |
| 256 |
|
| 257 |
def generate_media_choices(): |
| 258 |
yield "Audio", generate_audio_choices() |
| 259 |
yield "Video", generate_video_choices() |
| 260 |
yield "unknown", _("Unknown") |
| 261 |
|
| 262 |
choices = generate_media_choices() |
| 263 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 264 |
|
| 265 |
def test_callable_non_canonical(self): |
| 266 |
# Canonical form is list of 2-tuple, but nested lists should work. |
| 267 |
def get_choices(): |
| 268 |
return [ |
| 269 |
["C", _("Club")], |
| 270 |
["D", _("Diamond")], |
| 271 |
["H", _("Heart")], |
| 272 |
["S", _("Spade")], |
| 273 |
] |
| 274 |
|
| 275 |
get_choices_spy = mock.Mock(wraps=get_choices) |
| 276 |
output = normalize_choices(get_choices_spy) |
| 277 |
|
| 278 |
get_choices_spy.assert_not_called() |
| 279 |
self.assertIsInstance(output, CallableChoiceIterator) |
| 280 |
self.assertEqual(output, self.expected) |
| 281 |
get_choices_spy.assert_called_once() |
| 282 |
|
| 283 |
def test_iterable_non_canonical(self): |
| 284 |
# Canonical form is list of 2-tuple, but nested lists should work. |
| 285 |
choices = [ |
| 286 |
["C", _("Club")], |
| 287 |
["D", _("Diamond")], |
| 288 |
["H", _("Heart")], |
| 289 |
["S", _("Spade")], |
| 290 |
] |
| 291 |
self.assertEqual(normalize_choices(choices), self.expected) |
| 292 |
|
| 293 |
def test_iterator_non_canonical(self): |
| 294 |
# Canonical form is list of 2-tuple, but nested lists should work. |
| 295 |
def generator(): |
| 296 |
yield ["C", _("Club")] |
| 297 |
yield ["D", _("Diamond")] |
| 298 |
yield ["H", _("Heart")] |
| 299 |
yield ["S", _("Spade")] |
| 300 |
|
| 301 |
choices = generator() |
| 302 |
self.assertEqual(normalize_choices(choices), self.expected) |
| 303 |
|
| 304 |
def test_nested_callable_non_canonical(self): |
| 305 |
# Canonical form is list of 2-tuple, but nested lists should work. |
| 306 |
|
| 307 |
def get_audio_choices(): |
| 308 |
return [["vinyl", _("Vinyl")], ["cd", _("CD")]] |
| 309 |
|
| 310 |
def get_video_choices(): |
| 311 |
return [["vhs", _("VHS Tape")], ["dvd", _("DVD")]] |
| 312 |
|
| 313 |
def get_media_choices(): |
| 314 |
return [ |
| 315 |
["Audio", get_audio_choices], |
| 316 |
["Video", get_video_choices], |
| 317 |
["unknown", _("Unknown")], |
| 318 |
] |
| 319 |
|
| 320 |
get_media_choices_spy = mock.Mock(wraps=get_media_choices) |
| 321 |
output = normalize_choices(get_media_choices_spy) |
| 322 |
|
| 323 |
get_media_choices_spy.assert_not_called() |
| 324 |
self.assertIsInstance(output, CallableChoiceIterator) |
| 325 |
self.assertEqual(output, self.expected_nested) |
| 326 |
get_media_choices_spy.assert_called_once() |
| 327 |
|
| 328 |
def test_nested_iterable_non_canonical(self): |
| 329 |
# Canonical form is list of 2-tuple, but nested lists should work. |
| 330 |
choices = [ |
| 331 |
["Audio", [["vinyl", _("Vinyl")], ["cd", _("CD")]]], |
| 332 |
["Video", [["vhs", _("VHS Tape")], ["dvd", _("DVD")]]], |
| 333 |
["unknown", _("Unknown")], |
| 334 |
] |
| 335 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 336 |
|
| 337 |
def test_nested_iterator_non_canonical(self): |
| 338 |
# Canonical form is list of 2-tuple, but nested lists should work. |
| 339 |
def generator(): |
| 340 |
yield ["Audio", [["vinyl", _("Vinyl")], ["cd", _("CD")]]] |
| 341 |
yield ["Video", [["vhs", _("VHS Tape")], ["dvd", _("DVD")]]] |
| 342 |
yield ["unknown", _("Unknown")] |
| 343 |
|
| 344 |
choices = generator() |
| 345 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 346 |
|
| 347 |
def test_nested_mixed_mapping_and_iterable(self): |
| 348 |
# Although not documented, as it's better to stick to either mappings |
| 349 |
# or iterables, nesting of mappings within iterables and vice versa |
| 350 |
# works and is likely to occur in the wild. This is supported by the |
| 351 |
# recursive call to `normalize_choices()` which will normalize nested |
| 352 |
# choices. |
| 353 |
choices = { |
| 354 |
"Audio": [("vinyl", _("Vinyl")), ("cd", _("CD"))], |
| 355 |
"Video": [("vhs", _("VHS Tape")), ("dvd", _("DVD"))], |
| 356 |
"unknown": _("Unknown"), |
| 357 |
} |
| 358 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 359 |
choices = [ |
| 360 |
("Audio", {"vinyl": _("Vinyl"), "cd": _("CD")}), |
| 361 |
("Video", {"vhs": _("VHS Tape"), "dvd": _("DVD")}), |
| 362 |
("unknown", _("Unknown")), |
| 363 |
] |
| 364 |
self.assertEqual(normalize_choices(choices), self.expected_nested) |
| 365 |
|
| 366 |
def test_iterable_set(self): |
| 367 |
# Although not documented, as sets are unordered which results in |
| 368 |
# randomised order in form fields, passing a set of 2-tuples works. |
| 369 |
# Consistent ordering of choices on model fields in migrations is |
| 370 |
# enforced by the migrations serializer. |
| 371 |
choices = { |
| 372 |
("C", _("Club")), |
| 373 |
("D", _("Diamond")), |
| 374 |
("H", _("Heart")), |
| 375 |
("S", _("Spade")), |
| 376 |
} |
| 377 |
self.assertEqual(sorted(normalize_choices(choices)), sorted(self.expected)) |
| 378 |
|
| 379 |
def test_unsupported_values_returned_unmodified(self): |
| 380 |
# Unsupported values must be returned unmodified for model system check |
| 381 |
# to work correctly. |
| 382 |
for value in self.invalid + self.invalid_iterable + self.invalid_nested: |
| 383 |
with self.subTest(value=value): |
| 384 |
self.assertEqual(normalize_choices(value), value) |
| 385 |
|
| 386 |
def test_unsupported_values_from_callable_returned_unmodified(self): |
| 387 |
for value in self.invalid_iterable + self.invalid_nested: |
| 388 |
with self.subTest(value=value): |
| 389 |
self.assertEqual(normalize_choices(lambda: value), value) |
| 390 |
|
| 391 |
def test_unsupported_values_from_iterator_returned_unmodified(self): |
| 392 |
for value in self.invalid_nested: |
| 393 |
with self.subTest(value=value): |
| 394 |
self.assertEqual( |
| 395 |
normalize_choices((lambda: (yield from value))()), |
| 396 |
value, |
| 397 |
) |