tests/testthat/test-as_tbl.R

test_that("as_tbl()", {
  skip_if_not_installed("dbplyr")

  df <- duckdb_tibble(a = 1L)

  tbl <- as_tbl(df)
  expect_s3_class(tbl, "tbl_dbi")
  expect_equal(collect(tbl), tibble(a = 1L))

  sql <- dbplyr::sql_render(tbl)

  out <-
    tbl %>%
    mutate(b = sql("a + 1")) %>%
    as_duckdb_tibble()

  expect_s3_class(out, "duckplyr_df")
  expect_equal(head(out), duckdb_tibble(a = 1L, b = 2L))

  # Test destruction of underlying view
  expect_equal(DBI::dbGetQuery(get_default_duckdb_connection(), sql), data.frame(a = 1L))

  rm(tbl)
  gc()

  expect_error(DBI::dbGetQuery(get_default_duckdb_connection(), sql))
  # This is expected but perhaps undesirable.
  # We could carry over the duckplyr_scope_guard to the relational object.
  expect_error(collect(out))
})

Try the duckplyr package in your browser

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

duckplyr documentation built on June 8, 2025, 10:53 a.m.