<s> import random <EOL> from unittest import TestCase <EOL> from redis_completion . engine import RedisEngine <EOL> stop_words = set ( [ '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class RedisCompletionTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . engine = self . get_engine ( ) <EOL> self . engine . flush ( ) <EOL> def get_engine ( self ) : <EOL> return RedisEngine ( prefix = '<STR_LIT>' , db = <NUM_LIT:15> ) <EOL> def store_data ( self , id = None ) : <EOL> test_data = ( <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' ) , <EOL> ) <EOL> for obj_id , title in test_data : <EOL> if id is None or id == obj_id : <EOL> self . engine . store_json ( obj_id , title , { <EOL> '<STR_LIT>' : obj_id , <EOL> '<STR_LIT:title>' : title , <EOL> '<STR_LIT>' : obj_id % <NUM_LIT:2> == <NUM_LIT:0> and '<STR_LIT>' or '<STR_LIT>' , <EOL> } ) <EOL> def sort_results ( self , r ) : <EOL> return sorted ( r , key = lambda i : i [ '<STR_LIT>' ] ) <EOL> def test_search ( self ) : <EOL> self . store_data ( ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . sort_results ( results ) , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT:test>' ) <EOL> self . assertEqual ( self . sort_results ( results ) , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ <EOL> { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ ] ) <EOL> def test_boosting ( self ) : <EOL> test_data = ( <EOL> ( <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:6> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:7> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:8> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:9> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:10> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:11> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:12> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> for obj_id , title , obj_type in test_data : <EOL> self . engine . store_json ( obj_id , title , { <EOL> '<STR_LIT>' : obj_id , <EOL> '<STR_LIT:title>' : title , <EOL> } , obj_type ) <EOL> def assertExpected ( results , id_list ) : <EOL> self . assertEqual ( [ r [ '<STR_LIT>' ] for r in results ] , id_list ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> assertExpected ( results , [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:9> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' , boosts = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> assertExpected ( results , [ <NUM_LIT:5> , <NUM_LIT:1> , <NUM_LIT:9> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT:test>' , boosts = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> assertExpected ( results , [ <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:12> , <NUM_LIT:11> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:7> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:3> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' , boosts = { '<STR_LIT>' : <NUM_LIT:0.5> } ) <EOL> assertExpected ( results , [ <NUM_LIT:5> , <NUM_LIT:9> , <NUM_LIT:1> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' , boosts = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> assertExpected ( results , [ <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:5> ] ) <EOL> results = self . engine . search_json ( '<STR_LIT>' , boosts = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT:5>' : <NUM_LIT> } ) <EOL> assertExpected ( results , [ <NUM_LIT:5> , <NUM_LIT:9> , <NUM_LIT:1> ] ) <EOL> def test_autoboost ( self ) : <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def assertExpected ( results , id_list ) : <EOL> self . assertEqual ( results , [ '<STR_LIT>' % i for i in id_list ] ) <EOL> results = self . engine . search ( '<STR_LIT>' , autoboost = True ) <EOL> assertExpected ( results , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> self . engine . boost ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' , autoboost = True ) <EOL> assertExpected ( results , [ <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> self . engine . boost ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' , autoboost = True ) <EOL> assertExpected ( results , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> self . engine . boost ( '<STR_LIT>' , negative = True ) <EOL> results = self . engine . search ( '<STR_LIT>' , autoboost = True ) <EOL> assertExpected ( results , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:1> ] ) <EOL> results = self . engine . search ( '<STR_LIT>' , boosts = { '<STR_LIT>' : <NUM_LIT> } , autoboost = True ) <EOL> assertExpected ( results , [ <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> results = self . engine . search ( '<STR_LIT>' , boosts = { '<STR_LIT>' : <NUM_LIT> } , autoboost = True ) <EOL> assertExpected ( results , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:1> ] ) <EOL> def test_limit ( self ) : <EOL> self . store_data ( ) <EOL> results = self . engine . search_json ( '<STR_LIT>' , limit = <NUM_LIT:1> ) <EOL> self . assertEqual ( results , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> def test_filters ( self ) : <EOL> self . store_data ( ) <EOL> f = lambda i : i [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> results = self . engine . search_json ( '<STR_LIT>' , filters = [ f ] ) <EOL> self . assertEqual ( self . sort_results ( results ) , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> def test_simple ( self ) : <EOL> self . engine . print_scores = True <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> results = self . engine . search ( '<STR_LIT:code>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_correct_sorting ( self ) : <EOL> strings = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> strings . append ( '<STR_LIT>' % chr ( i + ord ( '<STR_LIT:a>' ) ) ) <EOL> if i > <NUM_LIT:0> : <EOL> strings . append ( '<STR_LIT>' % chr ( i + ord ( '<STR_LIT:a>' ) ) ) <EOL> random . shuffle ( strings ) <EOL> for s in strings : <EOL> self . engine . store ( s ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , sorted ( strings ) ) <EOL> results = self . engine . search ( '<STR_LIT>' , limit = <NUM_LIT:30> ) <EOL> self . assertEqual ( results , sorted ( strings ) [ : <NUM_LIT:30> ] ) <EOL> def test_removing_objects ( self ) : <EOL> self . store_data ( ) <EOL> self . engine . remove ( <NUM_LIT:1> ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . sort_results ( results ) , [ <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> self . store_data ( <NUM_LIT:1> ) <EOL> self . engine . remove ( <NUM_LIT:2> ) <EOL> results = self . engine . search_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . sort_results ( results ) , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] ) <EOL> def test_clean_phrase ( self ) : <EOL> self . assertEqual ( self . engine . clean_phrase ( '<STR_LIT>' ) , [ '<STR_LIT:abc>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . engine . clean_phrase ( '<STR_LIT>' ) , [ ] ) <EOL> self . assertEqual ( self . engine . clean_phrase ( '<STR_LIT>' ) , [ ] ) <EOL> self . assertEqual ( <EOL> self . engine . clean_phrase ( '<STR_LIT>' ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_exists ( self ) : <EOL> self . assertFalse ( self . engine . exists ( '<STR_LIT:test>' ) ) <EOL> self . engine . store ( '<STR_LIT:test>' ) <EOL> self . assertTrue ( self . engine . exists ( '<STR_LIT:test>' ) ) <EOL> def test_removing_objects_in_depth ( self ) : <EOL> redis_client = self . engine . client <EOL> prefix = self . engine . prefix <EOL> initial_key_count = len ( redis_client . keys ( ) ) <EOL> self . store_data ( <NUM_LIT:1> ) <EOL> key_len = len ( redis_client . keys ( ) ) <EOL> self . store_data ( <NUM_LIT:2> ) <EOL> key_len2 = len ( redis_client . keys ( ) ) <EOL> self . assertTrue ( key_len != key_len2 ) <EOL> self . engine . remove ( <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( redis_client . keys ( ) ) , key_len ) <EOL> self . engine . remove ( <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( redis_client . keys ( ) ) , initial_key_count ) <EOL> def test_updating ( self ) : <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' ] ) <EOL> self . engine . store ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , False ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' ] ) <EOL> def test_issue9_ordering ( self ) : <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> results = self . engine . search ( '<STR_LIT>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> self . engine . store ( '<STR_LIT>' ) <EOL> results = self . engine . search ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( results , [ '<STR_LIT>' , '<STR_LIT>' ] ) </s>
