tests/testthat/test_f_split_by.R

test_that("write_vc() handles the split_by argument", {
  root <- tempfile(pattern = "git2rdata-split-by")
  dir.create(root)

  expect_warning(
    write_vc(
      test_data, file = "unsorted", root = root, split_by = "test_factor"
    ),
    "No sorting applied."
  )
  expect_is({
    z <- read_vc("unsorted", root)
    },
    "data.frame"
  )
  expect_equal(
    z[order(z$test_numeric), ],
    test_data[order(test_data$test_numeric), ],
    check.attributes = FALSE
  )

  expect_is({
    sorted_file <- write_vc(
      test_data, file = "sorted", root = root,
      sorting = "test_Date", split_by = "test_factor"
    )
  },
    "character"
  )

  expect_is({
    z <- read_vc(sorted_file[1], root)
    },
    "data.frame"
  )
  expect_equal(
    z,
    test_data[order(test_data$test_factor, test_data$test_Date), ],
    check.attributes = FALSE
  )

  expect_error(
    write_vc(
      test_data, file = "sorted", root = root, split_by = character(0)
    ),
    "The split_by variables changed."
  )
  expect_warning(
    write_vc(
      test_data, file = "sorted", root = root, split_by = character(0),
      strict = FALSE
    ),
    "The split_by variables changed."
  )
  expect_error(
    write_vc(
      test_data, file = "sorted", root = root, split_by = "test_factor"
    ),
    "The split_by variables changed."
  )
  expect_warning(
    write_vc(
      test_data, file = "sorted", root = root, split_by = "test_factor",
      strict = FALSE
    ),
    "The split_by variables changed."
  )

  data_file <- list.files(
    file.path(root, sorted_file[1]), pattern = "[[:xdigit:]]{20}",
    full.names = TRUE
  )
  data_file <- sample(data_file, 1)
  raw_data <- readLines(data_file)
  writeLines(raw_data[-1], data_file)
  expect_warning(
    is_git2rdata("sorted", root, "warning"),
    "Corrupt data, incorrect header"
  )
  expect_error(
    is_git2rdata("sorted", root, "error"),
    "Corrupt data, incorrect header"
  )
  expect_false(
    suppressWarnings(is_git2rdata("sorted", root, "warning")),
    "Corrupt data, incorrect header"
  )

  index_file <- file.path(root, sorted_file[1], "index.tsv")
  index <- readLines(index_file)
  writeLines(index[-1], index_file)
  expect_warning(
    is_git2rdata("sorted", root, "warning"),
    "Corrupt data, incorrect header in index.tsv"
  )
  expect_error(
    is_git2rdata("sorted", root, "error"),
    "Corrupt data, incorrect header in index.tsv"
  )
  expect_false(
    suppressWarnings(is_git2rdata("sorted", root, "warning")),
    "Corrupt data, incorrect header in index.tsv"
  )
})

Try the git2rdata package in your browser

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

git2rdata documentation built on Sept. 11, 2024, 5:51 p.m.