Skip to content

Commit

Permalink
Merge pull request #39 from tidymodels/test-sql
Browse files Browse the repository at this point in the history
test on SQLite with RSQLite
  • Loading branch information
EmilHvitfeldt authored Jul 26, 2024
2 parents 172ad85 + a6b5041 commit 2450b9b
Show file tree
Hide file tree
Showing 48 changed files with 1,025 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Imports:
rlang
Suggests:
arrow,
DBI,
dbplyr,
dtplyr,
embed,
Expand All @@ -30,6 +31,7 @@ Suggests:
R6,
recipes,
rmarkdown,
RSQLite,
rstanarm,
sparklyr,
testthat (>= 3.0.0),
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/_snaps/dt.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# dt works

Code
orbital_dt(obj)
Output
copy(`_DT2`)[, `:=`(c("cyl", "disp", "hp", "drat", "wt", "qsec",
"vs", "am", "gear", "carb", ".pred"), {
cyl <- (cyl - 6.1875)/1.785922
disp <- (disp - 230.7219)/123.9387
hp <- (hp - 146.6875)/68.56287
drat <- (drat - 3.596562)/0.5346787
wt <- (wt - 3.21725)/0.9784574
qsec <- (qsec - 17.84875)/1.786943
vs <- (vs - 0.4375)/0.5040161
am <- (am - 0.40625)/0.4989909
gear <- (gear - 3.6875)/0.7378041
carb <- (carb - 2.8125)/1.6152
.pred <- 20.09062 + (cyl * -0.199024) + (disp *
1.652752) + (hp * -1.472876) + (drat *
0.4208515) + (wt * -3.635267) + (qsec *
1.467153) + (vs * 0.1601576) + (am *
1.25757) + (gear * 0.4835664) + (carb *
-0.322102)
.(cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb, .pred)
})]

27 changes: 27 additions & 0 deletions tests/testthat/test-step_adasyn.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ test_that("spark - step_adasyn works", {

expect_equal(res_spark, res)
})

test_that("SQLite - step_adasyn works", {
skip_if_not_installed("recipes")
skip_if_not_installed("themis")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_adasyn <- dplyr::as_tibble(mtcars)
mtcars_adasyn$vs <- as.factor(mtcars$vs)

rec <- recipes::recipe(mpg ~ ., data = mtcars_adasyn) %>%
themis::step_adasyn(vs, skip = TRUE) %>%
recipes::prep()

res <- dplyr::mutate(mtcars_adasyn, !!!orbital_inline(orbital(rec)))
res$vs <- as.character(res$vs)

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_adasyn)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
21 changes: 21 additions & 0 deletions tests/testthat/test-step_bin2factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,26 @@ test_that("spark - step_bin2factor works", {
expect_equal(res_spark, res)
})

test_that("SQLite - step_bin2factor works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars <- dplyr::as_tibble(mtcars)

rec <- recipes::recipe(mpg ~ ., data = mtcars) %>%
recipes::step_bin2factor(vs, am) %>%
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
21 changes: 21 additions & 0 deletions tests/testthat/test-step_boxcox.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,26 @@ test_that("spark - step_BoxCox works", {
expect_equal(res_spark, res)
})

test_that("SQLite - step_BoxCox works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars <- dplyr::as_tibble(mtcars)

rec <- recipes::recipe(~ ., data = mtcars) %>%
recipes::step_BoxCox(mpg, disp) %>%
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
27 changes: 27 additions & 0 deletions tests/testthat/test-step_bsmote.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ test_that("spark - step_bsmote works", {

expect_equal(res_spark, res)
})

test_that("SQLite - step_bsmote works", {
skip_if_not_installed("recipes")
skip_if_not_installed("themis")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_bsmote <- dplyr::as_tibble(mtcars)
mtcars_bsmote$vs <- as.factor(mtcars$vs)

rec <- recipes::recipe(mpg ~ ., data = mtcars_bsmote) %>%
themis::step_bsmote(vs, skip = TRUE) %>%
recipes::prep()

res <- dplyr::mutate(mtcars_bsmote, !!!orbital_inline(orbital(rec)))
res$vs <- as.character(res$vs)

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_bsmote)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
24 changes: 24 additions & 0 deletions tests/testthat/test-step_center.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,27 @@ test_that("spark - step_center works", {

expect_equal(res_spark, res)
})

test_that("spark - step_center works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars <- dplyr::as_tibble(mtcars)

rec <- recipes::recipe(mpg ~ ., data = mtcars) %>%
recipes::step_center(recipes::all_predictors()) %>%
recipes::prep()

res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
5 changes: 5 additions & 0 deletions tests/testthat/test-step_corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ test_that("step_corr works with empty selections", {
test_that("spark - step_corr works", {
# Isn't needed as `step_corr()` doesn't produce code
expect_true(TRUE)
})

test_that("SQLite - step_corr works", {
# Isn't needed as `step_corr()` doesn't produce code
expect_true(TRUE)
})
24 changes: 24 additions & 0 deletions tests/testthat/test-step_discretize.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,29 @@ test_that("spark - step_discretize works", {
expect_equal(res_spark, res)
})

test_that("SQLite - step_discretize works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_discretize <- dplyr::as_tibble(mtcars)
mtcars_discretize[1, ] <- NA

suppressWarnings(
rec <- recipes::recipe(mpg ~ ., data = mtcars_discretize) %>%
recipes::step_discretize(mpg, disp, min_unique = 4) %>%
recipes::prep()
)

res <- dplyr::mutate(mtcars_discretize, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_discretize)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
27 changes: 27 additions & 0 deletions tests/testthat/test-step_downsample.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ test_that("spark - step_downsample works", {

expect_equal(res_spark, res)
})

test_that("SQLite - step_downsample works", {
skip_if_not_installed("recipes")
skip_if_not_installed("themis")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_downsample <- dplyr::as_tibble(mtcars)
mtcars_downsample$vs <- as.factor(mtcars$vs)

rec <- recipes::recipe(mpg ~ ., data = mtcars_downsample) %>%
themis::step_downsample(vs, skip = TRUE) %>%
recipes::prep()

res <- dplyr::mutate(mtcars_downsample, !!!orbital_inline(orbital(rec)))
res$vs <- as.character(res$vs)

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_downsample)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
27 changes: 27 additions & 0 deletions tests/testthat/test-step_dummy.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,31 @@ test_that("spark - step_dummy works", {
dplyr::collect()

expect_equal(res_spark, exp)
})

test_that("SQL - step_dummy works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars1 <- dplyr::as_tibble(mtcars)

mtcars1$gear <- as.character(mtcars1$gear)
mtcars1$carb <- as.character(mtcars1$carb)

rec <- recipes::recipe(mpg ~ ., data = mtcars1) %>%
recipes::step_dummy(recipes::all_nominal_predictors()) %>%
recipes::prep()

exp <- dplyr::mutate(mtcars1, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars1)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, exp)

DBI::dbDisconnect(con)
})
5 changes: 5 additions & 0 deletions tests/testthat/test-step_filter_missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ test_that("step_filter_missing works with empty selections", {
test_that("spark - step_filter_missing works", {
# Isn't needed as `step_filter_missing()` doesn't produce code
expect_true(TRUE)
})

test_that("SQLite - step_filter_missing works", {
# Isn't needed as `step_filter_missing()` doesn't produce code
expect_true(TRUE)
})
22 changes: 22 additions & 0 deletions tests/testthat/test-step_impute_mean.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,27 @@ test_that("spark - step_impute_mean works", {
expect_equal(res_spark, res)
})

test_that("SQLite - step_impute_mean works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_impute_mean <- dplyr::as_tibble(mtcars)
mtcars_impute_mean[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_mean) %>%
recipes::step_impute_mean(recipes::all_predictors()) %>%
recipes::prep()

res <- dplyr::mutate(mtcars_impute_mean, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_impute_mean)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
22 changes: 22 additions & 0 deletions tests/testthat/test-step_impute_median.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,27 @@ test_that("spark - step_impute_median works", {
expect_equal(res_spark, res)
})

test_that("SQL - step_impute_median works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_impute_median <- dplyr::as_tibble(mtcars)
mtcars_impute_median[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_median) %>%
recipes::step_impute_median(recipes::all_predictors()) %>%
recipes::prep()

res <- dplyr::mutate(mtcars_impute_median, !!!orbital_inline(orbital(rec)))

con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_impute_median)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
25 changes: 25 additions & 0 deletions tests/testthat/test-step_impute_mode.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,30 @@ test_that("spark - step_impute_mode works", {
expect_equal(res_spark, res)
})

test_that("SQLite - step_impute_mode works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")

mtcars_impute_mode <- dplyr::as_tibble(mtcars)
mtcars_impute_mode$gear <- letters[mtcars$gear]
mtcars_impute_mode$carb <- letters[mtcars$carb]
mtcars_impute_mode[2:4, ] <- NA

rec <- recipes::recipe(mpg ~ ., data = mtcars_impute_mode) %>%
recipes::step_impute_mode(recipes::all_nominal_predictors()) %>%
recipes::prep(strings_as_factors = FALSE)

res <- dplyr::mutate(mtcars_impute_mode, !!!orbital_inline(orbital(rec)))


con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars_impute_mode)

res_sql <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)

DBI::dbDisconnect(con)
})
Loading

0 comments on commit 2450b9b

Please sign in to comment.