Пример #1
0
class TestWildCardSearch(unittest.TestCase):
    def test_trie_asterisk_search(self):
        self.trie = Trie()
        self.trie.add_all(['ash', 'ashley'])
        self.assertIsInstance(self.trie, Trie,
                              "Object should be of type `lexpy.trie.Trie`")
        self.assertTrue('ash' in self.trie, "Word should be in trie")
        self.assertTrue('ashley' in self.trie, "Word should be in trie")
        self.assertEqual(sorted(self.trie.search('a*')),
                         sorted(['ash', 'ashley']),
                         'The lists should be equal')

    def test_trie_question_search(self):
        self.trie = Trie()
        self.trie.add_all(['ab', 'as', 'ash', 'ashley'])
        self.assertIsInstance(self.trie, Trie,
                              "Object should be of type `lexpy.trie.Trie`")
        self.assertTrue('ash' in self.trie, "Word should be in trie")
        self.assertTrue('ashley' in self.trie, "Word should be in trie")
        self.assertEqual(sorted(self.trie.search('a?')), sorted(['ab', 'as']),
                         'The lists should be equal')

    def test_trie_wildcard_search(self):
        self.trie = Trie()
        self.trie.add_all(['ab', 'as', 'ash', 'ashley'])
        self.assertIsInstance(self.trie, Trie,
                              "Object should be of type `lexpy.trie.Trie`")
        self.assertTrue('ash' in self.trie, "Word should be in trie")
        self.assertTrue('ashley' in self.trie, "Word should be in trie")
        self.assertEqual(sorted(self.trie.search('*a******?')),
                         sorted(['ab', 'as', 'ash', 'ashley']),
                         'The lists should be equal')

    def test_trie_wildcard_exception(self):
        self.trie = Trie()
        self.trie.add_all(['ab', 'as', 'ash', 'ashley'])
        self.assertIsInstance(self.trie, Trie,
                              "Object should be of type `lexpy.trie.Trie`")
        self.assertTrue('ash' in self.trie, "Word should be in trie")
        self.assertTrue('ashley' in self.trie, "Word should be in trie")
        self.assertRaises(InvalidWildCardExpressionError, self.trie.search,
                          '#$%^a')
Пример #2
0
 def test_without_count(self):
     trie = Trie()
     trie.add_all(['ash', 'ashley', 'ashes', 'ashes'])
     expected = ['ash', 'ashley', 'ashes']
     self.assertListEqual(expected, trie.search('a*'))
Пример #3
0
 def test_with_count(self):
     trie = Trie()
     trie.add_all(['ash', 'ashley', 'ashes', 'ashes'])
     expected = [('ash', 1), ('ashley', 1), ('ashes', 2)]
     self.assertListEqual(expected, trie.search('a*', with_count=True))