use of com.google.spanner.v1.Mutation in project DataflowTemplates by GoogleCloudPlatform.
the class BeamRowToBigtableFnTest method processElementNullColumn.
@Test
public void processElementNullColumn() {
String columnFamily = "default";
String rowKeyValue = "rowkeyvalue";
String rowKeyColumnName = "rowkey";
String nullColumnName = "nullColumnName";
String nullValue = null;
Schema schema = Schema.builder().addField(Schema.Field.of(rowKeyColumnName, FieldType.STRING.withMetadata(CassandraRowMapperFn.KEY_ORDER_METADATA_KEY, "0"))).addNullableField(nullColumnName, FieldType.STRING).build();
Row input = Row.withSchema(schema).addValue(rowKeyValue).addValue(nullValue).build();
final List<Row> rows = Collections.singletonList(input);
List<Mutation> mutations = new ArrayList<>();
mutations.add(createMutation(columnFamily, nullColumnName, ByteString.EMPTY));
final List<KV<ByteString, Iterable<Mutation>>> expectedBigtableRows = ImmutableList.of(KV.of(ByteString.copyFrom(Bytes.toBytes("rowkeyvalue")), mutations));
PCollection<KV<ByteString, Iterable<Mutation>>> bigtableRows = pipeline.apply("Create", Create.of(rows)).apply("Transform to Bigtable", ParDo.of(BeamRowToBigtableFn.create(ValueProvider.StaticValueProvider.of("#"), ValueProvider.StaticValueProvider.of("default"))));
PAssert.that(bigtableRows).containsInAnyOrder(expectedBigtableRows);
pipeline.run();
}
use of com.google.spanner.v1.Mutation in project java-spanner by googleapis.
the class SessionPoolTest method testSessionNotFoundWrite.
@Test
public void testSessionNotFoundWrite() {
SpannerException sessionNotFound = SpannerExceptionFactoryTest.newSessionNotFoundException(sessionName);
List<Mutation> mutations = Collections.singletonList(Mutation.newInsertBuilder("FOO").build());
final SessionImpl closedSession = mockSession();
when(closedSession.writeWithOptions(mutations)).thenThrow(sessionNotFound);
final SessionImpl openSession = mockSession();
com.google.cloud.spanner.CommitResponse response = mock(com.google.cloud.spanner.CommitResponse.class);
when(response.getCommitTimestamp()).thenReturn(Timestamp.now());
when(openSession.writeWithOptions(mutations)).thenReturn(response);
doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(closedSession);
});
return null;
}).doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(openSession);
});
return null;
}).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
FakeClock clock = new FakeClock();
clock.currentTimeMillis = System.currentTimeMillis();
pool = createPool(clock);
DatabaseClientImpl impl = new DatabaseClientImpl(pool);
assertThat(impl.write(mutations)).isNotNull();
}
use of com.google.spanner.v1.Mutation in project java-spanner by googleapis.
the class SessionImplTest method writeAtLeastOnceWithOptions.
@Test
public void writeAtLeastOnceWithOptions() throws ParseException {
String tag = "app=spanner,env=test";
String timestampString = "2015-10-01T10:54:20.021Z";
ArgumentCaptor<CommitRequest> commit = ArgumentCaptor.forClass(CommitRequest.class);
CommitResponse response = CommitResponse.newBuilder().setCommitTimestamp(Timestamps.parse(timestampString)).build();
Mockito.when(rpc.commit(commit.capture(), Mockito.eq(options))).thenReturn(response);
session.writeAtLeastOnceWithOptions(Collections.singletonList(Mutation.newInsertBuilder("T").set("C").to("x").build()), Options.tag(tag));
CommitRequest request = commit.getValue();
assertThat(request.getRequestOptions().getTransactionTag()).isEqualTo(tag);
com.google.spanner.v1.Mutation mutation = com.google.spanner.v1.Mutation.newBuilder().setInsert(Write.newBuilder().setTable("T").addColumns("C").addValues(ListValue.newBuilder().addValues(com.google.protobuf.Value.newBuilder().setStringValue("x")))).build();
assertThat(request.getMutationsList()).containsExactly(mutation);
}
use of com.google.spanner.v1.Mutation in project java-spanner by googleapis.
the class SpannerClientTest method commitTest3.
@Test
public void commitTest3() throws Exception {
CommitResponse expectedResponse = CommitResponse.newBuilder().setCommitTimestamp(Timestamp.newBuilder().build()).setCommitStats(CommitResponse.CommitStats.newBuilder().build()).build();
mockSpanner.addResponse(expectedResponse);
String session = "session1984987798";
ByteString transactionId = ByteString.EMPTY;
List<Mutation> mutations = new ArrayList<>();
CommitResponse actualResponse = client.commit(session, transactionId, mutations);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockSpanner.getRequests();
Assert.assertEquals(1, actualRequests.size());
CommitRequest actualRequest = ((CommitRequest) actualRequests.get(0));
Assert.assertEquals(session, actualRequest.getSession());
Assert.assertEquals(transactionId, actualRequest.getTransactionId());
Assert.assertEquals(mutations, actualRequest.getMutationsList());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.v1.Mutation in project java-spanner by googleapis.
the class SpannerClientTest method commitExceptionTest2.
@Test
public void commitExceptionTest2() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockSpanner.addException(exception);
try {
SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
TransactionOptions singleUseTransaction = TransactionOptions.newBuilder().build();
List<Mutation> mutations = new ArrayList<>();
client.commit(session, singleUseTransaction, mutations);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
Aggregations