Example #1
0
File: base.py Project: hattya/ayame
 def assert_element_equal(self, a, b):
     self.assert_is_not(a, b)
     # qname
     self.assert_is_instance(a.qname, markup.QName)
     self.assert_is_instance(b.qname, markup.QName)
     self.assert_equal(a.qname, b.qname)
     # attrib
     self.assert_is_instance(a.attrib, markup._AttributeDict)
     self.assert_is_instance(b.attrib, markup._AttributeDict)
     self.assert_is_not(a.attrib, b.attrib)
     self.assert_equal(a.attrib, b.attrib)
     # type
     self.assert_equal(a.type, b.type)
     # ns
     self.assert_is_instance(a.ns, dict)
     self.assert_is_instance(b.ns, dict)
     self.assert_is_not(a.ns, b.ns)
     self.assert_equal(a.ns, b.ns)
     # children
     self.assert_is_instance(a.children, list)
     self.assert_is_instance(b.children, list)
     self.assert_is_not(a.children, b.children)
     self.assert_equal(len(a.children), len(b.children))
     for i in five.range(len(a.children)):
         if isinstance(a[i], markup.Element):
             self.assert_is_instance(b[i], markup.Element)
             self.assert_element_equal(a[i], b[i])
         else:
             self.assert_is_instance(a[i], five.string_type)
             self.assert_is_instance(b[i], five.string_type)
             self.assert_equal(a[i], b[i])
Example #2
0
 def assert_element_equal(self, a, b):
     self.assert_is_not(a, b)
     # qname
     self.assert_is_instance(a.qname, markup.QName)
     self.assert_is_instance(b.qname, markup.QName)
     self.assert_equal(a.qname, b.qname)
     # attrib
     self.assert_is_instance(a.attrib, markup._AttributeDict)
     self.assert_is_instance(b.attrib, markup._AttributeDict)
     self.assert_is_not(a.attrib, b.attrib)
     self.assert_equal(a.attrib, b.attrib)
     # type
     self.assert_equal(a.type, b.type)
     # ns
     self.assert_is_instance(a.ns, dict)
     self.assert_is_instance(b.ns, dict)
     self.assert_is_not(a.ns, b.ns)
     self.assert_equal(a.ns, b.ns)
     # children
     self.assert_is_instance(a.children, list)
     self.assert_is_instance(b.children, list)
     self.assert_is_not(a.children, b.children)
     self.assert_equal(len(a.children), len(b.children))
     for i in five.range(len(a.children)):
         if isinstance(a[i], markup.Element):
             self.assert_is_instance(b[i], markup.Element)
             self.assert_element_equal(a[i], b[i])
         else:
             self.assert_is_instance(a[i], five.string_type)
             self.assert_is_instance(b[i], five.string_type)
             self.assert_equal(a[i], b[i])
Example #3
0
 def _test_get(self, page, locale):
     with self.application():
         l = i18n.Localizer()
         for i in five.range(1, 3):
             c = page.find('a{}:b'.format(i))
             for k in ('spam', 'eggs', 'ham', 'toast', 'beans', 'bacon'):
                 v = k
                 if k in ('ham', 'toast'):
                     v += str(i)
                 self.assert_equal(l.get(c, locale, k), v)
Example #4
0
    def test_list_view_error(self):
        root = markup.Element(self.of('root'), attrib={markup.AYAME_ID: 'b'})
        label = markup.Element(self.of('label'), attrib={markup.AYAME_ID: 'c'})
        root.append(label)
        mc = ayame.MarkupContainer('a')
        m = model.Model([str(i) for i in five.range(3)])
        mc.add(basic.ListView('b', m, None))

        with self.assert_raises_regex(ayame.ComponentError,
                                      r"\bcomponent .* 'c' .* not found\b"):
            mc.render(root)
Example #5
0
    def test_list_view_error(self):
        root = markup.Element(self.of('root'),
                              attrib={markup.AYAME_ID: 'b'})
        label = markup.Element(self.of('label'),
                               attrib={markup.AYAME_ID: 'c'})
        root.append(label)
        mc = ayame.MarkupContainer('a')
        m = model.Model([str(i) for i in five.range(3)])
        mc.add(basic.ListView('b', m, None))

        with self.assert_raises_regex(ayame.ComponentError,
                                      r"\bcomponent .* 'c' .* not found\b"):
            mc.render(root)
Example #6
0
    def test_property_list_view(self):
        def populate_item(li):
            li.add(basic.Label('c', li.model.object))

        root = markup.Element(self.of('root'),
                              attrib={markup.AYAME_ID: 'b'})
        label = markup.Element(self.of('label'),
                               attrib={markup.AYAME_ID: 'c'})
        root.append(label)
        m = model.CompoundModel({'b': [str(i) for i in five.range(3)]})
        mc = ayame.MarkupContainer('a', m)
        mc.add(basic.PropertyListView('b', None, populate_item))

        root = mc.render(root)
        self.assert_equal(root.qname, self.of('root'))
        self.assert_equal(root.attrib, {})
        self.assert_equal(len(root), 3)

        label = root[0]
        self.assert_equal(label.qname, self.of('label'))
        self.assert_equal(label.attrib, {})
        self.assert_equal(label.children, ['0'])

        label = root[1]
        self.assert_equal(label.qname, self.of('label'))
        self.assert_equal(label.attrib, {})
        self.assert_equal(label.children, ['1'])

        label = root[2]
        self.assert_equal(label.qname, self.of('label'))
        self.assert_equal(label.attrib, {})
        self.assert_equal(label.children, ['2'])

        self.assert_equal(len(mc.children), 1)
        lv = mc.children[0]
        self.assert_equal(len(lv.children), 3)
        self.assert_equal(lv.children[0].index, 0)
        self.assert_equal(lv.children[1].index, 1)
        self.assert_equal(lv.children[2].index, 2)

        self.assert_is_instance(lv.children[0].model, model.CompoundModel)
        self.assert_is_instance(lv.children[1].model, model.CompoundModel)
        self.assert_is_instance(lv.children[2].model, model.CompoundModel)
        lv.children[0].model.object = 10
        lv.children[1].model.object = 11
        lv.children[2].model.object = 12
        self.assert_equal(lv.children[0].model.object, 10)
        self.assert_equal(lv.children[1].model.object, 11)
        self.assert_equal(lv.children[2].model.object, 12)
Example #7
0
    def test_list_view(self):
        def populate_item(li):
            li.add(basic.Label('c', li.model.object))

        root = markup.Element(self.of('root'), attrib={markup.AYAME_ID: 'b'})
        label = markup.Element(self.of('label'), attrib={markup.AYAME_ID: 'c'})
        root.append(label)
        mc = ayame.MarkupContainer('a')
        m = model.Model([str(i) for i in five.range(3)])
        mc.add(basic.ListView('b', m, populate_item))

        root = mc.render(root)
        self.assert_equal(root.qname, self.of('root'))
        self.assert_equal(root.attrib, {})
        self.assert_equal(len(root), 3)

        label = root[0]
        self.assert_equal(label.qname, self.of('label'))
        self.assert_equal(label.attrib, {})
        self.assert_equal(label.children, ['0'])

        label = root[1]
        self.assert_equal(label.qname, self.of('label'))
        self.assert_equal(label.attrib, {})
        self.assert_equal(label.children, ['1'])

        label = root[2]
        self.assert_equal(label.qname, self.of('label'))
        self.assert_equal(label.attrib, {})
        self.assert_equal(label.children, ['2'])

        self.assert_equal(len(mc.children), 1)
        lv = mc.children[0]
        self.assert_equal(len(lv.children), 3)
        self.assert_equal(lv.children[0].index, 0)
        self.assert_equal(lv.children[1].index, 1)
        self.assert_equal(lv.children[2].index, 2)

        self.assert_is_instance(lv.children[0].model, basic._ListItemModel)
        self.assert_is_instance(lv.children[1].model, basic._ListItemModel)
        self.assert_is_instance(lv.children[2].model, basic._ListItemModel)
        lv.children[0].model.object = 10
        lv.children[1].model.object = 11
        lv.children[2].model.object = 12
        self.assert_equal(lv.children[0].model.object, 10)
        self.assert_equal(lv.children[1].model.object, 11)
        self.assert_equal(lv.children[2].model.object, 12)
Example #8
0
    def test_cache(self):
        config = self.app.config.copy()
        try:
            with self.application():
                locale = (None,) * 2
                l = i18n.Localizer()
                p = Page()
                for i in five.range(1, 3):
                    self.app.config['ayame.resource.loader'] = self.new_resource_loader()
                    self.app.config['ayame.i18n.cache'] = config['ayame.i18n.cache'].copy()

                    c = p.find('a{}:b'.format(i))
                    self.assert_equal(l.get(c, locale, 'spam'), 'spam')
                    self.assert_is_none(l.get(c, locale, 'spam'))
                    self.assert_is_none(l.get(c, locale, 'eggs'))
        finally:
            self.app.config = config
Example #9
0
    def test_property_list_view_render_body_only(self):
        def populate_item(li):
            li.add(basic.Label('c', li.model.object))
            li.find('c').render_body_only = True

        root = markup.Element(self.of('root'), attrib={markup.AYAME_ID: 'b'})
        root.append('[')
        label = markup.Element(self.of('label'), attrib={markup.AYAME_ID: 'c'})
        root.append(label)
        root.append(']')
        m = model.CompoundModel({'b': [str(i) for i in five.range(3)]})
        mc = ayame.MarkupContainer('a', m)
        mc.add(basic.PropertyListView('b', None, populate_item))

        root = mc.render(root)
        self.assert_equal(root.qname, self.of('root'))
        self.assert_equal(root.attrib, {})
        self.assert_equal(len(root), 9)
        root.normalize()
        self.assert_equal(root.children, ['[0][1][2]'])
Example #10
0
    def test_list_view_render_body_only(self):
        def populate_item(li):
            li.add(basic.Label('c', li.model.object))
            li.find('c').render_body_only = True

        root = markup.Element(self.of('root'),
                              attrib={markup.AYAME_ID: 'b'})
        root.append('[')
        label = markup.Element(self.of('label'),
                               attrib={markup.AYAME_ID: 'c'})
        root.append(label)
        root.append(']')
        mc = ayame.MarkupContainer('a')
        mc.add(basic.ListView('b', [str(i) for i in five.range(3)], populate_item))

        root = mc.render(root)
        self.assert_equal(root.qname, self.of('root'))
        self.assert_equal(root.attrib, {})
        self.assert_equal(len(root), 9)
        root.normalize()
        self.assert_equal(root.children, ['[0][1][2]'])
Example #11
0
    def test_rwlock(self):
        def reader():
            with lock.read():
                self.assert_greater(lock._rcnt, 0)
                self.assert_equal(lock._rwait, 0)
                time.sleep(0.01)

        def writer():
            with lock.write():
                self.assert_equal(lock._rcnt, -1)
                self.assert_equal(lock._rwait, 0)
                time.sleep(0.01)

        lock = util.RWLock()
        for _ in five.range(10):
            thr = threading.Thread(target=random.choice((reader, writer)))
            thr.daemon = True
            thr.start()
            time.sleep(0.01)
        time.sleep(0.17)
        self.assert_equal(lock._rcnt, 0)
        self.assert_equal(lock._rwait, 0)
        self.assert_equal(threading.active_count(), 1)
Example #12
0
    def test_rwlock(self):
        def reader():
            with lock.read():
                self.assert_greater(lock._rcnt, 0)
                self.assert_equal(lock._rwait, 0)
                time.sleep(0.01)

        def writer():
            with lock.write():
                self.assert_equal(lock._rcnt, -1)
                self.assert_equal(lock._rwait, 0)
                time.sleep(0.01)

        lock = util.RWLock()
        for _ in five.range(10):
            thr = threading.Thread(target=random.choice((reader, writer)))
            thr.daemon = True
            thr.start()
            time.sleep(0.01)
        time.sleep(0.17)
        self.assert_equal(lock._rcnt, 0)
        self.assert_equal(lock._rwait, 0)
        self.assert_equal(threading.active_count(), 1)
Example #13
0
    def test_del(self):
        c = self.lfu_cache(3)
        del c['a']
        self.assert_equal(list(reversed(c)), ['b', 'c'])
        self.assert_equal(list(c.items()), [('c', 3), ('b', 2)])
        self.assert_equal(c.evicted, [('a', 1)])

        c = self.lfu_cache(3)
        del c['b']
        self.assert_equal(list(reversed(c)), ['a', 'c'])
        self.assert_equal(list(c.items()), [('c', 3), ('a', 1)])
        self.assert_equal(c.evicted, [('b', 2)])

        c = self.lfu_cache(3)
        del c['c']
        self.assert_equal(list(reversed(c)), ['a', 'b'])
        self.assert_equal(list(c.items()), [('b', 2), ('a', 1)])
        self.assert_equal(c.evicted, [('c', 3)])

        c = self.lfu_cache(3)
        self.assert_equal(c.pop('b'), 2)
        self.assert_equal(list(reversed(c)), ['a', 'c'])
        self.assert_equal(list(c.items()), [('c', 3), ('a', 1)])
        self.assert_equal(c.evicted, [('b', 2)])

        with self.assert_raises(KeyError):
            c.pop('b')
        self.assert_is_none(c.pop('b', None))

        c = self.lfu_cache(3)
        n = len(c)
        for i in five.range(1, n + 1):
            self.assert_equal(len(c.popitem()), 2)
            self.assert_equal(len(c), n - i)
            self.assert_equal(len(c.evicted), i)
        with self.assert_raises(KeyError):
            c.popitem()
Example #14
0
    def test_del(self):
        c = self.lfu_cache(3)
        del c["a"]
        self.assert_equal(list(reversed(c)), ["b", "c"])
        self.assert_equal(list(c.items()), [("c", 3), ("b", 2)])
        self.assert_equal(c.evicted, [("a", 1)])

        c = self.lfu_cache(3)
        del c["b"]
        self.assert_equal(list(reversed(c)), ["a", "c"])
        self.assert_equal(list(c.items()), [("c", 3), ("a", 1)])
        self.assert_equal(c.evicted, [("b", 2)])

        c = self.lfu_cache(3)
        del c["c"]
        self.assert_equal(list(reversed(c)), ["a", "b"])
        self.assert_equal(list(c.items()), [("b", 2), ("a", 1)])
        self.assert_equal(c.evicted, [("c", 3)])

        c = self.lfu_cache(3)
        self.assert_equal(c.pop("b"), 2)
        self.assert_equal(list(reversed(c)), ["a", "c"])
        self.assert_equal(list(c.items()), [("c", 3), ("a", 1)])
        self.assert_equal(c.evicted, [("b", 2)])

        with self.assert_raises(KeyError):
            c.pop("b")
        self.assert_is_none(c.pop("b", None))

        c = self.lfu_cache(3)
        n = len(c)
        for i in five.range(1, n + 1):
            self.assert_equal(len(c.popitem()), 2)
            self.assert_equal(len(c), n - i)
            self.assert_equal(len(c.evicted), i)
        with self.assert_raises(KeyError):
            c.popitem()
Example #15
0
 def lfu_cache(self, n):
     c = LFUCache(n)
     for i in five.range(n):
         c[chr(ord('a') + i)] = i + 1
     return c
Example #16
0
 def lfu_cache(self, n):
     c = LFUCache(n)
     for i in five.range(n):
         c[chr(ord("a") + i)] = i + 1
     return c