<s> import os <EOL> import unittest <EOL> import query <EOL> from basename_ranker import BasenameRanker <EOL> from query import Query <EOL> from query_cache import QueryCache <EOL> from query_result import QueryResult <EOL> def make_result ( hits ) : <EOL> res = QueryResult ( ) <EOL> for h in hits : <EOL> res . filenames . append ( h ) <EOL> res . ranks . append ( <NUM_LIT:10> ) <EOL> return res <EOL> class FakeDBShardManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , files = [ ] , dirs = [ ] ) : <EOL> self . dirs = dirs <EOL> self . files = files <EOL> self . files_by_lower_basename = { } <EOL> for f in files : <EOL> bn = os . path . basename ( f ) . lower ( ) <EOL> if bn not in self . files_by_lower_basename : <EOL> self . files_by_lower_basename [ bn ] = [ ] <EOL> self . files_by_lower_basename [ bn ] . append ( f ) <EOL> def search_basenames ( self , basename_query ) : <EOL> res = [ ] <EOL> lower_basename_query = basename_query . lower ( ) <EOL> for bn in self . files_by_lower_basename . keys ( ) : <EOL> if bn . find ( lower_basename_query ) != - <NUM_LIT:1> : <EOL> res . append ( bn ) <EOL> return res , False <EOL> class MockQuery ( Query ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> Query . __init__ ( self , * args , ** kwargs ) <EOL> self . did_call_execute_nocache = False <EOL> def execute_nocache ( self , shard_manager , query_cache ) : <EOL> self . did_call_execute_nocache = True <EOL> return Query . execute_nocache ( self , shard_manager , query_cache ) <EOL> class QueryTest ( unittest . TestCase ) : <EOL> def test_cons ( self ) : <EOL> Query ( "<STR_LIT:a>" ) <EOL> Query ( "<STR_LIT:a>" , <NUM_LIT:10> ) <EOL> Query ( "<STR_LIT:a>" , <NUM_LIT:10> , True ) <EOL> Query ( "<STR_LIT:a>" , <NUM_LIT:10> , True , "<STR_LIT>" ) <EOL> Query ( "<STR_LIT:a>" , <NUM_LIT:10> , True , "<STR_LIT>" , [ "<STR_LIT:bar>" ] ) <EOL> Query ( "<STR_LIT:a>" , <NUM_LIT:10> , True , "<STR_LIT>" , [ "<STR_LIT:bar>" ] ) <EOL> def test_to_and_from_dict ( self ) : <EOL> x = Query ( "<STR_LIT:a>" , <NUM_LIT:10> , True , "<STR_LIT>" , [ "<STR_LIT:bar>" ] ) <EOL> x . debug = True <EOL> y = x . as_dict ( ) <EOL> z = Query . from_dict ( y ) <EOL> self . assertEquals ( x . text , z . text ) <EOL> self . assertEquals ( x . max_hits , z . max_hits ) <EOL> self . assertEquals ( x . exact_match , z . exact_match ) <EOL> self . assertEquals ( x . current_filename , z . current_filename ) <EOL> self . assertEquals ( x . open_filenames , z . open_filenames ) <EOL> self . assertEquals ( x . debug , z . debug ) <EOL> def test_is_exact_match_1 ( self ) : <EOL> self . assertTrue ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertTrue ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertTrue ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertTrue ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertFalse ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertFalse ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertTrue ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertFalse ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertFalse ( query . _is_exact_match ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def test_filter_result_for_exact_matches ( self ) : <EOL> res = make_result ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> exact_res = query . _filter_result_for_exact_matches ( "<STR_LIT>" , res ) <EOL> self . assertEquals ( len ( exact_res . ranks ) , len ( exact_res . filenames ) ) <EOL> self . assertEquals ( <NUM_LIT:2> , len ( exact_res . filenames ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , "<STR_LIT>" ] , exact_res . filenames ) <EOL> exact_res = query . _filter_result_for_exact_matches ( "<STR_LIT>" , res ) <EOL> self . assertEquals ( len ( exact_res . ranks ) , len ( exact_res . filenames ) ) <EOL> self . assertEquals ( <NUM_LIT:1> , len ( exact_res . filenames ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" ] , exact_res . filenames ) <EOL> exact_res = query . _filter_result_for_exact_matches ( "<STR_LIT>" , res ) <EOL> self . assertEquals ( len ( exact_res . ranks ) , len ( exact_res . filenames ) ) <EOL> self . assertEquals ( <NUM_LIT:0> , len ( exact_res . filenames ) ) <EOL> self . assertEquals ( [ ] , exact_res . filenames ) <EOL> def test_global_rank_adjustment_puts_suffixes_into_predictable_order ( self ) : <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , [ "<STR_LIT:/>" ] , Query ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , ] , res . filenames ) <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , [ "<STR_LIT:/>" ] , Query ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] , res . filenames ) <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:12> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , [ "<STR_LIT:/>" ] , Query ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] , res . filenames ) <EOL> def test_global_rank_adjustment_puts_directories_into_predictable_order ( self ) : <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , [ "<STR_LIT:/>" ] , Query ( "<STR_LIT>" ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] , res . filenames ) <EOL> def test_adjustment_puts_current_project_above_noncurrent ( self ) : <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> Query ( "<STR_LIT>" , current_filename = "<STR_LIT>" ) ) <EOL> rA = res . rank_of ( "<STR_LIT>" ) <EOL> rB = res . rank_of ( "<STR_LIT>" ) <EOL> self . assertTrue ( rB > rA , "<STR_LIT>" % ( rB , rA ) ) <EOL> def test_adjustment_puts_current_project_then_inactives_in_alphabetical ( self ) : <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> Query ( "<STR_LIT>" , current_filename = "<STR_LIT>" ) ) <EOL> rA = res . rank_of ( "<STR_LIT>" ) <EOL> rB = res . rank_of ( "<STR_LIT>" ) <EOL> rC = res . rank_of ( "<STR_LIT>" ) <EOL> self . assertTrue ( rA > rB , "<STR_LIT>" % ( rA , rB ) ) <EOL> self . assertTrue ( rB > rC , "<STR_LIT>" % ( rB , rC ) ) <EOL> self . assertTrue ( rA > rC , "<STR_LIT>" % ( rA , rC ) ) <EOL> def test_adjustment_puts_different_files_in_different_base_directories_together ( self ) : <EOL> in_res = QueryResult ( hits = [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:9> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:9> ) ] ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> Query ( "<STR_LIT>" , current_filename = "<STR_LIT>" ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] , res . filenames ) <EOL> res = query . _apply_global_rank_adjustment ( in_res , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> Query ( "<STR_LIT>" , current_filename = "<STR_LIT>" ) ) <EOL> self . assertEquals ( [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] , res . filenames ) <EOL> def test_rerank ( self ) : <EOL> self . assertEquals ( [ ] , <EOL> query . _rerank ( [ ] ) ) <EOL> self . assertEquals ( [ ( "<STR_LIT>" , <NUM_LIT:10> ) ] , <EOL> query . _rerank ( [ ( "<STR_LIT>" , <NUM_LIT:10> ) ] ) ) <EOL> self . assertEquals ( [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ) ] , <EOL> query . _rerank ( [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:9> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:7> ) <EOL> ] ) ) <EOL> self . assertEquals ( [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:9> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ) ] , <EOL> query . _rerank ( [ ( "<STR_LIT>" , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:9> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:11> ) ] ) ) <EOL> def test_cache_same_maxhits ( self ) : <EOL> q1 = MockQuery ( "<STR_LIT:a>" , <NUM_LIT:10> ) <EOL> query_cache = QueryCache ( ) <EOL> shard_manager = FakeDBShardManager ( ) <EOL> q1 . execute ( shard_manager , query_cache ) <EOL> self . assertTrue ( q1 . did_call_execute_nocache ) <EOL> q2 = MockQuery ( "<STR_LIT:a>" , <NUM_LIT:10> ) <EOL> q2 . execute ( shard_manager , query_cache ) <EOL> self . assertFalse ( q2 . did_call_execute_nocache ) <EOL> def test_cache_different_maxhits ( self ) : <EOL> q1 = MockQuery ( "<STR_LIT:a>" , <NUM_LIT:10> ) <EOL> query_cache = QueryCache ( ) <EOL> shard_manager = FakeDBShardManager ( ) <EOL> q1 . execute ( shard_manager , query_cache ) <EOL> self . assertTrue ( q1 . did_call_execute_nocache ) <EOL> q2 = MockQuery ( "<STR_LIT:a>" , <NUM_LIT:11> ) <EOL> q2 . execute ( shard_manager , query_cache ) <EOL> self . assertTrue ( q2 . did_call_execute_nocache ) <EOL> def test_query_with_debug ( self ) : <EOL> q = MockQuery ( "<STR_LIT:a>" , <NUM_LIT:10> ) <EOL> q . debug = True <EOL> query_cache = QueryCache ( ) <EOL> shard_manager = FakeDBShardManager ( ) <EOL> q . execute ( shard_manager , query_cache ) <EOL> def test_empty_query ( self ) : <EOL> q1 = MockQuery ( "<STR_LIT>" , <NUM_LIT:10> ) <EOL> query_cache = QueryCache ( ) <EOL> shard_manager = FakeDBShardManager ( ) <EOL> q1 . execute ( shard_manager , query_cache ) <EOL> self . assertFalse ( q1 . did_call_execute_nocache ) <EOL> def test_is_dirmatch ( self ) : <EOL> self . assertTrue ( query . _is_dirmatch ( "<STR_LIT:foo>" , "<STR_LIT>" ) ) <EOL> self . assertTrue ( query . _is_dirmatch ( "<STR_LIT:foo>" , "<STR_LIT>" ) ) <EOL> self . assertFalse ( query . _is_dirmatch ( "<STR_LIT:foo>" , "<STR_LIT>" ) ) <EOL> self . assertTrue ( query . _is_dirmatch ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertFalse ( query . _is_dirmatch ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def test_exact_filter_plumbing ( self ) : <EOL> shard_manager = FakeDBShardManager ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> query_cache = QueryCache ( ) <EOL> res = MockQuery ( "<STR_LIT:bar>" , <NUM_LIT:10> , exact_match = True ) . execute ( shard_manager , query_cache ) <EOL> self . assertTrue ( res . is_empty ( ) ) <EOL> res = MockQuery ( "<STR_LIT>" , <NUM_LIT:10> , exact_match = True ) . execute ( shard_manager , query_cache ) <EOL> self . assertEquals ( [ "<STR_LIT>" ] , res . filenames ) <EOL> def test_empty_dir_query ( self ) : <EOL> shard_manager = FakeDBShardManager ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> query_cache = QueryCache ( ) <EOL> res = MockQuery ( "<STR_LIT:/>" ) . execute ( shard_manager , query_cache ) <EOL> self . assertTrue ( len ( res . filenames ) != <NUM_LIT:0> ) <EOL> def test_dir_only_query ( self ) : <EOL> shard_manager = FakeDBShardManager ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> query_cache = QueryCache ( ) <EOL> res = MockQuery ( "<STR_LIT>" ) . execute ( shard_manager , query_cache ) <EOL> self . assertEquals ( [ "<STR_LIT>" , "<STR_LIT>" ] , res . filenames ) <EOL> def test_query_that_times_out ( self ) : <EOL> files = [ ] <EOL> for i in range ( <NUM_LIT> ) : <EOL> files . append ( "<STR_LIT>" % i ) <EOL> shard_manager = FakeDBShardManager ( files ) <EOL> query_cache = QueryCache ( ) <EOL> q = MockQuery ( "<STR_LIT>" ) <EOL> q . _dir_search_timeout = <NUM_LIT> <EOL> res = q . execute ( shard_manager , query_cache ) <EOL> self . assertTrue ( res . truncated ) <EOL> def test_basename_only_query ( self ) : <EOL> shard_manager = FakeDBShardManager ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> query_cache = QueryCache ( ) <EOL> res = MockQuery ( "<STR_LIT:bar>" ) . execute ( shard_manager , query_cache ) <EOL> self . assertEquals ( [ "<STR_LIT>" , "<STR_LIT>" ] , res . filenames ) <EOL> def test_dir_and_basename_query ( self ) : <EOL> shard_manager = FakeDBShardManager ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> query_cache = QueryCache ( ) <EOL> res = MockQuery ( "<STR_LIT>" ) . execute ( shard_manager , query_cache ) <EOL> self . assertEquals ( [ "<STR_LIT>" , "<STR_LIT>" ] , res . filenames ) <EOL> def test_basename_only_query_rank_results ( self ) : <EOL> shard_manager = FakeDBShardManager ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> query_cache = QueryCache ( ) <EOL> res = MockQuery ( "<STR_LIT:bar>" ) . execute_nocache ( shard_manager , query_cache ) <EOL> self . assertEquals ( set ( [ "<STR_LIT>" , "<STR_LIT>" ] ) , set ( res . filenames ) ) <EOL> self . assertEquals ( [ BasenameRanker ( ) . rank_query ( "<STR_LIT:bar>" , os . path . basename ( res . filenames [ <NUM_LIT:0> ] ) ) , <EOL> BasenameRanker ( ) . rank_query ( "<STR_LIT:bar>" , os . path . basename ( res . filenames [ <NUM_LIT:1> ] ) ) ] , res . ranks ) <EOL> def test_adjustment_creates_decreasing_hit_order ( self ) : <EOL> initial_result = QueryResult . from_dict ( { '<STR_LIT>' : [ ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) , ( '<STR_LIT>' , <NUM_LIT> ) ] , '<STR_LIT>' : [ ] , '<STR_LIT>' : True } ) <EOL> dirs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> q = Query ( "<STR_LIT>" ) <EOL> res = query . _apply_global_rank_adjustment ( initial_result , dirs , q ) <EOL> for i in range ( <NUM_LIT:1> , len ( res . ranks ) ) : <EOL> self . assertTrue ( res . ranks [ i - <NUM_LIT:1> ] > res . ranks [ i ] ) <EOL> self . assertEquals ( '<STR_LIT>' , res . filenames [ <NUM_LIT:0> ] ) </s>
