2010-09-28 16:18:39 +08:00
|
|
|
"""
|
|
|
|
Tests for stuff in django.utils.datastructures.
|
|
|
|
"""
|
2011-03-28 10:11:19 +08:00
|
|
|
|
|
|
|
import copy
|
2010-09-28 16:18:39 +08:00
|
|
|
import pickle
|
2010-09-28 16:18:12 +08:00
|
|
|
|
2014-12-22 04:19:05 +08:00
|
|
|
from django.test import SimpleTestCase, ignore_warnings
|
2012-07-15 10:04:37 +08:00
|
|
|
from django.utils.datastructures import (DictWrapper, ImmutableList,
|
2014-11-19 01:09:20 +08:00
|
|
|
MultiValueDict, MultiValueDictKeyError, OrderedSet, SortedDict)
|
2014-12-22 04:19:05 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango19Warning
|
2012-07-25 15:12:59 +08:00
|
|
|
from django.utils import six
|
2008-07-13 16:48:18 +08:00
|
|
|
|
2010-09-28 16:18:39 +08:00
|
|
|
|
2014-12-22 04:19:05 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango19Warning)
|
|
|
|
class SortedDictTests(SimpleTestCase):
|
2010-09-27 23:15:58 +08:00
|
|
|
def setUp(self):
|
2013-09-03 21:48:46 +08:00
|
|
|
super(SortedDictTests, self).setUp()
|
2010-09-27 23:15:58 +08:00
|
|
|
self.d1 = SortedDict()
|
|
|
|
self.d1[7] = 'seven'
|
|
|
|
self.d1[1] = 'one'
|
|
|
|
self.d1[9] = 'nine'
|
|
|
|
|
|
|
|
self.d2 = SortedDict()
|
|
|
|
self.d2[1] = 'one'
|
|
|
|
self.d2[9] = 'nine'
|
|
|
|
self.d2[0] = 'nil'
|
|
|
|
self.d2[7] = 'seven'
|
|
|
|
|
|
|
|
def test_basic_methods(self):
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(self.d1)), [7, 1, 9])
|
|
|
|
self.assertEqual(list(six.itervalues(self.d1)), ['seven', 'one', 'nine'])
|
|
|
|
self.assertEqual(list(six.iteritems(self.d1)), [(7, 'seven'), (1, 'one'), (9, 'nine')])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
|
|
|
def test_overwrite_ordering(self):
|
2012-07-25 15:12:59 +08:00
|
|
|
""" Overwriting an item keeps its place. """
|
2010-09-27 23:15:58 +08:00
|
|
|
self.d1[1] = 'ONE'
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.itervalues(self.d1)), ['seven', 'ONE', 'nine'])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
|
|
|
def test_append_items(self):
|
|
|
|
""" New items go to the end. """
|
|
|
|
self.d1[0] = 'nil'
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(self.d1)), [7, 1, 9, 0])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
|
|
|
def test_delete_and_insert(self):
|
|
|
|
"""
|
|
|
|
Deleting an item, then inserting the same key again will place it
|
|
|
|
at the end.
|
|
|
|
"""
|
|
|
|
del self.d2[7]
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(self.d2)), [1, 9, 0])
|
2010-09-27 23:15:58 +08:00
|
|
|
self.d2[7] = 'lucky number 7'
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(self.d2)), [1, 9, 0, 7])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2:
|
2012-07-25 15:12:59 +08:00
|
|
|
def test_change_keys(self):
|
|
|
|
"""
|
|
|
|
Changing the keys won't do anything, it's only a copy of the
|
|
|
|
keys dict.
|
|
|
|
|
|
|
|
This test doesn't make sense under Python 3 because keys is
|
|
|
|
an iterator.
|
|
|
|
"""
|
|
|
|
k = self.d2.keys()
|
|
|
|
k.remove(9)
|
|
|
|
self.assertEqual(self.d2.keys(), [1, 9, 0, 7])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
|
|
|
def test_init_keys(self):
|
|
|
|
"""
|
|
|
|
Initialising a SortedDict with two keys will just take the first one.
|
|
|
|
|
|
|
|
A real dict will actually take the second value so we will too, but
|
|
|
|
we'll keep the ordering from the first key found.
|
|
|
|
"""
|
|
|
|
tuples = ((2, 'two'), (1, 'one'), (2, 'second-two'))
|
|
|
|
d = SortedDict(tuples)
|
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(d)), [2, 1])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
|
|
|
real_dict = dict(tuples)
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(sorted(six.itervalues(real_dict)), ['one', 'second-two'])
|
2010-09-27 23:15:58 +08:00
|
|
|
|
|
|
|
# Here the order of SortedDict values *is* what we are testing
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.itervalues(d)), ['second-two', 'one'])
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_overwrite(self):
|
|
|
|
self.d1[1] = 'not one'
|
|
|
|
self.assertEqual(self.d1[1], 'not one')
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(self.d1)), list(six.iterkeys(self.d1.copy())))
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_append(self):
|
|
|
|
self.d1[13] = 'thirteen'
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-09-28 16:18:39 +08:00
|
|
|
repr(self.d1),
|
|
|
|
"{7: 'seven', 1: 'one', 9: 'nine', 13: 'thirteen'}"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_pop(self):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(self.d1.pop(1, 'missing'), 'one')
|
|
|
|
self.assertEqual(self.d1.pop(1, 'missing'), 'missing')
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
# We don't know which item will be popped in popitem(), so we'll
|
|
|
|
# just check that the number of keys has decreased.
|
|
|
|
l = len(self.d1)
|
|
|
|
self.d1.popitem()
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(l - len(self.d1), 1)
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_dict_equality(self):
|
2012-07-21 00:53:11 +08:00
|
|
|
d = SortedDict((i, i) for i in range(3))
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(d, {0: 0, 1: 1, 2: 2})
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_tuple_init(self):
|
|
|
|
d = SortedDict(((1, "one"), (0, "zero"), (2, "two")))
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(repr(d), "{1: 'one', 0: 'zero', 2: 'two'}")
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_pickle(self):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(
|
2010-09-28 16:18:39 +08:00
|
|
|
pickle.loads(pickle.dumps(self.d1, 2)),
|
|
|
|
{7: 'seven', 1: 'one', 9: 'nine'}
|
|
|
|
)
|
|
|
|
|
2012-04-30 22:12:38 +08:00
|
|
|
def test_copy(self):
|
|
|
|
orig = SortedDict(((1, "one"), (0, "zero"), (2, "two")))
|
|
|
|
copied = copy.copy(orig)
|
2012-07-25 15:12:59 +08:00
|
|
|
self.assertEqual(list(six.iterkeys(orig)), [1, 0, 2])
|
|
|
|
self.assertEqual(list(six.iterkeys(copied)), [1, 0, 2])
|
2012-04-30 22:12:38 +08:00
|
|
|
|
2010-09-28 16:18:39 +08:00
|
|
|
def test_clear(self):
|
|
|
|
self.d1.clear()
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(self.d1, {})
|
|
|
|
self.assertEqual(self.d1.keyOrder, [])
|
2010-09-28 16:18:39 +08:00
|
|
|
|
2012-10-16 03:12:10 +08:00
|
|
|
def test_reversed(self):
|
|
|
|
self.assertEqual(list(self.d1), [7, 1, 9])
|
|
|
|
self.assertEqual(list(self.d2), [1, 9, 0, 7])
|
|
|
|
self.assertEqual(list(reversed(self.d1)), [9, 1, 7])
|
|
|
|
self.assertEqual(list(reversed(self.d2)), [7, 0, 9, 1])
|
|
|
|
|
2012-07-24 22:01:57 +08:00
|
|
|
|
2014-10-16 18:43:46 +08:00
|
|
|
class OrderedSetTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_bool(self):
|
|
|
|
# Refs #23664
|
|
|
|
s = OrderedSet()
|
|
|
|
self.assertFalse(s)
|
|
|
|
s.add(1)
|
|
|
|
self.assertTrue(s)
|
|
|
|
|
|
|
|
|
2011-08-13 08:42:08 +08:00
|
|
|
class MultiValueDictTests(SimpleTestCase):
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_multivaluedict(self):
|
|
|
|
d = MultiValueDict({'name': ['Adrian', 'Simon'],
|
|
|
|
'position': ['Developer']})
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(d['name'], 'Simon')
|
|
|
|
self.assertEqual(d.get('name'), 'Simon')
|
|
|
|
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
|
2013-12-13 04:23:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
sorted(list(six.iteritems(d))),
|
|
|
|
[('name', 'Simon'), ('position', 'Developer')]
|
|
|
|
)
|
2010-09-28 16:18:39 +08:00
|
|
|
|
2013-12-13 04:23:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
sorted(list(six.iterlists(d))),
|
|
|
|
[('name', ['Adrian', 'Simon']), ('position', ['Developer'])]
|
|
|
|
)
|
2010-09-28 16:18:39 +08:00
|
|
|
|
2013-09-17 04:20:29 +08:00
|
|
|
six.assertRaisesRegex(self, MultiValueDictKeyError, 'lastname',
|
2010-09-28 16:18:39 +08:00
|
|
|
d.__getitem__, 'lastname')
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(d.get('lastname'), None)
|
|
|
|
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
|
|
|
self.assertEqual(d.getlist('lastname'), [])
|
2011-05-22 23:05:29 +08:00
|
|
|
self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
|
|
|
|
['Adrian', 'Simon'])
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
d.setlist('lastname', ['Holovaty', 'Willison'])
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
|
2012-10-26 09:00:21 +08:00
|
|
|
self.assertEqual(sorted(list(six.itervalues(d))),
|
|
|
|
['Developer', 'Simon', 'Willison'])
|
2010-09-28 16:18:39 +08:00
|
|
|
|
2012-02-10 02:41:20 +08:00
|
|
|
def test_appendlist(self):
|
|
|
|
d = MultiValueDict()
|
|
|
|
d.appendlist('name', 'Adrian')
|
|
|
|
d.appendlist('name', 'Simon')
|
|
|
|
self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
|
|
|
|
|
2010-10-28 04:39:20 +08:00
|
|
|
def test_copy(self):
|
2011-03-28 22:20:15 +08:00
|
|
|
for copy_func in [copy.copy, lambda d: d.copy()]:
|
2010-10-28 04:39:20 +08: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-17 00:34:55 +08:00
|
|
|
def test_dict_translation(self):
|
|
|
|
mvd = MultiValueDict({
|
|
|
|
'devs': ['Bob', 'Joe'],
|
|
|
|
'pm': ['Rory'],
|
|
|
|
})
|
|
|
|
d = mvd.dict()
|
2014-03-31 02:25:06 +08:00
|
|
|
self.assertEqual(sorted(six.iterkeys(d)), sorted(six.iterkeys(mvd)))
|
2012-07-25 15:12:59 +08:00
|
|
|
for key in six.iterkeys(mvd):
|
2011-06-17 00:34:55 +08:00
|
|
|
self.assertEqual(d[key], mvd[key])
|
|
|
|
|
|
|
|
self.assertEqual({}, MultiValueDict().dict())
|
|
|
|
|
2010-09-28 16:18:39 +08:00
|
|
|
|
2011-08-13 08:42:08 +08:00
|
|
|
class ImmutableListTests(SimpleTestCase):
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_sort(self):
|
|
|
|
d = ImmutableList(range(10))
|
|
|
|
|
|
|
|
# AttributeError: ImmutableList object is immutable.
|
2011-08-13 08:42:08 +08:00
|
|
|
self.assertRaisesMessage(AttributeError,
|
2010-09-28 16:18:39 +08:00
|
|
|
'ImmutableList object is immutable.', d.sort)
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(repr(d), '(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)')
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_custom_warning(self):
|
|
|
|
d = ImmutableList(range(10), warning="Object is immutable!")
|
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(d[1], 1)
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
# AttributeError: Object is immutable!
|
2011-08-13 08:42:08 +08:00
|
|
|
self.assertRaisesMessage(AttributeError,
|
2010-09-28 16:18:39 +08:00
|
|
|
'Object is immutable!', d.__setitem__, 1, 'test')
|
|
|
|
|
|
|
|
|
2011-08-13 08:42:08 +08:00
|
|
|
class DictWrapperTests(SimpleTestCase):
|
2010-09-28 16:18:39 +08:00
|
|
|
|
|
|
|
def test_dictwrapper(self):
|
|
|
|
f = lambda x: "*%s" % x
|
|
|
|
d = DictWrapper({'a': 'a'}, f, 'xx_')
|
2013-12-13 04:23:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
"Normal: %(a)s. Modified: %(xx_a)s" % d,
|
|
|
|
'Normal: a. Modified: *a'
|
|
|
|
)
|