Skip to content

Commit 39d7b56

Browse files
authored
Eliminate src_sqlite() (#1734)
And make `local_memdb_frame()` easier to use interactively by setting `overwrite = TRUE`
1 parent 668f271 commit 39d7b56

File tree

11 files changed

+14
-58
lines changed

11 files changed

+14
-58
lines changed

R/memdb.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#' `memdb_frame()` works like [tibble::tibble()], but instead of creating a new
99
#' data frame in R, it creates a table in `memdb()`. `local_memdb_frame()`
1010
#' is like `memdb_frame()` but the table will be automatically deleted when
11-
#' the current scope ends. It's useful for tests.
11+
#' the current scope ends. It's useful for tests. But beware: this function
12+
#' will overwrite an existing table of the same name.
1213
#'
1314
#' @inheritParams tibble::tibble
1415
#' @param .name Name of table in database: defaults to a random name that's
@@ -45,7 +46,7 @@ local_memdb_frame <- function(
4546
...,
4647
frame = caller_env()
4748
) {
48-
tbl <- copy_to(memdb(), tibble(...), .name)
49+
tbl <- copy_to(memdb(), tibble(...), .name, overwrite = TRUE)
4950
withr::defer(DBI::dbRemoveTable(memdb(), .name), envir = frame)
5051
tbl
5152
}

man/memdb.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/helper-src.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ cache_test_con <- function(name, get_args) {
3333
}
3434
}
3535

36-
test_sqlite <- function() {
37-
cache_test_con("sqlite", function() {
38-
list(drv = RSQLite::SQLite(), dbname = ":memory:")
39-
})
40-
}
41-
4236
test_postgres <- function() {
4337
cache_test_con("postgres", function() {
4438
if (on_github_postgres()) {

tests/testthat/test-backend-sqlite.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test_that("date extraction agrees with R", {
117117

118118
test_that("can explain a query", {
119119
db <- copy_to(
120-
test_sqlite(),
120+
memdb(),
121121
data.frame(x = 1:5),
122122
name = "test",
123123
indexes = list("x"),

tests/testthat/test-remote.R

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
test_that("remote_table returns name when it makes sense", {
2-
mf <- copy_to(
3-
test_sqlite(),
4-
tibble(x = 5),
5-
name = "refxiudlph",
6-
overwrite = TRUE
7-
)
2+
mf <- local_memdb_frame("refxiudlph", x = 5)
83

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

2116
test_that("remote_table returns null for computed tables", {
22-
mf <- copy_to(
23-
test_sqlite(),
24-
tibble(x = 5, y = 1),
25-
name = "refxiudlph",
26-
overwrite = TRUE
27-
)
17+
mf <- local_memdb_frame("refxiudlph", x = 5, y = 1)
2818
expect_equal(remote_table(mf), table_path("`refxiudlph`"))
2919

3020
expect_null(mf |> filter(x == 3) |> remote_table())

tests/testthat/test-sql-build.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
test_that("rendering table wraps in SELECT *", {
2-
out <- copy_to(
3-
test_sqlite(),
4-
tibble(x = 1),
5-
name = "test-sql-build",
6-
overwrite = TRUE
7-
)
2+
out <- local_memdb_frame("test-sql-build", x = 1)
83
expect_snapshot(out |> sql_render())
94
expect_equal(out |> collect(), tibble(x = 1))
105
})

tests/testthat/test-tbl-sql.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ test_that("same_src distinguishes srcs", {
2525
})
2626

2727
test_that("has nice print method", {
28-
mf <- copy_to(
29-
test_sqlite(),
30-
tibble(x = 1, y = 1),
31-
name = "tbl_sum_test",
32-
overwrite = TRUE
33-
)
28+
mf <- local_memdb_frame("tbl_sum_test", x = 1, y = 1)
3429
expect_snapshot(mf, transform = scrub_sqlite_version)
3530

3631
out2 <- mf |> group_by(x, y) |> arrange(x) |> mutate(z = x + y)

tests/testthat/test-verb-arrange.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ test_that("correctly inlines across all verbs", {
3030
# sql_render --------------------------------------------------------------
3131

3232
test_that("quoting for rendering ordered grouped table", {
33-
db <- copy_to(
34-
test_sqlite(),
35-
tibble(x = 1, y = 2),
36-
name = "test-verb-arrange",
37-
overwrite = TRUE
38-
)
33+
db <- local_memdb_frame("test-verb-arrange", x = 1, y = 2)
3934
out <- db |> group_by(x) |> arrange(y) |> ungroup()
4035
expect_snapshot(sql_render(out))
4136
expect_equal(collect(out), tibble(x = 1, y = 2))

tests/testthat/test-verb-mutate.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ test_that("can refer to fresly created values", {
6767
collect()
6868
expect_equal(out1, tibble(x1 = 1, x2 = 2, x3 = 3, x4 = 4))
6969

70-
out2 <- copy_to(
71-
test_sqlite(),
72-
tibble(x = 1),
73-
name = "multi_mutate",
74-
overwrite = TRUE
75-
) |>
70+
out2 <- local_memdb_frame("multi_mutate", x = 1) |>
7671
mutate(x = x + 1, x = x + 2, x = x + 4)
7772
expect_equal(collect(out2), tibble(x = 8))
7873
expect_snapshot(show_query(out2))

tests/testthat/test-verb-summarise.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,7 @@ test_that("catches `.by` with grouped-df", {
195195
# sql-render --------------------------------------------------------------
196196

197197
test_that("quoting for rendering summarized grouped table", {
198-
out <- copy_to(
199-
test_sqlite(),
200-
tibble(x = 1),
201-
name = "verb-summarise",
202-
overwrite = TRUE
203-
) |>
198+
out <- local_memdb_frame("verb-summarise", x = 1) |>
204199
group_by(x) |>
205200
summarise(n = n())
206201
expect_snapshot(out |> sql_render())

0 commit comments

Comments
 (0)