Skip to content

Commit

Permalink
Merge pull request #41 from tidymodels/duckdb
Browse files Browse the repository at this point in the history
add testing for duckdb backend
  • Loading branch information
EmilHvitfeldt authored Jul 26, 2024
2 parents 26c561c + c2e79f0 commit 6043bba
Show file tree
Hide file tree
Showing 47 changed files with 1,206 additions and 163 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Suggests:
DBI,
dbplyr,
dtplyr,
duckdb,
embed,
glue,
hardhat,
Expand Down
34 changes: 30 additions & 4 deletions tests/testthat/test-step_adasyn.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ test_that("spark - step_adasyn works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars_adasyn")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("SQLite - step_adasyn works", {
Expand All @@ -94,10 +94,36 @@ test_that("SQLite - step_adasyn works", {
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))) %>%
res_new <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)
expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

test_that("duckdb - 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)))

con <- DBI::dbConnect(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars_adasyn)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})
32 changes: 28 additions & 4 deletions tests/testthat/test-step_bin2factor.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ test_that("spark - step_bin2factor works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("SQLite - step_bin2factor works", {
Expand All @@ -88,10 +88,34 @@ test_that("SQLite - step_bin2factor works", {
con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars)

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

expect_equal(res_sql, res)
expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

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

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(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})
32 changes: 28 additions & 4 deletions tests/testthat/test-step_boxcox.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ test_that("spark - step_BoxCox works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("SQLite - step_BoxCox works", {
Expand All @@ -86,10 +86,34 @@ test_that("SQLite - step_BoxCox works", {
con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars)

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

expect_equal(res_sql, res)
expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

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

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(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})
34 changes: 30 additions & 4 deletions tests/testthat/test-step_bsmote.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ test_that("spark - step_bsmote works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars_bsmote")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("SQLite - step_bsmote works", {
Expand All @@ -94,10 +94,36 @@ test_that("SQLite - step_bsmote works", {
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))) %>%
res_new <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)
expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

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

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)))

con <- DBI::dbConnect(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars_bsmote)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})
34 changes: 29 additions & 5 deletions tests/testthat/test-step_center.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ test_that("spark - step_center works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("spark - step_center works", {
test_that("SQLite - step_center works", {
skip_if_not_installed("recipes")
skip_if_not_installed("DBI")
skip_if_not_installed("RSQLite")
Expand All @@ -86,10 +86,34 @@ test_that("spark - step_center works", {
con <- DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
mtcars_tbl <- dplyr::copy_to(con, mtcars)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

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

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(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars)

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

expect_equal(res_sql, res)
expect_equal(res_new, 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 @@ -37,4 +37,9 @@ test_that("spark - step_corr works", {
test_that("SQLite - step_corr works", {
# Isn't needed as `step_corr()` doesn't produce code
expect_true(TRUE)
})

test_that("duckdb - step_corr works", {
# Isn't needed as `step_corr()` doesn't produce code
expect_true(TRUE)
})
35 changes: 31 additions & 4 deletions tests/testthat/test-step_discretize.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ test_that("spark - step_discretize works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars_discretize")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("SQLite - step_discretize works", {
Expand All @@ -142,10 +142,37 @@ test_that("SQLite - step_discretize works", {
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))) %>%
res_new <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)
expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

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

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(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars_discretize)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})
34 changes: 30 additions & 4 deletions tests/testthat/test-step_downsample.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ test_that("spark - step_downsample works", {
sc <- testthat_spark_connection()
mtcars_tbl <- testthat_tbl("mtcars_downsample")

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

expect_equal(res_spark, res)
expect_equal(res_new, res)
})

test_that("SQLite - step_downsample works", {
Expand All @@ -94,10 +94,36 @@ test_that("SQLite - step_downsample works", {
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))) %>%
res_new <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>%
dplyr::collect()

expect_equal(res_sql, res)
expect_equal(res_new, res)

DBI::dbDisconnect(con)
})

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

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)))

con <- DBI::dbConnect(duckdb::duckdb(dbdir = ":memory:"))
mtcars_tbl <- dplyr::copy_to(con, mtcars_downsample)

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

expect_equal(res_new, res)

DBI::dbDisconnect(con)
})
Loading

0 comments on commit 6043bba

Please sign in to comment.