Search in sources :

Example 61 with ParsedStatement

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

Example 62 with ParsedStatement

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

Example 63 with ParsedStatement

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

Example 64 with ParsedStatement

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
    }
}
Also used : 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) Mockito.anyString(org.mockito.Mockito.anyString) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 65 with ParsedStatement

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

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