Search in sources :

Example 6 with ParsedStatement

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());
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 7 with ParsedStatement

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();
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) QueryOption(com.google.cloud.spanner.Options.QueryOption) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 8 with ParsedStatement

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());
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 9 with ParsedStatement

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()));
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) TimestampBound(com.google.cloud.spanner.TimestampBound) ResultSet(com.google.cloud.spanner.ResultSet) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 10 with ParsedStatement

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()));
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) TimestampBound(com.google.cloud.spanner.TimestampBound) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Aggregations

ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)75 Test (org.junit.Test)61 Statement (com.google.cloud.spanner.Statement)23 SpannerException (com.google.cloud.spanner.SpannerException)20 ResultSet (com.google.cloud.spanner.ResultSet)12 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 TimestampBound (com.google.cloud.spanner.TimestampBound)6 LinkedList (java.util.LinkedList)3 Mockito.anyString (org.mockito.Mockito.anyString)3 AbortedException (com.google.cloud.spanner.AbortedException)2 QueryOption (com.google.cloud.spanner.Options.QueryOption)2 Mutation (com.google.cloud.spanner.Mutation)1 ReadContext (com.google.cloud.spanner.ReadContext)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionManager (com.google.cloud.spanner.TransactionManager)1 ImmutableList (com.google.common.collect.ImmutableList)1 Duration (com.google.protobuf.Duration)1 BigDecimal (java.math.BigDecimal)1 TimeUnit (java.util.concurrent.TimeUnit)1