summaryrefslogtreecommitdiff
path: root/tests/regressiontests/datastructures/tests.py
blob: cbba99b61175434dfe71349bf9a9b821e326eade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
# Tests for stuff in django.utils.datastructures.

>>> import pickle
>>> from django.utils.datastructures import *

### MergeDict #################################################################

>>> d1 = {'chris':'cool','camri':'cute','cotton':'adorable','tulip':'snuggable', 'twoofme':'firstone'}
>>> d2 = {'chris2':'cool2','camri2':'cute2','cotton2':'adorable2','tulip2':'snuggable2'}
>>> d3 = {'chris3':'cool3','camri3':'cute3','cotton3':'adorable3','tulip3':'snuggable3'}
>>> d4 = {'twoofme':'secondone'}
>>> md = MergeDict( d1,d2,d3 )
>>> md['chris']
'cool'
>>> md['camri']
'cute'
>>> md['twoofme']
'firstone'
>>> md2 = md.copy()
>>> md2['chris']
'cool'

MergeDict can merge MultiValueDicts
>>> multi1 = MultiValueDict({'key1': ['value1'], 'key2': ['value2', 'value3']})
>>> multi2 = MultiValueDict({'key2': ['value4'], 'key4': ['value5', 'value6']})
>>> mm = MergeDict(multi1, multi2)

# Although 'key2' appears in both dictionaries, only the first value is used.
>>> mm.getlist('key2')
['value2', 'value3']
>>> mm.getlist('key4')
['value5', 'value6']
>>> mm.getlist('undefined')
[]

>>> sorted(mm.keys())
['key1', 'key2', 'key4']
>>> len(mm.values())
3
>>> "value1" in mm.values()
True
>>> sorted(mm.items(), key=lambda k: k[0])
[('key1', 'value1'), ('key2', 'value3'), ('key4', 'value6')]
>>> [(k,mm.getlist(k)) for k in sorted(mm.keys())]
[('key1', ['value1']), ('key2', ['value2', 'value3']), ('key4', ['value5', 'value6'])]

### MultiValueDict ##########################################################

>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
>>> d['name']
'Simon'
>>> d.get('name')
'Simon'
>>> d.getlist('name')
['Adrian', 'Simon']
>>> list(d.iteritems())
[('position', 'Developer'), ('name', 'Simon')]
>>> list(d.iterlists())
[('position', ['Developer']), ('name', ['Adrian', 'Simon'])]
>>> d['lastname']
Traceback (most recent call last):
...
MultiValueDictKeyError: "Key 'lastname' not found in <MultiValueDict: {'position': ['Developer'], 'name': ['Adrian', 'Simon']}>"
>>> d.get('lastname')

>>> d.get('lastname', 'nonexistent')
'nonexistent'
>>> d.getlist('lastname')
[]
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
>>> d.getlist('lastname')
['Holovaty', 'Willison']
>>> d.values()
['Developer', 'Simon', 'Willison']
>>> list(d.itervalues())
['Developer', 'Simon', 'Willison']

### SortedDict #################################################################

>>> d = SortedDict()
>>> d['one'] = 'one'
>>> d['two'] = 'two'
>>> d['three'] = 'three'
>>> d['one']
'one'
>>> d['two']
'two'
>>> d['three']
'three'
>>> d.keys()
['one', 'two', 'three']
>>> d.values()
['one', 'two', 'three']
>>> d['one'] = 'not one'
>>> d['one']
'not one'
>>> d.keys() == d.copy().keys()
True
>>> d2 = d.copy()
>>> d2['four'] = 'four'
>>> print repr(d)
{'one': 'not one', 'two': 'two', 'three': 'three'}
>>> d.pop('one', 'missing')
'not one'
>>> d.pop('one', 'missing')
'missing'

This SortedDict test tests that it works properly when called with a
a generator expression. But having that syntax anywhere in the test
when run under Python 2.3 will cause a SyntaxError. Thus the rigamorale
with eval so that the test will run and test what it is intended to test
when run on Pythons > 2.3, but not cause a SyntaxError when run on Python 2.3.
>>> import sys
>>> if sys.version_info[0] == 2 and sys.version_info[1] == 3:
...     arg = '[(i, i) for i in xrange(3)]'
... else:
...     arg = '((i, i) for i in xrange(3))'
>>> SortedDict(eval(arg))
{0: 0, 1: 1, 2: 2}

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(d)
>>> _ = d.popitem()
>>> l - len(d)
1

Init from sequence of tuples
>>> d = SortedDict((
... (1, "one"),
... (0, "zero"),
... (2, "two")))
>>> print repr(d)
{1: 'one', 0: 'zero', 2: 'two'}

>>> pickle.loads(pickle.dumps(d, 2))
{1: 'one', 0: 'zero', 2: 'two'}

>>> d.clear()
>>> d
{}
>>> d.keyOrder
[]

### DotExpandedDict ##########################################################

>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], 'person.1.lastname': ['Willison'], 'person.2.firstname': ['Adrian'], 'person.2.lastname': ['Holovaty']})
>>> d['person']['1']['lastname']
['Willison']
>>> d['person']['2']['lastname']
['Holovaty']
>>> d['person']['2']['firstname']
['Adrian']

### ImmutableList ############################################################
>>> d = ImmutableList(range(10))
>>> d.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/python-support/python2.5/django/utils/datastructures.py", line 359, in complain
    raise AttributeError, self.warning
AttributeError: ImmutableList object is immutable.
>>> repr(d)
'(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)'
>>> d = ImmutableList(range(10), warning="Object is immutable!")
>>> d[1]
1
>>> d[1] = 'test'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/python-support/python2.5/django/utils/datastructures.py", line 359, in complain
    raise AttributeError, self.warning
AttributeError: Object is immutable!

### DictWrapper #############################################################

>>> f = lambda x: "*%s" % x
>>> d = DictWrapper({'a': 'a'}, f, 'xx_')
>>> "Normal: %(a)s. Modified: %(xx_a)s" % d
'Normal: a. Modified: *a'

"""

try:
    sorted
except NameError:
    from django.utils.itercompat import sorted  # For Python 2.3