Пример #1
0
 def setUp(self):
     self.index = Index([0, 1, 2])
     self.index2 = Index([2, 0, 1])
Пример #2
0
class TestIndex(unittest.TestCase):
    
    def setUp(self):
        self.index = Index([0, 1, 2])
        self.index2 = Index([2, 0, 1])
    
    def test___create_key(self):
        self.assertEqual(('a', 'b', 'c'), self.index._create_key(('a', 'b', 'c')))
        self.assertEqual(('c', 'a', 'b'), self.index2._create_key(('a', 'b', 'c')))
    
    def test_insert(self):
        self.index.insert(('a', 'b', 'c'))
        self.assertEqual({ 'a': { 'b': { 'c': ('a', 'b', 'c') } } },
                         self.index._index)
        
        self.index2.insert(('a', 'b', 'c'))
        self.assertEqual({ 'c': { 'a': { 'b': ('a', 'b', 'c') } } },
                         self.index2._index)
    
    def _check_match_full(self, index):
        index.insert(('a', 'b', 'c'))
        index.insert(('c', 'c', 'c'))
        index.insert(('a', 'b', 'b'))
        self.assertEqual(
            [('a', 'b', 'c')],
            list(index.match(('a', 'b', 'c')))
        )
        self.assertEqual(
            [],
            list(index.match(('a', 'b', 'd')))
        )
        self.assertEqual(
            [],
            list(index.match(('a', 'd', 'c')))
        )
        self.assertEqual(
            [],
            list(index.match(('d', 'b', 'c')))
        )
    
    def test_match_full(self):
        self._check_match_full(self.index)
        self._check_match_full(self.index2)
    
    def test_match_partial(self):
        self.index.insert(('a', 'b', 'c'))
        self.index.insert(('c', 'c', 'c'))
        self.index.insert(('a', 'b', 'b'))
        self.index.insert(('a', 'a', 'b'))
        
        self.assertEqual(
            set([('a', 'b', 'c'),
                 ('a', 'b', 'b')]),
            set(self.index.match(('a', 'b', None)))
        )
        self.assertEqual(
            set([('a', 'b', 'c'),
                 ('a', 'b', 'b'),
                 ('a', 'a', 'b')]),
            set(self.index.match(('a', None, None)))
        )
        self.assertEqual(
            set([('a', 'b', 'c'),
                 ('c', 'c', 'c'),
                 ('a', 'b', 'b'),
                 ('a', 'a', 'b')]),
            set(self.index.match((None, None, None)))
        )
    
    def test_key_error_if_not_indexed(self):
        self.index2.insert(('a', 'b', 'c'))
        self.index2.insert(('c', 'c', 'c'))
        self.index2.insert(('a', 'b', 'b'))
        self.index2.insert(('a', 'a', 'b'))
        
        self.assertEqual(
            set([('a', 'b', 'c')]),
            set(self.index2.match(('a', None, 'c')))
        )
        
        try:
            set(self.index2.match(('a', 'b', None)))
            self.fail('This index should not work with provided match')
        except LookupError:
            pass
        
        try:
            set(self.index2.match((None, 'b', 'c')))
            self.fail('This index should not work with provided match')
        except LookupError:
            pass