def test_retrieve_file_handles_of_same_dataset_A_ok_patched( self, getpatch): ''' In this test, only strategy 1 is used. serverconnector.send_query returns three handles on the first call. ''' # Define the replacement for the patched method: handles = ["123/456", 3, "123/234", 1, "987/567", 2] getpatch.return_value = self.fake_solr_response(handles) # Preparations task = self.make_testtask() # Test variables: args = self.get_args_dict() # received_handles code to be tested: received_handles = task.retrieve_file_handles_of_same_dataset(**args) # Check result: # Was the correct query sent? #expected_query = {'format': 'application/solr+json', 'facets': 'handle,tracking_id', 'limit': 0, 'distrib': False, 'dataset_id': 'abc.v2016|foo.de', 'type': 'File'} expected_query = QUERY1 getpatch.assert_called_once_with(expected_query) # Was the response treated correctly? expected_handles = ['hdl:123/987/567', 'hdl:123/234', 'hdl:123/456'] self.assertEqual( expected_handles, received_handles, 'Expected %s, but got %s' % (expected_handles, received_handles))
def test_retrieve_file_handles_of_same_dataset_A_nohandle_B_error_patched( self, getpatch): ''' In this test, both strategies are used. serverconnector.send_query returns [] on the first call, so the second call is issued, but this also returns []. ''' # Test variables: args = self.get_args_dict() # Define the replacement for the patched method: def different_mock_response_depending_on_query(query): if query == QUERY1: return self.fake_solr_response([]) elif query == QUERY2: raise esgfpid.exceptions.SolrError('Whatever 2...') else: raise ValueError( 'Something went wrong with the test. Wrong query: ' + str(query)) getpatch.side_effect = different_mock_response_depending_on_query # Preparations task = self.make_testtask() # Run code to be tested and check exception: with self.assertRaises(esgfpid.exceptions.SolrError) as raised: received_handles = task.retrieve_file_handles_of_same_dataset( **args) self.assertIn('Failure in both queries', raised.exception.message) self.assertIn('First query returned an empty list', raised.exception.message) self.assertIn('Whatever 2', raised.exception.message)
def test_retrieve_file_handles_of_same_dataset_AB_nohandles_patched( self, getpatch): ''' In this test, both strategies are used. serverconnector.send_query returns [] on the first call, so the second call is issued, but this also returns []. ''' # Define the replacement for the patched method: getpatch.return_value = self.fake_solr_response([]) # Preparations task = self.make_testtask() # Test variables: args = self.get_args_dict() # Run code to be tested: received_handles = task.retrieve_file_handles_of_same_dataset(**args) # Check result: # Was the correct query sent? expected_query_1 = QUERY1 expected_query_2 = QUERY2 getpatch.assert_any_call(expected_query_1) getpatch.assert_called_with(expected_query_2) # Was the response treated correctly? self.assertEqual( received_handles, [], 'Expected empty list, but got: ' + str(received_handles))
def test_retrieve_file_handles_of_same_dataset_A_error_B_ok_patched( self, getpatch): ''' In this test, both strategies are used. serverconnector.send_query returns [] on the first call, so the second call is issued, but this also returns []. ''' # Test variables: args = self.get_args_dict() handles = ["123/456", 3, "123/234", 1, "987/567", 2] # Define the replacement for the patched method: def different_mock_response_depending_on_query(query): if query == QUERY1: raise esgfpid.exceptions.SolrError('Whatever...') elif query == QUERY2: return self.fake_solr_response(handles) else: raise ValueError( 'Something went wrong with the test. Wrong query: ' + str(query)) getpatch.side_effect = different_mock_response_depending_on_query # Preparations task = self.make_testtask() # Run code to be tested: received_handles = task.retrieve_file_handles_of_same_dataset(**args) # Check result: # Was the correct query sent? expected_query_1 = QUERY1 expected_query_2 = QUERY2 getpatch.assert_any_call(expected_query_1) getpatch.assert_called_with(expected_query_2) # Was the response treated correctly? expected_handles = ['hdl:123/987/567', 'hdl:123/234', 'hdl:123/456'] self.assertEqual( expected_handles, received_handles, 'Expected %s, but got %s' % (expected_handles, received_handles))