Exemplo n.º 1
0
    def test_basic(self):
        @datastructures.native_itermethods(['keys', 'values', 'items'])
        class StupidDict(object):
            def keys(self, multi=1):
                return iter(['a', 'b', 'c'] * multi)

            def values(self, multi=1):
                return iter([1, 2, 3] * multi)

            def items(self, multi=1):
                return iter(
                    zip(iterkeys(self, multi=multi),
                        itervalues(self, multi=multi)))

        d = StupidDict()
        expected_keys = ['a', 'b', 'c']
        expected_values = [1, 2, 3]
        expected_items = list(zip(expected_keys, expected_values))

        self.assert_equal(list(iterkeys(d)), expected_keys)
        self.assert_equal(list(itervalues(d)), expected_values)
        self.assert_equal(list(iteritems(d)), expected_items)

        self.assert_equal(list(iterkeys(d, 2)), expected_keys * 2)
        self.assert_equal(list(itervalues(d, 2)), expected_values * 2)
        self.assert_equal(list(iteritems(d, 2)), expected_items * 2)
Exemplo n.º 2
0
    def test_basic(self):
        @datastructures.native_itermethods(["keys", "values", "items"])
        class StupidDict(object):
            def keys(self, multi=1):
                return iter(["a", "b", "c"] * multi)

            def values(self, multi=1):
                return iter([1, 2, 3] * multi)

            def items(self, multi=1):
                return iter(
                    zip(iterkeys(self, multi=multi),
                        itervalues(self, multi=multi)))

        d = StupidDict()
        expected_keys = ["a", "b", "c"]
        expected_values = [1, 2, 3]
        expected_items = list(zip(expected_keys, expected_values))

        assert list(iterkeys(d)) == expected_keys
        assert list(itervalues(d)) == expected_values
        assert list(iteritems(d)) == expected_items

        assert list(iterkeys(d, 2)) == expected_keys * 2
        assert list(itervalues(d, 2)) == expected_values * 2
        assert list(iteritems(d, 2)) == expected_items * 2
Exemplo n.º 3
0
    def test_basic(self):
        @datastructures.native_itermethods(["keys", "values", "items"])
        class StupidDict(object):
            def keys(self, multi=1):
                return iter(["a", "b", "c"] * multi)

            def values(self, multi=1):
                return iter([1, 2, 3] * multi)

            def items(self, multi=1):
                return iter(
                    zip(iterkeys(self, multi=multi), itervalues(self, multi=multi))
                )

        d = StupidDict()
        expected_keys = ["a", "b", "c"]
        expected_values = [1, 2, 3]
        expected_items = list(zip(expected_keys, expected_values))

        assert list(iterkeys(d)) == expected_keys
        assert list(itervalues(d)) == expected_values
        assert list(iteritems(d)) == expected_items

        assert list(iterkeys(d, 2)) == expected_keys * 2
        assert list(itervalues(d, 2)) == expected_values * 2
        assert list(iteritems(d, 2)) == expected_items * 2
Exemplo n.º 4
0
    def test_basic(self):
        @datastructures.native_itermethods(['keys', 'values', 'items'])
        class StupidDict(object):
            def keys(self, multi=1):
                return iter(['a', 'b', 'c'] * multi)

            def values(self, multi=1):
                return iter([1, 2, 3] * multi)

            def items(self, multi=1):
                return iter(zip(iterkeys(self, multi=multi),
                                itervalues(self, multi=multi)))

        d = StupidDict()
        expected_keys = ['a', 'b', 'c']
        expected_values = [1, 2, 3]
        expected_items = list(zip(expected_keys, expected_values))

        self.assert_equal(list(iterkeys(d)), expected_keys)
        self.assert_equal(list(itervalues(d)), expected_values)
        self.assert_equal(list(iteritems(d)), expected_items)

        self.assert_equal(list(iterkeys(d, 2)), expected_keys * 2)
        self.assert_equal(list(itervalues(d, 2)), expected_values * 2)
        self.assert_equal(list(iteritems(d, 2)), expected_items * 2)
Exemplo n.º 5
0
 def update_lang(self, value):
     self.lang = langs.get_lang_dict(value)
     self.locale = langs.get_locale_dict(self, value)
     common.SETTINGS['LANGUAGE'] = value
     common.LOCALE = self.locale
     for key in iterkeys(common.LOCALE):
         common.__dict__[key] = common.LOCALE[key]
Exemplo n.º 6
0
 def update_lang(self, value):
     self.lang = langs.get_lang_dict(self, value)
     self.locale = langs.get_locale_dict(self, value)
     common.SETTINGS['LANGUAGE'] = value
     common.LOCALE = self.locale
     for key in iterkeys(common.LOCALE):
         common.__dict__[key] = common.LOCALE[key]
Exemplo n.º 7
0
def remove_attr(task):
    for key in list(iterkeys(task.__dict__)):
        try:
            value = task.init_dict[key]
            if hasattr(task.__dict__[key], '__call__'):
                task.__dict__[key] = value
        except:
            del task.__dict__[key]
Exemplo n.º 8
0
 def test_iter_interfaces(self):
     mapping = [('a', 1), ('b', 2), ('a', 2), ('d', 3), ('a', 1), ('a', 3),
                ('d', 4), ('c', 3)]
     md = self.storage_class(mapping)
     assert list(zip(md.keys(), md.listvalues())) == list(md.lists())
     assert list(zip(md, iterlistvalues(md))) == list(iterlists(md))
     assert list(zip(iterkeys(md), iterlistvalues(md))) == \
         list(iterlists(md))
Exemplo n.º 9
0
 def read_settings(self, keys=None):
     if not keys:
         keys = list(iterkeys(self.DEFAULT_SETTINGS))
     params = self.read_params(keys)
     for key, value in iteritems(params):
         self.__dict__[key] = value
     if self.__dict__.get('upload_file_ext'):
         del self.__dict__['upload_file_ext']
Exemplo n.º 10
0
 def test_iter_interfaces(self):
     mapping = [('a', 1), ('b', 2), ('a', 2), ('d', 3),
                ('a', 1), ('a', 3), ('d', 4), ('c', 3)]
     md = self.storage_class(mapping)
     assert list(zip(md.keys(), md.listvalues())) == list(md.lists())
     assert list(zip(md, iterlistvalues(md))) == list(iterlists(md))
     assert list(zip(iterkeys(md), iterlistvalues(md))) == \
         list(iterlists(md))
Exemplo n.º 11
0
 def print_band(self, band, dic=None, update_band_text=None):
     text = self.template_content['bands'][band]
     if dic:
         d = dic.copy()
         for key, value in iteritems(d):
             if type(value) in string_types:
                 d[key] = escape(value)
         cell_start = 0
         cell_start_tag = to_bytes('<table:table-cell', 'utf-8')
         cell_type_tag = to_bytes('office:value-type="string"', 'utf-8')
         calcext_type_tag = to_bytes('calcext:value-type="string"', 'utf-8')
         start_tag = to_bytes('<text:p>', 'utf-8')
         end_tag = to_bytes('</text:p>', 'utf-8')
         while True:
             cell_start = self.find(text, cell_start_tag, cell_start)
             if cell_start == -1:
                 break
             else:
                 start = self.find(text, start_tag, cell_start)
                 if start != -1:
                     end = self.find(text, end_tag, start + len(start_tag))
                     if end != -1:
                         text_start = start + len(start_tag)
                         text_end = end
                         cell_text = text[text_start:text_end]
                         cell_text_start = self.find(cell_text, to_bytes('%(', 'utf-8'), 0)
                         if cell_text_start != -1:
                             end = self.find(cell_text, to_bytes(')s', 'utf-8'), cell_text_start + 2)
                             if end != -1:
                                 end += 2
                                 val = cell_text[cell_text_start:end]
                                 key = val[2:-2]
                                 value = d.get(to_unicode(key, 'utf-8'))
                                 if isinstance(value, DBField):
                                     raise Exception('Report: "%s" band: "%s" key "%s" a field object is passed. Specify the value attribute.' % \
                                         (self.item_name, band, key))
                                 elif not value is None:
                                     val = to_unicode(val, 'utf-8')
                                     val = val % d
                                     val = to_bytes(val, 'utf-8')
                                     if type(value) == float:
                                         val = self.replace(val, '.', consts.DECIMAL_POINT)
                                 else:
                                     if not key in iterkeys(d):
                                         self.log.info('Report: "%s" band: "%s" key "%s" not found in the dictionary' % \
                                             (self.item_name, band, key))
                                 cell_text = to_bytes('%s%s%s', 'utf-8') % (cell_text[:cell_text_start], val, cell_text[end:])
                                 text = to_bytes('', 'utf-8').join([text[:text_start], cell_text, text[text_end:]])
                                 if type(value) in (int, float):
                                     start_text = text[cell_start:start]
                                     office_value = value
                                     start_text = self.replace(start_text, cell_type_tag, 'office:value-type="float" office:value="%s"' % office_value)
                                     start_text = self.replace(start_text, calcext_type_tag, 'calcext:value-type="float"')
                                     text = to_bytes('', 'utf-8').join([text[:cell_start], start_text, text[start:]])
                 cell_start += 1
         if update_band_text:
             text = update_band_text(text)
     self.content.write(text)
Exemplo n.º 12
0
    def test_iterables(self):
        a = datastructures.MultiDict((("key_a", "value_a"),))
        b = datastructures.MultiDict((("key_b", "value_b"),))
        ab = datastructures.CombinedMultiDict((a, b))

        assert sorted(ab.lists()) == [("key_a", ["value_a"]), ("key_b", ["value_b"])]
        assert sorted(ab.listvalues()) == [["value_a"], ["value_b"]]
        assert sorted(ab.keys()) == ["key_a", "key_b"]

        assert sorted(iterlists(ab)) == [("key_a", ["value_a"]), ("key_b", ["value_b"])]
        assert sorted(iterlistvalues(ab)) == [["value_a"], ["value_b"]]
        assert sorted(iterkeys(ab)) == ["key_a", "key_b"]
Exemplo n.º 13
0
    def test_iterables(self):
        a = datastructures.MultiDict((("key_a", "value_a"),))
        b = datastructures.MultiDict((("key_b", "value_b"),))
        ab = datastructures.CombinedMultiDict((a,b))

        self.assert_equal(sorted(ab.lists()), [('key_a', ['value_a']), ('key_b', ['value_b'])])
        self.assert_equal(sorted(ab.listvalues()), [['value_a'], ['value_b']])
        self.assert_equal(sorted(ab.keys()), ["key_a", "key_b"])

        self.assert_equal(sorted(iterlists(ab)), [('key_a', ['value_a']), ('key_b', ['value_b'])])
        self.assert_equal(sorted(iterlistvalues(ab)), [['value_a'], ['value_b']])
        self.assert_equal(sorted(iterkeys(ab)), ["key_a", "key_b"])
Exemplo n.º 14
0
    def test_iterables(self):
        a = datastructures.MultiDict((("key_a", "value_a"),))
        b = datastructures.MultiDict((("key_b", "value_b"),))
        ab = datastructures.CombinedMultiDict((a, b))

        assert sorted(ab.lists()) == [('key_a', ['value_a']), ('key_b', ['value_b'])]
        assert sorted(ab.listvalues()) == [['value_a'], ['value_b']]
        assert sorted(ab.keys()) == ["key_a", "key_b"]

        assert sorted(iterlists(ab)) == [('key_a', ['value_a']), ('key_b', ['value_b'])]
        assert sorted(iterlistvalues(ab)) == [['value_a'], ['value_b']]
        assert sorted(iterkeys(ab)) == ["key_a", "key_b"]
Exemplo n.º 15
0
 def test_iter_interfaces(self):
     mapping = [
         ("a", 1),
         ("b", 2),
         ("a", 2),
         ("d", 3),
         ("a", 1),
         ("a", 3),
         ("d", 4),
         ("c", 3),
     ]
     md = self.storage_class(mapping)
     assert list(zip(md.keys(), md.listvalues())) == list(md.lists())
     assert list(zip(md, iterlistvalues(md))) == list(iterlists(md))
     assert list(zip(iterkeys(md), iterlistvalues(md))) == list(iterlists(md))
Exemplo n.º 16
0
 def test_iter_interfaces(self):
     mapping = [
         ("a", 1),
         ("b", 2),
         ("a", 2),
         ("d", 3),
         ("a", 1),
         ("a", 3),
         ("d", 4),
         ("c", 3),
     ]
     md = self.storage_class(mapping)
     assert list(zip(md.keys(), md.listvalues())) == list(md.lists())
     assert list(zip(md, iterlistvalues(md))) == list(iterlists(md))
     assert list(zip(iterkeys(md), iterlistvalues(md))) == list(iterlists(md))
Exemplo n.º 17
0
    def test_basic_interface(self):
        md = self.storage_class()
        assert isinstance(md, dict)

        mapping = [('a', 1), ('b', 2), ('a', 2), ('d', 3), ('a', 1), ('a', 3),
                   ('d', 4), ('c', 3)]
        md = self.storage_class(mapping)

        # simple getitem gives the first value
        assert md['a'] == 1
        assert md['c'] == 3
        with pytest.raises(KeyError):
            md['e']
        assert md.get('a') == 1

        # list getitem
        assert md.getlist('a') == [1, 2, 1, 3]
        assert md.getlist('d') == [3, 4]
        # do not raise if key not found
        assert md.getlist('x') == []

        # simple setitem overwrites all values
        md['a'] = 42
        assert md.getlist('a') == [42]

        # list setitem
        md.setlist('a', [1, 2, 3])
        assert md['a'] == 1
        assert md.getlist('a') == [1, 2, 3]

        # verify that it does not change original lists
        l1 = [1, 2, 3]
        md.setlist('a', l1)
        del l1[:]
        assert md['a'] == 1

        # setdefault, setlistdefault
        assert md.setdefault('u', 23) == 23
        assert md.getlist('u') == [23]
        del md['u']

        md.setlist('u', [-1, -2])

        # delitem
        del md['u']
        with pytest.raises(KeyError):
            md['u']
        del md['d']
        assert md.getlist('d') == []

        # keys, values, items, lists
        assert list(sorted(md.keys())) == ['a', 'b', 'c']
        assert list(sorted(iterkeys(md))) == ['a', 'b', 'c']

        assert list(sorted(itervalues(md))) == [1, 2, 3]
        assert list(sorted(itervalues(md))) == [1, 2, 3]

        assert list(sorted(md.items())) == [('a', 1), ('b', 2), ('c', 3)]
        assert list(sorted(md.items(multi=True))) == \
            [('a', 1), ('a', 2), ('a', 3), ('b', 2), ('c', 3)]
        assert list(sorted(iteritems(md))) == [('a', 1), ('b', 2), ('c', 3)]
        assert list(sorted(iteritems(md, multi=True))) == \
            [('a', 1), ('a', 2), ('a', 3), ('b', 2), ('c', 3)]

        assert list(sorted(md.lists())) == \
            [('a', [1, 2, 3]), ('b', [2]), ('c', [3])]
        assert list(sorted(iterlists(md))) == \
            [('a', [1, 2, 3]), ('b', [2]), ('c', [3])]

        # copy method
        c = md.copy()
        assert c['a'] == 1
        assert c.getlist('a') == [1, 2, 3]

        # copy method 2
        c = copy(md)
        assert c['a'] == 1
        assert c.getlist('a') == [1, 2, 3]

        # deepcopy method
        c = md.deepcopy()
        assert c['a'] == 1
        assert c.getlist('a') == [1, 2, 3]

        # deepcopy method 2
        c = deepcopy(md)
        assert c['a'] == 1
        assert c.getlist('a') == [1, 2, 3]

        # update with a multidict
        od = self.storage_class([('a', 4), ('a', 5), ('y', 0)])
        md.update(od)
        assert md.getlist('a') == [1, 2, 3, 4, 5]
        assert md.getlist('y') == [0]

        # update with a regular dict
        md = c
        od = {'a': 4, 'y': 0}
        md.update(od)
        assert md.getlist('a') == [1, 2, 3, 4]
        assert md.getlist('y') == [0]

        # pop, poplist, popitem, popitemlist
        assert md.pop('y') == 0
        assert 'y' not in md
        assert md.poplist('a') == [1, 2, 3, 4]
        assert 'a' not in md
        assert md.poplist('missing') == []

        # remaining: b=2, c=3
        popped = md.popitem()
        assert popped in [('b', 2), ('c', 3)]
        popped = md.popitemlist()
        assert popped in [('b', [2]), ('c', [3])]

        # type conversion
        md = self.storage_class({'a': '4', 'b': ['2', '3']})
        assert md.get('a', type=int) == 4
        assert md.getlist('b', type=int) == [2, 3]

        # repr
        md = self.storage_class([('a', 1), ('a', 2), ('b', 3)])
        assert "('a', 1)" in repr(md)
        assert "('a', 2)" in repr(md)
        assert "('b', 3)" in repr(md)

        # add and getlist
        md.add('c', '42')
        md.add('c', '23')
        assert md.getlist('c') == ['42', '23']
        md.add('c', 'blah')
        assert md.getlist('c', type=int) == [42, 23]

        # setdefault
        md = self.storage_class()
        md.setdefault('x', []).append(42)
        md.setdefault('x', []).append(23)
        assert md['x'] == [42, 23]

        # to dict
        md = self.storage_class()
        md['foo'] = 42
        md.add('bar', 1)
        md.add('bar', 2)
        assert md.to_dict() == {'foo': 42, 'bar': 1}
        assert md.to_dict(flat=False) == {'foo': [42], 'bar': [1, 2]}

        # popitem from empty dict
        with pytest.raises(KeyError):
            self.storage_class().popitem()

        with pytest.raises(KeyError):
            self.storage_class().popitemlist()

        # key errors are of a special type
        with pytest.raises(BadRequestKeyError):
            self.storage_class()[42]

        # setlist works
        md = self.storage_class()
        md['foo'] = 42
        md.setlist('foo', [1, 2])
        assert md.getlist('foo') == [1, 2]
Exemplo n.º 18
0
    def test_basic_interface(self):
        md = self.storage_class()
        assert isinstance(md, dict)

        mapping = [
            ("a", 1),
            ("b", 2),
            ("a", 2),
            ("d", 3),
            ("a", 1),
            ("a", 3),
            ("d", 4),
            ("c", 3),
        ]
        md = self.storage_class(mapping)

        # simple getitem gives the first value
        assert md["a"] == 1
        assert md["c"] == 3
        with pytest.raises(KeyError):
            md["e"]
        assert md.get("a") == 1

        # list getitem
        assert md.getlist("a") == [1, 2, 1, 3]
        assert md.getlist("d") == [3, 4]
        # do not raise if key not found
        assert md.getlist("x") == []

        # simple setitem overwrites all values
        md["a"] = 42
        assert md.getlist("a") == [42]

        # list setitem
        md.setlist("a", [1, 2, 3])
        assert md["a"] == 1
        assert md.getlist("a") == [1, 2, 3]

        # verify that it does not change original lists
        l1 = [1, 2, 3]
        md.setlist("a", l1)
        del l1[:]
        assert md["a"] == 1

        # setdefault, setlistdefault
        assert md.setdefault("u", 23) == 23
        assert md.getlist("u") == [23]
        del md["u"]

        md.setlist("u", [-1, -2])

        # delitem
        del md["u"]
        with pytest.raises(KeyError):
            md["u"]
        del md["d"]
        assert md.getlist("d") == []

        # keys, values, items, lists
        assert list(sorted(md.keys())) == ["a", "b", "c"]
        assert list(sorted(iterkeys(md))) == ["a", "b", "c"]

        assert list(sorted(itervalues(md))) == [1, 2, 3]
        assert list(sorted(itervalues(md))) == [1, 2, 3]

        assert list(sorted(md.items())) == [("a", 1), ("b", 2), ("c", 3)]
        assert list(sorted(md.items(multi=True))) == [
            ("a", 1),
            ("a", 2),
            ("a", 3),
            ("b", 2),
            ("c", 3),
        ]
        assert list(sorted(iteritems(md))) == [("a", 1), ("b", 2), ("c", 3)]
        assert list(sorted(iteritems(md, multi=True))) == [
            ("a", 1),
            ("a", 2),
            ("a", 3),
            ("b", 2),
            ("c", 3),
        ]

        assert list(sorted(md.lists())) == [("a", [1, 2, 3]), ("b", [2]), ("c", [3])]
        assert list(sorted(iterlists(md))) == [("a", [1, 2, 3]), ("b", [2]), ("c", [3])]

        # copy method
        c = md.copy()
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # copy method 2
        c = copy(md)
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # deepcopy method
        c = md.deepcopy()
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # deepcopy method 2
        c = deepcopy(md)
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # update with a multidict
        od = self.storage_class([("a", 4), ("a", 5), ("y", 0)])
        md.update(od)
        assert md.getlist("a") == [1, 2, 3, 4, 5]
        assert md.getlist("y") == [0]

        # update with a regular dict
        md = c
        od = {"a": 4, "y": 0}
        md.update(od)
        assert md.getlist("a") == [1, 2, 3, 4]
        assert md.getlist("y") == [0]

        # pop, poplist, popitem, popitemlist
        assert md.pop("y") == 0
        assert "y" not in md
        assert md.poplist("a") == [1, 2, 3, 4]
        assert "a" not in md
        assert md.poplist("missing") == []

        # remaining: b=2, c=3
        popped = md.popitem()
        assert popped in [("b", 2), ("c", 3)]
        popped = md.popitemlist()
        assert popped in [("b", [2]), ("c", [3])]

        # type conversion
        md = self.storage_class({"a": "4", "b": ["2", "3"]})
        assert md.get("a", type=int) == 4
        assert md.getlist("b", type=int) == [2, 3]

        # repr
        md = self.storage_class([("a", 1), ("a", 2), ("b", 3)])
        assert "('a', 1)" in repr(md)
        assert "('a', 2)" in repr(md)
        assert "('b', 3)" in repr(md)

        # add and getlist
        md.add("c", "42")
        md.add("c", "23")
        assert md.getlist("c") == ["42", "23"]
        md.add("c", "blah")
        assert md.getlist("c", type=int) == [42, 23]

        # setdefault
        md = self.storage_class()
        md.setdefault("x", []).append(42)
        md.setdefault("x", []).append(23)
        assert md["x"] == [42, 23]

        # to dict
        md = self.storage_class()
        md["foo"] = 42
        md.add("bar", 1)
        md.add("bar", 2)
        assert md.to_dict() == {"foo": 42, "bar": 1}
        assert md.to_dict(flat=False) == {"foo": [42], "bar": [1, 2]}

        # popitem from empty dict
        with pytest.raises(KeyError):
            self.storage_class().popitem()

        with pytest.raises(KeyError):
            self.storage_class().popitemlist()

        # key errors are of a special type
        with pytest.raises(BadRequestKeyError):
            self.storage_class()[42]

        # setlist works
        md = self.storage_class()
        md["foo"] = 42
        md.setlist("foo", [1, 2])
        assert md.getlist("foo") == [1, 2]
Exemplo n.º 19
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add('foo', 'bar')
        assert len(d) == 1
        d.add('foo', 'baz')
        assert len(d) == 1
        assert list(iteritems(d)) == [('foo', 'bar')]
        assert list(d) == ['foo']
        assert list(iteritems(d, multi=True)) == \
            [('foo', 'bar'), ('foo', 'baz')]
        del d['foo']
        assert not d
        assert len(d) == 0
        assert list(d) == []

        d.update([('foo', 1), ('foo', 2), ('bar', 42)])
        d.add('foo', 3)
        assert d.getlist('foo') == [1, 2, 3]
        assert d.getlist('bar') == [42]
        assert list(iteritems(d)) == [('foo', 1), ('bar', 42)]

        expected = ['foo', 'bar']

        assert list(d.keys()) == expected
        assert list(d) == expected
        assert list(iterkeys(d)) == expected

        assert list(iteritems(d, multi=True)) == \
            [('foo', 1), ('foo', 2), ('bar', 42), ('foo', 3)]
        assert len(d) == 2

        assert d.pop('foo') == 1
        assert d.pop('blafasel', None) is None
        assert d.pop('blafasel', 42) == 42
        assert len(d) == 1
        assert d.poplist('bar') == [42]
        assert not d

        d.get('missingkey') is None

        d.add('foo', 42)
        d.add('foo', 23)
        d.add('bar', 2)
        d.add('foo', 42)
        assert d == datastructures.MultiDict(d)
        id = self.storage_class(d)
        assert d == id
        d.add('foo', 2)
        assert d != id

        d.update({'blah': [1, 2, 3]})
        assert d['blah'] == 1
        assert d.getlist('blah') == [1, 2, 3]

        # setlist works
        d = self.storage_class()
        d['foo'] = 42
        d.setlist('foo', [1, 2])
        assert d.getlist('foo') == [1, 2]

        with pytest.raises(BadRequestKeyError):
            d.pop('missing')
        with pytest.raises(BadRequestKeyError):
            d['missing']

        # popping
        d = self.storage_class()
        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        assert d.popitem() == ('foo', 23)
        with pytest.raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        assert d.popitemlist() == ('foo', [23, 42, 1])

        with pytest.raises(BadRequestKeyError):
            d.popitemlist()
Exemplo n.º 20
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add('foo', 'bar')
        assert len(d) == 1
        d.add('foo', 'baz')
        assert len(d) == 1
        assert list(iteritems(d)) == [('foo', 'bar')]
        assert list(d) == ['foo']
        assert list(iteritems(d, multi=True)) == \
            [('foo', 'bar'), ('foo', 'baz')]
        del d['foo']
        assert not d
        assert len(d) == 0
        assert list(d) == []

        d.update([('foo', 1), ('foo', 2), ('bar', 42)])
        d.add('foo', 3)
        assert d.getlist('foo') == [1, 2, 3]
        assert d.getlist('bar') == [42]
        assert list(iteritems(d)) == [('foo', 1), ('bar', 42)]

        expected = ['foo', 'bar']

        assert list(d.keys()) == expected
        assert list(d) == expected
        assert list(iterkeys(d)) == expected

        assert list(iteritems(d, multi=True)) == \
            [('foo', 1), ('foo', 2), ('bar', 42), ('foo', 3)]
        assert len(d) == 2

        assert d.pop('foo') == 1
        assert d.pop('blafasel', None) is None
        assert d.pop('blafasel', 42) == 42
        assert len(d) == 1
        assert d.poplist('bar') == [42]
        assert not d

        d.get('missingkey') is None

        d.add('foo', 42)
        d.add('foo', 23)
        d.add('bar', 2)
        d.add('foo', 42)
        assert d == datastructures.MultiDict(d)
        id = self.storage_class(d)
        assert d == id
        d.add('foo', 2)
        assert d != id

        d.update({'blah': [1, 2, 3]})
        assert d['blah'] == 1
        assert d.getlist('blah') == [1, 2, 3]

        # setlist works
        d = self.storage_class()
        d['foo'] = 42
        d.setlist('foo', [1, 2])
        assert d.getlist('foo') == [1, 2]

        with pytest.raises(BadRequestKeyError):
            d.pop('missing')
        with pytest.raises(BadRequestKeyError):
            d['missing']

        # popping
        d = self.storage_class()
        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        assert d.popitem() == ('foo', 23)
        with pytest.raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        assert d.popitemlist() == ('foo', [23, 42, 1])

        with pytest.raises(BadRequestKeyError):
            d.popitemlist()
Exemplo n.º 21
0
 def write_settings(self, keys=None):
     if not keys:
         keys = list(iterkeys(self.DEFAULT_SETTINGS))
     self.write_params(keys)
Exemplo n.º 22
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add('foo', 'bar')
        self.assert_equal(len(d), 1)
        d.add('foo', 'baz')
        self.assert_equal(len(d), 1)
        self.assert_equal(list(iteritems(d)), [('foo', 'bar')])
        self.assert_equal(list(d), ['foo'])
        self.assert_equal(list(iteritems(d, multi=True)),
                          [('foo', 'bar'), ('foo', 'baz')])
        del d['foo']
        assert not d
        self.assert_equal(len(d), 0)
        self.assert_equal(list(d), [])

        d.update([('foo', 1), ('foo', 2), ('bar', 42)])
        d.add('foo', 3)
        self.assert_equal(d.getlist('foo'), [1, 2, 3])
        self.assert_equal(d.getlist('bar'), [42])
        self.assert_equal(list(iteritems(d)), [('foo', 1), ('bar', 42)])

        expected = ['foo', 'bar']

        self.assert_sequence_equal(list(d.keys()), expected)
        self.assert_sequence_equal(list(d), expected)
        self.assert_sequence_equal(list(iterkeys(d)), expected)

        self.assert_equal(list(iteritems(d, multi=True)),
                          [('foo', 1), ('foo', 2), ('bar', 42), ('foo', 3)])
        self.assert_equal(len(d), 2)

        self.assert_equal(d.pop('foo'), 1)
        assert d.pop('blafasel', None) is None
        self.assert_equal(d.pop('blafasel', 42), 42)
        self.assert_equal(len(d), 1)
        self.assert_equal(d.poplist('bar'), [42])
        assert not d

        d.get('missingkey') is None

        d.add('foo', 42)
        d.add('foo', 23)
        d.add('bar', 2)
        d.add('foo', 42)
        self.assert_equal(d, datastructures.MultiDict(d))
        id = self.storage_class(d)
        self.assert_equal(d, id)
        d.add('foo', 2)
        assert d != id

        d.update({'blah': [1, 2, 3]})
        self.assert_equal(d['blah'], 1)
        self.assert_equal(d.getlist('blah'), [1, 2, 3])

        # setlist works
        d = self.storage_class()
        d['foo'] = 42
        d.setlist('foo', [1, 2])
        self.assert_equal(d.getlist('foo'), [1, 2])

        with self.assert_raises(BadRequestKeyError):
            d.pop('missing')
        with self.assert_raises(BadRequestKeyError):
            d['missing']

        # popping
        d = self.storage_class()
        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        self.assert_equal(d.popitem(), ('foo', 23))
        with self.assert_raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        self.assert_equal(d.popitemlist(), ('foo', [23, 42, 1]))

        with self.assert_raises(BadRequestKeyError):
            d.popitemlist()
Exemplo n.º 23
0
 def settings(self):
     result = {}
     keys = list(iterkeys(consts.DEFAULT_SETTINGS))
     for key in keys:
         result[key] = self.__dict__[key]
     return result
Exemplo n.º 24
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add("foo", "bar")
        assert len(d) == 1
        d.add("foo", "baz")
        assert len(d) == 1
        assert list(iteritems(d)) == [("foo", "bar")]
        assert list(d) == ["foo"]
        assert list(iteritems(d, multi=True)) == [("foo", "bar"), ("foo", "baz")]
        del d["foo"]
        assert not d
        assert len(d) == 0
        assert list(d) == []

        d.update([("foo", 1), ("foo", 2), ("bar", 42)])
        d.add("foo", 3)
        assert d.getlist("foo") == [1, 2, 3]
        assert d.getlist("bar") == [42]
        assert list(iteritems(d)) == [("foo", 1), ("bar", 42)]

        expected = ["foo", "bar"]

        assert list(d.keys()) == expected
        assert list(d) == expected
        assert list(iterkeys(d)) == expected

        assert list(iteritems(d, multi=True)) == [
            ("foo", 1),
            ("foo", 2),
            ("bar", 42),
            ("foo", 3),
        ]
        assert len(d) == 2

        assert d.pop("foo") == 1
        assert d.pop("blafasel", None) is None
        assert d.pop("blafasel", 42) == 42
        assert len(d) == 1
        assert d.poplist("bar") == [42]
        assert not d

        d.get("missingkey") is None

        d.add("foo", 42)
        d.add("foo", 23)
        d.add("bar", 2)
        d.add("foo", 42)
        assert d == datastructures.MultiDict(d)
        id = self.storage_class(d)
        assert d == id
        d.add("foo", 2)
        assert d != id

        d.update({"blah": [1, 2, 3]})
        assert d["blah"] == 1
        assert d.getlist("blah") == [1, 2, 3]

        # setlist works
        d = self.storage_class()
        d["foo"] = 42
        d.setlist("foo", [1, 2])
        assert d.getlist("foo") == [1, 2]
        with pytest.raises(BadRequestKeyError):
            d.pop("missing")

        with pytest.raises(BadRequestKeyError):
            d["missing"]

        # popping
        d = self.storage_class()
        d.add("foo", 23)
        d.add("foo", 42)
        d.add("foo", 1)
        assert d.popitem() == ("foo", 23)
        with pytest.raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add("foo", 23)
        d.add("foo", 42)
        d.add("foo", 1)
        assert d.popitemlist() == ("foo", [23, 42, 1])

        with pytest.raises(BadRequestKeyError):
            d.popitemlist()

        # Unhashable
        d = self.storage_class()
        d.add("foo", 23)
        pytest.raises(TypeError, hash, d)
Exemplo n.º 25
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add("foo", "bar")
        self.assert_equal(len(d), 1)
        d.add("foo", "baz")
        self.assert_equal(len(d), 1)
        self.assert_equal(list(iteritems(d)), [("foo", "bar")])
        self.assert_equal(list(d), ["foo"])
        self.assert_equal(list(iteritems(d, multi=True)), [("foo", "bar"), ("foo", "baz")])
        del d["foo"]
        assert not d
        self.assert_equal(len(d), 0)
        self.assert_equal(list(d), [])

        d.update([("foo", 1), ("foo", 2), ("bar", 42)])
        d.add("foo", 3)
        self.assert_equal(d.getlist("foo"), [1, 2, 3])
        self.assert_equal(d.getlist("bar"), [42])
        self.assert_equal(list(iteritems(d)), [("foo", 1), ("bar", 42)])

        expected = ["foo", "bar"]

        self.assert_sequence_equal(list(d.keys()), expected)
        self.assert_sequence_equal(list(d), expected)
        self.assert_sequence_equal(list(iterkeys(d)), expected)

        self.assert_equal(list(iteritems(d, multi=True)), [("foo", 1), ("foo", 2), ("bar", 42), ("foo", 3)])
        self.assert_equal(len(d), 2)

        self.assert_equal(d.pop("foo"), 1)
        assert d.pop("blafasel", None) is None
        self.assert_equal(d.pop("blafasel", 42), 42)
        self.assert_equal(len(d), 1)
        self.assert_equal(d.poplist("bar"), [42])
        assert not d

        d.get("missingkey") is None

        d.add("foo", 42)
        d.add("foo", 23)
        d.add("bar", 2)
        d.add("foo", 42)
        self.assert_equal(d, datastructures.MultiDict(d))
        id = self.storage_class(d)
        self.assert_equal(d, id)
        d.add("foo", 2)
        assert d != id

        d.update({"blah": [1, 2, 3]})
        self.assert_equal(d["blah"], 1)
        self.assert_equal(d.getlist("blah"), [1, 2, 3])

        # setlist works
        d = self.storage_class()
        d["foo"] = 42
        d.setlist("foo", [1, 2])
        self.assert_equal(d.getlist("foo"), [1, 2])

        with self.assert_raises(BadRequestKeyError):
            d.pop("missing")
        with self.assert_raises(BadRequestKeyError):
            d["missing"]

        # popping
        d = self.storage_class()
        d.add("foo", 23)
        d.add("foo", 42)
        d.add("foo", 1)
        self.assert_equal(d.popitem(), ("foo", 23))
        with self.assert_raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add("foo", 23)
        d.add("foo", 42)
        d.add("foo", 1)
        self.assert_equal(d.popitemlist(), ("foo", [23, 42, 1]))

        with self.assert_raises(BadRequestKeyError):
            d.popitemlist()
Exemplo n.º 26
0
 def items(self, multi=1):
     return iter(zip(iterkeys(self, multi=multi),
                     itervalues(self, multi=multi)))
Exemplo n.º 27
0
    def test_basic_interface(self):
        md = self.storage_class()
        assert isinstance(md, dict)

        mapping = [
            ("a", 1),
            ("b", 2),
            ("a", 2),
            ("d", 3),
            ("a", 1),
            ("a", 3),
            ("d", 4),
            ("c", 3),
        ]
        md = self.storage_class(mapping)

        # simple getitem gives the first value
        assert md["a"] == 1
        assert md["c"] == 3
        with pytest.raises(KeyError):
            md["e"]
        assert md.get("a") == 1

        # list getitem
        assert md.getlist("a") == [1, 2, 1, 3]
        assert md.getlist("d") == [3, 4]
        # do not raise if key not found
        assert md.getlist("x") == []

        # simple setitem overwrites all values
        md["a"] = 42
        assert md.getlist("a") == [42]

        # list setitem
        md.setlist("a", [1, 2, 3])
        assert md["a"] == 1
        assert md.getlist("a") == [1, 2, 3]

        # verify that it does not change original lists
        l1 = [1, 2, 3]
        md.setlist("a", l1)
        del l1[:]
        assert md["a"] == 1

        # setdefault, setlistdefault
        assert md.setdefault("u", 23) == 23
        assert md.getlist("u") == [23]
        del md["u"]

        md.setlist("u", [-1, -2])

        # delitem
        del md["u"]
        with pytest.raises(KeyError):
            md["u"]
        del md["d"]
        assert md.getlist("d") == []

        # keys, values, items, lists
        assert list(sorted(md.keys())) == ["a", "b", "c"]
        assert list(sorted(iterkeys(md))) == ["a", "b", "c"]

        assert list(sorted(itervalues(md))) == [1, 2, 3]
        assert list(sorted(itervalues(md))) == [1, 2, 3]

        assert list(sorted(md.items())) == [("a", 1), ("b", 2), ("c", 3)]
        assert list(sorted(md.items(multi=True))) == [
            ("a", 1),
            ("a", 2),
            ("a", 3),
            ("b", 2),
            ("c", 3),
        ]
        assert list(sorted(iteritems(md))) == [("a", 1), ("b", 2), ("c", 3)]
        assert list(sorted(iteritems(md, multi=True))) == [
            ("a", 1),
            ("a", 2),
            ("a", 3),
            ("b", 2),
            ("c", 3),
        ]

        assert list(sorted(md.lists())) == [("a", [1, 2, 3]), ("b", [2]),
                                            ("c", [3])]
        assert list(sorted(iterlists(md))) == [("a", [1, 2, 3]), ("b", [2]),
                                               ("c", [3])]

        # copy method
        c = md.copy()
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # copy method 2
        c = copy(md)
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # deepcopy method
        c = md.deepcopy()
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # deepcopy method 2
        c = deepcopy(md)
        assert c["a"] == 1
        assert c.getlist("a") == [1, 2, 3]

        # update with a multidict
        od = self.storage_class([("a", 4), ("a", 5), ("y", 0)])
        md.update(od)
        assert md.getlist("a") == [1, 2, 3, 4, 5]
        assert md.getlist("y") == [0]

        # update with a regular dict
        md = c
        od = {"a": 4, "y": 0}
        md.update(od)
        assert md.getlist("a") == [1, 2, 3, 4]
        assert md.getlist("y") == [0]

        # pop, poplist, popitem, popitemlist
        assert md.pop("y") == 0
        assert "y" not in md
        assert md.poplist("a") == [1, 2, 3, 4]
        assert "a" not in md
        assert md.poplist("missing") == []

        # remaining: b=2, c=3
        popped = md.popitem()
        assert popped in [("b", 2), ("c", 3)]
        popped = md.popitemlist()
        assert popped in [("b", [2]), ("c", [3])]

        # type conversion
        md = self.storage_class({"a": "4", "b": ["2", "3"]})
        assert md.get("a", type=int) == 4
        assert md.getlist("b", type=int) == [2, 3]

        # repr
        md = self.storage_class([("a", 1), ("a", 2), ("b", 3)])
        assert "('a', 1)" in repr(md)
        assert "('a', 2)" in repr(md)
        assert "('b', 3)" in repr(md)

        # add and getlist
        md.add("c", "42")
        md.add("c", "23")
        assert md.getlist("c") == ["42", "23"]
        md.add("c", "blah")
        assert md.getlist("c", type=int) == [42, 23]

        # setdefault
        md = self.storage_class()
        md.setdefault("x", []).append(42)
        md.setdefault("x", []).append(23)
        assert md["x"] == [42, 23]

        # to dict
        md = self.storage_class()
        md["foo"] = 42
        md.add("bar", 1)
        md.add("bar", 2)
        assert md.to_dict() == {"foo": 42, "bar": 1}
        assert md.to_dict(flat=False) == {"foo": [42], "bar": [1, 2]}

        # popitem from empty dict
        with pytest.raises(KeyError):
            self.storage_class().popitem()

        with pytest.raises(KeyError):
            self.storage_class().popitemlist()

        # key errors are of a special type
        with pytest.raises(BadRequestKeyError):
            self.storage_class()[42]

        # setlist works
        md = self.storage_class()
        md["foo"] = 42
        md.setlist("foo", [1, 2])
        assert md.getlist("foo") == [1, 2]
Exemplo n.º 28
0
    def test_basic_interface(self):
        md = self.storage_class()
        assert isinstance(md, dict)

        mapping = [('a', 1), ('b', 2), ('a', 2), ('d', 3),
                   ('a', 1), ('a', 3), ('d', 4), ('c', 3)]
        md = self.storage_class(mapping)

        # simple getitem gives the first value
        self.assert_equal(md['a'], 1)
        self.assert_equal(md['c'], 3)
        with self.assert_raises(KeyError):
            md['e']
        self.assert_equal(md.get('a'), 1)

        # list getitem
        self.assert_equal(md.getlist('a'), [1, 2, 1, 3])
        self.assert_equal(md.getlist('d'), [3, 4])
        # do not raise if key not found
        self.assert_equal(md.getlist('x'), [])

        # simple setitem overwrites all values
        md['a'] = 42
        self.assert_equal(md.getlist('a'), [42])

        # list setitem
        md.setlist('a', [1, 2, 3])
        self.assert_equal(md['a'], 1)
        self.assert_equal(md.getlist('a'), [1, 2, 3])

        # verify that it does not change original lists
        l1 = [1, 2, 3]
        md.setlist('a', l1)
        del l1[:]
        self.assert_equal(md['a'], 1)

        # setdefault, setlistdefault
        self.assert_equal(md.setdefault('u', 23), 23)
        self.assert_equal(md.getlist('u'), [23])
        del md['u']

        md.setlist('u', [-1, -2])

        # delitem
        del md['u']
        with self.assert_raises(KeyError):
            md['u']
        del md['d']
        self.assert_equal(md.getlist('d'), [])

        # keys, values, items, lists
        self.assert_equal(list(sorted(md.keys())), ['a', 'b', 'c'])
        self.assert_equal(list(sorted(iterkeys(md))), ['a', 'b', 'c'])

        self.assert_equal(list(sorted(itervalues(md))), [1, 2, 3])
        self.assert_equal(list(sorted(itervalues(md))), [1, 2, 3])

        self.assert_equal(list(sorted(md.items())),
                          [('a', 1), ('b', 2), ('c', 3)])
        self.assert_equal(list(sorted(md.items(multi=True))),
                          [('a', 1), ('a', 2), ('a', 3), ('b', 2), ('c', 3)])
        self.assert_equal(list(sorted(iteritems(md))),
                          [('a', 1), ('b', 2), ('c', 3)])
        self.assert_equal(list(sorted(iteritems(md, multi=True))),
                          [('a', 1), ('a', 2), ('a', 3), ('b', 2), ('c', 3)])

        self.assert_equal(list(sorted(md.lists())),
                          [('a', [1, 2, 3]), ('b', [2]), ('c', [3])])
        self.assert_equal(list(sorted(iterlists(md))),
                          [('a', [1, 2, 3]), ('b', [2]), ('c', [3])])

        # copy method
        c = md.copy()
        self.assert_equal(c['a'], 1)
        self.assert_equal(c.getlist('a'), [1, 2, 3])

        # copy method 2
        c = copy(md)
        self.assert_equal(c['a'], 1)
        self.assert_equal(c.getlist('a'), [1, 2, 3])

        # update with a multidict
        od = self.storage_class([('a', 4), ('a', 5), ('y', 0)])
        md.update(od)
        self.assert_equal(md.getlist('a'), [1, 2, 3, 4, 5])
        self.assert_equal(md.getlist('y'), [0])

        # update with a regular dict
        md = c
        od = {'a': 4, 'y': 0}
        md.update(od)
        self.assert_equal(md.getlist('a'), [1, 2, 3, 4])
        self.assert_equal(md.getlist('y'), [0])

        # pop, poplist, popitem, popitemlist
        self.assert_equal(md.pop('y'), 0)
        assert 'y' not in md
        self.assert_equal(md.poplist('a'), [1, 2, 3, 4])
        assert 'a' not in md
        self.assert_equal(md.poplist('missing'), [])

        # remaining: b=2, c=3
        popped = md.popitem()
        assert popped in [('b', 2), ('c', 3)]
        popped = md.popitemlist()
        assert popped in [('b', [2]), ('c', [3])]

        # type conversion
        md = self.storage_class({'a': '4', 'b': ['2', '3']})
        self.assert_equal(md.get('a', type=int), 4)
        self.assert_equal(md.getlist('b', type=int), [2, 3])

        # repr
        md = self.storage_class([('a', 1), ('a', 2), ('b', 3)])
        assert "('a', 1)" in repr(md)
        assert "('a', 2)" in repr(md)
        assert "('b', 3)" in repr(md)

        # add and getlist
        md.add('c', '42')
        md.add('c', '23')
        self.assert_equal(md.getlist('c'), ['42', '23'])
        md.add('c', 'blah')
        self.assert_equal(md.getlist('c', type=int), [42, 23])

        # setdefault
        md = self.storage_class()
        md.setdefault('x', []).append(42)
        md.setdefault('x', []).append(23)
        self.assert_equal(md['x'], [42, 23])

        # to dict
        md = self.storage_class()
        md['foo'] = 42
        md.add('bar', 1)
        md.add('bar', 2)
        self.assert_equal(md.to_dict(), {'foo': 42, 'bar': 1})
        self.assert_equal(md.to_dict(flat=False), {'foo': [42], 'bar': [1, 2]})

        # popitem from empty dict
        with self.assert_raises(KeyError):
            self.storage_class().popitem()

        with self.assert_raises(KeyError):
            self.storage_class().popitemlist()

        # key errors are of a special type
        with self.assert_raises(BadRequestKeyError):
            self.storage_class()[42]

        # setlist works
        md = self.storage_class()
        md['foo'] = 42
        md.setlist('foo', [1, 2])
        self.assert_equal(md.getlist('foo'), [1, 2])
Exemplo n.º 29
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add("foo", "bar")
        assert len(d) == 1
        d.add("foo", "baz")
        assert len(d) == 1
        assert list(iteritems(d)) == [("foo", "bar")]
        assert list(d) == ["foo"]
        assert list(iteritems(d, multi=True)) == [("foo", "bar"),
                                                  ("foo", "baz")]
        del d["foo"]
        assert not d
        assert len(d) == 0
        assert list(d) == []

        d.update([("foo", 1), ("foo", 2), ("bar", 42)])
        d.add("foo", 3)
        assert d.getlist("foo") == [1, 2, 3]
        assert d.getlist("bar") == [42]
        assert list(iteritems(d)) == [("foo", 1), ("bar", 42)]

        expected = ["foo", "bar"]

        assert list(d.keys()) == expected
        assert list(d) == expected
        assert list(iterkeys(d)) == expected

        assert list(iteritems(d, multi=True)) == [
            ("foo", 1),
            ("foo", 2),
            ("bar", 42),
            ("foo", 3),
        ]
        assert len(d) == 2

        assert d.pop("foo") == 1
        assert d.pop("blafasel", None) is None
        assert d.pop("blafasel", 42) == 42
        assert len(d) == 1
        assert d.poplist("bar") == [42]
        assert not d

        d.get("missingkey") is None

        d.add("foo", 42)
        d.add("foo", 23)
        d.add("bar", 2)
        d.add("foo", 42)
        assert d == datastructures.MultiDict(d)
        id = self.storage_class(d)
        assert d == id
        d.add("foo", 2)
        assert d != id

        d.update({"blah": [1, 2, 3]})
        assert d["blah"] == 1
        assert d.getlist("blah") == [1, 2, 3]

        # setlist works
        d = self.storage_class()
        d["foo"] = 42
        d.setlist("foo", [1, 2])
        assert d.getlist("foo") == [1, 2]
        with pytest.raises(BadRequestKeyError):
            d.pop("missing")

        with pytest.raises(BadRequestKeyError):
            d["missing"]

        # popping
        d = self.storage_class()
        d.add("foo", 23)
        d.add("foo", 42)
        d.add("foo", 1)
        assert d.popitem() == ("foo", 23)
        with pytest.raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add("foo", 23)
        d.add("foo", 42)
        d.add("foo", 1)
        assert d.popitemlist() == ("foo", [23, 42, 1])

        with pytest.raises(BadRequestKeyError):
            d.popitemlist()

        # Unhashable
        d = self.storage_class()
        d.add("foo", 23)
        pytest.raises(TypeError, hash, d)
Exemplo n.º 30
0
    def test_ordered_interface(self):
        cls = self.storage_class

        d = cls()
        assert not d
        d.add('foo', 'bar')
        self.assert_equal(len(d), 1)
        d.add('foo', 'baz')
        self.assert_equal(len(d), 1)
        self.assert_equal(list(iteritems(d)), [('foo', 'bar')])
        self.assert_equal(list(d), ['foo'])
        self.assert_equal(list(iteritems(d, multi=True)), [('foo', 'bar'),
                                                           ('foo', 'baz')])
        del d['foo']
        assert not d
        self.assert_equal(len(d), 0)
        self.assert_equal(list(d), [])

        d.update([('foo', 1), ('foo', 2), ('bar', 42)])
        d.add('foo', 3)
        self.assert_equal(d.getlist('foo'), [1, 2, 3])
        self.assert_equal(d.getlist('bar'), [42])
        self.assert_equal(list(iteritems(d)), [('foo', 1), ('bar', 42)])

        expected = ['foo', 'bar']

        self.assert_sequence_equal(list(d.keys()), expected)
        self.assert_sequence_equal(list(d), expected)
        self.assert_sequence_equal(list(iterkeys(d)), expected)

        self.assert_equal(list(iteritems(d, multi=True)), [('foo', 1),
                                                           ('foo', 2),
                                                           ('bar', 42),
                                                           ('foo', 3)])
        self.assert_equal(len(d), 2)

        self.assert_equal(d.pop('foo'), 1)
        assert d.pop('blafasel', None) is None
        self.assert_equal(d.pop('blafasel', 42), 42)
        self.assert_equal(len(d), 1)
        self.assert_equal(d.poplist('bar'), [42])
        assert not d

        d.get('missingkey') is None

        d.add('foo', 42)
        d.add('foo', 23)
        d.add('bar', 2)
        d.add('foo', 42)
        self.assert_equal(d, datastructures.MultiDict(d))
        id = self.storage_class(d)
        self.assert_equal(d, id)
        d.add('foo', 2)
        assert d != id

        d.update({'blah': [1, 2, 3]})
        self.assert_equal(d['blah'], 1)
        self.assert_equal(d.getlist('blah'), [1, 2, 3])

        # setlist works
        d = self.storage_class()
        d['foo'] = 42
        d.setlist('foo', [1, 2])
        self.assert_equal(d.getlist('foo'), [1, 2])

        with self.assert_raises(BadRequestKeyError):
            d.pop('missing')
        with self.assert_raises(BadRequestKeyError):
            d['missing']

        # popping
        d = self.storage_class()
        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        self.assert_equal(d.popitem(), ('foo', 23))
        with self.assert_raises(BadRequestKeyError):
            d.popitem()
        assert not d

        d.add('foo', 23)
        d.add('foo', 42)
        d.add('foo', 1)
        self.assert_equal(d.popitemlist(), ('foo', [23, 42, 1]))

        with self.assert_raises(BadRequestKeyError):
            d.popitemlist()
Exemplo n.º 31
0
 def read_settings(self, keys=None):
     if not keys:
         keys = list(iterkeys(self.DEFAULT_SETTINGS))
     params = self.read_params(keys)
     for key, value in iteritems(params):
         self.__dict__[key] = value
Exemplo n.º 32
0
 def items(self, multi=1):
     return iter(
         zip(iterkeys(self, multi=multi),
             itervalues(self, multi=multi)))
Exemplo n.º 33
0
 def get_settings(self):
     result = {}
     keys = list(iterkeys(common.DEFAULT_SETTINGS))
     for key in keys:
         result[key] = self.app.__dict__[key]
     return result