Skip to content

Commit

Permalink
spanner: Expand test coverage for SpannerClient (#3725)
Browse files Browse the repository at this point in the history
Expands coverage from 53% to 78%.
  • Loading branch information
nithinsujir authored and pongad committed Sep 27, 2018
1 parent 9187488 commit 82fda75
Showing 1 changed file with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.cloud.spanner.v1;

import static com.google.cloud.spanner.v1.SpannerClient.ListSessionsPagedResponse;
import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GaxGrpcProperties;
Expand All @@ -27,6 +28,8 @@
import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StatusCode;
import com.google.cloud.spanner.v1.stub.SpannerStub;
import com.google.cloud.spanner.v1.stub.SpannerStubSettings;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
Expand Down Expand Up @@ -101,6 +104,16 @@ public void tearDown() throws Exception {
client.close();
}

@Test
public void createClientTest() throws IOException {
SpannerStub stub =
SpannerStubSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create()).build().createStub();
SpannerClient client1 = SpannerClient.create(stub);
assertThat(client1.getStub()).isEqualTo(stub);
assertThat(client1.getSettings()).isNull();
}

@Test
@SuppressWarnings("all")
public void createSessionTest() {
Expand All @@ -124,6 +137,27 @@ public void createSessionTest() {
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

@Test
public void createSessionWithStringName() {
String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]";
Session expectedResponse = Session.newBuilder().setName(name).build();
mockSpanner.addResponse(expectedResponse);

DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");

Session actualResponse = client.createSession(name);
assertThat(actualResponse).isEqualTo(expectedResponse);

List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests();
assertThat(actualRequests).hasSize(1);

CreateSessionRequest actualRequest = (CreateSessionRequest) actualRequests.get(0);
assertThat(DatabaseName.parse(actualRequest.getDatabase())).isEqualTo(database);
assertThat(channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue();
}

@Test
@SuppressWarnings("all")
public void createSessionExceptionTest() throws Exception {
Expand Down Expand Up @@ -163,6 +197,24 @@ public void getSessionTest() {
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

@Test
public void getSessionByStringNameTest() {
String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]";
SessionName sessionName = SessionName.parse(name);
Session expectedResponse = Session.newBuilder().setName(name).build();
mockSpanner.addResponse(expectedResponse);

Session actualResponse = client.getSession(name);
assertThat(actualResponse).isEqualTo(expectedResponse);

List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests();
assertThat(actualRequests).hasSize(1);

GetSessionRequest actualRequest = (GetSessionRequest) actualRequests.get(0);
assertThat(SessionName.parse(actualRequest.getName())).isEqualTo(sessionName);

}

@Test
@SuppressWarnings("all")
public void getSessionExceptionTest() throws Exception {
Expand Down Expand Up @@ -211,6 +263,27 @@ public void listSessionsTest() {
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

@Test
public void listSessionsCallableTest() {
List<Session> sessions = Arrays.asList(Session.newBuilder().build());
ListSessionsResponse expectedResponse =
ListSessionsResponse.newBuilder()
.addAllSessions(sessions)
.build();
mockSpanner.addResponse(expectedResponse);

String formattedDatabase = DatabaseName.format("[PROJECT]", "[INSTANCE]", "[DATABASE]");

ListSessionsRequest request = ListSessionsRequest.newBuilder()
.setDatabase(formattedDatabase)
.build();

ListSessionsResponse listSessionsResponse = client.listSessionsCallable().call(request);
List<Session> resources = Lists.newArrayList(listSessionsResponse.getSessionsList());
assertThat(resources).hasSize(1);
assertThat(expectedResponse.getSessionsList().get(0)).isEqualTo(resources.get(0));
}

@Test
@SuppressWarnings("all")
public void listSessionsExceptionTest() throws Exception {
Expand Down Expand Up @@ -248,6 +321,25 @@ public void deleteSessionTest() {
GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}

@Test
public void deleteSessionWithStringName() {
Empty expectedResponse = Empty.newBuilder().build();
mockSpanner.addResponse(expectedResponse);

String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]";
SessionName sessionName = SessionName.parse(name);
client.deleteSession(name);

List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests();
assertThat(actualRequests).hasSize(1);

DeleteSessionRequest actualRequest = (DeleteSessionRequest) actualRequests.get(0);
assertThat(SessionName.parse(actualRequest.getName())).isEqualTo(sessionName);
assertThat(channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue();
}

@Test
@SuppressWarnings("all")
public void deleteSessionExceptionTest() throws Exception {
Expand Down Expand Up @@ -426,6 +518,31 @@ public void beginTransactionExceptionTest() throws Exception {
}
}

@Test
public void commitWithStringNameTest() {
CommitResponse expectedResponse = CommitResponse.newBuilder().build();
mockSpanner.addResponse(expectedResponse);

ByteString transactionId = ByteString.copyFromUtf8("28");
List<Mutation> mutations = new ArrayList<>();

String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]";
SessionName sessionName = SessionName.parse(name);
CommitResponse actualResponse = client.commit(name, transactionId, mutations);
assertThat(actualResponse).isEqualTo(expectedResponse);

List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests();
assertThat(actualRequests).hasSize(1);

CommitRequest actualRequest = (CommitRequest) actualRequests.get(0);
assertThat(SessionName.parse(actualRequest.getSession())).isEqualTo(sessionName);
assertThat(actualRequest.getTransactionId()).isEqualTo(transactionId);
assertThat(actualRequest.getMutationsList()).isEqualTo(mutations);
assertThat(channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue();
}

@Test
@SuppressWarnings("all")
public void commitTest() {
Expand Down Expand Up @@ -470,6 +587,29 @@ public void commitExceptionTest() throws Exception {
}
}

@Test
public void commitWithStringNameViaTransactionTest() {
CommitResponse expectedResponse = CommitResponse.newBuilder().build();
mockSpanner.addResponse(expectedResponse);

String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]";
TransactionOptions singleUseTransaction = TransactionOptions.newBuilder().build();
List<Mutation> mutations = new ArrayList<>();

CommitResponse actualResponse = client.commit(name, singleUseTransaction, mutations);
assertThat(actualResponse).isEqualTo(expectedResponse);

List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests();
assertThat(actualRequests.size()).isEqualTo(1);

CommitRequest actualRequest = (CommitRequest) actualRequests.get(0);
assertThat(actualRequest.getSingleUseTransaction()).isEqualTo(singleUseTransaction);
assertThat(actualRequest.getMutationsList()).isEqualTo(mutations);
assertThat(channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue();
}

@Test
@SuppressWarnings("all")
public void commitTest2() {
Expand Down Expand Up @@ -514,6 +654,26 @@ public void commitExceptionTest2() throws Exception {
}
}

@Test
public void rollbackWithStringNameTest() {
Empty expectedResponse = Empty.newBuilder().build();
mockSpanner.addResponse(expectedResponse);

String name = "projects/[PROJECT]/instances/[INSTANCE]/databases/[DATABASE]/sessions/[SESSION]";
ByteString transactionId = ByteString.copyFromUtf8("28");

client.rollback(name, transactionId);

List<GeneratedMessageV3> actualRequests = mockSpanner.getRequests();
assertThat(actualRequests).hasSize(1);

RollbackRequest actualRequest = (RollbackRequest) actualRequests.get(0);
assertThat(actualRequest.getTransactionId()).isEqualTo(transactionId);
assertThat(channelProvider.isHeaderSent(
ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
GaxGrpcProperties.getDefaultApiClientHeaderPattern())).isTrue();
}

@Test
@SuppressWarnings("all")
public void rollbackTest() {
Expand Down

0 comments on commit 82fda75

Please sign in to comment.