2010-09-28 08:18:39 +00:00
|
|
|
"""
|
|
|
|
Tests for stuff in django.utils.datastructures.
|
|
|
|
"""
|
2011-03-28 02:11:19 +00:00
|
|
|
|
|
|
|
import copy
|
2010-09-28 08:18:12 +00:00
|
|
|
|
2014-11-19 17:22:23 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.datastructures import (
|
2018-05-04 23:37:01 +00:00
|
|
|
CaseInsensitiveMapping, DictWrapper, ImmutableList, MultiValueDict,
|
|
|
|
MultiValueDictKeyError, OrderedSet,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
2008-07-13 08:48:18 +00:00
|
|
|
|
2010-09-28 08:18:39 +00:00
|
|
|
|
2014-10-16 10:43:46 +00:00
|
|
|
class OrderedSetTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_bool(self):
|
|
|
|
# Refs #23664
|
|
|
|
s = OrderedSet()
|
|
|
|
self.assertFalse(s)
|
|
|
|
s.add(1)
|
|
|
|
self.assertTrue(s)
|
|
|
|
|
2015-07-09 18:00:36 +00:00
|
|
|
def test_len(self):
|
|
|
|
s = OrderedSet()
|
|
|
|
self.assertEqual(len(s), 0)
|
|
|
|
s.add(1)
|
|
|
|
s.add(2)
|
|
|
|
s.add(2)
|
|
|
|
self.assertEqual(len(s), 2)
|
|
|
|
|
2014-10-16 10:43:46 +00:00
|
|
|
|
2011-08-13 00:42:08 +00:00
|
|
|
class MultiValueDictTests(SimpleTestCase):
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
def test_multivaluedict(self):
|
2019-01-02 23:18:19 +00:00
|
|
|
d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(d['name'], 'Simon')
|
|
|
|
self.assertEqual(d.get('name'), 'Simon')
|
|
|
|
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
|
2013-12-12 20:23:24 +00:00
|
|
|
self.assertEqual(
|
2017-01-07 11:11:46 +00:00
|
|
|
sorted(d.items()),
|
2013-12-12 20:23:24 +00:00
|
|
|
[('name', 'Simon'), ('position', 'Developer')]
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2017-01-07 11:11:46 +00:00
|
|
|
sorted(d.lists()),
|
2013-12-12 20:23:24 +00:00
|
|
|
[('name', ['Adrian', 'Simon']), ('position', ['Developer'])]
|
|
|
|
)
|
2020-02-04 20:58:07 +00:00
|
|
|
with self.assertRaisesMessage(MultiValueDictKeyError, "'lastname'"):
|
2016-01-18 08:45:45 +00:00
|
|
|
d.__getitem__('lastname')
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(d.get('lastname'))
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
|
|
|
self.assertEqual(d.getlist('lastname'), [])
|
2019-01-02 23:18:19 +00:00
|
|
|
self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']), ['Adrian', 'Simon'])
|
2010-09-28 08:18:39 +00:00
|
|
|
d.setlist('lastname', ['Holovaty', 'Willison'])
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
|
2017-01-07 11:11:46 +00:00
|
|
|
self.assertEqual(sorted(d.values()), ['Developer', 'Simon', 'Willison'])
|
2010-09-28 08:18:39 +00:00
|
|
|
|
2012-02-09 18:41:20 +00:00
|
|
|
def test_appendlist(self):
|
|
|
|
d = MultiValueDict()
|
|
|
|
d.appendlist('name', 'Adrian')
|
|
|
|
d.appendlist('name', 'Simon')
|
|
|
|
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
|
|
|
|
|
2010-10-27 20:39:20 +00:00
|
|
|
def test_copy(self):
|
2011-03-28 14:20:15 +00:00
|
|
|
for copy_func in [copy.copy, lambda d: d.copy()]:
|
2010-10-27 20:39:20 +00:00
|
|
|
d1 = MultiValueDict({
|
|
|
|
"developers": ["Carl", "Fred"]
|
|
|
|
})
|
|
|
|
self.assertEqual(d1["developers"], "Fred")
|
|
|
|
d2 = copy_func(d1)
|
|
|
|
d2.update({"developers": "Groucho"})
|
|
|
|
self.assertEqual(d2["developers"], "Groucho")
|
|
|
|
self.assertEqual(d1["developers"], "Fred")
|
|
|
|
|
|
|
|
d1 = MultiValueDict({
|
|
|
|
"key": [[]]
|
|
|
|
})
|
|
|
|
self.assertEqual(d1["key"], [])
|
|
|
|
d2 = copy_func(d1)
|
|
|
|
d2["key"].append("Penguin")
|
|
|
|
self.assertEqual(d1["key"], ["Penguin"])
|
|
|
|
self.assertEqual(d2["key"], ["Penguin"])
|
|
|
|
|
2011-06-16 16:34:55 +00:00
|
|
|
def test_dict_translation(self):
|
|
|
|
mvd = MultiValueDict({
|
|
|
|
'devs': ['Bob', 'Joe'],
|
|
|
|
'pm': ['Rory'],
|
|
|
|
})
|
|
|
|
d = mvd.dict()
|
2017-05-27 23:08:46 +00:00
|
|
|
self.assertEqual(sorted(d), sorted(mvd))
|
|
|
|
for key in mvd:
|
2011-06-16 16:34:55 +00:00
|
|
|
self.assertEqual(d[key], mvd[key])
|
|
|
|
|
|
|
|
self.assertEqual({}, MultiValueDict().dict())
|
|
|
|
|
2016-09-10 09:25:42 +00:00
|
|
|
def test_getlist_doesnt_mutate(self):
|
|
|
|
x = MultiValueDict({'a': ['1', '2'], 'b': ['3']})
|
|
|
|
values = x.getlist('a')
|
|
|
|
values += x.getlist('b')
|
|
|
|
self.assertEqual(x.getlist('a'), ['1', '2'])
|
|
|
|
|
|
|
|
def test_internal_getlist_does_mutate(self):
|
|
|
|
x = MultiValueDict({'a': ['1', '2'], 'b': ['3']})
|
|
|
|
values = x._getlist('a')
|
|
|
|
values += x._getlist('b')
|
|
|
|
self.assertEqual(x._getlist('a'), ['1', '2', '3'])
|
|
|
|
|
|
|
|
def test_getlist_default(self):
|
|
|
|
x = MultiValueDict({'a': [1]})
|
|
|
|
MISSING = object()
|
|
|
|
values = x.getlist('b', default=MISSING)
|
|
|
|
self.assertIs(values, MISSING)
|
|
|
|
|
2016-12-09 20:31:52 +00:00
|
|
|
def test_getlist_none_empty_values(self):
|
|
|
|
x = MultiValueDict({'a': None, 'b': []})
|
|
|
|
self.assertIsNone(x.getlist('a'))
|
|
|
|
self.assertEqual(x.getlist('b'), [])
|
|
|
|
|
2010-09-28 08:18:39 +00:00
|
|
|
|
2011-08-13 00:42:08 +00:00
|
|
|
class ImmutableListTests(SimpleTestCase):
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
def test_sort(self):
|
|
|
|
d = ImmutableList(range(10))
|
|
|
|
|
|
|
|
# AttributeError: ImmutableList object is immutable.
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(AttributeError, 'ImmutableList object is immutable.'):
|
|
|
|
d.sort()
|
2010-09-28 08:18:39 +00:00
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)')
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
def test_custom_warning(self):
|
|
|
|
d = ImmutableList(range(10), warning="Object is immutable!")
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(d[1], 1)
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
# AttributeError: Object is immutable!
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(AttributeError, 'Object is immutable!'):
|
|
|
|
d.__setitem__(1, 'test')
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
|
2011-08-13 00:42:08 +00:00
|
|
|
class DictWrapperTests(SimpleTestCase):
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
def test_dictwrapper(self):
|
2016-01-23 16:47:07 +00:00
|
|
|
def f(x):
|
|
|
|
return "*%s" % x
|
2010-09-28 08:18:39 +00:00
|
|
|
d = DictWrapper({'a': 'a'}, f, 'xx_')
|
2013-12-12 20:23:24 +00:00
|
|
|
self.assertEqual(
|
|
|
|
"Normal: %(a)s. Modified: %(xx_a)s" % d,
|
|
|
|
'Normal: a. Modified: *a'
|
|
|
|
)
|
2018-05-04 23:37:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CaseInsensitiveMappingTests(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.dict1 = CaseInsensitiveMapping({
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'content-type': 'text/html',
|
|
|
|
})
|
|
|
|
|
|
|
|
def test_create_with_invalid_values(self):
|
|
|
|
msg = 'dictionary update sequence element #1 has length 4; 2 is required'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
CaseInsensitiveMapping([('Key1', 'Val1'), 'Key2'])
|
|
|
|
|
|
|
|
def test_create_with_invalid_key(self):
|
|
|
|
msg = 'Element key 1 invalid, only strings are allowed'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
CaseInsensitiveMapping([(1, '2')])
|
|
|
|
|
|
|
|
def test_list(self):
|
2019-02-09 10:07:04 +00:00
|
|
|
self.assertEqual(list(self.dict1), ['Accept', 'content-type'])
|
2018-05-04 23:37:01 +00:00
|
|
|
|
|
|
|
def test_dict(self):
|
|
|
|
self.assertEqual(dict(self.dict1), {'Accept': 'application/json', 'content-type': 'text/html'})
|
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
dict1 = CaseInsensitiveMapping({'Accept': 'application/json'})
|
|
|
|
dict2 = CaseInsensitiveMapping({'content-type': 'text/html'})
|
|
|
|
self.assertEqual(repr(dict1), repr({'Accept': 'application/json'}))
|
|
|
|
self.assertEqual(repr(dict2), repr({'content-type': 'text/html'}))
|
|
|
|
|
|
|
|
def test_str(self):
|
|
|
|
dict1 = CaseInsensitiveMapping({'Accept': 'application/json'})
|
|
|
|
dict2 = CaseInsensitiveMapping({'content-type': 'text/html'})
|
|
|
|
self.assertEqual(str(dict1), str({'Accept': 'application/json'}))
|
|
|
|
self.assertEqual(str(dict2), str({'content-type': 'text/html'}))
|
|
|
|
|
|
|
|
def test_equal(self):
|
|
|
|
self.assertEqual(self.dict1, {'Accept': 'application/json', 'content-type': 'text/html'})
|
|
|
|
self.assertNotEqual(self.dict1, {'accept': 'application/jso', 'Content-Type': 'text/html'})
|
|
|
|
self.assertNotEqual(self.dict1, 'string')
|
|
|
|
|
|
|
|
def test_items(self):
|
|
|
|
other = {'Accept': 'application/json', 'content-type': 'text/html'}
|
|
|
|
self.assertEqual(sorted(self.dict1.items()), sorted(other.items()))
|
|
|
|
|
|
|
|
def test_copy(self):
|
|
|
|
copy = self.dict1.copy()
|
|
|
|
self.assertIs(copy, self.dict1)
|
|
|
|
self.assertEqual(copy, self.dict1)
|
|
|
|
|
|
|
|
def test_getitem(self):
|
|
|
|
self.assertEqual(self.dict1['Accept'], 'application/json')
|
|
|
|
self.assertEqual(self.dict1['accept'], 'application/json')
|
|
|
|
self.assertEqual(self.dict1['aCCept'], 'application/json')
|
|
|
|
self.assertEqual(self.dict1['content-type'], 'text/html')
|
|
|
|
self.assertEqual(self.dict1['Content-Type'], 'text/html')
|
|
|
|
self.assertEqual(self.dict1['Content-type'], 'text/html')
|
|
|
|
|
|
|
|
def test_in(self):
|
|
|
|
self.assertIn('Accept', self.dict1)
|
|
|
|
self.assertIn('accept', self.dict1)
|
|
|
|
self.assertIn('aCCept', self.dict1)
|
|
|
|
self.assertIn('content-type', self.dict1)
|
|
|
|
self.assertIn('Content-Type', self.dict1)
|
|
|
|
|
|
|
|
def test_del(self):
|
|
|
|
self.assertIn('Accept', self.dict1)
|
|
|
|
msg = "'CaseInsensitiveMapping' object does not support item deletion"
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
del self.dict1['Accept']
|
|
|
|
self.assertIn('Accept', self.dict1)
|
|
|
|
|
|
|
|
def test_set(self):
|
|
|
|
self.assertEqual(len(self.dict1), 2)
|
|
|
|
msg = "'CaseInsensitiveMapping' object does not support item assignment"
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
self.dict1['New Key'] = 1
|
|
|
|
self.assertEqual(len(self.dict1), 2)
|