diff options
author | Nick Pope <nick.pope@flightdataservices.com> | 2020-10-05 15:57:47 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-10-30 10:44:44 +0100 |
commit | 966b5b49b6521483f1c90b4499c4c80e80136de3 (patch) | |
tree | 3cdbeda0dae479a13839db9d001cbb35745772b3 /tests/utils_tests/test_datastructures.py | |
parent | 1a8ad8a5c6f3344959e81531177164d1c4c4e52a (diff) | |
download | django-966b5b49b6521483f1c90b4499c4c80e80136de3.tar.gz |
Updated MultiValueDict.update() to mirror dict.update() behavior.
Changes in behavior include:
- Accepting iteration over empty sequences, updating nothing.
- Accepting iterable of 2-tuples providing key-value pairs.
- Failing with the same or comparable exceptions for invalid input.
Notably this replaces the previous attempt to catch TypeError which was
unreachable as the call to .items() resulted in AttributeError on
non-dict objects.
Diffstat (limited to 'tests/utils_tests/test_datastructures.py')
-rw-r--r-- | tests/utils_tests/test_datastructures.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index 940c8c9e7d..45d172f984 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -195,6 +195,35 @@ class MultiValueDictTests(SimpleTestCase): x.update(a=4, b=5) self.assertEqual(list(x.lists()), [('a', [1, 4]), ('b', [2, 5]), ('c', [3])]) + def test_update_with_empty_iterable(self): + for value in ['', b'', (), [], set(), {}]: + d = MultiValueDict() + d.update(value) + self.assertEqual(d, MultiValueDict()) + + def test_update_with_iterable_of_pairs(self): + for value in [(('a', 1),), [('a', 1)], {('a', 1)}]: + d = MultiValueDict() + d.update(value) + self.assertEqual(d, MultiValueDict({'a': [1]})) + + def test_update_raises_correct_exceptions(self): + # MultiValueDict.update() raises equivalent exceptions to + # dict.update(). + # Non-iterable values raise TypeError. + for value in [None, True, False, 123, 123.45]: + with self.subTest(value), self.assertRaises(TypeError): + MultiValueDict().update(value) + # Iterables of objects that cannot be unpacked raise TypeError. + for value in [b'123', b'abc', (1, 2, 3), [1, 2, 3], {1, 2, 3}]: + with self.subTest(value), self.assertRaises(TypeError): + MultiValueDict().update(value) + # Iterables of unpackable objects with incorrect number of items raise + # ValueError. + for value in ['123', 'abc', ('a', 'b', 'c'), ['a', 'b', 'c'], {'a', 'b', 'c'}]: + with self.subTest(value), self.assertRaises(ValueError): + MultiValueDict().update(value) + class ImmutableListTests(SimpleTestCase): |