<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from django . apps import apps <EOL> from haystack . backends import BaseEngine , BaseSearchBackend , BaseSearchQuery , log_query <EOL> from haystack . models import SearchResult <EOL> from haystack . routers import BaseRouter <EOL> from haystack . utils import get_identifier <EOL> class MockMasterSlaveRouter ( BaseRouter ) : <EOL> def for_read ( self , ** hints ) : <EOL> return '<STR_LIT>' <EOL> def for_write ( self , ** hints ) : <EOL> return '<STR_LIT>' <EOL> class MockPassthroughRouter ( BaseRouter ) : <EOL> def for_read ( self , ** hints ) : <EOL> if hints . get ( '<STR_LIT>' ) is False : <EOL> return '<STR_LIT>' <EOL> return None <EOL> def for_write ( self , ** hints ) : <EOL> if hints . get ( '<STR_LIT>' ) is False : <EOL> return '<STR_LIT>' <EOL> return None <EOL> class MockMultiRouter ( BaseRouter ) : <EOL> def for_write ( self , ** hints ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class MockSearchResult ( SearchResult ) : <EOL> def __init__ ( self , app_label , model_name , pk , score , ** kwargs ) : <EOL> super ( MockSearchResult , self ) . __init__ ( app_label , model_name , pk , score , ** kwargs ) <EOL> self . _model = apps . get_model ( '<STR_LIT>' , model_name ) <EOL> MOCK_SEARCH_RESULTS = [ MockSearchResult ( '<STR_LIT>' , '<STR_LIT>' , i , <NUM_LIT:1> - ( i / <NUM_LIT> ) ) for i in range ( <NUM_LIT:1> , <NUM_LIT:100> ) ] <EOL> MOCK_INDEX_DATA = { } <EOL> class MockSearchBackend ( BaseSearchBackend ) : <EOL> model_name = '<STR_LIT>' <EOL> def update ( self , index , iterable , commit = True ) : <EOL> global MOCK_INDEX_DATA <EOL> for obj in iterable : <EOL> doc = index . full_prepare ( obj ) <EOL> MOCK_INDEX_DATA [ doc [ '<STR_LIT:id>' ] ] = doc <EOL> def remove ( self , obj , commit = True ) : <EOL> global MOCK_INDEX_DATA <EOL> if commit == True : <EOL> del ( MOCK_INDEX_DATA [ get_identifier ( obj ) ] ) <EOL> def clear ( self , models = None , commit = True ) : <EOL> global MOCK_INDEX_DATA <EOL> MOCK_INDEX_DATA = { } <EOL> @ log_query <EOL> def search ( self , query_string , ** kwargs ) : <EOL> from haystack import connections <EOL> global MOCK_INDEX_DATA <EOL> results = [ ] <EOL> hits = len ( MOCK_INDEX_DATA ) <EOL> indexed_models = connections [ '<STR_LIT:default>' ] . get_unified_index ( ) . get_indexed_models ( ) <EOL> def junk_sort ( key ) : <EOL> app , model , pk = key . split ( '<STR_LIT:.>' ) <EOL> if pk . isdigit ( ) : <EOL> return int ( pk ) <EOL> else : <EOL> return ord ( pk [ <NUM_LIT:0> ] ) <EOL> sliced = sorted ( MOCK_INDEX_DATA , key = junk_sort ) <EOL> for i , result in enumerate ( sliced ) : <EOL> app_label , model_name , pk = result . split ( '<STR_LIT:.>' ) <EOL> model = apps . get_model ( app_label , model_name ) <EOL> if model : <EOL> if model in indexed_models : <EOL> results . append ( MockSearchResult ( app_label , model_name , pk , <NUM_LIT:1> - ( i / <NUM_LIT> ) ) ) <EOL> else : <EOL> hits -= <NUM_LIT:1> <EOL> else : <EOL> hits -= <NUM_LIT:1> <EOL> return { <EOL> '<STR_LIT>' : results [ kwargs . get ( '<STR_LIT>' ) : kwargs . get ( '<STR_LIT>' ) ] , <EOL> '<STR_LIT>' : hits , <EOL> } <EOL> def more_like_this ( self , model_instance , additional_query_string = None , result_class = None ) : <EOL> return self . search ( query_string = '<STR_LIT:*>' ) <EOL> class CharPKMockSearchBackend ( MockSearchBackend ) : <EOL> model_name = '<STR_LIT>' <EOL> mock_search_results = [ MockSearchResult ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0.5> ) , <EOL> MockSearchResult ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> ) ] <EOL> class ReadQuerySetMockSearchBackend ( MockSearchBackend ) : <EOL> model_name = '<STR_LIT>' <EOL> mock_search_results = [ MockSearchResult ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> MockSearchResult ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:2> ) ] <EOL> class MixedMockSearchBackend ( MockSearchBackend ) : <EOL> @ log_query <EOL> def search ( self , query_string , ** kwargs ) : <EOL> if kwargs . get ( '<STR_LIT>' ) and kwargs [ '<STR_LIT>' ] > <NUM_LIT:30> : <EOL> kwargs [ '<STR_LIT>' ] = <NUM_LIT:30> <EOL> result_info = super ( MixedMockSearchBackend , self ) . search ( query_string , ** kwargs ) <EOL> result_info [ '<STR_LIT>' ] = <NUM_LIT:30> <EOL> temp_results = [ ] <EOL> for result in result_info [ '<STR_LIT>' ] : <EOL> if not int ( result . pk ) in ( <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT> ) : <EOL> temp_results . append ( result ) <EOL> result_info [ '<STR_LIT>' ] = temp_results <EOL> return result_info <EOL> class MockSearchQuery ( BaseSearchQuery ) : <EOL> def build_query ( self ) : <EOL> return '<STR_LIT>' <EOL> def clean ( self , query_fragment ) : <EOL> return query_fragment <EOL> class MockEngine ( BaseEngine ) : <EOL> backend = MockSearchBackend <EOL> query = MockSearchQuery </s>
