From 7dda3385f76a83a8a96907b76aa0e7f04796afa3 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Thu, 4 Nov 2021 21:23:02 -0500 Subject: [PATCH] test repeating instruments w/ sparse records ref #143 --- inst/misc/example.credentials | 1 + .../batch-size-1.R | 14 ++ .../batch-size-3.R | 14 ++ .../batch-szie-2.R | 14 ++ .../read-batch-repeating-sparse/default.R | 14 ++ .../test-read-batch-repeating-sparse.R | 129 ++++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-1.R create mode 100644 inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-3.R create mode 100644 inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-szie-2.R create mode 100644 inst/test-data/specific-redcapr/read-batch-repeating-sparse/default.R create mode 100644 tests/testthat/test-read-batch-repeating-sparse.R diff --git a/inst/misc/example.credentials b/inst/misc/example.credentials index e14a06ac..936c1913 100644 --- a/inst/misc/example.credentials +++ b/inst/misc/example.credentials @@ -23,3 +23,4 @@ redcap_uri,username,project_id,token,comment "https://bbmc.ouhsc.edu/redcap/api/","myusername","2545","0BF11B9CB01F0B8F8EE203B7E07DEFD9","DAG Write" "https://bbmc.ouhsc.edu/redcap/api/","myusername","2593","1C31398F332FCACA4C0A7B93B18D5CD4","super-wide #2--5,785 columns" "https://bbmc.ouhsc.edu/redcap/api/","myusername","2597","5C1526186C4D04AE0A0630743E69B53C","super-wide #3--35,000 columns" +"https://bbmc.ouhsc.edu/redcap/api/","myusername","2603","56F43A10D01D6578A46393394D76D88F","Repeating Instruments --Sparse" diff --git a/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-1.R b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-1.R new file mode 100644 index 00000000..3a1c25a4 --- /dev/null +++ b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-1.R @@ -0,0 +1,14 @@ +structure(list(record_id = c(1, 1, 1, 1, 2, 2, 3, 4, 5), redcap_repeat_instrument = c(NA, +"bp", "bp", "bp", NA, "bp", NA, NA, NA), redcap_repeat_instance = c(NA, +1, 2, 3, NA, 1, NA, NA, NA), date_enrolled = structure(c(18183, +NA, NA, NA, 17929, NA, 18935, 18935, NA), class = "Date"), first_name = c("aaa", +NA, NA, NA, "bbb", NA, "empty 3", "empty 4", "empty 5"), dob = structure(c(14896, +NA, NA, NA, 14642, NA, NA, NA, NA), class = "Date"), age = c(8.9, +NA, NA, NA, 9.6, NA, NA, NA, NA), ethnicity = c(1, NA, NA, NA, +0, NA, NA, NA, NA), race = c(4, NA, NA, NA, 6, NA, NA, NA, NA +), sex = c(0, NA, NA, NA, 1, NA, NA, NA, NA), demographics_complete = c(2, +NA, NA, NA, 2, NA, 0, 0, 0), date_bp = structure(c(NA, 18183, +18183, 18183, NA, 12512, NA, NA, NA), class = "Date"), bp_systolic = c(NA, +110, 111, 112, NA, 114, NA, NA, NA), bp_diastolic = c(NA, 100, +101, 102, NA, 104, NA, NA, NA), bp_complete = c(NA, 2, 2, 2, +NA, 2, NA, NA, NA)), row.names = c(NA, -9L), class = "data.frame") diff --git a/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-3.R b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-3.R new file mode 100644 index 00000000..3a1c25a4 --- /dev/null +++ b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-3.R @@ -0,0 +1,14 @@ +structure(list(record_id = c(1, 1, 1, 1, 2, 2, 3, 4, 5), redcap_repeat_instrument = c(NA, +"bp", "bp", "bp", NA, "bp", NA, NA, NA), redcap_repeat_instance = c(NA, +1, 2, 3, NA, 1, NA, NA, NA), date_enrolled = structure(c(18183, +NA, NA, NA, 17929, NA, 18935, 18935, NA), class = "Date"), first_name = c("aaa", +NA, NA, NA, "bbb", NA, "empty 3", "empty 4", "empty 5"), dob = structure(c(14896, +NA, NA, NA, 14642, NA, NA, NA, NA), class = "Date"), age = c(8.9, +NA, NA, NA, 9.6, NA, NA, NA, NA), ethnicity = c(1, NA, NA, NA, +0, NA, NA, NA, NA), race = c(4, NA, NA, NA, 6, NA, NA, NA, NA +), sex = c(0, NA, NA, NA, 1, NA, NA, NA, NA), demographics_complete = c(2, +NA, NA, NA, 2, NA, 0, 0, 0), date_bp = structure(c(NA, 18183, +18183, 18183, NA, 12512, NA, NA, NA), class = "Date"), bp_systolic = c(NA, +110, 111, 112, NA, 114, NA, NA, NA), bp_diastolic = c(NA, 100, +101, 102, NA, 104, NA, NA, NA), bp_complete = c(NA, 2, 2, 2, +NA, 2, NA, NA, NA)), row.names = c(NA, -9L), class = "data.frame") diff --git a/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-szie-2.R b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-szie-2.R new file mode 100644 index 00000000..3a1c25a4 --- /dev/null +++ b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/batch-szie-2.R @@ -0,0 +1,14 @@ +structure(list(record_id = c(1, 1, 1, 1, 2, 2, 3, 4, 5), redcap_repeat_instrument = c(NA, +"bp", "bp", "bp", NA, "bp", NA, NA, NA), redcap_repeat_instance = c(NA, +1, 2, 3, NA, 1, NA, NA, NA), date_enrolled = structure(c(18183, +NA, NA, NA, 17929, NA, 18935, 18935, NA), class = "Date"), first_name = c("aaa", +NA, NA, NA, "bbb", NA, "empty 3", "empty 4", "empty 5"), dob = structure(c(14896, +NA, NA, NA, 14642, NA, NA, NA, NA), class = "Date"), age = c(8.9, +NA, NA, NA, 9.6, NA, NA, NA, NA), ethnicity = c(1, NA, NA, NA, +0, NA, NA, NA, NA), race = c(4, NA, NA, NA, 6, NA, NA, NA, NA +), sex = c(0, NA, NA, NA, 1, NA, NA, NA, NA), demographics_complete = c(2, +NA, NA, NA, 2, NA, 0, 0, 0), date_bp = structure(c(NA, 18183, +18183, 18183, NA, 12512, NA, NA, NA), class = "Date"), bp_systolic = c(NA, +110, 111, 112, NA, 114, NA, NA, NA), bp_diastolic = c(NA, 100, +101, 102, NA, 104, NA, NA, NA), bp_complete = c(NA, 2, 2, 2, +NA, 2, NA, NA, NA)), row.names = c(NA, -9L), class = "data.frame") diff --git a/inst/test-data/specific-redcapr/read-batch-repeating-sparse/default.R b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/default.R new file mode 100644 index 00000000..3a1c25a4 --- /dev/null +++ b/inst/test-data/specific-redcapr/read-batch-repeating-sparse/default.R @@ -0,0 +1,14 @@ +structure(list(record_id = c(1, 1, 1, 1, 2, 2, 3, 4, 5), redcap_repeat_instrument = c(NA, +"bp", "bp", "bp", NA, "bp", NA, NA, NA), redcap_repeat_instance = c(NA, +1, 2, 3, NA, 1, NA, NA, NA), date_enrolled = structure(c(18183, +NA, NA, NA, 17929, NA, 18935, 18935, NA), class = "Date"), first_name = c("aaa", +NA, NA, NA, "bbb", NA, "empty 3", "empty 4", "empty 5"), dob = structure(c(14896, +NA, NA, NA, 14642, NA, NA, NA, NA), class = "Date"), age = c(8.9, +NA, NA, NA, 9.6, NA, NA, NA, NA), ethnicity = c(1, NA, NA, NA, +0, NA, NA, NA, NA), race = c(4, NA, NA, NA, 6, NA, NA, NA, NA +), sex = c(0, NA, NA, NA, 1, NA, NA, NA, NA), demographics_complete = c(2, +NA, NA, NA, 2, NA, 0, 0, 0), date_bp = structure(c(NA, 18183, +18183, 18183, NA, 12512, NA, NA, NA), class = "Date"), bp_systolic = c(NA, +110, 111, 112, NA, 114, NA, NA, NA), bp_diastolic = c(NA, 100, +101, 102, NA, 104, NA, NA, NA), bp_complete = c(NA, 2, 2, 2, +NA, 2, NA, NA, NA)), row.names = c(NA, -9L), class = "data.frame") diff --git a/tests/testthat/test-read-batch-repeating-sparse.R b/tests/testthat/test-read-batch-repeating-sparse.R new file mode 100644 index 00000000..c1d4b739 --- /dev/null +++ b/tests/testthat/test-read-batch-repeating-sparse.R @@ -0,0 +1,129 @@ +library(testthat) + +credential <- retrieve_credential_testing(project_id = 2603L) +update_expectation <- FALSE + +test_that("smoke test", { + testthat::skip_on_cran() + expect_message( + returned_object <- + redcap_read( + redcap_uri = credential$redcap_uri, + token = credential$token + ) + ) +}) + +test_that("default", { + testthat::skip_on_cran() + path_expected <- "test-data/specific-redcapr/read-batch-repeating-sparse/default.R" + expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." + + expect_message( + regexp = expected_outcome_message, + returned_object <- + redcap_read( + redcap_uri = credential$redcap_uri, + token = credential$token#, + # batch_size = 1 + ) + ) + + if (update_expectation) save_expected(returned_object$data, path_expected) + expected_data_frame <- retrieve_expected(path_expected) + + expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data) + expect_equal(returned_object$status_code, expected="200") + expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.") + expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.") + expect_true(returned_object$filter_logic=="", "A filter was not specified.") + expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) + expect_true(returned_object$success) +}) + + +test_that("batch size 3", { + testthat::skip_on_cran() + path_expected <- "test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-3.R" + expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." + + expect_message( + regexp = expected_outcome_message, + returned_object <- + redcap_read( + redcap_uri = credential$redcap_uri, + token = credential$token, + batch_size = 3 + ) + ) + + if (update_expectation) save_expected(returned_object$data, path_expected) + expected_data_frame <- retrieve_expected(path_expected) + + expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data) + expect_equal(returned_object$status_code, expected="200; 200") + expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.") + expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.") + expect_true(returned_object$filter_logic=="", "A filter was not specified.") + expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) + expect_true(returned_object$success) +}) + + +test_that("batch size 2", { + testthat::skip_on_cran() + path_expected <- "test-data/specific-redcapr/read-batch-repeating-sparse/batch-szie-2.R" + expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." + + expect_message( + regexp = expected_outcome_message, + returned_object <- + redcap_read( + redcap_uri = credential$redcap_uri, + token = credential$token, + batch_size = 2 + ) + ) + + if (update_expectation) save_expected(returned_object$data, path_expected) + expected_data_frame <- retrieve_expected(path_expected) + + expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data) + expect_equal(returned_object$status_code, expected="200; 200; 200") + expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.") + expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.") + expect_true(returned_object$filter_logic=="", "A filter was not specified.") + expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) + expect_true(returned_object$success) +}) + + +test_that("batch size 1", { + testthat::skip_on_cran() + path_expected <- "test-data/specific-redcapr/read-batch-repeating-sparse/batch-size-1.R" + expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." + + expect_message( + regexp = expected_outcome_message, + returned_object <- + redcap_read( + redcap_uri = credential$redcap_uri, + token = credential$token, + batch_size = 1 + ) + ) + + if (update_expectation) save_expected(returned_object$data, path_expected) + expected_data_frame <- retrieve_expected(path_expected) + + expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data) + expect_equal(returned_object$status_code, expected="200; 200; 200; 200; 200") + expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.") + expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.") + expect_true(returned_object$filter_logic=="", "A filter was not specified.") + expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) + expect_true(returned_object$success) +}) + +rm(credential) +