Migrated datastructures utils doctests. Thanks to Stephan Jaekel.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13891 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9abfd55a29
commit
082c5d2ae3
|
@ -1,52 +1,67 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.utils.datastructures import SortedDict
|
||||||
|
|
||||||
|
class DatastructuresTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
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):
|
||||||
|
self.assertEquals(self.d1.keys(), [7, 1, 9])
|
||||||
|
self.assertEquals(self.d1.values(), ['seven', 'one', 'nine'])
|
||||||
|
self.assertEquals(self.d1.items(), [(7, 'seven'), (1, 'one'), (9, 'nine')])
|
||||||
|
|
||||||
|
def test_overwrite_ordering(self):
|
||||||
|
""" Overwriting an item keeps it's place. """
|
||||||
|
self.d1[1] = 'ONE'
|
||||||
|
self.assertEquals(self.d1.values(), ['seven', 'ONE', 'nine'])
|
||||||
|
|
||||||
|
def test_append_items(self):
|
||||||
|
""" New items go to the end. """
|
||||||
|
self.d1[0] = 'nil'
|
||||||
|
self.assertEquals(self.d1.keys(), [7, 1, 9, 0])
|
||||||
|
|
||||||
|
def test_delete_and_insert(self):
|
||||||
"""
|
"""
|
||||||
>>> from django.utils.datastructures import SortedDict
|
Deleting an item, then inserting the same key again will place it
|
||||||
|
at the end.
|
||||||
>>> d = SortedDict()
|
|
||||||
>>> d[7] = 'seven'
|
|
||||||
>>> d[1] = 'one'
|
|
||||||
>>> d[9] = 'nine'
|
|
||||||
>>> d.keys()
|
|
||||||
[7, 1, 9]
|
|
||||||
>>> d.values()
|
|
||||||
['seven', 'one', 'nine']
|
|
||||||
>>> d.items()
|
|
||||||
[(7, 'seven'), (1, 'one'), (9, 'nine')]
|
|
||||||
|
|
||||||
# Overwriting an item keeps it's place.
|
|
||||||
>>> d[1] = 'ONE'
|
|
||||||
>>> d.values()
|
|
||||||
['seven', 'ONE', 'nine']
|
|
||||||
|
|
||||||
# New items go to the end.
|
|
||||||
>>> d[0] = 'nil'
|
|
||||||
>>> d.keys()
|
|
||||||
[7, 1, 9, 0]
|
|
||||||
|
|
||||||
# Deleting an item, then inserting the same key again will place it at the end.
|
|
||||||
>>> del d[7]
|
|
||||||
>>> d.keys()
|
|
||||||
[1, 9, 0]
|
|
||||||
>>> d[7] = 'lucky number 7'
|
|
||||||
>>> d.keys()
|
|
||||||
[1, 9, 0, 7]
|
|
||||||
|
|
||||||
# Changing the keys won't do anything, it's only a copy of the keys dict.
|
|
||||||
>>> k = d.keys()
|
|
||||||
>>> k.remove(9)
|
|
||||||
>>> d.keys()
|
|
||||||
[1, 9, 0, 7]
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
>>> d.keys()
|
|
||||||
[2, 1]
|
|
||||||
>>> real_dict = dict(tuples)
|
|
||||||
>>> sorted(real_dict.values())
|
|
||||||
['one', 'second-two']
|
|
||||||
>>> d.values() # Here the order of SortedDict values *is* what we are testing
|
|
||||||
['second-two', 'one']
|
|
||||||
"""
|
"""
|
||||||
|
del self.d2[7]
|
||||||
|
self.assertEquals(self.d2.keys(), [1, 9, 0])
|
||||||
|
self.d2[7] = 'lucky number 7'
|
||||||
|
self.assertEquals(self.d2.keys(), [1, 9, 0, 7])
|
||||||
|
|
||||||
|
def test_change_keys(self):
|
||||||
|
"""
|
||||||
|
Changing the keys won't do anything, it's only a copy of the
|
||||||
|
keys dict.
|
||||||
|
"""
|
||||||
|
k = self.d2.keys()
|
||||||
|
k.remove(9)
|
||||||
|
self.assertEquals(self.d2.keys(), [1, 9, 0, 7])
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
self.assertEquals(d.keys(), [2, 1])
|
||||||
|
|
||||||
|
real_dict = dict(tuples)
|
||||||
|
self.assertEquals(sorted(real_dict.values()), ['one', 'second-two'])
|
||||||
|
|
||||||
|
# Here the order of SortedDict values *is* what we are testing
|
||||||
|
self.assertEquals(d.values(), ['second-two', 'one'])
|
||||||
|
|
Loading…
Reference in New Issue