<s> from uuid import uuid4 <EOL> from tornadoes import ESConnection <EOL> from tornado import escape <EOL> from tornado . testing import AsyncTestCase , gen_test <EOL> from tornado . ioloop import IOLoop <EOL> class ESConnectionTestBase ( AsyncTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( ESConnectionTestBase , self ) . setUp ( ) <EOL> self . es_connection = ESConnection ( "<STR_LIT:localhost>" , "<STR_LIT>" , self . io_loop ) <EOL> self . _set_version ( ) <EOL> def tearDown ( self ) : <EOL> if ( not IOLoop . initialized ( ) or self . io_loop is not IOLoop . instance ( ) ) : <EOL> self . io_loop . close ( all_fds = True ) <EOL> super ( AsyncTestCase , self ) . tearDown ( ) <EOL> def _set_version ( self ) : <EOL> self . es_connection . get_by_path ( "<STR_LIT:/>" , self . stop ) <EOL> response = self . wait ( ) <EOL> response = escape . json_decode ( response . body ) <EOL> version = response [ '<STR_LIT:version>' ] [ '<STR_LIT>' ] <EOL> self . version = [ int ( n ) for n in version . split ( '<STR_LIT:.>' ) if n . isdigit ( ) ] <EOL> def _set_count_query ( self , query ) : <EOL> if self . version [ <NUM_LIT:0> ] < <NUM_LIT:1> : <EOL> return query [ '<STR_LIT>' ] <EOL> return query <EOL> class TestESConnection ( ESConnectionTestBase ) : <EOL> def test_simple_search ( self ) : <EOL> self . es_connection . get_by_path ( "<STR_LIT>" , self . stop ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , u'<STR_LIT>' ) <EOL> def test_search_for_specific_type_with_query ( self ) : <EOL> self . es_connection . search ( callback = self . stop , <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } , <EOL> type = "<STR_LIT>" , index = "<STR_LIT>" ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , u'<STR_LIT>' ) <EOL> def test_search_specific_index ( self ) : <EOL> self . es_connection . search ( callback = self . stop , index = "<STR_LIT>" ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> def test_search_apecific_type ( self ) : <EOL> self . es_connection . search ( self . stop , type = '<STR_LIT>' ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:2> ) <EOL> def test_should_access_specific_document ( self ) : <EOL> self . es_connection . get ( index = "<STR_LIT>" , type = "<STR_LIT>" , uid = "<STR_LIT>" , callback = self . stop ) <EOL> response = self . wait ( ) [ "<STR_LIT>" ] <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> def test_should_accumulate_searches_before_search ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( "<STR_LIT>" , source = source ) <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT:body>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( "<STR_LIT>" , source = source ) <EOL> self . assertListEqual ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , self . es_connection . bulk . bulk_list ) <EOL> def test_should_generate_empty_header_with_no_index_specified ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( index = None , source = source ) <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT:body>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( index = None , source = source ) <EOL> self . assertListEqual ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , self . es_connection . bulk . bulk_list ) <EOL> def test_should_make_two_searches ( self ) : <EOL> self . _make_multisearch ( ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> self . assertFalse ( "<STR_LIT>" in response [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> def test_should_clean_search_list_after_search ( self ) : <EOL> self . _make_multisearch ( ) <EOL> self . wait ( ) <EOL> self . assertListEqual ( [ ] , self . es_connection . bulk . bulk_list ) <EOL> def _make_multisearch ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( index = "<STR_LIT>" , source = source ) <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( index = "<STR_LIT>" , source = source ) <EOL> self . es_connection . apply_search ( callback = self . stop ) <EOL> def _verify_status_code_and_return_response ( self ) : <EOL> response = self . wait ( ) <EOL> return self . _verify_response_and_returns_dict ( response ) <EOL> def _verify_response_and_returns_dict ( self , response ) : <EOL> self . assertTrue ( response . code in [ <NUM_LIT:200> , <NUM_LIT> ] , "<STR_LIT>" % response . code ) <EOL> response = escape . json_decode ( response . body ) <EOL> return response <EOL> def test_can_put_and_delete_document ( self ) : <EOL> try : <EOL> doc_id = str ( uuid4 ( ) ) <EOL> self . es_connection . put ( "<STR_LIT:test>" , "<STR_LIT>" , doc_id , { <EOL> "<STR_LIT:test>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , parameters = { '<STR_LIT>' : True } , callback = self . stop ) <EOL> response = self . wait ( ) <EOL> response_dict = self . _verify_response_and_returns_dict ( response ) <EOL> self . assertEqual ( response_dict [ '<STR_LIT>' ] , '<STR_LIT:test>' ) <EOL> self . assertEqual ( response_dict [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response_dict [ '<STR_LIT>' ] , doc_id ) <EOL> self . assertIn ( '<STR_LIT>' , response . request . url ) <EOL> finally : <EOL> self . es_connection . delete ( "<STR_LIT:test>" , "<STR_LIT>" , doc_id , <EOL> parameters = { '<STR_LIT>' : True } , callback = self . stop ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT:test>' ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , doc_id ) <EOL> def test_count_specific_index ( self ) : <EOL> self . es_connection . count ( callback = self . stop , index = "<STR_LIT>" ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT:count>" ] , <NUM_LIT> ) <EOL> def test_count_specific_type ( self ) : <EOL> self . es_connection . count ( callback = self . stop , type = '<STR_LIT>' ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT:count>" ] , <NUM_LIT:2> ) <EOL> def test_count_specific_query ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> source = self . _set_count_query ( source ) <EOL> self . es_connection . count ( callback = self . stop , source = source ) <EOL> response = self . _verify_status_code_and_return_response ( ) <EOL> self . assertEqual ( response [ "<STR_LIT:count>" ] , <NUM_LIT:1> ) <EOL> def test_count_specific_query_with_parameters ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> source = self . _set_count_query ( source ) <EOL> parameters = { '<STR_LIT>' : True } <EOL> self . es_connection . count ( callback = self . stop , source = source , parameters = parameters ) <EOL> response = self . wait ( ) <EOL> response_dict = self . _verify_response_and_returns_dict ( response ) <EOL> self . assertEqual ( response_dict [ "<STR_LIT:count>" ] , <NUM_LIT:1> ) <EOL> self . assertTrue ( response . request . url . endswith ( '<STR_LIT>' ) ) <EOL> def test_count_specific_query_with_many_parameters ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> source = self . _set_count_query ( source ) <EOL> parameters = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:test>' : True } <EOL> self . es_connection . count ( callback = self . stop , source = source , parameters = parameters ) <EOL> response = self . wait ( ) <EOL> response_dict = self . _verify_response_and_returns_dict ( response ) <EOL> self . assertEqual ( response_dict [ "<STR_LIT:count>" ] , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT>' in response . request . url ) <EOL> self . assertTrue ( '<STR_LIT>' in response . request . url ) <EOL> class TestESConnectionWithTornadoGen ( ESConnectionTestBase ) : <EOL> @ gen_test <EOL> def test_simple_search ( self ) : <EOL> response = yield self . es_connection . get_by_path ( "<STR_LIT>" , self . stop ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , u'<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_search_for_specific_type_with_query ( self ) : <EOL> response = yield self . es_connection . search ( <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } , <EOL> type = "<STR_LIT>" , index = "<STR_LIT>" <EOL> ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , u'<STR_LIT>' ) <EOL> @ gen_test <EOL> def test_search_specific_index ( self ) : <EOL> response = yield self . es_connection . search ( index = "<STR_LIT>" ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> @ gen_test <EOL> def test_search_apecific_type ( self ) : <EOL> response = yield self . es_connection . search ( type = '<STR_LIT>' ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <NUM_LIT:2> ) <EOL> @ gen_test <EOL> def test_should_access_specific_document_using_tornado_gen ( self ) : <EOL> response = yield self . es_connection . get ( index = "<STR_LIT>" , type = "<STR_LIT>" , uid = "<STR_LIT>" ) <EOL> response = response [ "<STR_LIT>" ] <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> @ gen_test <EOL> def test_should_make_two_searches ( self ) : <EOL> self . _make_multisearch ( ) <EOL> response = yield self . es_connection . apply_search ( ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> self . assertFalse ( "<STR_LIT>" in response [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) <EOL> @ gen_test <EOL> def test_should_clean_search_list_after_search ( self ) : <EOL> self . _make_multisearch ( ) <EOL> response = yield self . es_connection . apply_search ( ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertListEqual ( [ ] , self . es_connection . bulk . bulk_list ) <EOL> @ gen_test <EOL> def test_can_put_and_delete_document ( self ) : <EOL> try : <EOL> doc_id = str ( uuid4 ( ) ) <EOL> response = yield self . es_connection . put ( "<STR_LIT:test>" , "<STR_LIT>" , doc_id , { <EOL> "<STR_LIT:test>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , parameters = { '<STR_LIT>' : True } ) <EOL> response_dict = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response_dict [ '<STR_LIT>' ] , '<STR_LIT:test>' ) <EOL> self . assertEqual ( response_dict [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response_dict [ '<STR_LIT>' ] , doc_id ) <EOL> self . assertIn ( '<STR_LIT>' , response . request . url ) <EOL> finally : <EOL> response = yield self . es_connection . delete ( "<STR_LIT:test>" , "<STR_LIT>" , doc_id , <EOL> parameters = { '<STR_LIT>' : True } ) <EOL> response = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT:test>' ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , doc_id ) <EOL> @ gen_test <EOL> def test_count_specific_index ( self ) : <EOL> response = yield self . es_connection . count ( index = "<STR_LIT>" ) <EOL> self . assertCount ( response , <NUM_LIT> ) <EOL> @ gen_test <EOL> def test_count_specific_type ( self ) : <EOL> response = yield self . es_connection . count ( type = '<STR_LIT>' ) <EOL> self . assertCount ( response , <NUM_LIT:2> ) <EOL> @ gen_test <EOL> def test_count_specific_query ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> source = self . _set_count_query ( source ) <EOL> response = yield self . es_connection . count ( source = source ) <EOL> self . assertCount ( response , <NUM_LIT:1> ) <EOL> @ gen_test <EOL> def test_count_specific_query_with_parameters ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> source = self . _set_count_query ( source ) <EOL> parameters = { '<STR_LIT>' : True } <EOL> response = yield self . es_connection . count ( callback = self . stop , source = source , parameters = parameters ) <EOL> self . assertCount ( response , <NUM_LIT:1> ) <EOL> self . assertTrue ( response . request . url . endswith ( '<STR_LIT>' ) ) <EOL> @ gen_test <EOL> def test_count_specific_query_with_many_parameters ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> source = self . _set_count_query ( source ) <EOL> parameters = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:test>' : True } <EOL> response = yield self . es_connection . count ( callback = self . stop , source = source , parameters = parameters ) <EOL> self . assertCount ( response , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT>' in response . request . url ) <EOL> self . assertTrue ( '<STR_LIT>' in response . request . url ) <EOL> def assertCount ( self , response , count ) : <EOL> response_dict = self . _verify_status_code_and_return_response ( response ) <EOL> self . assertEqual ( response_dict [ "<STR_LIT:count>" ] , count ) <EOL> def _make_multisearch ( self ) : <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( index = "<STR_LIT>" , source = source ) <EOL> source = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } } } <EOL> self . es_connection . multi_search ( index = "<STR_LIT>" , source = source ) <EOL> def _verify_status_code_and_return_response ( self , response ) : <EOL> self . assertTrue ( response . code in [ <NUM_LIT:200> , <NUM_LIT> ] , "<STR_LIT>" % response . code ) <EOL> response = escape . json_decode ( response . body ) <EOL> return response </s>
