tests/testthat/test-write-vtr-node.R

test_that("write_vtr streams VTR -> VTR", {
  f <- tempfile(fileext = ".vtr")
  f2 <- tempfile(fileext = ".vtr")
  on.exit(unlink(c(f, f2)))
  write_vtr(mtcars, f)
  tbl(f) |> write_vtr(f2)
  result <- tbl(f2) |> collect()
  expect_equal(nrow(result), 32L)
  expect_equal(ncol(result), 11L)
  expect_equal(result$mpg, mtcars$mpg)
})

test_that("write_vtr streams CSV -> VTR", {
  csv <- tempfile(fileext = ".csv")
  f <- tempfile(fileext = ".vtr")
  on.exit(unlink(c(csv, f)))
  write.csv(mtcars, csv, row.names = FALSE)
  tbl_csv(csv) |> write_vtr(f)
  result <- tbl(f) |> collect()
  expect_equal(nrow(result), 32L)
  expect_equal(result$mpg, mtcars$mpg)
})

test_that("write_vtr streams SQLite -> VTR", {
  f <- tempfile(fileext = ".vtr")
  db <- tempfile(fileext = ".sqlite")
  f2 <- tempfile(fileext = ".vtr")
  on.exit(unlink(c(f, db, f2)))
  write_vtr(mtcars, f)
  tbl(f) |> write_sqlite(db, "cars")
  tbl_sqlite(db, "cars") |> write_vtr(f2)
  result <- tbl(f2) |> collect()
  expect_equal(nrow(result), 32L)
})

test_that("write_vtr streams filtered node", {
  f <- tempfile(fileext = ".vtr")
  f2 <- tempfile(fileext = ".vtr")
  on.exit(unlink(c(f, f2)))
  write_vtr(mtcars, f)
  tbl(f) |> filter(mpg > 20) |> write_vtr(f2)
  result <- tbl(f2) |> collect()
  expect_equal(nrow(result), sum(mtcars$mpg > 20))
})

test_that("write_vtr streams mutated node", {
  f <- tempfile(fileext = ".vtr")
  f2 <- tempfile(fileext = ".vtr")
  on.exit(unlink(c(f, f2)))
  write_vtr(mtcars, f)
  tbl(f) |> mutate(kpl = mpg * 0.425144) |> write_vtr(f2)
  result <- tbl(f2) |> collect()
  expect_equal(ncol(result), 12L)
  expect_true("kpl" %in% names(result))
})

test_that("write_vtr atomic write leaves no temp file", {
  f <- tempfile(fileext = ".vtr")
  f2 <- tempfile(fileext = ".vtr")
  on.exit(unlink(c(f, f2)))
  write_vtr(mtcars, f)
  tbl(f) |> write_vtr(f2)
  expect_false(file.exists(paste0(f2, ".~writing")))
  expect_true(file.exists(f2))
})

test_that("write_vtr.data.frame still works", {
  f <- tempfile(fileext = ".vtr")
  on.exit(unlink(f))
  write_vtr(iris, f)
  result <- tbl(f) |> collect()
  expect_equal(nrow(result), 150L)
  expect_equal(ncol(result), 5L)
})

Try the vectra package in your browser

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

vectra documentation built on May 8, 2026, 9:06 a.m.