| 1 |
""" |
|---|
| 2 |
# Tests for stuff in django.utils.datastructures. |
|---|
| 3 |
|
|---|
| 4 |
>>> import pickle |
|---|
| 5 |
>>> from django.utils.datastructures import * |
|---|
| 6 |
|
|---|
| 7 |
### MergeDict ################################################################# |
|---|
| 8 |
|
|---|
| 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 ) |
|---|
| 14 |
>>> md['chris'] |
|---|
| 15 |
'cool' |
|---|
| 16 |
>>> md['camri'] |
|---|
| 17 |
'cute' |
|---|
| 18 |
>>> md['twoofme'] |
|---|
| 19 |
'firstone' |
|---|
| 20 |
>>> md2 = md.copy() |
|---|
| 21 |
>>> md2['chris'] |
|---|
| 22 |
'cool' |
|---|
| 23 |
|
|---|
| 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) |
|---|
| 28 |
|
|---|
| 29 |
# Although 'key2' appears in both dictionaries, only the first value is used. |
|---|
| 30 |
>>> mm.getlist('key2') |
|---|
| 31 |
['value2', 'value3'] |
|---|
| 32 |
>>> mm.getlist('key4') |
|---|
| 33 |
['value5', 'value6'] |
|---|
| 34 |
>>> mm.getlist('undefined') |
|---|
| 35 |
[] |
|---|
| 36 |
|
|---|
| 37 |
### MultiValueDict ########################################################## |
|---|
| 38 |
|
|---|
| 39 |
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']}) |
|---|
| 40 |
>>> d['name'] |
|---|
| 41 |
'Simon' |
|---|
| 42 |
>>> d.get('name') |
|---|
| 43 |
'Simon' |
|---|
| 44 |
>>> d.getlist('name') |
|---|
| 45 |
['Adrian', 'Simon'] |
|---|
| 46 |
>>> list(d.iteritems()) |
|---|
| 47 |
[('position', 'Developer'), ('name', 'Simon')] |
|---|
| 48 |
>>> d['lastname'] |
|---|
| 49 |
Traceback (most recent call last): |
|---|
| 50 |
... |
|---|
| 51 |
MultiValueDictKeyError: "Key 'lastname' not found in <MultiValueDict: {'position': ['Developer'], 'name': ['Adrian', 'Simon']}>" |
|---|
| 52 |
>>> d.get('lastname') |
|---|
| 53 |
|
|---|
| 54 |
>>> d.get('lastname', 'nonexistent') |
|---|
| 55 |
'nonexistent' |
|---|
| 56 |
>>> d.getlist('lastname') |
|---|
| 57 |
[] |
|---|
| 58 |
>>> d.setlist('lastname', ['Holovaty', 'Willison']) |
|---|
| 59 |
>>> d.getlist('lastname') |
|---|
| 60 |
['Holovaty', 'Willison'] |
|---|
| 61 |
|
|---|
| 62 |
### SortedDict ################################################################# |
|---|
| 63 |
|
|---|
| 64 |
>>> d = SortedDict() |
|---|
| 65 |
>>> d['one'] = 'one' |
|---|
| 66 |
>>> d['two'] = 'two' |
|---|
| 67 |
>>> d['three'] = 'three' |
|---|
| 68 |
>>> d['one'] |
|---|
| 69 |
'one' |
|---|
| 70 |
>>> d['two'] |
|---|
| 71 |
'two' |
|---|
| 72 |
>>> d['three'] |
|---|
| 73 |
'three' |
|---|
| 74 |
>>> d.keys() |
|---|
| 75 |
['one', 'two', 'three'] |
|---|
| 76 |
>>> d.values() |
|---|
| 77 |
['one', 'two', 'three'] |
|---|
| 78 |
>>> d['one'] = 'not one' |
|---|
| 79 |
>>> d['one'] |
|---|
| 80 |
'not one' |
|---|
| 81 |
>>> d.keys() == d.copy().keys() |
|---|
| 82 |
True |
|---|
| 83 |
>>> d2 = d.copy() |
|---|
| 84 |
>>> d2['four'] = 'four' |
|---|
| 85 |
>>> print repr(d) |
|---|
| 86 |
{'one': 'not one', 'two': 'two', 'three': 'three'} |
|---|
| 87 |
>>> d.pop('one', 'missing') |
|---|
| 88 |
'not one' |
|---|
| 89 |
>>> d.pop('one', 'missing') |
|---|
| 90 |
'missing' |
|---|
| 91 |
|
|---|
| 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. |
|---|
| 94 |
>>> l = len(d) |
|---|
| 95 |
>>> _ = d.popitem() |
|---|
| 96 |
>>> l - len(d) |
|---|
| 97 |
1 |
|---|
| 98 |
|
|---|
| 99 |
Init from sequence of tuples |
|---|
| 100 |
>>> d = SortedDict(( |
|---|
| 101 |
... (1, "one"), |
|---|
| 102 |
... (0, "zero"), |
|---|
| 103 |
... (2, "two"))) |
|---|
| 104 |
>>> print repr(d) |
|---|
| 105 |
{1: 'one', 0: 'zero', 2: 'two'} |
|---|
| 106 |
|
|---|
| 107 |
>>> pickle.loads(pickle.dumps(d, 2)) |
|---|
| 108 |
{1: 'one', 0: 'zero', 2: 'two'} |
|---|
| 109 |
|
|---|
| 110 |
>>> d.clear() |
|---|
| 111 |
>>> d |
|---|
| 112 |
{} |
|---|
| 113 |
>>> d.keyOrder |
|---|
| 114 |
[] |
|---|
| 115 |
|
|---|
| 116 |
### DotExpandedDict ########################################################## |
|---|
| 117 |
|
|---|
| 118 |
>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], 'person.1.lastname': ['Willison'], 'person.2.firstname': ['Adrian'], 'person.2.lastname': ['Holovaty']}) |
|---|
| 119 |
>>> d['person']['1']['lastname'] |
|---|
| 120 |
['Willison'] |
|---|
| 121 |
>>> d['person']['2']['lastname'] |
|---|
| 122 |
['Holovaty'] |
|---|
| 123 |
>>> d['person']['2']['firstname'] |
|---|
| 124 |
['Adrian'] |
|---|
| 125 |
|
|---|
| 126 |
### ImmutableList ############################################################ |
|---|
| 127 |
>>> d = ImmutableList(range(10)) |
|---|
| 128 |
>>> d.sort() |
|---|
| 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. |
|---|
| 134 |
>>> repr(d) |
|---|
| 135 |
'(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)' |
|---|
| 136 |
>>> d = ImmutableList(range(10), warning="Object is immutable!") |
|---|
| 137 |
>>> d[1] |
|---|
| 138 |
1 |
|---|
| 139 |
>>> d[1] = 'test' |
|---|
| 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! |
|---|
| 145 |
|
|---|
| 146 |
### DictWrapper ############################################################# |
|---|
| 147 |
|
|---|
| 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' |
|---|
| 152 |
|
|---|
| 153 |
""" |
|---|