@Test public void basicsUsingRead() throws InterruptedException { doBasicsTest( new ReadStrategy() { @Override public Struct read(ReadContext ctx, String key) { return ctx.readRow("T", Key.of(key), Arrays.asList("V")); } }); }
private void doBasicsTest(final ReadStrategy strategy) throws InterruptedException { final String key = uniqueKey();
@Test public void nestedReadWriteTxnThrows() { try { doNestedRwTransaction(); fail("Expected exception"); } catch (SpannerException e) { assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INTERNAL); assertThat(e.getMessage()).contains("not supported"); } }
@Test public void userExceptionIsSpannerException() { final String key = uniqueKey(); TransactionCallable<Void> callable = new TransactionCallable<Void>() { @Override public Void run(TransactionContext transaction) { transaction.buffer(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).build()); throw newSpannerException(ErrorCode.OUT_OF_RANGE, "User failure"); } }; try { client.readWriteTransaction().run(callable); fail("Expected user exception"); } catch (SpannerException e) { assertThat(e.getErrorCode()).isEqualTo(ErrorCode.OUT_OF_RANGE); assertThat(e.getMessage()).contains("User failure"); } Struct row = client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key), Arrays.asList("K")); assertThat(row).isNull(); }
@Test public void userExceptionPreventsCommit() { class UserException extends Exception { UserException(String message) { super(message); } } final String key = uniqueKey(); TransactionCallable<Void> callable = new TransactionCallable<Void>() { @Override public Void run(TransactionContext transaction) throws UserException { transaction.buffer(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).build()); throw new UserException("User failure"); } }; try { client.readWriteTransaction().run(callable); fail("Expected user exception"); } catch (SpannerException e) { assertThat(e.getErrorCode()).isEqualTo(ErrorCode.UNKNOWN); assertThat(e.getMessage()).contains("User failure"); assertThat(e.getCause()).isInstanceOf(UserException.class); } Struct row = client.singleUse(TimestampBound.strong()).readRow("T", Key.of(key), Arrays.asList("K")); assertThat(row).isNull(); }
@Test public void basicsUsingQuery() throws InterruptedException { doBasicsTest( new ReadStrategy() { @Override public Struct read(ReadContext ctx, String key) { ResultSet resultSet = ctx.executeQuery( Statement.newBuilder("SELECT V FROM T WHERE K = @key") .bind("key") .to(key) .build()); assertThat(resultSet.next()).isTrue(); Struct row = resultSet.getCurrentRowAsStruct(); assertThat(resultSet.next()).isFalse(); return row; } }); }