def test_expand(): suggestive.expand("Lincoln Clarete").should.equal([ 'l', 'li', 'lin', 'linc', 'linco', 'lincol', 'lincoln', 'c', 'cl', 'cla', 'clar', 'clare', 'claret', 'clarete', ]) suggestive.expand("Lincoln", min_chars=2).should.equal([ 'li', 'lin', 'linc', 'linco', 'lincol', 'lincoln', ])
def test_redis_backend_indexing(): # Given that I have an instance of our redis backend conn = Mock() pipe = conn.pipeline.return_value data = [{"id": 0, "name": "Lincoln"}, {"id": 1, "name": "Clarete"}] backend = suggestive.RedisBackend(conn=conn) backend.remove = Mock() # We don't care about removing stuff here # When I try to index stuff indexed = backend.index(data, field='name', score='id') # Then I see that the number of indexed items is right indexed.should.equal(2) # And I see that the document set was fed list(conn.hset.call_args_list).should.equal([ call('suggestive:d', 0, '{"id": 0, "name": "Lincoln"}'), call('suggestive:d', 1, '{"id": 1, "name": "Clarete"}') ]) # And I see that we have a special stash to save to which terms each # document was added (it will make deletions way easier) list(pipe.sadd.call_args_list).should.equal([ call('suggestive:dt:0', *suggestive.expand('lincoln')), call('suggestive:dt:1', *suggestive.expand('clarete')), ]) # And the term set was also fed list(pipe.zadd.call_args_list).should.equal([ call('suggestive:d:l', 0, 0), call('suggestive:d:li', 0, 0), call('suggestive:d:lin', 0, 0), call('suggestive:d:linc', 0, 0), call('suggestive:d:linco', 0, 0), call('suggestive:d:lincol', 0, 0), call('suggestive:d:lincoln', 0, 0), call('suggestive:d:c', 1, 1), call('suggestive:d:cl', 1, 1), call('suggestive:d:cla', 1, 1), call('suggestive:d:clar', 1, 1), call('suggestive:d:clare', 1, 1), call('suggestive:d:claret', 1, 1), call('suggestive:d:clarete', 1, 1) ]) # And that all the documents are indexed conn.hgetall.return_value = { '0': '{"id": 0, "name": "Lincoln"}', '1': '{"id": 1, "name": "Clarete"}', } backend.documents().should.equal({ '0': {u'id': 0, u'name': u'Lincoln'}, '1': {u'id': 1, u'name': u'Clarete'} }) conn.hgetall.assert_called_once_with('suggestive:d') # And I also see that the pipeline was executed! pipe.execute.assert_called_once_with()
def test_redis_backend_remove_items(): # Given that I have some indexed data conn = Mock() pipe = conn.pipeline.return_value data = [ { "id": 0, "first_name": "Lincoln", "last_name": "Clarete" }, { "id": 1, "first_name": "Mingwei", "last_name": "Gu" }, { "id": 2, "first_name": "Livia", "last_name": "C" }, ] backend = suggestive.RedisBackend(conn=conn) with patch.object(backend, 'remove'): backend.index(data, field=['first_name', 'last_name'], score='id') # Mocking the term X doc cache set conn.smembers.return_value = (suggestive.expand('lincoln') + suggestive.expand('clarete')) # When I try to remove stuff backend.remove(0) # Then I see that the terms for this document were removed list(pipe.zrem.call_args_list).should.equal([ call('suggestive:d:l', 0), call('suggestive:d:li', 0), call('suggestive:d:lin', 0), call('suggestive:d:linc', 0), call('suggestive:d:linco', 0), call('suggestive:d:lincol', 0), call('suggestive:d:lincoln', 0), call('suggestive:d:c', 0), call('suggestive:d:cl', 0), call('suggestive:d:cla', 0), call('suggestive:d:clar', 0), call('suggestive:d:clare', 0), call('suggestive:d:claret', 0), call('suggestive:d:clarete', 0), ]) pipe.execute.call_count.should.equal(2) # And the cache key should also be removed conn.delete.assert_called_once_with('suggestive:dt:0') # And I also see that we successfuly removed the document too conn.hdel.assert_called_once_with('suggestive:d', 0)
def test_redis_backend_remove_items(): # Given that I have some indexed data conn = Mock() pipe = conn.pipeline.return_value data = [ {"id": 0, "first_name": "Lincoln", "last_name": "Clarete"}, {"id": 1, "first_name": "Mingwei", "last_name": "Gu"}, {"id": 2, "first_name": "Livia", "last_name": "C"}, ] backend = suggestive.RedisBackend(conn=conn) with patch.object(backend, 'remove'): backend.index(data, field=['first_name', 'last_name'], score='id') # Mocking the term X doc cache set conn.smembers.return_value = ( suggestive.expand('lincoln') + suggestive.expand('clarete')) # When I try to remove stuff backend.remove(0) # Then I see that the terms for this document were removed list(pipe.zrem.call_args_list).should.equal([ call('suggestive:d:l', 0), call('suggestive:d:li', 0), call('suggestive:d:lin', 0), call('suggestive:d:linc', 0), call('suggestive:d:linco', 0), call('suggestive:d:lincol', 0), call('suggestive:d:lincoln', 0), call('suggestive:d:c', 0), call('suggestive:d:cl', 0), call('suggestive:d:cla', 0), call('suggestive:d:clar', 0), call('suggestive:d:clare', 0), call('suggestive:d:claret', 0), call('suggestive:d:clarete', 0), ]) pipe.execute.call_count.should.equal(2) # And the cache key should also be removed conn.delete.assert_called_once_with('suggestive:dt:0') # And I also see that we successfuly removed the document too conn.hdel.assert_called_once_with('suggestive:d', 0)
def test_redis_backend_indexing(): # Given that I have an instance of our redis backend conn = Mock() pipe = conn.pipeline.return_value data = [{"id": 0, "name": "Lincoln"}, {"id": 1, "name": "Clarete"}] backend = suggestive.RedisBackend(conn=conn) backend.remove = Mock() # We don't care about removing stuff here # When I try to index stuff indexed = backend.index(data, field='name', score='id') # Then I see that the number of indexed items is right indexed.should.equal(2) # And I see that the document set was fed list(conn.hset.call_args_list).should.equal([ call('suggestive:d', 0, '{"id": 0, "name": "Lincoln"}'), call('suggestive:d', 1, '{"id": 1, "name": "Clarete"}') ]) # And I see that we have a special stash to save to which terms each # document was added (it will make deletions way easier) list(pipe.sadd.call_args_list).should.equal([ call('suggestive:dt:0', *suggestive.expand('lincoln')), call('suggestive:dt:1', *suggestive.expand('clarete')), ]) # And the term set was also fed list(pipe.zadd.call_args_list).should.equal([ call('suggestive:d:l', 0, 0), call('suggestive:d:li', 0, 0), call('suggestive:d:lin', 0, 0), call('suggestive:d:linc', 0, 0), call('suggestive:d:linco', 0, 0), call('suggestive:d:lincol', 0, 0), call('suggestive:d:lincoln', 0, 0), call('suggestive:d:c', 1, 1), call('suggestive:d:cl', 1, 1), call('suggestive:d:cla', 1, 1), call('suggestive:d:clar', 1, 1), call('suggestive:d:clare', 1, 1), call('suggestive:d:claret', 1, 1), call('suggestive:d:clarete', 1, 1) ]) # And that all the documents are indexed conn.hgetall.return_value = { '0': '{"id": 0, "name": "Lincoln"}', '1': '{"id": 1, "name": "Clarete"}', } backend.documents().should.equal({ '0': { u'id': 0, u'name': u'Lincoln' }, '1': { u'id': 1, u'name': u'Clarete' } }) conn.hgetall.assert_called_once_with('suggestive:d') # And I also see that the pipeline was executed! pipe.execute.assert_called_once_with()