Search in sources :

Example 21 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionStatementWithOneParameterTest method testExecuteSetAutocommit.

@Test
public void testExecuteSetAutocommit() {
    ParsedStatement subject = parser.parse(Statement.of("set autocommit = true"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetAutocommit(any(Boolean.class))).thenCallRealMethod();
    for (Boolean mode : new Boolean[] { Boolean.FALSE, Boolean.TRUE }) {
        subject.getClientSideStatement().execute(executor, String.format("set autocommit = %s", mode));
        verify(connection, times(1)).setAutocommit(mode);
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 22 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionStatementWithOneParameterTest method testExecuteSetOptimizerVersion.

@Test
public void testExecuteSetOptimizerVersion() {
    ParsedStatement subject = parser.parse(Statement.of("set optimizer_version='foo'"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetOptimizerVersion(any(String.class))).thenCallRealMethod();
    for (String version : new String[] { "1", "200", "", "LATEST" }) {
        subject.getClientSideStatement().execute(executor, String.format("set optimizer_version='%s'", version));
        verify(connection, times(1)).setOptimizerVersion(version);
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 23 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionStatementWithOneParameterTest method testExecuteSetOptimizerStatisticsPackage.

@Test
public void testExecuteSetOptimizerStatisticsPackage() {
    ParsedStatement subject = parser.parse(Statement.of("set optimizer_statistics_package='foo'"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetOptimizerStatisticsPackage(any(String.class))).thenCallRealMethod();
    for (String statisticsPackage : new String[] { "custom-package", "" }) {
        subject.getClientSideStatement().execute(executor, String.format("set optimizer_statistics_package='%s'", statisticsPackage));
        verify(connection, times(1)).setOptimizerStatisticsPackage(statisticsPackage);
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 24 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ConnectionStatementWithOneParameterTest method testExecuteSetTransaction.

@Test
public void testExecuteSetTransaction() {
    ParsedStatement subject = parser.parse(Statement.of("set transaction read_only"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetTransactionMode(any(TransactionMode.class))).thenCallRealMethod();
    for (TransactionMode mode : TransactionMode.values()) {
        subject.getClientSideStatement().execute(executor, String.format("set transaction %s", mode.getStatementString()));
        verify(connection, times(1)).setTransactionMode(mode);
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 25 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class DdlBatchTest method testGetStateAndIsActive.

@Test
public void testGetStateAndIsActive() {
    DdlBatch batch = createSubject();
    assertThat(batch.getState(), is(UnitOfWorkState.STARTED));
    assertThat(batch.isActive(), is(true));
    get(batch.runBatchAsync());
    assertThat(batch.getState(), is(UnitOfWorkState.RAN));
    assertThat(batch.isActive(), is(false));
    batch = createSubject();
    assertThat(batch.getState(), is(UnitOfWorkState.STARTED));
    assertThat(batch.isActive(), is(true));
    batch.abortBatch();
    assertThat(batch.getState(), is(UnitOfWorkState.ABORTED));
    assertThat(batch.isActive(), is(false));
    DdlClient client = mock(DdlClient.class);
    SpannerException exception = mock(SpannerException.class);
    when(exception.getErrorCode()).thenReturn(ErrorCode.FAILED_PRECONDITION);
    doThrow(exception).when(client).executeDdl(anyList());
    batch = createSubject(client);
    assertThat(batch.getState(), is(UnitOfWorkState.STARTED));
    assertThat(batch.isActive(), is(true));
    ParsedStatement statement = mock(ParsedStatement.class);
    when(statement.getStatement()).thenReturn(Statement.of("CREATE TABLE FOO"));
    when(statement.getSqlWithoutComments()).thenReturn("CREATE TABLE FOO");
    when(statement.getType()).thenReturn(StatementType.DDL);
    batch.executeDdlAsync(statement);
    try {
        get(batch.runBatchAsync());
        fail("Missing expected exception");
    } catch (SpannerException e) {
        assertThat(e.getErrorCode(), is(equalTo(ErrorCode.FAILED_PRECONDITION)));
    }
    assertThat(batch.getState(), is(UnitOfWorkState.RUN_FAILED));
    assertThat(batch.isActive(), is(false));
}
Also used : 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