def setUp(self): self.d = Dictionary(storage_type='redis', flush_db=True, host=redis_host, port=redis_port, db=redis_db) self.words = DictionaryTests.some_words()
fd.close() return "Binary message written!" else: return "415 Unsupported Media Type ;)" app.config['flask_profiler'] = { "enabled": True, "storage": { "engine": "mongodb" }, "basicAuth": { "enabled": True, "username": "******", "password": "******" }, "ignore": ["^/static/.*"] } flask_profiler.init_app(app) if __name__ == '__main__': f = Dictionary(storage_type='redis', edit_distance_max=2, best_suggestions_only=False) store = storage(storage_type='redis') pool = redis.ConnectionPool( host='localhost', port=6379, decode_responses=True) # host是redis主机,需要redis服务端和客户端都起着 redis默认端口是6379 r = redis.Redis(connection_pool=pool) app.run(host="127.0.0.1", port=5666)
def setUp(self): self.d = Dictionary() self.d_all = Dictionary(best_suggestions_only=False) self.words = DictionaryTests.some_words()
class DictionaryTests(unittest.TestCase): def setUp(self): self.d = Dictionary() self.d_all = Dictionary(best_suggestions_only=False) self.words = DictionaryTests.some_words() @staticmethod def some_words(): words = ['orange', 'prange', 'rng'] words += ['banan', 'banana', 'banans'] words += ['aaple', 'apple', 'aple', 'aXpple', 'aXppYle'] return words def test_add_word(self): self.d.add_word('ciao') ciao_deletes = Word.deletes('ciao', self.d.edit_distance_max) for delete in ciao_deletes: self.assertIn(delete, self.d._suggestions.terms) self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) self.d.add_word('ciaoB') ciaoB_deletes = Word.deletes('ciaoB', self.d.edit_distance_max) for delete in ciaoB_deletes: self.assertIn(delete, self.d._suggestions.terms) try: self.assertSetEqual(set(['ciaoB']), self.d._suggestions[delete]) except AssertionError: # collisions with deletes of ciao -- ciao is preserved in the suggestions since its shorter self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) for delete in ciao_deletes.intersection(ciaoB_deletes): self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) for delete in ciao_deletes.difference(ciaoB_deletes): self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) for delete in ciaoB_deletes.difference(ciao_deletes): self.assertSetEqual(set(['ciaoB']), self.d._suggestions[delete]) self.d.add_word('miao') miao_deletes = Word.deletes('miao', self.d.edit_distance_max) for delete in miao_deletes: self.assertIn(delete, self.d._suggestions.terms) for delete in miao_deletes.intersection( ciao_deletes): # deletes in common have two elements now self.assertSetEqual(set(['ciao', 'miao']), self.d._suggestions[delete]) for delete in miao_deletes.difference( ciao_deletes): # these are only 'miao' deletes self.assertSetEqual(set(['miao']), self.d._suggestions[delete]) def test_lookup(self): # typos have been made on purpose :) bags = [['apl', 'aple', 'apple', 'applex', 'applexy'], ['orange', 'rnge', 'rn'], ['kiwi', 'kiwi;;', 'ki'], ['watermelon', 'wassermelon', 'waxermekon']] for bag in bags: self.d.add_words(bag) res = [] for word in bag: res += self.d.lookup(word) self.assertListEqual(sorted(list(set(res))), sorted(bag)) def test_lookup_2(self): self.d.add_word('simone') self.assertListEqual(['simone'], self.d.lookup('simo')) self.d.add_word('simon') # a closer word arrives self.assertListEqual(['simon'], self.d.lookup('simo')) self.assertListEqual(['simone', 'simon'], self.d.lookup('simone'))
class DictionaryTests(unittest.TestCase): def setUp(self): self.d = Dictionary() self.d_all = Dictionary(best_suggestions_only=False) self.words = DictionaryTests.some_words() @staticmethod def some_words(): words = ['orange', 'prange', 'rng'] words += ['banan', 'banana', 'banans'] words += ['aaple', 'apple', 'aple', 'aXpple', 'aXppYle'] return words def test_add_word(self): self.d.add_word('ciao') ciao_deletes = Word.deletes('ciao', self.d.edit_distance_max) for delete in ciao_deletes: self.assertIn(delete, self.d._suggestions.terms) self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) self.d.add_word('ciaoB') ciaoB_deletes = Word.deletes('ciaoB', self.d.edit_distance_max) for delete in ciaoB_deletes: self.assertIn(delete, self.d._suggestions.terms) try: self.assertSetEqual(set(['ciaoB']), self.d._suggestions[delete]) except AssertionError: # collisions with deletes of ciao -- ciao is preserved in the suggestions since its shorter self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) for delete in ciao_deletes.intersection(ciaoB_deletes): self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) for delete in ciao_deletes.difference(ciaoB_deletes): self.assertSetEqual(set(['ciao']), self.d._suggestions[delete]) for delete in ciaoB_deletes.difference(ciao_deletes): self.assertSetEqual(set(['ciaoB']), self.d._suggestions[delete]) self.d.add_word('miao') miao_deletes = Word.deletes('miao', self.d.edit_distance_max) for delete in miao_deletes: self.assertIn(delete, self.d._suggestions.terms) for delete in miao_deletes.intersection(ciao_deletes): # deletes in common have two elements now self.assertSetEqual(set(['ciao', 'miao']), self.d._suggestions[delete]) for delete in miao_deletes.difference(ciao_deletes): # these are only 'miao' deletes self.assertSetEqual(set(['miao']), self.d._suggestions[delete]) def test_lookup(self): # typos have been made on purpose :) bags = [['apl', 'aple', 'apple', 'applex', 'applexy'], ['orange', 'rnge', 'rn'], ['kiwi', 'kiwi;;', 'ki'], ['watermelon', 'wassermelon', 'waxermekon']] for bag in bags: self.d.add_words(bag) res = [] for word in bag: res += self.d.lookup(word) self.assertListEqual(sorted(list(set(res))), sorted(bag)) def test_lookup_2(self): self.d.add_word('simone') self.assertListEqual(['simone'], self.d.lookup('simo')) self.d.add_word('simon') # a closer word arrives self.assertListEqual(['simon'], self.d.lookup('simo')) self.assertListEqual(['simone', 'simon'], self.d.lookup('simone'))