Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions R/memdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#' `memdb_frame()` works like [tibble::tibble()], but instead of creating a new
#' data frame in R, it creates a table in `memdb()`. `local_memdb_frame()`
#' is like `memdb_frame()` but the table will be automatically deleted when
#' the current scope ends. It's useful for tests.
#' the current scope ends. It's useful for tests. But beware: this function
#' will overwrite an existing table of the same name.
#'
#' @inheritParams tibble::tibble
#' @param .name Name of table in database: defaults to a random name that's
Expand Down Expand Up @@ -45,7 +46,7 @@ local_memdb_frame <- function(
...,
frame = caller_env()
) {
tbl <- copy_to(memdb(), tibble(...), .name)
tbl <- copy_to(memdb(), tibble(...), .name, overwrite = TRUE)
withr::defer(DBI::dbRemoveTable(memdb(), .name), envir = frame)
tbl
}
Expand Down
3 changes: 2 additions & 1 deletion man/memdb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions tests/testthat/helper-src.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ cache_test_con <- function(name, get_args) {
}
}

test_sqlite <- function() {
cache_test_con("sqlite", function() {
list(drv = RSQLite::SQLite(), dbname = ":memory:")
})
}

test_postgres <- function() {
cache_test_con("postgres", function() {
if (on_github_postgres()) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-backend-sqlite.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test_that("date extraction agrees with R", {

test_that("can explain a query", {
db <- copy_to(
test_sqlite(),
memdb(),
data.frame(x = 1:5),
name = "test",
indexes = list("x"),
Expand Down
14 changes: 2 additions & 12 deletions tests/testthat/test-remote.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
test_that("remote_table returns name when it makes sense", {
mf <- copy_to(
test_sqlite(),
tibble(x = 5),
name = "refxiudlph",
overwrite = TRUE
)
mf <- local_memdb_frame("refxiudlph", x = 5)

# produces name after `group_by()`
expect_equal(
Expand All @@ -19,12 +14,7 @@ test_that("remote_table returns name when it makes sense", {
})

test_that("remote_table returns null for computed tables", {
mf <- copy_to(
test_sqlite(),
tibble(x = 5, y = 1),
name = "refxiudlph",
overwrite = TRUE
)
mf <- local_memdb_frame("refxiudlph", x = 5, y = 1)
expect_equal(remote_table(mf), table_path("`refxiudlph`"))

expect_null(mf |> filter(x == 3) |> remote_table())
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-sql-build.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
test_that("rendering table wraps in SELECT *", {
out <- copy_to(
test_sqlite(),
tibble(x = 1),
name = "test-sql-build",
overwrite = TRUE
)
out <- local_memdb_frame("test-sql-build", x = 1)
expect_snapshot(out |> sql_render())
expect_equal(out |> collect(), tibble(x = 1))
})
7 changes: 1 addition & 6 deletions tests/testthat/test-tbl-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ test_that("same_src distinguishes srcs", {
})

test_that("has nice print method", {
mf <- copy_to(
test_sqlite(),
tibble(x = 1, y = 1),
name = "tbl_sum_test",
overwrite = TRUE
)
mf <- local_memdb_frame("tbl_sum_test", x = 1, y = 1)
expect_snapshot(mf, transform = scrub_sqlite_version)

out2 <- mf |> group_by(x, y) |> arrange(x) |> mutate(z = x + y)
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-verb-arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ test_that("correctly inlines across all verbs", {
# sql_render --------------------------------------------------------------

test_that("quoting for rendering ordered grouped table", {
db <- copy_to(
test_sqlite(),
tibble(x = 1, y = 2),
name = "test-verb-arrange",
overwrite = TRUE
)
db <- local_memdb_frame("test-verb-arrange", x = 1, y = 2)
out <- db |> group_by(x) |> arrange(y) |> ungroup()
expect_snapshot(sql_render(out))
expect_equal(collect(out), tibble(x = 1, y = 2))
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-verb-mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ test_that("can refer to fresly created values", {
collect()
expect_equal(out1, tibble(x1 = 1, x2 = 2, x3 = 3, x4 = 4))

out2 <- copy_to(
test_sqlite(),
tibble(x = 1),
name = "multi_mutate",
overwrite = TRUE
) |>
out2 <- local_memdb_frame("multi_mutate", x = 1) |>
mutate(x = x + 1, x = x + 2, x = x + 4)
expect_equal(collect(out2), tibble(x = 8))
expect_snapshot(show_query(out2))
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-verb-summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,7 @@ test_that("catches `.by` with grouped-df", {
# sql-render --------------------------------------------------------------

test_that("quoting for rendering summarized grouped table", {
out <- copy_to(
test_sqlite(),
tibble(x = 1),
name = "verb-summarise",
overwrite = TRUE
) |>
out <- local_memdb_frame("verb-summarise", x = 1) |>
group_by(x) |>
summarise(n = n())
expect_snapshot(out |> sql_render())
Expand Down
7 changes: 1 addition & 6 deletions tests/testthat/test-verb-uncount.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
test_that("symbols weights are dropped in output", {
df <- copy_to(
test_sqlite(),
tibble(x = 1, w = 1),
name = "test",
overwrite = TRUE
)
df <- local_memdb_frame("test", x = 1, w = 1)
expect_equal(dbplyr_uncount(df, w) |> collect(), tibble(x = 1))

expect_snapshot(
Expand Down