REDCapCAST/tests/testthat/test-csv-exports.R

56 lines
1.9 KiB
R
Raw Normal View History

# Set up the path and data -------------------------------------------------
metadata <- read.csv(
get_data_location("ExampleProject_DataDictionary_2018-06-07.csv"),
stringsAsFactors = TRUE
)
2023-01-16 09:49:17 +01:00
records <- read.csv(get_data_location("ExampleProject_DATA_2018-06-07_1129.csv"),
stringsAsFactors = TRUE)
redcap_output_csv1 <- REDCap_split(records, metadata)
# Test that basic CSV export matches reference ------------------------------
test_that("CSV export matches reference", {
expect_known_hash(redcap_output_csv1, "f74558d1939c17d9ff0e08a19b956e26")
})
# Test that R code enhanced CSV export matches reference --------------------
if (requireNamespace("Hmisc", quietly = TRUE)) {
test_that("R code enhanced export matches reference", {
2023-01-16 09:49:17 +01:00
redcap_output_csv2 <-
REDCap_split(REDCap_process_csv(records), metadata)
2023-01-16 09:49:17 +01:00
expect_known_hash(redcap_output_csv2, "34f82cab35bf8aae47d08cd96f743e6b")
})
}
if (requireNamespace("readr", quietly = TRUE)) {
context("Compatibility with readr")
2023-01-16 09:49:17 +01:00
metadata <- readr::read_csv(get_data_location("ExampleProject_DataDictionary_2018-06-07.csv"))
2023-01-16 09:49:17 +01:00
records <- readr::read_csv(get_data_location("ExampleProject_DATA_2018-06-07_1129.csv"))
redcap_output_readr <- REDCap_split(records, metadata)
expect_matching_elements <- function(FUN) {
FUN <- match.fun(FUN)
2023-01-16 09:49:17 +01:00
expect_identical(lapply(redcap_output_readr, FUN),
lapply(redcap_output_csv1, FUN))
}
2023-01-16 09:49:17 +01:00
test_that("Result of data read in with `readr` will match result with `read.csv`",
{
# The list itself
expect_identical(length(redcap_output_readr), length(redcap_output_csv1))
expect_identical(names(redcap_output_readr), names(redcap_output_csv1))
2023-01-16 09:49:17 +01:00
# Each element of the list
expect_matching_elements(names)
expect_matching_elements(dim)
})
}