1 # -*- coding: utf-8 -*- 2 # (c) 2007 Ian Bicking and Philip Jenvey; written for Paste (http://pythonpaste.org) 3 # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 4 import cgi 5 import six 6 from six.moves import StringIO 7 8 from nose.tools import assert_raises 9 10 from paste.util.multidict import MultiDict, UnicodeMultiDict 11 12 def test_dict(): 13 d = MultiDict({'a': 1}) 14 assert d.items() == [('a', 1)] 15 16 d['b'] = 2 17 d['c'] = 3 18 assert d.items() == [('a', 1), ('b', 2), ('c', 3)] 19 20 d['b'] = 4 21 assert d.items() == [('a', 1), ('c', 3), ('b', 4)] 22 23 d.add('b', 5) 24 assert_raises(KeyError, d.getone, "b") 25 assert d.getall('b') == [4, 5] 26 assert d.items() == [('a', 1), ('c', 3), ('b', 4), ('b', 5)] 27 28 del d['b'] 29 assert d.items() == [('a', 1), ('c', 3)] 30 assert d.pop('xxx', 5) == 5 31 assert d.getone('a') == 1 32 assert d.popitem() == ('c', 3) 33 assert d.items() == [('a', 1)] 34 35 item = [] 36 assert d.setdefault('z', item) is item 37 assert d.items() == [('a', 1), ('z', item)] 38 39 assert d.setdefault('y', 6) == 6 40 41 assert d.mixed() == {'a': 1, 'y': 6, 'z': item} 42 assert d.dict_of_lists() == {'a': [1], 'y': [6], 'z': [item]} 43 44 assert 'a' in d 45 dcopy = d.copy() 46 assert dcopy is not d 47 assert dcopy == d 48 d['x'] = 'x test' 49 assert dcopy != d 50 51 d[(1, None)] = (None, 1) 52 assert d.items() == [('a', 1), ('z', []), ('y', 6), ('x', 'x test'), 53 ((1, None), (None, 1))] 54 55 def test_unicode_dict(): 56 _test_unicode_dict() 57 _test_unicode_dict(decode_param_names=True) 58 59 def _test_unicode_dict(decode_param_names=False): 60 d = UnicodeMultiDict(MultiDict({b'a': 'a test'})) 61 d.encoding = 'utf-8' 62 d.errors = 'ignore' 63 64 if decode_param_names: 65 key_str = six.text_type 66 k = lambda key: key 67 d.decode_keys = True 68 else: 69 key_str = six.binary_type 70 k = lambda key: key.encode() 71 72 def assert_unicode(obj): 73 assert isinstance(obj, six.text_type) 74 75 def assert_key_str(obj): 76 assert isinstance(obj, key_str) 77 78 def assert_unicode_item(obj): 79 key, value = obj 80 assert isinstance(key, key_str) 81 assert isinstance(value, six.text_type) 82 83 assert d.items() == [(k('a'), u'a test')] 84 map(assert_key_str, d.keys()) 85 map(assert_unicode, d.values()) 86 87 d[b'b'] = b'2 test' 88 d[b'c'] = b'3 test' 89 assert d.items() == [(k('a'), u'a test'), (k('b'), u'2 test'), (k('c'), u'3 test')] 90 list(map(assert_unicode_item, d.items())) 91 92 d[k('b')] = b'4 test' 93 assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test'), (k('b'), u'4 test')], d.items() 94 list(map(assert_unicode_item, d.items())) 95 96 d.add(k('b'), b'5 test') 97 assert_raises(KeyError, d.getone, k("b")) 98 assert d.getall(k('b')) == [u'4 test', u'5 test'] 99 map(assert_unicode, d.getall('b')) 100 assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test'), (k('b'), u'4 test'), 101 (k('b'), u'5 test')] 102 list(map(assert_unicode_item, d.items())) 103 104 del d[k('b')] 105 assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test')] 106 list(map(assert_unicode_item, d.items())) 107 assert d.pop('xxx', u'5 test') == u'5 test' 108 assert isinstance(d.pop('xxx', u'5 test'), six.text_type) 109 assert d.getone(k('a')) == u'a test' 110 assert isinstance(d.getone(k('a')), six.text_type) 111 assert d.popitem() == (k('c'), u'3 test') 112 d[k('c')] = b'3 test' 113 assert_unicode_item(d.popitem()) 114 assert d.items() == [(k('a'), u'a test')] 115 list(map(assert_unicode_item, d.items())) 116 117 item = [] 118 assert d.setdefault(k('z'), item) is item 119 items = d.items() 120 assert items == [(k('a'), u'a test'), (k('z'), item)] 121 assert isinstance(items[1][0], key_str) 122 assert isinstance(items[1][1], list) 123 124 assert isinstance(d.setdefault(k('y'), b'y test'), six.text_type) 125 assert isinstance(d[k('y')], six.text_type) 126 127 assert d.mixed() == {k('a'): u'a test', k('y'): u'y test', k('z'): item} 128 assert d.dict_of_lists() == {k('a'): [u'a test'], k('y'): [u'y test'], 129 k('z'): [item]} 130 del d[k('z')] 131 list(map(assert_unicode_item, six.iteritems(d.mixed()))) 132 list(map(assert_unicode_item, [(key, value[0]) for \ 133 key, value in six.iteritems(d.dict_of_lists())])) 134 135 assert k('a') in d 136 dcopy = d.copy() 137 assert dcopy is not d 138 assert dcopy == d 139 d[k('x')] = 'x test' 140 assert dcopy != d 141 142 d[(1, None)] = (None, 1) 143 assert d.items() == [(k('a'), u'a test'), (k('y'), u'y test'), (k('x'), u'x test'), 144 ((1, None), (None, 1))] 145 item = d.items()[-1] 146 assert isinstance(item[0], tuple) 147 assert isinstance(item[1], tuple) 148 149 fs = cgi.FieldStorage() 150 fs.name = 'thefile' 151 fs.filename = 'hello.txt' 152 fs.file = StringIO('hello') 153 d[k('f')] = fs 154 ufs = d[k('f')] 155 assert isinstance(ufs, cgi.FieldStorage) 156 assert ufs is not fs 157 assert ufs.name == fs.name 158 assert isinstance(ufs.name, str if six.PY3 else key_str) 159 assert ufs.filename == fs.filename 160 assert isinstance(ufs.filename, six.text_type) 161 assert isinstance(ufs.value, str) 162 assert ufs.value == 'hello' 163