SqlDatabaseFunctions { @NonNull public static <T> Function<SqlRequest, Result<List<T>>> databaseQueryFunction( @NonNull final Supplier<Result<SQLiteDatabase>> database, @NonNull Function<Cursor, T> rowMap) { return new DatabaseFunction<>(database, new DatabaseQueryMerger<>(rowMap)); } private  SqlDatabaseFunctions(); @NonNull static Function<SqlRequest, Result<List<T>>> databaseQueryFunction(
      @NonNull final Supplier<Result<SQLiteDatabase>> database,
      @NonNull Function<Cursor, T> rowMap); @NonNull static Function<SqlInsertRequest, Result<Long>> databaseInsertFunction(
      @NonNull final Supplier<Result<SQLiteDatabase>> database); @NonNull static Function<SqlUpdateRequest, Result<Integer>> databaseUpdateFunction(
      @NonNull final Supplier<Result<SQLiteDatabase>> database); @NonNull static Function<SqlDeleteRequest, Result<Integer>> databaseDeleteFunction(
      @NonNull final Supplier<Result<SQLiteDatabase>> database);  }
@Test public void shouldGetValuesForDatabaseQuery() throws Throwable { assertThat(databaseQueryFunction(databaseSupplier, CURSOR_STRING_FUNCTION) .apply(sqlRequest() .sql(SQL_QUERY) .compile()).get(), contains("value1", "value2", "value3")); }
@Test public void shouldNotGetValuesForDatabaseQueryWithNonMatchingWhere() throws Throwable { assertThat(databaseQueryFunction(databaseSupplier, CURSOR_STRING_FUNCTION) .apply(sqlRequest() .sql(NON_MATCHING_SQL_QUERY) .compile()).get(), empty()); }
@Test public void shouldReturnFailureForInvalidQuery() { assertThat(databaseQueryFunction(databaseSupplier, CURSOR_STRING_FUNCTION) .apply(sqlRequest() .sql(INVALID_QUERY) .compile()).getFailure(), instanceOf(SQLException.class)); }
@Test public void shouldPassArgumentsToDatabaseQuery() throws Throwable { assertThat(databaseQueryFunction(databaseSupplier, CURSOR_STRING_FUNCTION) .apply(sqlRequest() .sql(SQL_QUERY_FOR_ARGUMENT) .arguments("value2").compile()).get(), contains("value2")); }
@Test public void shouldReturnErrorForFailedDatabaseCreationInQuery() throws Throwable { assertThat(databaseQueryFunction(FAILURE, CURSOR_STRING_FUNCTION) .apply(sqlRequest() .sql(SQL_QUERY_FOR_ARGUMENT) .arguments("value2") .compile()).failed(), is(true)); }