예제 #1
0
    def test_find_match(self):
        """
        Test if appropriate handler is found.
        """
        h1 = core.Handler(lambda: None, r'a')
        h2 = core.Handler(lambda: None, r'b')
        h3 = core.Handler(lambda: None, r'c')
        handler_list = core.HandlerList([h1, h2, h3])

        self.assertEqual(id(handler_list.find_match('a')), id(h1))
        self.assertIsNone(handler_list.find_match('x'))
예제 #2
0
    def test_find_match_priority(self):
        """
        Test if appropriate handler is found when multiple handlers are matched
        and should be selected handler with higher priority.
        """
        h1 = core.Handler(lambda: None, r'a', priority=1)
        h2 = core.Handler(lambda: None, r'a', priority=2)
        h3 = core.Handler(lambda: None, r'a', priority=3)
        handler_list = core.HandlerList([h1, h2, h3])

        self.assertEqual(id(handler_list.find_match('a')), id(h3))
        self.assertIsNone(handler_list.find_match('x'))
예제 #3
0
    def test_priority_sorting(self):
        """
        Test if handler list correctly sorting handlers by by their priority
        (highest priority first) when handlers are added via constructor.
        """
        h1 = core.Handler(lambda: None, r'.*', priority=1)
        h2 = core.Handler(lambda: None, r'.*', priority=2)
        h3 = core.Handler(lambda: None, r'.*', priority=3)
        handlers_sorted = [h3, h2, h1]

        handlers_list = core.HandlerList([h1, h2, h3])
        for a, b in zip(handlers_sorted, handlers_list):
            self.assertEqual(id(a), id(b))
예제 #4
0
    def test_priority_sorting(self):
        """
        Test if handlers can be sorted and are correctly sorted descending by
        their priority (highest priority first).
        """
        dummy_func = lambda: None
        h1 = core.Handler(dummy_func, r'.*', priority=1)
        h2 = core.Handler(dummy_func, r'.*', priority=2)
        h3 = core.Handler(dummy_func, r'.*', priority=3)
        handlers_sorted = [h3, h2, h1]

        handlers_unsorted = [h1, h2, h3]
        for a, b in zip(handlers_sorted, sorted(handlers_unsorted)):
            self.assertEqual(id(a), id(b))
예제 #5
0
    def test_priority_sorting_append(self):
        """
        Test if handler list correctly sorting handlers by by their priority
        (highest priority first) when adding with append method.
        """
        h1 = core.Handler(lambda: None, r'.*', priority=1)
        h2 = core.Handler(lambda: None, r'.*', priority=2)
        h3 = core.Handler(lambda: None, r'.*', priority=3)
        handlers_sorted = [h3, h2, h1]

        handlers_list = core.HandlerList()
        handlers_list.append(h1)
        handlers_list.append(h2)
        handlers_list.append(h3)
        for a, b in zip(handlers_sorted, handlers_list):
            self.assertEqual(id(a), id(b))
예제 #6
0
 def test_match(self):
     """
     Test if handlers are correctly matched to the URL.
     """
     dummy_func = lambda: None
     h = core.Handler(dummy_func, r'//google\.com/')
     self.assertTrue(h.match('//google.com/'))
     self.assertTrue(h.match('https://google.com/'))
     self.assertTrue(h.match('https://google.com/?q=query'))
     self.assertFalse(h.match('//wikipedia.org/'))
예제 #7
0
 def test_groups(self):
     """
     Test returning match groups.
     """
     dummy_func = lambda: None
     h = core.Handler(dummy_func, r'//google\.com/\?q=(?P<query>.+)')
     self.assertDictEqual(h.groups('https://google.com/?q=search'), {
         0: '//google.com/?q=search',
         1: 'search',
         'query': 'search'
     })