use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testChecksumResultSet.
@Test
public void testChecksumResultSet() {
DatabaseClient client = mock(DatabaseClient.class);
ReadWriteTransaction transaction = ReadWriteTransaction.newBuilder().setRetryAbortsInternally(true).setTransactionRetryListeners(Collections.emptyList()).setDatabaseClient(client).withStatementExecutor(new StatementExecutor()).build();
ParsedStatement parsedStatement = mock(ParsedStatement.class);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
String arrayJson = "[{\"color\":\"red\",\"value\":\"#f00\"},{\"color\":\"green\",\"value\":\"#0f0\"},{\"color\":\"blue\",\"value\":\"#00f\"},{\"color\":\"cyan\",\"value\":\"#0ff\"},{\"color\":\"magenta\",\"value\":\"#f0f\"},{\"color\":\"yellow\",\"value\":\"#ff0\"},{\"color\":\"black\",\"value\":\"#000\"}]";
String emptyArrayJson = "[]";
String simpleJson = "{\"color\":\"red\",\"value\":\"#f00\"}";
ResultSet delegate1 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(BigDecimal.valueOf(550, 2)).set("JSON").to(Value.json(simpleJson)).build(), Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(BigDecimal.valueOf(750, 2)).set("JSON").to(Value.json(arrayJson)).build()));
ChecksumResultSet rs1 = transaction.createChecksumResultSet(delegate1, parsedStatement, AnalyzeMode.NONE);
ResultSet delegate2 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(new BigDecimal("5.50")).set("JSON").to(Value.json(simpleJson)).build(), Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(new BigDecimal("7.50")).set("JSON").to(Value.json(arrayJson)).build()));
ChecksumResultSet rs2 = transaction.createChecksumResultSet(delegate2, parsedStatement, AnalyzeMode.NONE);
// rs1 and rs2 are equal, rs3 contains the same rows, but in a different order
ResultSet delegate3 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(new BigDecimal("7.50")).set("JSON").to(Value.json(arrayJson)).build(), Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(new BigDecimal("5.50")).set("JSON").to(Value.json(simpleJson)).build()));
ChecksumResultSet rs3 = transaction.createChecksumResultSet(delegate3, parsedStatement, AnalyzeMode.NONE);
// rs4 contains the same rows as rs1 and rs2, but also an additional row
ResultSet delegate4 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(new BigDecimal("5.50")).set("JSON").to(Value.json(simpleJson)).build(), Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(new BigDecimal("7.50")).set("JSON").to(Value.json(arrayJson)).build(), Struct.newBuilder().set("ID").to(3L).set("NAME").to("TEST 3").set("AMOUNT").to(new BigDecimal("9.99")).set("JSON").to(Value.json(emptyArrayJson)).build()));
ChecksumResultSet rs4 = transaction.createChecksumResultSet(delegate4, parsedStatement, AnalyzeMode.NONE);
assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
while (rs1.next() && rs2.next() && rs3.next() && rs4.next()) {
assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
assertThat(rs1.getChecksum(), is(not(equalTo(rs3.getChecksum()))));
assertThat(rs1.getChecksum(), is(equalTo(rs4.getChecksum())));
}
assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
assertThat(rs1.getChecksum(), is(not(equalTo(rs3.getChecksum()))));
// rs4 contains one more row than rs1, but the last row of rs4 hasn't been consumed yet
assertThat(rs1.getChecksum(), is(equalTo(rs4.getChecksum())));
assertThat(rs4.next(), is(true));
assertThat(rs1.getChecksum(), is(not(equalTo(rs4.getChecksum()))));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testGetReadTimestamp.
@Test
public void testGetReadTimestamp() {
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);
ReadWriteTransaction transaction = createSubject();
assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(notNullValue()));
try {
transaction.getReadTimestamp();
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 testExecuteUpdate.
@Test
public void testExecuteUpdate() {
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));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testPlanQuery.
@Test
public void testPlanQuery() {
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);
ReadWriteTransaction transaction = createSubject();
ResultSet rs = get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.PLAN));
assertThat(rs, is(notNullValue()));
while (rs.next()) {
// do nothing
}
assertThat(rs.getStats(), is(notNullValue()));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ConnectionStatementWithOneParameterTest method testExecuteSetAutocommitDmlMode.
@Test
public void testExecuteSetAutocommitDmlMode() {
ParsedStatement subject = parser.parse(Statement.of("set autocommit_dml_mode='foo'"));
ConnectionImpl connection = mock(ConnectionImpl.class);
ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
when(executor.getConnection()).thenReturn(connection);
when(executor.statementSetAutocommitDmlMode(any(AutocommitDmlMode.class))).thenCallRealMethod();
for (AutocommitDmlMode mode : AutocommitDmlMode.values()) {
subject.getClientSideStatement().execute(executor, String.format("set autocommit_dml_mode='%s'", mode.name()));
verify(connection, times(1)).setAutocommitDmlMode(mode);
}
}
Aggregations