use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testGetCommitTimestampBeforeCommit.
@Test
public void testGetCommitTimestampBeforeCommit() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.UPDATE);
when(parsedStatement.isUpdate()).thenReturn(true);
Statement statement = Statement.of("UPDATE FOO SET BAR=1 WHERE ID=2");
when(parsedStatement.getStatement()).thenReturn(statement);
ReadWriteTransaction transaction = createSubject();
assertThat(get(transaction.executeUpdateAsync(parsedStatement)), is(1L));
try {
transaction.getCommitTimestamp();
fail("Expected exception");
} catch (SpannerException ex) {
assertEquals(ErrorCode.FAILED_PRECONDITION, ex.getErrorCode());
}
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testGetCommitResponseAfterCommit.
@Test
public void testGetCommitResponseAfterCommit() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.UPDATE);
when(parsedStatement.isUpdate()).thenReturn(true);
Statement statement = Statement.of("UPDATE FOO SET BAR=1 WHERE ID=2");
when(parsedStatement.getStatement()).thenReturn(statement);
ReadWriteTransaction transaction = createSubject();
get(transaction.executeUpdateAsync(parsedStatement));
get(transaction.commitAsync());
assertNotNull(transaction.getCommitResponse());
assertNotNull(transaction.getCommitResponseOrNull());
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class SingleUseTransactionTest method testExecuteDdl.
@Test
public void testExecuteDdl() {
String sql = "CREATE TABLE FOO";
ParsedStatement ddl = createParsedDdl(sql);
DdlClient ddlClient = createDefaultMockDdlClient();
SingleUseTransaction subject = createDdlSubject(ddlClient);
get(subject.executeDdlAsync(ddl));
verify(ddlClient).executeDdl(sql);
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class SingleUseTransactionTest method testMultiUse.
@Test
public void testMultiUse() {
for (TimestampBound staleness : getTestTimestampBounds()) {
SingleUseTransaction subject = createReadOnlySubject(staleness);
ResultSet rs = get(subject.executeQueryAsync(createParsedQuery(VALID_QUERY), AnalyzeMode.NONE));
assertThat(rs).isNotNull();
assertThat(subject.getReadTimestamp()).isNotNull();
try {
get(subject.executeQueryAsync(createParsedQuery(VALID_QUERY), AnalyzeMode.NONE));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
}
String sql = "CREATE TABLE FOO";
ParsedStatement ddl = createParsedDdl(sql);
DdlClient ddlClient = createDefaultMockDdlClient();
SingleUseTransaction subject = createDdlSubject(ddlClient);
get(subject.executeDdlAsync(ddl));
verify(ddlClient).executeDdl(sql);
try {
get(subject.executeDdlAsync(ddl));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
ParsedStatement update = createParsedUpdate(VALID_UPDATE);
subject = createSubject();
long updateCount = get(subject.executeUpdateAsync(update));
assertThat(updateCount).isEqualTo(VALID_UPDATE_COUNT);
assertThat(subject.getCommitTimestamp()).isNotNull();
try {
get(subject.executeUpdateAsync(update));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
subject = createSubject();
get(subject.writeAsync(Collections.singleton(Mutation.newInsertBuilder("FOO").build())));
assertThat(subject.getCommitTimestamp()).isNotNull();
try {
get(subject.writeAsync(Collections.singleton(Mutation.newInsertBuilder("FOO").build())));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
subject = createSubject();
Mutation mutation = Mutation.newInsertBuilder("FOO").build();
get(subject.writeAsync(Arrays.asList(mutation, mutation)));
assertThat(subject.getCommitTimestamp()).isNotNull();
try {
get(subject.writeAsync(Arrays.asList(mutation, mutation)));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class SingleUseTransactionTest method createParsedDdl.
private ParsedStatement createParsedDdl(String sql) {
ParsedStatement statement = mock(ParsedStatement.class);
when(statement.getType()).thenReturn(StatementType.DDL);
when(statement.getStatement()).thenReturn(Statement.of(sql));
when(statement.getSqlWithoutComments()).thenReturn(sql);
return statement;
}
Aggregations