2
# Tests for stuff in django.utils.datastructures.
5
>>> from django.utils.datastructures import *
7
### MergeDict #################################################################
9
>>> d1 = {'chris':'cool','camri':'cute','cotton':'adorable','tulip':'snuggable', 'twoofme':'firstone'}
10
>>> d2 = {'chris2':'cool2','camri2':'cute2','cotton2':'adorable2','tulip2':'snuggable2'}
11
>>> d3 = {'chris3':'cool3','camri3':'cute3','cotton3':'adorable3','tulip3':'snuggable3'}
12
>>> d4 = {'twoofme':'secondone'}
13
>>> md = MergeDict( d1,d2,d3 )
24
MergeDict can merge MultiValueDicts
25
>>> multi1 = MultiValueDict({'key1': ['value1'], 'key2': ['value2', 'value3']})
26
>>> multi2 = MultiValueDict({'key2': ['value4'], 'key4': ['value5', 'value6']})
27
>>> mm = MergeDict(multi1, multi2)
29
# Although 'key2' appears in both dictionaries, only the first value is used.
30
>>> mm.getlist('key2')
32
>>> mm.getlist('key4')
34
>>> mm.getlist('undefined')
37
### MultiValueDict ##########################################################
39
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
46
>>> list(d.iteritems())
47
[('position', 'Developer'), ('name', 'Simon')]
49
Traceback (most recent call last):
51
MultiValueDictKeyError: "Key 'lastname' not found in <MultiValueDict: {'position': ['Developer'], 'name': ['Adrian', 'Simon']}>"
54
>>> d.get('lastname', 'nonexistent')
56
>>> d.getlist('lastname')
58
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
59
>>> d.getlist('lastname')
60
['Holovaty', 'Willison']
62
### SortedDict #################################################################
67
>>> d['three'] = 'three'
75
['one', 'two', 'three']
77
['one', 'two', 'three']
78
>>> d['one'] = 'not one'
81
>>> d.keys() == d.copy().keys()
84
>>> d2['four'] = 'four'
86
{'one': 'not one', 'two': 'two', 'three': 'three'}
87
>>> d.pop('one', 'missing')
89
>>> d.pop('one', 'missing')
92
We don't know which item will be popped in popitem(), so we'll just check that
93
the number of keys has decreased.
99
Init from sequence of tuples
105
{1: 'one', 0: 'zero', 2: 'two'}
107
>>> pickle.loads(pickle.dumps(d, 2))
108
{1: 'one', 0: 'zero', 2: 'two'}
116
### DotExpandedDict ##########################################################
118
>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], 'person.1.lastname': ['Willison'], 'person.2.firstname': ['Adrian'], 'person.2.lastname': ['Holovaty']})
119
>>> d['person']['1']['lastname']
121
>>> d['person']['2']['lastname']
123
>>> d['person']['2']['firstname']
126
### ImmutableList ############################################################
127
>>> d = ImmutableList(range(10))
129
Traceback (most recent call last):
130
File "<stdin>", line 1, in <module>
131
File "/var/lib/python-support/python2.5/django/utils/datastructures.py", line 359, in complain
132
raise AttributeError, self.warning
133
AttributeError: ImmutableList object is immutable.
135
'(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)'
136
>>> d = ImmutableList(range(10), warning="Object is immutable!")
140
Traceback (most recent call last):
141
File "<stdin>", line 1, in <module>
142
File "/var/lib/python-support/python2.5/django/utils/datastructures.py", line 359, in complain
143
raise AttributeError, self.warning
144
AttributeError: Object is immutable!
146
### DictWrapper #############################################################
148
>>> f = lambda x: "*%s" % x
149
>>> d = DictWrapper({'a': 'a'}, f, 'xx_')
150
>>> "Normal: %(a)s. Modified: %(xx_a)s" % d
151
'Normal: a. Modified: *a'