use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class SingleUseTransactionTest method testGetCommitResponseAfterDdl.
@Test
public void testGetCommitResponseAfterDdl() {
ParsedStatement ddl = createParsedDdl(VALID_DDL);
SingleUseTransaction transaction = createSubject();
get(transaction.executeDdlAsync(ddl));
try {
transaction.getCommitResponse();
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.FAILED_PRECONDITION, e.getErrorCode());
}
assertNull(transaction.getCommitResponseOrNull());
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class SingleUseTransactionTest method testExecuteQueryWithOptionsTest.
@Test
public void testExecuteQueryWithOptionsTest() {
String sql = "SELECT * FROM FOO";
QueryOption option = Options.prefetchChunks(10000);
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of(sql);
when(parsedStatement.getStatement()).thenReturn(statement);
DatabaseClient client = mock(DatabaseClient.class);
com.google.cloud.spanner.ReadOnlyTransaction tx = mock(com.google.cloud.spanner.ReadOnlyTransaction.class);
when(tx.executeQuery(Statement.of(sql), option)).thenReturn(mock(ResultSet.class));
when(client.singleUseReadOnlyTransaction(TimestampBound.strong())).thenReturn(tx);
SingleUseTransaction transaction = SingleUseTransaction.newBuilder().setDatabaseClient(client).setDdlClient(mock(DdlClient.class)).setAutocommitDmlMode(AutocommitDmlMode.TRANSACTIONAL).withStatementExecutor(executor).setReadOnlyStaleness(TimestampBound.strong()).build();
assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE, option))).isNotNull();
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadOnlyTransactionTest method testExecuteUpdate.
@Test
public void testExecuteUpdate() {
ParsedStatement update = mock(ParsedStatement.class);
when(update.getType()).thenReturn(StatementType.UPDATE);
try {
createSubject().executeUpdateAsync(update);
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 ReadOnlyTransactionTest method testExecuteQuery.
@Test
public void testExecuteQuery() {
for (TimestampBound staleness : getTestTimestampBounds()) {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
ReadOnlyTransaction transaction = createSubject(staleness);
ResultSet rs = get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE));
assertThat(rs, is(notNullValue()));
assertThat(rs.getStats(), is(nullValue()));
}
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadOnlyTransactionTest method testGetReadTimestamp.
@Test
public void testGetReadTimestamp() {
for (TimestampBound staleness : getTestTimestampBounds()) {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
ReadOnlyTransaction transaction = createSubject(staleness);
boolean expectedException = false;
try {
transaction.getReadTimestamp();
} catch (SpannerException e) {
if (e.getErrorCode() == ErrorCode.FAILED_PRECONDITION) {
expectedException = true;
}
}
assertThat(expectedException, is(true));
assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(notNullValue()));
assertThat(transaction.getReadTimestamp(), is(notNullValue()));
}
}
Aggregations