def test_search_accepts_umlauts(self): # we can enter umlauts in searches source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-foo") result = [x.title for x in source.search(u"Bär")] assert u"Bär (4)" in result
def test_get_term_by_token(self): # we can get a term by its token source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-foo") result = source.getTermByToken(u'1') self.assertTrue(ITitledTokenizedTerm.providedBy(result))
def test_allow_iter_on_the_fly(self): # we can forbid to iterate even after creation of source source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-foo", allow_iter=True) self.assertEqual(len([x for x in source]), 4) source.allow_iter = False self.assertEqual(len([x for x in source]), 0)
def test_split_entry(self): # we can split ZSET entries. source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port) assert source._split_entry("one&&two") == ("one", u"two") assert source._split_entry(u"one&&two") == ("one", u"two") part1, part2 = source._split_entry("one&&two") assert isinstance(part1, str) assert isinstance(part2, unicode)
def test_search_num(self): # we get 10 result entries (if available) for n in range(50): self.redis.zadd(u"autocomplete-baz", 0, "baz %s&&%s" % (n, n)) self.redis.set("%s" % n, "Baz %s" % n) source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-baz") result1 = [x.title for x in source.search("baz 1")] self.assertEqual(len(result1), 10) assert u"Baz 1 (1)" in result1 assert u"Baz 19 (19)" not in result1
def test_get_term_contained(self): # we can get contained terms as ITerm source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-foo") term = source.getTerm(u'4') assert ITitledTokenizedTerm.providedBy(term) self.assertTrue(hasattr(term, 'value')) self.assertEqual(term.value, u'4') self.assertTrue(hasattr(term, 'token')) self.assertEqual(term.token, u'4') self.assertTrue(hasattr(term, 'title')) self.assertEqual(term.title, u'Bär (4)')
def test_search(self): # we can search autocomplete sources source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-foo") result1 = [x.title for x in source.search("b")] result2 = [x.title for x in source.search("fo")] result3 = [x.title for x in source.search("for")] result4 = [x.title for x in source.search("For (2)")] self.assertEqual( result1, [u'Bär (4)', u'Baz (3)', u'Foo (1)', u'For (2)']) self.assertEqual(result2, [u"Foo (1)", u"For (2)"]) self.assertEqual(result3, [u"For (2)"]) self.assertEqual(result4, [u"For (2)"])
def test_search_sorting(self): # we get a sorted list returned for n in range(5): # create 5 items of each variant (bar, bär, baer) self.redis.zadd(u"autocomplete-baz", 0, "bar&&2%s" % n) self.redis.set("2%s" % n, u"Bar") self.redis.zadd(u"autocomplete-baz", 0, "bar&&1%s" % n) self.redis.set("1%s" % n, u"Bär") self.redis.zadd(u"autocomplete-baz", 0, "baer&&3%s" % n) self.redis.set("3%s" % n, u"Baer") source = RedisAutocompleteSource( host=self.redis_host, port=self.redis_port, zset_name="autocomplete-baz") result = [x.title for x in source.search(u"Bar")] assert u"Bär (10)" in result assert u"Bar (20)" in result self.assertEqual(result[0], u"Bär (10)")