def test_streaming_read(self): # Setup Expected Response chunked_value = True resume_token = b'103' expected_response = { 'chunked_value': chunked_value, 'resume_token': resume_token } expected_response = result_set_pb2.PartialResultSet( **expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) client = spanner_v1.SpannerClient(channel=channel) # Setup Request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') table = 'table110115790' columns = [] key_set = {} response = client.streaming_read(session, table, columns, key_set) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = spanner_pb2.ReadRequest(session=session, table=table, columns=columns, key_set=key_set) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_begin_transaction(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_v1.SpannerClient() # Mock request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') options_ = {} # Mock response id_ = b'27' expected_response = {'id': id_} expected_response = transaction_pb2.Transaction(**expected_response) grpc_stub.BeginTransaction.return_value = expected_response response = client.begin_transaction(session, options_) self.assertEqual(expected_response, response) grpc_stub.BeginTransaction.assert_called_once() args, kwargs = grpc_stub.BeginTransaction.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_pb2.BeginTransactionRequest( session=session, options=options_) self.assertEqual(expected_request, actual_request)
def test_read(self): # Setup Expected Response expected_response = {} expected_response = result_set_pb2.ResultSet(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') table = 'table110115790' columns = [] key_set = {} response = client.read(session, table, columns, key_set) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.ReadRequest(session=session, table=table, columns=columns, key_set=key_set) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_commit(self): # Setup Expected Response expected_response = {} expected_response = spanner_pb2.CommitResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") mutations = [] response = client.commit(session, mutations) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.CommitRequest(session=session, mutations=mutations) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_partition_read(self): # Setup Expected Response expected_response = {} expected_response = spanner_pb2.PartitionResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") table = "table110115790" key_set = {} response = client.partition_read(session, table, key_set) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.PartitionReadRequest(session=session, table=table, key_set=key_set) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_execute_streaming_sql(self): # Setup Expected Response chunked_value = True resume_token = b"103" expected_response = { "chunked_value": chunked_value, "resume_token": resume_token, } expected_response = result_set_pb2.PartialResultSet( **expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") sql = "sql114126" response = client.execute_streaming_sql(session, sql) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = spanner_pb2.ExecuteSqlRequest(session=session, sql=sql) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_begin_transaction(self): # Setup Expected Response id_ = b"27" expected_response = {"id": id_} expected_response = transaction_pb2.Transaction(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") options_ = {} response = client.begin_transaction(session, options_) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.BeginTransactionRequest( session=session, options=options_) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_execute_sql(self): # Setup Expected Response expected_response = {} expected_response = result_set_pb2.ResultSet(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") sql = "sql114126" response = client.execute_sql(session, sql) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.ExecuteSqlRequest(session=session, sql=sql) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_sessions(self): # Setup Expected Response next_page_token = "" sessions_element = {} sessions = [sessions_element] expected_response = { "next_page_token": next_page_token, "sessions": sessions } expected_response = spanner_pb2.ListSessionsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request database = client.database_path("[PROJECT]", "[INSTANCE]", "[DATABASE]") paged_list_response = client.list_sessions(database) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.sessions[0] == resources[0] assert len(channel.requests) == 1 expected_request = spanner_pb2.ListSessionsRequest(database=database) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_execute_batch_dml(self): # Setup Expected Response expected_response = {} expected_response = spanner_pb2.ExecuteBatchDmlResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") transaction = {} statements = [] seqno = 109325920 response = client.execute_batch_dml(session, transaction, statements, seqno) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.ExecuteBatchDmlRequest( session=session, transaction=transaction, statements=statements, seqno=seqno) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_execute_sql(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_v1.SpannerClient() # Mock request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') sql = 'sql114126' # Mock response expected_response = {} expected_response = result_set_pb2.ResultSet(**expected_response) grpc_stub.ExecuteSql.return_value = expected_response response = client.execute_sql(session, sql) self.assertEqual(expected_response, response) grpc_stub.ExecuteSql.assert_called_once() args, kwargs = grpc_stub.ExecuteSql.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_pb2.ExecuteSqlRequest(session=session, sql=sql) self.assertEqual(expected_request, actual_request)
def test_read(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_v1.SpannerClient() # Mock request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') table = 'table110115790' columns = [] key_set = {} # Mock response expected_response = {} expected_response = result_set_pb2.ResultSet(**expected_response) grpc_stub.Read.return_value = expected_response response = client.read(session, table, columns, key_set) self.assertEqual(expected_response, response) grpc_stub.Read.assert_called_once() args, kwargs = grpc_stub.Read.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_pb2.ReadRequest(session=session, table=table, columns=columns, key_set=key_set) self.assertEqual(expected_request, actual_request)
def test_commit(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_v1.SpannerClient() # Mock request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') mutations = [] # Mock response expected_response = {} expected_response = spanner_pb2.CommitResponse(**expected_response) grpc_stub.Commit.return_value = expected_response response = client.commit(session, mutations) self.assertEqual(expected_response, response) grpc_stub.Commit.assert_called_once() args, kwargs = grpc_stub.Commit.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_pb2.CommitRequest(session=session, mutations=mutations) self.assertEqual(expected_request, actual_request)
def test_read(self): # Setup Expected Response expected_response = {} expected_response = result_set_pb2.ResultSet(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = spanner_v1.SpannerClient(channel=channel) # Setup Request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') table = 'table110115790' columns = [] key_set = {} response = client.read(session, table, columns, key_set) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.ReadRequest(session=session, table=table, columns=columns, key_set=key_set) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_sessions(self): # Setup Expected Response next_page_token = '' sessions_element = {} sessions = [sessions_element] expected_response = { 'next_page_token': next_page_token, 'sessions': sessions } expected_response = spanner_pb2.ListSessionsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = spanner_v1.SpannerClient(channel=channel) # Setup Request database = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') paged_list_response = client.list_sessions(database) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.sessions[0] == resources[0] assert len(channel.requests) == 1 expected_request = spanner_pb2.ListSessionsRequest(database=database) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_execute_streaming_sql(self): # Setup Expected Response chunked_value = True resume_token = b'103' expected_response = { 'chunked_value': chunked_value, 'resume_token': resume_token } expected_response = result_set_pb2.PartialResultSet( **expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) client = spanner_v1.SpannerClient(channel=channel) # Setup Request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') sql = 'sql114126' response = client.execute_streaming_sql(session, sql) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = spanner_pb2.ExecuteSqlRequest(session=session, sql=sql) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_session(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_v1.SpannerClient() # Mock request name = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') # Mock response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = spanner_pb2.Session(**expected_response) grpc_stub.GetSession.return_value = expected_response response = client.get_session(name) self.assertEqual(expected_response, response) grpc_stub.GetSession.assert_called_once() args, kwargs = grpc_stub.GetSession.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_pb2.GetSessionRequest(name=name) self.assertEqual(expected_request, actual_request)
def test_batch_create_sessions(self): # Setup Expected Response expected_response = {} expected_response = spanner_pb2.BatchCreateSessionsResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request database = client.database_path("[PROJECT]", "[INSTANCE]", "[DATABASE]") session_count = 185691686 response = client.batch_create_sessions(database, session_count) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.BatchCreateSessionsRequest( database=database, session_count=session_count) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_partition_query(self): # Setup Expected Response expected_response = {} expected_response = spanner_pb2.PartitionResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup Request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') sql = 'sql114126' response = client.partition_query(session, sql) assert expected_response == response assert len(channel.requests) == 1 expected_request = spanner_pb2.PartitionQueryRequest(session=session, sql=sql) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_session(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = spanner_v1.SpannerClient() # Mock request database = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') # Mock response name = 'name3373707' expected_response = {'name': name} expected_response = spanner_pb2.Session(**expected_response) grpc_stub.CreateSession.return_value = expected_response response = client.create_session(database) self.assertEqual(expected_response, response) grpc_stub.CreateSession.assert_called_once() args, kwargs = grpc_stub.CreateSession.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = spanner_pb2.CreateSessionRequest(database=database) self.assertEqual(expected_request, actual_request)
def test_client_with_grpc_gcp_channel(self, grpc_gcp_secure_channel, api_config, merge, auth_default): spanner_target = 'spanner.googleapis.com:443' client = spanner_v1.SpannerClient() merge.assert_called_once_with(mock.ANY, mock.sentinel.api_config) options = [('grpc_gcp.api_config', mock.sentinel.api_config)] grpc_gcp_secure_channel.assert_called_once_with(spanner_target, mock.ANY, options=options)
def test_delete_session_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request name = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') with pytest.raises(CustomException): client.delete_session(name)
def test_list_sessions_exception(self): channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request database = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') paged_list_response = client.list_sessions(database) with pytest.raises(CustomException): list(paged_list_response)
def test_create_session_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request database = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]') with pytest.raises(CustomException): client.create_session(database)
def test_begin_transaction_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') options_ = {} with pytest.raises(CustomException): client.begin_transaction(session, options_)
def test_partition_query_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') sql = 'sql114126' with pytest.raises(CustomException): client.partition_query(session, sql)
def test_rollback_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') transaction_id = b'28' with pytest.raises(CustomException): client.rollback(session, transaction_id)
def test_commit_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') mutations = [] with pytest.raises(CustomException): client.commit(session, mutations)
def test_create_session_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = spanner_v1.SpannerClient() # Setup request database = client.database_path("[PROJECT]", "[INSTANCE]", "[DATABASE]") with pytest.raises(CustomException): client.create_session(database)
def test_partition_read_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = spanner_v1.SpannerClient(channel=channel) # Setup request session = client.session_path('[PROJECT]', '[INSTANCE]', '[DATABASE]', '[SESSION]') table = 'table110115790' key_set = {} with pytest.raises(CustomException): client.partition_read(session, table, key_set)