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
|
2012-07-25 07:12:59 +00:00
|
|
|
from django.utils import six
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.datastructures import (
|
|
|
|
DictWrapper, ImmutableList, MultiValueDict, MultiValueDictKeyError,
|
|
|
|
OrderedSet,
|
|
|
|
)
|
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)
|
|
|
|
|
|
|
|
|
2011-08-13 00:42:08 +00:00
|
|
|
class MultiValueDictTests(SimpleTestCase):
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
def test_multivaluedict(self):
|
|
|
|
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(
|
|
|
|
sorted(list(six.iteritems(d))),
|
|
|
|
[('name', 'Simon'), ('position', 'Developer')]
|
|
|
|
)
|
2010-09-28 08:18:39 +00:00
|
|
|
|
2013-12-12 20:23:24 +00:00
|
|
|
self.assertEqual(
|
|
|
|
sorted(list(six.iterlists(d))),
|
|
|
|
[('name', ['Adrian', 'Simon']), ('position', ['Developer'])]
|
|
|
|
)
|
2010-09-28 08:18:39 +00:00
|
|
|
|
2013-09-16 20:20:29 +00:00
|
|
|
six.assertRaisesRegex(self, MultiValueDictKeyError, 'lastname',
|
2010-09-28 08:18:39 +00:00
|
|
|
d.__getitem__, 'lastname')
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(d.get('lastname'), None)
|
|
|
|
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
|
|
|
self.assertEqual(d.getlist('lastname'), [])
|
2011-05-22 15:05:29 +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'])
|
2012-10-26 01:00:21 +00:00
|
|
|
self.assertEqual(sorted(list(six.itervalues(d))),
|
|
|
|
['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()
|
2014-03-30 18:25:06 +00:00
|
|
|
self.assertEqual(sorted(six.iterkeys(d)), sorted(six.iterkeys(mvd)))
|
2012-07-25 07:12:59 +00:00
|
|
|
for key in six.iterkeys(mvd):
|
2011-06-16 16:34:55 +00:00
|
|
|
self.assertEqual(d[key], mvd[key])
|
|
|
|
|
|
|
|
self.assertEqual({}, MultiValueDict().dict())
|
|
|
|
|
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.
|
2011-08-13 00:42:08 +00:00
|
|
|
self.assertRaisesMessage(AttributeError,
|
2010-09-28 08:18:39 +00:00
|
|
|
'ImmutableList object is immutable.', d.sort)
|
|
|
|
|
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!
|
2011-08-13 00:42:08 +00:00
|
|
|
self.assertRaisesMessage(AttributeError,
|
2010-09-28 08:18:39 +00:00
|
|
|
'Object is immutable!', d.__setitem__, 1, 'test')
|
|
|
|
|
|
|
|
|
2011-08-13 00:42:08 +00:00
|
|
|
class DictWrapperTests(SimpleTestCase):
|
2010-09-28 08:18:39 +00:00
|
|
|
|
|
|
|
def test_dictwrapper(self):
|
|
|
|
f = lambda x: "*%s" % x
|
|
|
|
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'
|
|
|
|
)
|