<s> """<STR_LIT>""" <EOL> import datetime <EOL> import time <EOL> from google . appengine . api import search <EOL> from core . platform . search import gae_search_services <EOL> from core . tests import test_utils <EOL> class SearchAddToIndexTests ( test_utils . GenericTestBase ) : <EOL> """<STR_LIT>""" <EOL> def test_insert_document_with_id ( self ) : <EOL> date = datetime . date ( year = <NUM_LIT> , month = <NUM_LIT:3> , day = <NUM_LIT> ) <EOL> datetime_value = datetime . datetime ( <EOL> year = <NUM_LIT> , month = <NUM_LIT:6> , day = <NUM_LIT:20> , hour = <NUM_LIT:16> , minute = <NUM_LIT:30> , second = <NUM_LIT> ) <EOL> doc_id = '<STR_LIT>' <EOL> doc = { <EOL> '<STR_LIT:id>' : doc_id , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT>' : date , <EOL> '<STR_LIT>' : datetime_value <EOL> } <EOL> result = gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT>' ) <EOL> self . assertEqual ( result , [ doc_id ] ) <EOL> result_doc = search . Index ( '<STR_LIT>' ) . get ( doc_id ) <EOL> self . assertEqual ( result_doc . doc_id , doc_id ) <EOL> self . assertEqual ( result_doc . field ( '<STR_LIT>' ) . value , <NUM_LIT:5> ) <EOL> self . assertEqual ( result_doc . field ( '<STR_LIT>' ) . value , '<STR_LIT:abc>' ) <EOL> self . assertEqual ( <EOL> result_doc . field ( '<STR_LIT>' ) . value , <EOL> datetime . datetime . combine ( <EOL> date = date , <EOL> time = datetime . datetime . min . time ( ) ) ) <EOL> self . assertEqual ( <EOL> result_doc . field ( '<STR_LIT>' ) . value , datetime_value ) <EOL> def test_insert_document_without_id ( self ) : <EOL> doc = { '<STR_LIT:abc>' : '<STR_LIT>' } <EOL> result = gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT>' ) <EOL> retrieved_doc = search . Index ( '<STR_LIT>' ) . get ( result [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( retrieved_doc . field ( '<STR_LIT:abc>' ) . value , '<STR_LIT>' ) <EOL> def test_insert_multiple_with_id ( self ) : <EOL> docs = [ { '<STR_LIT:id>' : '<STR_LIT>' % n , '<STR_LIT:name>' : '<STR_LIT>' % n } for n in range ( <NUM_LIT:5> ) ] <EOL> result = gae_search_services . add_documents_to_index ( docs , '<STR_LIT>' ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> for ind in range ( <NUM_LIT:5> ) : <EOL> retrieved_doc = index . get ( '<STR_LIT>' % ind ) <EOL> self . assertEqual ( retrieved_doc . field ( '<STR_LIT:name>' ) . value , '<STR_LIT>' % ind ) <EOL> self . assertEqual ( result [ ind ] , '<STR_LIT>' % ind ) <EOL> def test_insert_document_with_multi_valued_property ( self ) : <EOL> doc = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] } <EOL> gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT:index>' ) <EOL> resultdoc = search . Index ( '<STR_LIT:index>' ) . get ( '<STR_LIT>' ) <EOL> values = set ( [ field . value for field in resultdoc [ '<STR_LIT>' ] ] ) <EOL> self . assertEqual ( values , set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> def test_disallow_unsuported_value_types ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> doc = { '<STR_LIT:abc>' : set ( '<STR_LIT>' ) } <EOL> gae_search_services . add_documents_to_index ( doc , '<STR_LIT>' ) <EOL> def test_add_document_with_rank ( self ) : <EOL> doc = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:value>' , '<STR_LIT>' : <NUM_LIT> } <EOL> gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT>' ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> self . assertEqual ( index . get ( '<STR_LIT>' ) . rank , <NUM_LIT> ) <EOL> def test_add_document_with_existing_id_updates_it ( self ) : <EOL> doc1 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:version>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:10> } <EOL> doc2 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:version>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:20> } <EOL> gae_search_services . add_documents_to_index ( [ doc1 ] , '<STR_LIT>' ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> self . assertEqual ( index . get ( '<STR_LIT>' ) . field ( '<STR_LIT:version>' ) . value , <NUM_LIT:1> ) <EOL> self . assertEqual ( index . get ( '<STR_LIT>' ) . rank , <NUM_LIT:10> ) <EOL> gae_search_services . add_documents_to_index ( [ doc2 ] , '<STR_LIT>' ) <EOL> self . assertEqual ( index . get ( '<STR_LIT>' ) . field ( '<STR_LIT:version>' ) . value , <NUM_LIT:2> ) <EOL> self . assertEqual ( index . get ( '<STR_LIT>' ) . rank , <NUM_LIT:20> ) <EOL> def test_validate_list_values ( self ) : <EOL> doc1 = { '<STR_LIT:f>' : [ '<STR_LIT:a>' , '<STR_LIT:b>' , [ '<STR_LIT:c>' , '<STR_LIT:d>' ] ] } <EOL> doc2 = { '<STR_LIT:f>' : [ '<STR_LIT:a>' , '<STR_LIT:b>' , <NUM_LIT:3> , set ( [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ] } <EOL> with self . assertRaisesRegexp ( ValueError , str ( list ) ) : <EOL> gae_search_services . add_documents_to_index ( [ doc1 ] , '<STR_LIT>' ) <EOL> with self . assertRaisesRegexp ( ValueError , str ( set ) ) : <EOL> gae_search_services . add_documents_to_index ( [ doc2 ] , '<STR_LIT>' ) <EOL> def test_index_must_be_string ( self ) : <EOL> index = search . Index ( '<STR_LIT:test>' ) <EOL> with self . assertRaisesRegexp ( ValueError , str ( type ( index ) ) ) : <EOL> gae_search_services . add_documents_to_index ( <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:key>' : '<STR_LIT:value>' } , index ) <EOL> def _get_put_error ( self , num_res , transient = None ) : <EOL> """<STR_LIT>""" <EOL> non_trans_code = search . OperationResult . INVALID_REQUEST <EOL> trans_code = search . OperationResult . TRANSIENT_ERROR <EOL> results = [ <EOL> search . PutResult ( code = non_trans_code ) for _ in range ( num_res ) ] <EOL> if transient is not None : <EOL> results [ transient ] = search . PutResult ( code = trans_code ) <EOL> return search . PutError ( '<STR_LIT>' , results ) <EOL> def test_use_default_num_retries ( self ) : <EOL> doc = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> exception = self . _get_put_error ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> failing_put = test_utils . FailingFunction ( <EOL> search . Index . put , <EOL> exception , <EOL> gae_search_services . DEFAULT_NUM_RETRIES , <EOL> ) <EOL> add_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . add_documents_to_index ) <EOL> put_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_put ) <EOL> add_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> add_docs_counter ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with put_ctx , add_docs_ctx , assert_raises_ctx as context_mgr : <EOL> gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT>' ) <EOL> self . assertEqual ( context_mgr . exception . original_exception , exception ) <EOL> self . assertEqual ( <EOL> add_docs_counter . times_called , <EOL> gae_search_services . DEFAULT_NUM_RETRIES ) <EOL> def test_use_custom_number_of_retries ( self ) : <EOL> doc = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> exception = self . _get_put_error ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> failing_put = test_utils . FailingFunction ( <EOL> search . Index . put , <EOL> exception , <EOL> <NUM_LIT> ) <EOL> add_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . add_documents_to_index ) <EOL> put_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_put ) <EOL> add_docs_ctx = self . swap ( <EOL> gae_search_services , '<STR_LIT>' , add_docs_counter ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with put_ctx , add_docs_ctx , assert_raises_ctx : <EOL> gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT>' , <NUM_LIT> ) <EOL> self . assertEqual ( add_docs_counter . times_called , <NUM_LIT> ) <EOL> def test_arguments_are_preserved_in_retries ( self ) : <EOL> doc = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> exception = self . _get_put_error ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> failing_put = test_utils . FailingFunction ( <EOL> search . Index . put , <EOL> exception , <EOL> <NUM_LIT:3> <EOL> ) <EOL> add_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . add_documents_to_index ) <EOL> put_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_put ) <EOL> add_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> add_docs_counter ) <EOL> with put_ctx , add_docs_ctx : <EOL> gae_search_services . add_documents_to_index ( [ doc ] , '<STR_LIT>' , <NUM_LIT:4> ) <EOL> self . assertEqual ( add_docs_counter . times_called , <NUM_LIT:4> ) <EOL> result = search . Index ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( result . field ( '<STR_LIT>' ) . value , '<STR_LIT>' ) <EOL> def test_put_error_with_transient_result ( self ) : <EOL> docs = [ { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ] <EOL> error = self . _get_put_error ( <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> failing_put = test_utils . FailingFunction ( <EOL> search . Index . put , <EOL> error , <EOL> <NUM_LIT:4> ) <EOL> add_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . add_documents_to_index ) <EOL> put_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_put ) <EOL> add_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> add_docs_counter ) <EOL> with put_ctx , add_docs_ctx : <EOL> gae_search_services . add_documents_to_index ( docs , '<STR_LIT>' , <NUM_LIT:5> ) <EOL> self . assertEqual ( add_docs_counter . times_called , <NUM_LIT:5> ) <EOL> for i in xrange ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> result = search . Index ( '<STR_LIT>' ) . get ( '<STR_LIT>' + str ( i ) ) <EOL> self . assertEqual ( result . field ( '<STR_LIT>' ) . value , '<STR_LIT>' + str ( i ) ) <EOL> def test_put_error_without_transient_result ( self ) : <EOL> docs = [ { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ] <EOL> error = self . _get_put_error ( <NUM_LIT:3> ) <EOL> failing_put = test_utils . FailingFunction ( search . Index . put , error , <NUM_LIT:1> ) <EOL> add_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . add_documents_to_index ) <EOL> add_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> add_docs_counter ) <EOL> put_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_put ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with add_docs_ctx , put_ctx , assert_raises_ctx as e : <EOL> gae_search_services . add_documents_to_index ( docs , '<STR_LIT>' ) <EOL> self . assertEqual ( add_docs_counter . times_called , <NUM_LIT:1> ) <EOL> self . assertEqual ( e . exception . original_exception , error ) <EOL> class SearchRemoveFromIndexTests ( test_utils . GenericTestBase ) : <EOL> """<STR_LIT>""" <EOL> def test_delete_single_document ( self ) : <EOL> doc = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT:v>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc ] ) <EOL> gae_search_services . delete_documents_from_index ( [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIsNone ( index . get ( '<STR_LIT>' ) ) <EOL> def test_delete_multiple_documents ( self ) : <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> field = search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' % i ) <EOL> doc = search . Document ( doc_id = '<STR_LIT>' % i , fields = [ field ] ) <EOL> index . put ( [ doc ] ) <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT>' + str ( i ) for i in xrange ( <NUM_LIT:10> ) ] , '<STR_LIT>' ) <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> self . assertIsNone ( index . get ( '<STR_LIT>' % i ) ) <EOL> def test_doc_ids_must_be_strings ( self ) : <EOL> with self . assertRaisesRegexp ( ValueError , str ( dict ) ) : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT>' , { '<STR_LIT:id>' : '<STR_LIT>' } ] , <EOL> '<STR_LIT:index>' ) <EOL> def test_index_must_be_string ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT>' ] , search . Index ( '<STR_LIT>' ) ) <EOL> def _get_delete_error ( self , num_res , transient = None ) : <EOL> """<STR_LIT>""" <EOL> non_trans_code = search . OperationResult . INVALID_REQUEST <EOL> trans_code = search . OperationResult . TRANSIENT_ERROR <EOL> results = [ <EOL> search . DeleteResult ( code = non_trans_code ) for _ in range ( num_res ) ] <EOL> if transient is not None : <EOL> results [ transient ] = search . PutResult ( code = trans_code ) <EOL> return search . DeleteError ( '<STR_LIT>' , results = results ) <EOL> def test_use_default_num_retries ( self ) : <EOL> exception = self . _get_delete_error ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> failing_delete = test_utils . FailingFunction ( <EOL> search . Index . delete , <EOL> exception , <EOL> gae_search_services . DEFAULT_NUM_RETRIES <EOL> ) <EOL> delete_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . delete_documents_from_index ) <EOL> delete_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_delete ) <EOL> delete_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> delete_docs_counter ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with delete_ctx , delete_docs_ctx , assert_raises_ctx as context_mgr : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( context_mgr . exception . original_exception , exception ) <EOL> self . assertEqual ( <EOL> delete_docs_counter . times_called , <EOL> gae_search_services . DEFAULT_NUM_RETRIES ) <EOL> def test_use_custom_number_of_retries ( self ) : <EOL> exception = self . _get_delete_error ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> failing_delete = test_utils . FailingFunction ( <EOL> search . Index . delete , exception , <NUM_LIT> ) <EOL> delete_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . delete_documents_from_index ) <EOL> delete_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_delete ) <EOL> delete_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> delete_docs_counter ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with delete_ctx , delete_docs_ctx , assert_raises_ctx : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT:id>' ] , '<STR_LIT:index>' , <NUM_LIT> ) <EOL> self . assertEqual ( delete_docs_counter . times_called , <NUM_LIT> ) <EOL> def test_arguments_are_preserved_in_retries ( self ) : <EOL> index = search . Index ( '<STR_LIT:index>' ) <EOL> index . put ( [ search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT>' , value = '<STR_LIT>' ) <EOL> ] ) ] ) <EOL> exception = self . _get_delete_error ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> failing_delete = test_utils . FailingFunction ( <EOL> search . Index . delete , exception , <NUM_LIT:3> ) <EOL> delete_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . delete_documents_from_index ) <EOL> index_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_delete ) <EOL> delete_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> delete_docs_counter ) <EOL> with index_ctx , delete_docs_ctx : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT>' ] , '<STR_LIT:index>' , <NUM_LIT:4> ) <EOL> self . assertEqual ( delete_docs_counter . times_called , <NUM_LIT:4> ) <EOL> result = search . Index ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> self . assertIsNone ( result ) <EOL> def test_delete_error_with_transient_result ( self ) : <EOL> error = self . _get_delete_error ( <NUM_LIT:3> , transient = <NUM_LIT:1> ) <EOL> failing_delete = test_utils . FailingFunction ( <EOL> search . Index . delete , <EOL> error , <EOL> <NUM_LIT:4> ) <EOL> delete_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . delete_documents_from_index ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> index . put ( search . Document ( doc_id = '<STR_LIT:d>' + str ( i ) , fields = [ <EOL> search . TextField ( name = '<STR_LIT>' , value = '<STR_LIT:value>' ) <EOL> ] ) ) <EOL> delete_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_delete ) <EOL> delete_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> delete_docs_counter ) <EOL> with delete_ctx , delete_docs_ctx : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> retries = <NUM_LIT:5> ) <EOL> self . assertEqual ( delete_docs_counter . times_called , <NUM_LIT:5> ) <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> result = search . Index ( '<STR_LIT>' ) . get ( '<STR_LIT>' + str ( i ) ) <EOL> self . assertIsNone ( result ) <EOL> def test_put_error_without_transient_result ( self ) : <EOL> error = self . _get_delete_error ( <NUM_LIT:3> ) <EOL> delete_spy = test_utils . FailingFunction ( search . Index . delete , error , <NUM_LIT:1> ) <EOL> delete_docs_counter = test_utils . CallCounter ( <EOL> gae_search_services . delete_documents_from_index ) <EOL> delete_docs_ctx = self . swap ( <EOL> gae_search_services , <EOL> '<STR_LIT>' , <EOL> delete_docs_counter ) <EOL> delete_ctx = self . swap ( search . Index , '<STR_LIT>' , delete_spy ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with delete_docs_ctx , delete_ctx , assert_raises_ctx as e : <EOL> gae_search_services . delete_documents_from_index ( <EOL> [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( delete_docs_counter . times_called , <NUM_LIT:1> ) <EOL> self . assertEqual ( e . exception . original_exception , error ) <EOL> class SearchQueryTests ( test_utils . GenericTestBase ) : <EOL> """<STR_LIT>""" <EOL> def test_search_all_documents ( self ) : <EOL> doc1 = search . Document ( doc_id = '<STR_LIT>' , language = '<STR_LIT>' , rank = <NUM_LIT:1> , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc2 = search . Document ( doc_id = '<STR_LIT>' , language = '<STR_LIT>' , rank = <NUM_LIT:2> , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc3 = search . Document ( doc_id = '<STR_LIT>' , language = '<STR_LIT>' , rank = <NUM_LIT:3> , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc1 , doc2 , doc3 ] ) <EOL> result = gae_search_services . search ( '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . assertIn ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' <EOL> } , result ) <EOL> self . assertIn ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' <EOL> } , result ) <EOL> self . assertIn ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : '<STR_LIT>' <EOL> } , result ) <EOL> def test_respect_search_query ( self ) : <EOL> doc1 = search . Document ( doc_id = '<STR_LIT>' , rank = <NUM_LIT:1> , language = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc2 = search . Document ( doc_id = '<STR_LIT>' , rank = <NUM_LIT:1> , language = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc3 = search . Document ( doc_id = '<STR_LIT>' , rank = <NUM_LIT:1> , language = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc1 , doc2 , doc3 ] ) <EOL> result = gae_search_services . search ( '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . assertNotIn ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> <EOL> } , result ) <EOL> self . assertIn ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> <EOL> } , result ) <EOL> self . assertIn ( { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> <EOL> } , result ) <EOL> def test_respect_limit ( self ) : <EOL> doc1 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc2 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc3 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc1 , doc2 , doc3 ] ) <EOL> result = gae_search_services . search ( '<STR_LIT>' , '<STR_LIT>' , limit = <NUM_LIT:2> ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:2> ) <EOL> def test_use_cursor ( self ) : <EOL> doc1 = search . Document ( doc_id = '<STR_LIT>' , language = '<STR_LIT>' , rank = <NUM_LIT:1> , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc2 = search . Document ( doc_id = '<STR_LIT>' , language = '<STR_LIT>' , rank = <NUM_LIT:1> , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc3 = search . Document ( doc_id = '<STR_LIT>' , language = '<STR_LIT>' , rank = <NUM_LIT:1> , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc1 , doc2 , doc3 ] ) <EOL> result1 , cursor = gae_search_services . search ( <EOL> '<STR_LIT>' , '<STR_LIT>' , limit = <NUM_LIT:2> ) <EOL> result2 , cursor = gae_search_services . search ( <EOL> '<STR_LIT>' , '<STR_LIT>' , cursor = cursor ) <EOL> self . assertEqual ( len ( result1 ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( result2 ) , <NUM_LIT:1> ) <EOL> dict1 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> } <EOL> self . assertIn ( dict1 , result1 + result2 ) <EOL> dict2 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> } <EOL> self . assertIn ( dict2 , result1 + result2 ) <EOL> dict3 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> } <EOL> self . assertIn ( dict3 , result1 + result2 ) <EOL> def test_ids_only ( self ) : <EOL> doc1 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc2 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc3 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc1 , doc2 , doc3 ] ) <EOL> result = gae_search_services . search ( <EOL> '<STR_LIT>' , '<STR_LIT>' , ids_only = True ) [ <NUM_LIT:0> ] <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> self . assertIn ( '<STR_LIT>' , result ) <EOL> def test_cursor_is_none_if_no_more_results ( self ) : <EOL> doc1 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc2 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> doc3 = search . Document ( doc_id = '<STR_LIT>' , fields = [ <EOL> search . TextField ( name = '<STR_LIT:k>' , value = '<STR_LIT>' ) ] ) <EOL> index = search . Index ( '<STR_LIT>' ) <EOL> index . put ( [ doc1 , doc2 , doc3 ] ) <EOL> cursor = gae_search_services . search ( '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> self . assertIsNone ( cursor ) <EOL> def test_default_rank_is_descending_date ( self ) : <EOL> dict1 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' } <EOL> dict2 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' } <EOL> dict3 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' } <EOL> gae_search_services . add_documents_to_index ( [ dict1 ] , '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> gae_search_services . add_documents_to_index ( [ dict2 ] , '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> gae_search_services . add_documents_to_index ( [ dict3 ] , '<STR_LIT>' ) <EOL> result = gae_search_services . search ( <EOL> '<STR_LIT>' , index = '<STR_LIT>' , ids_only = True ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( result , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_search_with_custom_rank_and_language ( self ) : <EOL> doc1 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : '<STR_LIT>' } <EOL> doc2 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> doc3 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' } <EOL> gae_search_services . add_documents_to_index ( [ doc1 , doc2 , doc3 ] , '<STR_LIT:index>' ) <EOL> result = gae_search_services . search ( '<STR_LIT>' , index = '<STR_LIT:index>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( result , [ doc1 , doc3 , doc2 ] ) <EOL> def test_search_using_single_sort_expression ( self ) : <EOL> doc1 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' } <EOL> doc2 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' } <EOL> doc3 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT:k>' : '<STR_LIT>' } <EOL> gae_search_services . add_documents_to_index ( [ doc1 , doc2 , doc3 ] , '<STR_LIT:index>' ) <EOL> result = gae_search_services . search ( '<STR_LIT>' , '<STR_LIT:index>' , sort = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result [ <NUM_LIT:1> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result [ <NUM_LIT:2> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> result = gae_search_services . search ( '<STR_LIT>' , '<STR_LIT:index>' , sort = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result [ <NUM_LIT:1> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result [ <NUM_LIT:2> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> def test_search_using_multiple_sort_expressions ( self ) : <EOL> doc1 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' } <EOL> doc2 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> doc3 = { '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } <EOL> gae_search_services . add_documents_to_index ( [ doc1 , doc2 , doc3 ] , '<STR_LIT:index>' ) <EOL> result = gae_search_services . search ( <EOL> '<STR_LIT>' , '<STR_LIT:index>' , sort = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result [ <NUM_LIT:1> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result [ <NUM_LIT:2> ] . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> def test_use_default_num_retries ( self ) : <EOL> exception = search . TransientError ( '<STR_LIT>' ) <EOL> failing_index_search = test_utils . FailingFunction ( <EOL> search . Index . search , <EOL> exception , <EOL> gae_search_services . DEFAULT_NUM_RETRIES ) <EOL> search_counter = test_utils . CallCounter ( gae_search_services . search ) <EOL> search_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_index_search ) <EOL> search_counter_ctx = self . swap ( <EOL> gae_search_services , '<STR_LIT>' , search_counter ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with search_ctx , search_counter_ctx , assert_raises_ctx as context_mgr : <EOL> gae_search_services . search ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( context_mgr . exception . original_exception , exception ) <EOL> self . assertEqual ( <EOL> search_counter . times_called , <EOL> gae_search_services . DEFAULT_NUM_RETRIES ) <EOL> def test_use_custom_number_of_retries ( self ) : <EOL> exception = search . TransientError ( '<STR_LIT>' ) <EOL> failing_index_search = test_utils . FailingFunction ( <EOL> search . Index . search , <EOL> exception , <EOL> <NUM_LIT:3> ) <EOL> search_counter = test_utils . CallCounter ( gae_search_services . search ) <EOL> index_ctx = self . swap ( search . Index , '<STR_LIT>' , failing_index_search ) <EOL> search_counter_ctx = self . swap ( <EOL> gae_search_services , '<STR_LIT>' , search_counter ) <EOL> assert_raises_ctx = self . assertRaises ( <EOL> gae_search_services . SearchFailureError ) <EOL> with index_ctx , search_counter_ctx , assert_raises_ctx : <EOL> gae_search_services . search ( '<STR_LIT>' , '<STR_LIT>' , retries = <NUM_LIT:3> ) <EOL> self . assertEqual ( search_counter . times_called , <NUM_LIT:3> ) <EOL> def test_arguments_are_preserved_in_retries ( self ) : <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> doc = search . Document ( doc_id = '<STR_LIT>' % i , fields = [ <EOL> search . TextField ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> search . NumberField ( '<STR_LIT:index>' , i ) <EOL> ] ) <EOL> search . Index ( '<STR_LIT>' ) . put ( doc ) <EOL> exception = search . TransientError ( '<STR_LIT>' ) <EOL> failing_index_search = test_utils . FailingFunction ( <EOL> search . Index . search , exception , <NUM_LIT:3> ) <EOL> search_counter = test_utils . CallCounter ( gae_search_services . search ) <EOL> gae_search_ctx = self . swap ( <EOL> search . Index , '<STR_LIT>' , failing_index_search ) <EOL> search_counter_ctx = self . swap ( <EOL> gae_search_services , '<STR_LIT>' , search_counter ) <EOL> with gae_search_ctx , search_counter_ctx : <EOL> result , cursor = gae_search_services . search ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> sort = '<STR_LIT>' , <EOL> limit = <NUM_LIT:2> , <EOL> ids_only = True , <EOL> retries = <NUM_LIT:4> ) <EOL> failing_index_search2 = test_utils . FailingFunction ( <EOL> search . Index . search , <EOL> exception , <EOL> <NUM_LIT:3> ) <EOL> search_counter2 = test_utils . CallCounter ( gae_search_services . search ) <EOL> gae_search_ctx2 = self . swap ( <EOL> search . Index , '<STR_LIT>' , failing_index_search2 ) <EOL> search_counter_ctx2 = self . swap ( <EOL> gae_search_services , '<STR_LIT>' , search_counter2 ) <EOL> with gae_search_ctx2 , search_counter_ctx2 : <EOL> result2 , cursor = gae_search_services . search ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> sort = '<STR_LIT>' , <EOL> limit = <NUM_LIT:2> , <EOL> cursor = cursor , <EOL> ids_only = True , <EOL> retries = <NUM_LIT:4> ) <EOL> self . assertEqual ( search_counter . times_called , <NUM_LIT:4> ) <EOL> self . assertEqual ( result , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( search_counter2 . times_called , <NUM_LIT:4> ) <EOL> self . assertEqual ( result2 , [ '<STR_LIT>' ] ) <EOL> class SearchGetFromIndexTests ( test_utils . GenericTestBase ) : <EOL> def test_get_document_from_index ( self ) : <EOL> document = search . Document ( doc_id = "<STR_LIT>" , fields = [ <EOL> search . TextField ( name = "<STR_LIT>" , value = "<STR_LIT:value>" ) <EOL> ] ) <EOL> search . Index ( '<STR_LIT>' ) . put ( document ) <EOL> result = gae_search_services . get_document_from_index ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( result . get ( '<STR_LIT:id>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( result . get ( '<STR_LIT>' ) , '<STR_LIT:value>' ) </s>
