예제 #1
0
def test_multidict():
    d = MultiDict(a=1, b=2)
    assert d['a'] == 1
    assert d.getall('c') == []

    d.add('a', 2)
    assert d['a'] == 2
    assert d.getall('a') == [1, 2]

    d['b'] = 4
    assert d.getall('b') == [4]
    assert list(d.keys()) == ['a', 'a', 'b']
    assert list(d.items()) == [('a', 1), ('a', 2), ('b', 4)]
    assert d.mixed() == {'a': [1, 2], 'b': 4}

    # test getone

    # KeyError: "Multiple values match 'a': [1, 2]"
    with pytest.raises(KeyError):
        d.getone('a')

    assert d.getone('b') == 4
    # KeyError: "Key not found: 'g'"
    with pytest.raises(KeyError):
        d.getone('g')

    assert d.dict_of_lists() == {'a': [1, 2], 'b': [4]}
    assert 'b' in d
    assert 'e' not in d
    d.clear()
    assert 'b' not in d
    d['a'] = 4
    d.add('a', 5)
    e = d.copy()
    assert 'a' in e
    e.clear()
    e['f'] = 42
    d.update(e)
    assert d == MultiDict([('a', 4), ('a', 5), ('f', 42)])
    f = d.pop('a')
    assert f == 4
    assert d['a'] == 5

    assert d.pop('g', 42) == 42
    with pytest.raises(KeyError):
        d.pop('n')
    # TypeError: pop expected at most 2 arguments, got 3
    with pytest.raises(TypeError):
        d.pop(4, 2, 3)
    d.setdefault('g', []).append(4)
    assert d == MultiDict([('a', 5), ('f', 42), ('g', [4])])
예제 #2
0
def test_multidict():
    d = MultiDict(a=1, b=2)
    assert d['a'] == 1
    assert d.getall('c') == []

    d.add('a', 2)
    assert d['a'] == 2
    assert d.getall('a') == [1, 2]

    d['b'] = 4
    assert d.getall('b') == [4]
    assert list(d.keys()) == ['a', 'a', 'b']
    assert list(d.items()) == [('a', 1), ('a', 2), ('b', 4)]
    assert d.mixed() == {'a': [1, 2], 'b': 4}

    # test getone

    # KeyError: "Multiple values match 'a': [1, 2]"
    with pytest.raises(KeyError):
        d.getone('a')

    assert d.getone('b') == 4
    # KeyError: "Key not found: 'g'"
    with pytest.raises(KeyError):
        d.getone('g')

    assert d.dict_of_lists() == {'a': [1, 2], 'b': [4]}
    assert 'b' in d
    assert 'e' not in d
    d.clear()
    assert 'b' not in d
    d['a'] = 4
    d.add('a', 5)
    e = d.copy()
    assert 'a' in e
    e.clear()
    e['f'] = 42
    d.update(e)
    assert d == MultiDict([('a', 4), ('a', 5), ('f', 42)])
    f = d.pop('a')
    assert f == 4
    assert d['a'] == 5

    assert d.pop('g', 42) == 42
    with pytest.raises(KeyError):
        d.pop('n')
    # TypeError: pop expected at most 2 arguments, got 3
    with pytest.raises(TypeError):
        d.pop(4, 2, 3)
    d.setdefault('g', []).append(4)
    assert d == MultiDict([('a', 5), ('f', 42), ('g', [4])])
예제 #3
0
def test_multidict():
    d = MultiDict(a=1, b=2)
    eq(d['a'], 1)
    eq(d.getall('c'), [])

    d.add('a', 2)
    eq(d['a'], 2)
    eq(d.getall('a'), [1, 2])

    d['b'] = 4
    eq(d.getall('b'), [4])
    eq(list(d.keys()), ['a', 'a', 'b'])
    eq(list(d.items()), [('a', 1), ('a', 2), ('b', 4)])
    eq(d.mixed(), {'a': [1, 2], 'b': 4})

    # test getone

    # KeyError: "Multiple values match 'a': [1, 2]"
    assert_raises(KeyError, d.getone, 'a')
    eq(d.getone('b'), 4)
    # KeyError: "Key not found: 'g'"
    assert_raises(KeyError, d.getone, 'g')

    eq(d.dict_of_lists(), {'a': [1, 2], 'b': [4]})
    assert 'b' in d
    assert 'e' not in d
    d.clear()
    assert 'b' not in d
    d['a'] = 4
    d.add('a', 5)
    e = d.copy()
    assert 'a' in e
    e.clear()
    e['f'] = 42
    d.update(e)
    eq(d, MultiDict([('a', 4), ('a', 5), ('f', 42)]))
    f = d.pop('a')
    eq(f, 4)
    eq(d['a'], 5)


    eq(d.pop('g', 42), 42)
    assert_raises(KeyError, d.pop, 'n')
    # TypeError: pop expected at most 2 arguments, got 3
    assert_raises(TypeError, d.pop, 4, 2, 3)
    d.setdefault('g', []).append(4)
    eq(d, MultiDict([('a', 5), ('f', 42), ('g', [4])]))