tests/testthat/test-db.R

test_that("scr_connect and scr_fetch work on an in-memory SQLite database", {
  skip_if_not_installed("RSQLite"); skip_if_not_installed("DBI")
  con <- scr_connect(driver = RSQLite::SQLite(), dbname = ":memory:")
  on.exit(DBI::dbDisconnect(con))
  d <- scr_demo; d$ref_date <- as.character(d$ref_date)
  DBI::dbWriteTable(con, "dtm", d)
  full <- scr_fetch(con, "dtm")
  expect_s3_class(full, "data.table")
  expect_equal(nrow(full), nrow(scr_demo))
  half <- scr_fetch(con, "dtm", sample_frac = 0.5, seed = 1)
  expect_gt(nrow(half), 0.35 * nrow(scr_demo)); expect_lt(nrow(half), 0.65 * nrow(scr_demo))
  expect_message(capped <- scr_fetch(con, "dtm", sample_frac = 1, max_rows = 1000), "fraction reduced")
  expect_lt(nrow(capped), 1500)
  expect_error(scr_fetch(con, "dtm", sample_frac = 0), "sample_frac")
  expect_error(scr_connect(), "dsn")
})

test_that("scr_fetch picks a random expression by connection class", {
  expect_match(.sample_expr(structure(list(), class = "SQLiteConnection")), "random\\(\\)")
  expect_equal(.sample_expr(structure(list(), class = "duckdb_connection")), "random()")
  expect_equal(.sample_expr(structure(list(), class = "Spark SQL"), seed = 7), "rand(7)")
  expect_equal(.sample_expr(structure(list(), class = "Spark SQL")), "rand()")
})

test_that("scr_run reads several targets from a database and builds a run set", {
  skip_if_not_installed("RSQLite"); skip_if_not_installed("DBI")
  con <- scr_connect(driver = RSQLite::SQLite(), dbname = ":memory:")
  on.exit(DBI::dbDisconnect(con))
  d <- scr_demo; d$ref_date <- as.character(d$ref_date)
  DBI::dbWriteTable(con, "dtm", d)
  rs <- scr_run(con, "dtm", targets = c("default", "churn", "missing_target"), config = cfg_test(),
                drop = c("id", "ref_date", "default", "churn"))
  expect_s3_class(rs, "scr_runset")
  expect_s3_class(rs$default, "scr_result")
  expect_s3_class(rs$churn, "scr_result")
  expect_true(!is.null(rs$missing_target$error))
  expect_equal(rs$default$config$sql_table, "dtm")
  expect_output(print(rs), "1 failed")
  cmp <- scr_compare(rs)
  expect_equal(cmp$target, c("default", "churn"))
  expect_true(all(cmp$auc > 0.6))
  core <- scr_core(rs, min_targets = 1)
  expect_true(all(core$n_targets >= 1))
  expect_true(any(scr_core(rs, min_targets = 2)$n_targets == 2))
})

test_that("scr_fetch works on duckdb with the placeholder table name", {
  skip_if_not_installed("duckdb"); skip_if_not_installed("DBI")
  con <- scr_connect(driver = duckdb::duckdb(), config = list(threads = "1"))
  on.exit(DBI::dbDisconnect(con, shutdown = TRUE))
  DBI::dbWriteTable(con, "dtm_default", scr_demo)
  rs <- scr_run(con, "dtm_{target}", targets = "default", config = cfg_test(),
                drop = c("id", "ref_date", "churn"), sample_frac = 0.8)
  expect_s3_class(rs$default, "scr_result")
  expect_lt(rs$default$meta$n_total, nrow(scr_demo))
})

Try the scorecraft package in your browser

Any scripts or data that you put into this service are public.

scorecraft documentation built on Dec. 6, 2026, 1:09 a.m.