use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class DatabaseClientImplTest method testReadWriteExecuteReadWithTag.
@Test
public void testReadWriteExecuteReadWithTag() {
DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
TransactionRunner runner = client.readWriteTransaction(Options.tag("app=spanner,env=test,action=txn"));
runner.run(transaction -> {
try (ResultSet resultSet = transaction.read(READ_TABLE_NAME, KeySet.singleKey(Key.of(1L)), READ_COLUMN_NAMES, Options.tag("app=spanner,env=test,action=read"))) {
while (resultSet.next()) {
}
}
return null;
});
List<ReadRequest> requests = mockSpanner.getRequestsOfType(ReadRequest.class);
assertThat(requests).hasSize(1);
ReadRequest request = requests.get(0);
assertNotNull(request.getRequestOptions());
assertThat(request.getRequestOptions().getRequestTag()).isEqualTo("app=spanner,env=test,action=read");
assertThat(request.getRequestOptions().getTransactionTag()).isEqualTo("app=spanner,env=test,action=txn");
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class DatabaseClientImplTest method testExecuteReadWithTag.
@Test
public void testExecuteReadWithTag() {
DatabaseClient client = spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
try (ResultSet resultSet = client.singleUse().read(READ_TABLE_NAME, KeySet.singleKey(Key.of(1L)), READ_COLUMN_NAMES, Options.tag("app=spanner,env=test,action=read"))) {
while (resultSet.next()) {
}
}
List<ReadRequest> requests = mockSpanner.getRequestsOfType(ReadRequest.class);
assertThat(requests).hasSize(1);
ReadRequest request = requests.get(0);
assertNotNull(request.getRequestOptions());
assertThat(request.getRequestOptions().getRequestTag()).isEqualTo("app=spanner,env=test,action=read");
assertThat(request.getRequestOptions().getTransactionTag()).isEmpty();
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class SpannerClientTest method readTest.
@Test
public void readTest() throws Exception {
ResultSet expectedResponse = ResultSet.newBuilder().setMetadata(ResultSetMetadata.newBuilder().build()).addAllRows(new ArrayList<ListValue>()).setStats(ResultSetStats.newBuilder().build()).build();
mockSpanner.addResponse(expectedResponse);
ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
ResultSet actualResponse = client.read(request);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockSpanner.getRequests();
Assert.assertEquals(1, actualRequests.size());
ReadRequest actualRequest = ((ReadRequest) actualRequests.get(0));
Assert.assertEquals(request.getSession(), actualRequest.getSession());
Assert.assertEquals(request.getTransaction(), actualRequest.getTransaction());
Assert.assertEquals(request.getTable(), actualRequest.getTable());
Assert.assertEquals(request.getIndex(), actualRequest.getIndex());
Assert.assertEquals(request.getColumnsList(), actualRequest.getColumnsList());
Assert.assertEquals(request.getKeySet(), actualRequest.getKeySet());
Assert.assertEquals(request.getLimit(), actualRequest.getLimit());
Assert.assertEquals(request.getResumeToken(), actualRequest.getResumeToken());
Assert.assertEquals(request.getPartitionToken(), actualRequest.getPartitionToken());
Assert.assertEquals(request.getRequestOptions(), actualRequest.getRequestOptions());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class SpannerClientTest method readExceptionTest.
@Test
public void readExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockSpanner.addException(exception);
try {
ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
client.read(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.spanner.v1.ReadRequest in project java-spanner by googleapis.
the class SpannerClientTest method streamingReadTest.
@Test
public void streamingReadTest() throws Exception {
PartialResultSet expectedResponse = PartialResultSet.newBuilder().setMetadata(ResultSetMetadata.newBuilder().build()).addAllValues(new ArrayList<Value>()).setChunkedValue(true).setResumeToken(ByteString.EMPTY).setStats(ResultSetStats.newBuilder().build()).build();
mockSpanner.addResponse(expectedResponse);
ReadRequest request = ReadRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setTable("table110115790").setIndex("index100346066").addAllColumns(new ArrayList<String>()).setKeySet(KeySet.newBuilder().build()).setLimit(102976443).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setRequestOptions(RequestOptions.newBuilder().build()).build();
MockStreamObserver<PartialResultSet> responseObserver = new MockStreamObserver<>();
ServerStreamingCallable<ReadRequest, PartialResultSet> callable = client.streamingReadCallable();
callable.serverStreamingCall(request, responseObserver);
List<PartialResultSet> actualResponses = responseObserver.future().get();
Assert.assertEquals(1, actualResponses.size());
Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Aggregations