Increased test coverage for django.utils.datastructures.MultiValueDict.
Co-authored-by: Mads Jensen <mje@inducks.org>
This commit is contained in:
parent
825f8470f5
commit
c3d9b8b28f
|
@ -3,6 +3,7 @@ Tests for stuff in django.utils.datastructures.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import pickle
|
||||||
|
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
from django.utils.datastructures import (
|
from django.utils.datastructures import (
|
||||||
|
@ -57,28 +58,34 @@ class OrderedSetTests(SimpleTestCase):
|
||||||
|
|
||||||
class MultiValueDictTests(SimpleTestCase):
|
class MultiValueDictTests(SimpleTestCase):
|
||||||
|
|
||||||
|
def test_repr(self):
|
||||||
|
d = MultiValueDict({'key': 'value'})
|
||||||
|
self.assertEqual(repr(d), "<MultiValueDict: {'key': 'value'}>")
|
||||||
|
|
||||||
def test_multivaluedict(self):
|
def test_multivaluedict(self):
|
||||||
d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
|
d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer'], 'empty': []})
|
||||||
self.assertEqual(d['name'], 'Simon')
|
self.assertEqual(d['name'], 'Simon')
|
||||||
self.assertEqual(d.get('name'), 'Simon')
|
self.assertEqual(d.get('name'), 'Simon')
|
||||||
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
|
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(d.items()),
|
list(d.items()),
|
||||||
[('name', 'Simon'), ('position', 'Developer')]
|
[('name', 'Simon'), ('position', 'Developer'), ('empty', [])]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(d.lists()),
|
list(d.lists()),
|
||||||
[('name', ['Adrian', 'Simon']), ('position', ['Developer'])]
|
[('name', ['Adrian', 'Simon']), ('position', ['Developer']), ('empty', [])]
|
||||||
)
|
)
|
||||||
with self.assertRaisesMessage(MultiValueDictKeyError, "'lastname'"):
|
with self.assertRaisesMessage(MultiValueDictKeyError, "'lastname'"):
|
||||||
d.__getitem__('lastname')
|
d.__getitem__('lastname')
|
||||||
|
self.assertIsNone(d.get('empty'))
|
||||||
|
self.assertEqual(d.get('empty', 'nonexistent'), 'nonexistent')
|
||||||
self.assertIsNone(d.get('lastname'))
|
self.assertIsNone(d.get('lastname'))
|
||||||
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
||||||
self.assertEqual(d.getlist('lastname'), [])
|
self.assertEqual(d.getlist('lastname'), [])
|
||||||
self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']), ['Adrian', 'Simon'])
|
self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']), ['Adrian', 'Simon'])
|
||||||
d.setlist('lastname', ['Holovaty', 'Willison'])
|
d.setlist('lastname', ['Holovaty', 'Willison'])
|
||||||
self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
|
self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
|
||||||
self.assertEqual(sorted(d.values()), ['Developer', 'Simon', 'Willison'])
|
self.assertEqual(list(d.values()), ['Simon', 'Developer', [], 'Willison'])
|
||||||
|
|
||||||
def test_appendlist(self):
|
def test_appendlist(self):
|
||||||
d = MultiValueDict()
|
d = MultiValueDict()
|
||||||
|
@ -88,23 +95,31 @@ class MultiValueDictTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_copy(self):
|
def test_copy(self):
|
||||||
for copy_func in [copy.copy, lambda d: d.copy()]:
|
for copy_func in [copy.copy, lambda d: d.copy()]:
|
||||||
d1 = MultiValueDict({
|
with self.subTest(copy_func):
|
||||||
"developers": ["Carl", "Fred"]
|
d1 = MultiValueDict({'developers': ['Carl', 'Fred']})
|
||||||
})
|
self.assertEqual(d1['developers'], 'Fred')
|
||||||
self.assertEqual(d1["developers"], "Fred")
|
|
||||||
d2 = copy_func(d1)
|
d2 = copy_func(d1)
|
||||||
d2.update({"developers": "Groucho"})
|
d2.update({'developers': 'Groucho'})
|
||||||
self.assertEqual(d2["developers"], "Groucho")
|
self.assertEqual(d2['developers'], 'Groucho')
|
||||||
self.assertEqual(d1["developers"], "Fred")
|
self.assertEqual(d1['developers'], 'Fred')
|
||||||
|
|
||||||
d1 = MultiValueDict({
|
d1 = MultiValueDict({'key': [[]]})
|
||||||
"key": [[]]
|
self.assertEqual(d1['key'], [])
|
||||||
})
|
|
||||||
self.assertEqual(d1["key"], [])
|
|
||||||
d2 = copy_func(d1)
|
d2 = copy_func(d1)
|
||||||
d2["key"].append("Penguin")
|
d2['key'].append('Penguin')
|
||||||
self.assertEqual(d1["key"], ["Penguin"])
|
self.assertEqual(d1['key'], ['Penguin'])
|
||||||
self.assertEqual(d2["key"], ["Penguin"])
|
self.assertEqual(d2['key'], ['Penguin'])
|
||||||
|
|
||||||
|
def test_deepcopy(self):
|
||||||
|
d1 = MultiValueDict({'a': [[123]]})
|
||||||
|
d2 = copy.copy(d1)
|
||||||
|
d3 = copy.deepcopy(d1)
|
||||||
|
self.assertIs(d1['a'], d2['a'])
|
||||||
|
self.assertIsNot(d1['a'], d3['a'])
|
||||||
|
|
||||||
|
def test_pickle(self):
|
||||||
|
x = MultiValueDict({'a': ['1', '2'], 'b': ['3']})
|
||||||
|
self.assertEqual(x, pickle.loads(pickle.dumps(x)))
|
||||||
|
|
||||||
def test_dict_translation(self):
|
def test_dict_translation(self):
|
||||||
mvd = MultiValueDict({
|
mvd = MultiValueDict({
|
||||||
|
@ -112,7 +127,7 @@ class MultiValueDictTests(SimpleTestCase):
|
||||||
'pm': ['Rory'],
|
'pm': ['Rory'],
|
||||||
})
|
})
|
||||||
d = mvd.dict()
|
d = mvd.dict()
|
||||||
self.assertEqual(sorted(d), sorted(mvd))
|
self.assertEqual(list(d), list(mvd))
|
||||||
for key in mvd:
|
for key in mvd:
|
||||||
self.assertEqual(d[key], mvd[key])
|
self.assertEqual(d[key], mvd[key])
|
||||||
|
|
||||||
|
@ -141,6 +156,45 @@ class MultiValueDictTests(SimpleTestCase):
|
||||||
self.assertIsNone(x.getlist('a'))
|
self.assertIsNone(x.getlist('a'))
|
||||||
self.assertEqual(x.getlist('b'), [])
|
self.assertEqual(x.getlist('b'), [])
|
||||||
|
|
||||||
|
def test_setitem(self):
|
||||||
|
x = MultiValueDict({'a': [1, 2]})
|
||||||
|
x['a'] = 3
|
||||||
|
self.assertEqual(list(x.lists()), [('a', [3])])
|
||||||
|
|
||||||
|
def test_setdefault(self):
|
||||||
|
x = MultiValueDict({'a': [1, 2]})
|
||||||
|
a = x.setdefault('a', 3)
|
||||||
|
b = x.setdefault('b', 3)
|
||||||
|
self.assertEqual(a, 2)
|
||||||
|
self.assertEqual(b, 3)
|
||||||
|
self.assertEqual(list(x.lists()), [('a', [1, 2]), ('b', [3])])
|
||||||
|
|
||||||
|
def test_update_too_many_args(self):
|
||||||
|
x = MultiValueDict({'a': []})
|
||||||
|
msg = 'update expected at most 1 argument, got 2'
|
||||||
|
with self.assertRaisesMessage(TypeError, msg):
|
||||||
|
x.update(1, 2)
|
||||||
|
|
||||||
|
def test_update_no_args(self):
|
||||||
|
x = MultiValueDict({'a': []})
|
||||||
|
x.update()
|
||||||
|
self.assertEqual(list(x.lists()), [('a', [])])
|
||||||
|
|
||||||
|
def test_update_dict_arg(self):
|
||||||
|
x = MultiValueDict({'a': [1], 'b': [2], 'c': [3]})
|
||||||
|
x.update({'a': 4, 'b': 5})
|
||||||
|
self.assertEqual(list(x.lists()), [('a', [1, 4]), ('b', [2, 5]), ('c', [3])])
|
||||||
|
|
||||||
|
def test_update_multivaluedict_arg(self):
|
||||||
|
x = MultiValueDict({'a': [1], 'b': [2], 'c': [3]})
|
||||||
|
x.update(MultiValueDict({'a': [4], 'b': [5]}))
|
||||||
|
self.assertEqual(list(x.lists()), [('a', [1, 4]), ('b', [2, 5]), ('c', [3])])
|
||||||
|
|
||||||
|
def test_update_kwargs(self):
|
||||||
|
x = MultiValueDict({'a': [1], 'b': [2], 'c': [3]})
|
||||||
|
x.update(a=4, b=5)
|
||||||
|
self.assertEqual(list(x.lists()), [('a', [1, 4]), ('b', [2, 5]), ('c', [3])])
|
||||||
|
|
||||||
|
|
||||||
class ImmutableListTests(SimpleTestCase):
|
class ImmutableListTests(SimpleTestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue