REDCapCAST/tests/testthat/test-forms-arg.R

57 lines
1.5 KiB
R
Raw Permalink Normal View History

2023-01-16 09:49:17 +01:00
2023-04-13 10:57:04 +02:00
# Global variables --------------------------------------------------------
# Cars
2023-01-16 09:49:17 +01:00
metadata <-
jsonlite::fromJSON(get_data_location("ExampleProject_metadata.json"))
2023-01-16 09:49:17 +01:00
records <-
jsonlite::fromJSON(get_data_location("ExampleProject_records.json"))
redcap_output_json <- REDCap_split(records, metadata, forms = "all")
# Longitudinal
2023-04-13 10:57:04 +02:00
file_paths <- vapply(
2023-01-16 09:49:17 +01:00
c(records = "WARRIORtestForSoftwa_DATA_2018-06-21_1431.csv",
metadata = "WARRIORtestForSoftwareUpgrades_DataDictionary_2018-06-21.csv"),
2023-04-13 10:57:04 +02:00
FUN.VALUE = "character",
2023-01-16 09:49:17 +01:00
get_data_location
)
redcap <- lapply(file_paths, read.csv, stringsAsFactors = FALSE)
2023-04-13 10:57:04 +02:00
redcap[["metadata"]] <- with(redcap, metadata[metadata[, 1] > "", ])
2023-01-16 09:49:17 +01:00
redcap_output_long <-
with(redcap, REDCap_split(records, metadata, forms = "all"))
redcap_long_names <- names(redcap[[1]])
# Tests -------------------------------------------------------------------
test_that("Each form is an element in the list", {
expect_length(redcap_output_json, 3L)
2023-01-16 09:49:17 +01:00
expect_identical(names(redcap_output_json),
c("motor_trend_cars", "grouping", "sale"))
})
test_that("All variables land somewhere", {
2023-01-16 09:49:17 +01:00
expect_true(setequal(names(records), Reduce(
2023-04-13 10:57:04 +02:00
"union", lapply(redcap_output_json, names)
2023-01-16 09:49:17 +01:00
)))
})
test_that("Primary table name is ignored", {
2023-01-16 09:49:17 +01:00
expect_identical(REDCap_split(records, metadata, "HELLO", "all"),
redcap_output_json)
})
test_that("Supports longitudinal data", {
2023-01-16 09:49:17 +01:00
expect_true(setequal(redcap_long_names, Reduce(
2023-04-13 10:57:04 +02:00
"union", lapply(redcap_output_long, names)
2023-01-16 09:49:17 +01:00
)))
})