Exemplo n.º 1
0
def _test_unicode_dict(decode_param_names=False):
    d = UnicodeMultiDict(MultiDict({'a': 'a test'}))
    d.encoding = 'utf-8'
    d.errors = 'ignore'

    if decode_param_names:
        key_str = unicode
        d.decode_keys = True
    else:
        key_str = str

    def assert_unicode(obj):
        assert isinstance(obj, unicode)

    def assert_key_str(obj):
        assert isinstance(obj, key_str)

    def assert_unicode_item(obj):
        key, value = obj
        assert isinstance(key, key_str)
        assert isinstance(value, unicode)

    assert d.items() == [('a', u'a test')]
    map(assert_key_str, d.keys())
    map(assert_unicode, d.values())

    d['b'] = '2 test'
    d['c'] = '3 test'
    assert d.items() == [('a', u'a test'), ('b', u'2 test'), ('c', u'3 test')]
    map(assert_unicode_item, d.items())

    d['b'] = '4 test'
    assert d.items() == [('a', u'a test'), ('c', u'3 test'), ('b', u'4 test')]
    map(assert_unicode_item, d.items())

    d.add('b', '5 test')
    raises(KeyError, 'd.getone("b")')
    assert d.getall('b') == [u'4 test', u'5 test']
    map(assert_unicode, d.getall('b'))
    assert d.items() == [('a', u'a test'), ('c', u'3 test'), ('b', u'4 test'),
                         ('b', u'5 test')]
    map(assert_unicode_item, d.items())

    del d['b']
    assert d.items() == [('a', u'a test'), ('c', u'3 test')]
    map(assert_unicode_item, d.items())
    assert d.pop('xxx', u'5 test') == u'5 test'
    assert isinstance(d.pop('xxx', u'5 test'), unicode)
    assert d.getone('a') == u'a test'
    assert isinstance(d.getone('a'), unicode)
    assert d.popitem() == ('c', u'3 test')
    d['c'] = '3 test'
    assert_unicode_item(d.popitem())
    assert d.items() == [('a', u'a test')]
    map(assert_unicode_item, d.items())

    item = []
    assert d.setdefault('z', item) is item
    items = d.items()
    assert items == [('a', u'a test'), ('z', item)]
    assert isinstance(items[1][0], key_str)
    assert isinstance(items[1][1], list)

    assert isinstance(d.setdefault('y', 'y test'), unicode)
    assert isinstance(d['y'], unicode)

    assert d.mixed() == {u'a': u'a test', u'y': u'y test', u'z': item}
    assert d.dict_of_lists() == {
        u'a': [u'a test'],
        u'y': [u'y test'],
        u'z': [item]
    }
    del d['z']
    map(assert_unicode_item, d.mixed().iteritems())
    map(assert_unicode_item, [(k, v[0]) for \
                                   k, v in d.dict_of_lists().iteritems()])

    assert u'a' in d
    dcopy = d.copy()
    assert dcopy is not d
    assert dcopy == d
    d['x'] = 'x test'
    assert dcopy != d

    d[(1, None)] = (None, 1)
    assert d.items() == [('a', u'a test'), ('y', u'y test'), ('x', u'x test'),
                         ((1, None), (None, 1))]
    item = d.items()[-1]
    assert isinstance(item[0], tuple)
    assert isinstance(item[1], tuple)

    fs = cgi.FieldStorage()
    fs.name = 'thefile'
    fs.filename = 'hello.txt'
    fs.file = StringIO('hello')
    d['f'] = fs
    ufs = d['f']
    assert isinstance(ufs, cgi.FieldStorage)
    assert ufs is not fs
    assert ufs.name == fs.name
    assert isinstance(ufs.name, key_str)
    assert ufs.filename == fs.filename
    assert isinstance(ufs.filename, unicode)
    assert isinstance(ufs.value, str)
    assert ufs.value == 'hello'
Exemplo n.º 2
0
def _test_unicode_dict(decode_param_names=False):
    d = UnicodeMultiDict(MultiDict({'a': 'a test'}))
    d.encoding = 'utf-8'
    d.errors = 'ignore'

    if decode_param_names:
        key_str = str
        d.decode_keys = True
    else:
        key_str = str

    def assert_unicode(obj):
        assert isinstance(obj, str)

    def assert_key_str(obj):
        assert isinstance(obj, key_str)

    def assert_unicode_item(obj):
        key, value = obj
        assert isinstance(key, key_str)
        assert isinstance(value, str)

    assert list(d.items()) == [('a', 'a test')]
    list(map(assert_key_str, list(d.keys())))
    list(map(assert_unicode, list(d.values())))

    d['b'] = '2 test'
    d['c'] = '3 test'
    assert list(d.items()) == [('a', 'a test'), ('b', '2 test'), ('c', '3 test')]
    list(map(assert_unicode_item, list(d.items())))

    d['b'] = '4 test'
    assert list(d.items()) == [('a', 'a test'), ('c', '3 test'), ('b', '4 test')]
    list(map(assert_unicode_item, list(d.items())))

    d.add('b', '5 test')
    assert_raises(KeyError, d.getone, "b")
    assert d.getall('b') == ['4 test', '5 test']
    list(map(assert_unicode, d.getall('b')))
    assert list(d.items()) == [('a', 'a test'), ('c', '3 test'), ('b', '4 test'),
                         ('b', '5 test')]
    list(map(assert_unicode_item, list(d.items())))

    del d['b']
    assert list(d.items()) == [('a', 'a test'), ('c', '3 test')]
    list(map(assert_unicode_item, list(d.items())))
    assert d.pop('xxx', '5 test') == '5 test'
    assert isinstance(d.pop('xxx', '5 test'), str)
    assert d.getone('a') == 'a test'
    assert isinstance(d.getone('a'), str)
    assert d.popitem() == ('c', '3 test')
    d['c'] = '3 test'
    assert_unicode_item(d.popitem())
    assert list(d.items()) == [('a', 'a test')]
    list(map(assert_unicode_item, list(d.items())))

    item = []
    assert d.setdefault('z', item) is item
    items = list(d.items())
    assert items == [('a', 'a test'), ('z', item)]
    assert isinstance(items[1][0], key_str)
    assert isinstance(items[1][1], list)

    assert isinstance(d.setdefault('y', 'y test'), str)
    assert isinstance(d['y'], str)

    assert d.mixed() == {'a': 'a test', 'y': 'y test', 'z': item}
    assert d.dict_of_lists() == {'a': ['a test'], 'y': ['y test'],
                                 'z': [item]}
    del d['z']
    list(map(assert_unicode_item, iter(d.mixed().items())))
    list(map(assert_unicode_item, [(k, v[0]) for \
                                   k, v in d.dict_of_lists().items()]))

    assert 'a' in d
    dcopy = d.copy()
    assert dcopy is not d
    assert dcopy == d
    d['x'] = 'x test'
    assert dcopy != d

    d[(1, None)] = (None, 1)
    assert list(d.items()) == [('a', 'a test'), ('y', 'y test'), ('x', 'x test'),
                         ((1, None), (None, 1))]
    item = list(d.items())[-1]
    assert isinstance(item[0], tuple)
    assert isinstance(item[1], tuple)

    fs = cgi.FieldStorage()
    fs.name = 'thefile'
    fs.filename = 'hello.txt'
    fs.file = StringIO('hello')
    d['f'] = fs
    ufs = d['f']
    assert isinstance(ufs, cgi.FieldStorage)
    assert ufs is not fs
    assert ufs.name == fs.name
    assert isinstance(ufs.name, key_str)
    assert ufs.filename == fs.filename
    assert isinstance(ufs.filename, str)
    assert isinstance(ufs.value, str)
    assert ufs.value == 'hello'
Exemplo n.º 3
0
def _test_unicode_dict(decode_param_names=False):
    d = UnicodeMultiDict(MultiDict({b'a': 'a test'}))
    d.encoding = 'utf-8'
    d.errors = 'ignore'

    if decode_param_names:
        key_str = six.text_type
        k = lambda key: key
        d.decode_keys = True
    else:
        key_str = six.binary_type
        k = lambda key: key.encode()

    def assert_unicode(obj):
        assert isinstance(obj, six.text_type)

    def assert_key_str(obj):
        assert isinstance(obj, key_str)

    def assert_unicode_item(obj):
        key, value = obj
        assert isinstance(key, key_str)
        assert isinstance(value, six.text_type)

    assert d.items() == [(k('a'), u'a test')]
    map(assert_key_str, d.keys())
    map(assert_unicode, d.values())

    d[b'b'] = b'2 test'
    d[b'c'] = b'3 test'
    assert d.items() == [(k('a'), u'a test'), (k('b'), u'2 test'), (k('c'), u'3 test')]
    list(map(assert_unicode_item, d.items()))

    d[k('b')] = b'4 test'
    assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test'), (k('b'), u'4 test')], d.items()
    list(map(assert_unicode_item, d.items()))

    d.add(k('b'), b'5 test')
    pytest.raises(KeyError, d.getone, k("b"))
    assert d.getall(k('b')) == [u'4 test', u'5 test']
    map(assert_unicode, d.getall('b'))
    assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test'), (k('b'), u'4 test'),
                         (k('b'), u'5 test')]
    list(map(assert_unicode_item, d.items()))

    del d[k('b')]
    assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test')]
    list(map(assert_unicode_item, d.items()))
    assert d.pop('xxx', u'5 test') == u'5 test'
    assert isinstance(d.pop('xxx', u'5 test'), six.text_type)
    assert d.getone(k('a')) == u'a test'
    assert isinstance(d.getone(k('a')), six.text_type)
    assert d.popitem() == (k('c'), u'3 test')
    d[k('c')] = b'3 test'
    assert_unicode_item(d.popitem())
    assert d.items() == [(k('a'), u'a test')]
    list(map(assert_unicode_item, d.items()))

    item = []
    assert d.setdefault(k('z'), item) is item
    items = d.items()
    assert items == [(k('a'), u'a test'), (k('z'), item)]
    assert isinstance(items[1][0], key_str)
    assert isinstance(items[1][1], list)

    assert isinstance(d.setdefault(k('y'), b'y test'), six.text_type)
    assert isinstance(d[k('y')], six.text_type)

    assert d.mixed() == {k('a'): u'a test', k('y'): u'y test', k('z'): item}
    assert d.dict_of_lists() == {k('a'): [u'a test'], k('y'): [u'y test'],
                                 k('z'): [item]}
    del d[k('z')]
    list(map(assert_unicode_item, six.iteritems(d.mixed())))
    list(map(assert_unicode_item, [(key, value[0]) for \
                                   key, value in six.iteritems(d.dict_of_lists())]))

    assert k('a') in d
    dcopy = d.copy()
    assert dcopy is not d
    assert dcopy == d
    d[k('x')] = 'x test'
    assert dcopy != d

    d[(1, None)] = (None, 1)
    assert d.items() == [(k('a'), u'a test'), (k('y'), u'y test'), (k('x'), u'x test'),
                         ((1, None), (None, 1))]
    item = d.items()[-1]
    assert isinstance(item[0], tuple)
    assert isinstance(item[1], tuple)

    fs = cgi.FieldStorage()
    fs.name = 'thefile'
    fs.filename = 'hello.txt'
    fs.file = StringIO('hello')
    d[k('f')] = fs
    ufs = d[k('f')]
    assert isinstance(ufs, cgi.FieldStorage)
    assert ufs is not fs
    assert ufs.name == fs.name
    assert isinstance(ufs.name, str if six.PY3 else key_str)
    assert ufs.filename == fs.filename
    assert isinstance(ufs.filename, six.text_type)
    assert isinstance(ufs.value, str)
    assert ufs.value == 'hello'
Exemplo n.º 4
0
def _test_unicode_dict(decode_param_names=False):
    d = UnicodeMultiDict(MultiDict({b'a': 'a test'}))
    d.encoding = 'utf-8'
    d.errors = 'ignore'

    if decode_param_names:
        key_str = six.text_type
        k = lambda key: key
        d.decode_keys = True
    else:
        key_str = six.binary_type
        k = lambda key: key.encode()

    def assert_unicode(obj):
        assert isinstance(obj, six.text_type)

    def assert_key_str(obj):
        assert isinstance(obj, key_str)

    def assert_unicode_item(obj):
        key, value = obj
        assert isinstance(key, key_str)
        assert isinstance(value, six.text_type)

    assert d.items() == [(k('a'), u'a test')]
    map(assert_key_str, d.keys())
    map(assert_unicode, d.values())

    d[b'b'] = b'2 test'
    d[b'c'] = b'3 test'
    assert d.items() == [(k('a'), u'a test'), (k('b'), u'2 test'), (k('c'), u'3 test')]
    list(map(assert_unicode_item, d.items()))

    d[k('b')] = b'4 test'
    assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test'), (k('b'), u'4 test')], d.items()
    list(map(assert_unicode_item, d.items()))

    d.add(k('b'), b'5 test')
    assert_raises(KeyError, d.getone, k("b"))
    assert d.getall(k('b')) == [u'4 test', u'5 test']
    map(assert_unicode, d.getall('b'))
    assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test'), (k('b'), u'4 test'),
                         (k('b'), u'5 test')]
    list(map(assert_unicode_item, d.items()))

    del d[k('b')]
    assert d.items() == [(k('a'), u'a test'), (k('c'), u'3 test')]
    list(map(assert_unicode_item, d.items()))
    assert d.pop('xxx', u'5 test') == u'5 test'
    assert isinstance(d.pop('xxx', u'5 test'), six.text_type)
    assert d.getone(k('a')) == u'a test'
    assert isinstance(d.getone(k('a')), six.text_type)
    assert d.popitem() == (k('c'), u'3 test')
    d[k('c')] = b'3 test'
    assert_unicode_item(d.popitem())
    assert d.items() == [(k('a'), u'a test')]
    list(map(assert_unicode_item, d.items()))

    item = []
    assert d.setdefault(k('z'), item) is item
    items = d.items()
    assert items == [(k('a'), u'a test'), (k('z'), item)]
    assert isinstance(items[1][0], key_str)
    assert isinstance(items[1][1], list)

    assert isinstance(d.setdefault(k('y'), b'y test'), six.text_type)
    assert isinstance(d[k('y')], six.text_type)

    assert d.mixed() == {k('a'): u'a test', k('y'): u'y test', k('z'): item}
    assert d.dict_of_lists() == {k('a'): [u'a test'], k('y'): [u'y test'],
                                 k('z'): [item]}
    del d[k('z')]
    list(map(assert_unicode_item, six.iteritems(d.mixed())))
    list(map(assert_unicode_item, [(key, value[0]) for \
                                   key, value in six.iteritems(d.dict_of_lists())]))

    assert k('a') in d
    dcopy = d.copy()
    assert dcopy is not d
    assert dcopy == d
    d[k('x')] = 'x test'
    assert dcopy != d

    d[(1, None)] = (None, 1)
    assert d.items() == [(k('a'), u'a test'), (k('y'), u'y test'), (k('x'), u'x test'),
                         ((1, None), (None, 1))]
    item = d.items()[-1]
    assert isinstance(item[0], tuple)
    assert isinstance(item[1], tuple)

    fs = cgi.FieldStorage()
    fs.name = 'thefile'
    fs.filename = 'hello.txt'
    fs.file = StringIO('hello')
    d[k('f')] = fs
    ufs = d[k('f')]
    assert isinstance(ufs, cgi.FieldStorage)
    assert ufs is not fs
    assert ufs.name == fs.name
    assert isinstance(ufs.name, str if six.PY3 else key_str)
    assert ufs.filename == fs.filename
    assert isinstance(ufs.filename, six.text_type)
    assert isinstance(ufs.value, str)
    assert ufs.value == 'hello'