Skip to content

Commit 9aaac0b

Browse files
committed
tests for raw_or_label_headers
ref #183 & #203, initiated by @hatemhosny
1 parent d28b68c commit 9aaac0b

7 files changed

+199
-29
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Minor New Features:
1414
* `redcap_read()`, `redcap_read_oneshot()`, and `redcap_read_eav()` always return Linux-style line endings (ie `\n`) instead of Windows style line endings (ie, `\r\n`) on all OSes. (#198)
1515
* Link to new secure Zenodo DOI resolver (katrinleinweber #191)
1616
* `read_metadata()` always returns `character` vectors for all variables. With readr 1.2.0, some column were returned differently than before. (#193)
17+
* 'raw_orLabel_headers' now supprted (Thanks Hatem Hosny - hatemhosny, #183 & #203)
1718

1819
Modified Internals:
1920
* When converting REDCap's CSV to R's data.frame, `readr::read_csv()` is used instead of `utils::read.csv()` (Issue #127).

R/redcap-read-oneshot-eav.R

+1-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ redcap_read_oneshot_eav <- function(
115115
# forms
116116
# events
117117
checkmate::assert_subset( raw_or_label , c("raw", "label"))
118-
# raw_or_label_headers
118+
checkmate::assert_subset( raw_or_label_headers , c("raw", "label"))
119119
# exportCheckboxLabel
120120
# returnFormat
121121
# export_survey_fields
@@ -127,10 +127,6 @@ redcap_read_oneshot_eav <- function(
127127
# config_options
128128
# id_position
129129

130-
# TODO: convert this to checkmate::assert_subset
131-
if( !(raw_or_label_headers %in% c("raw", "label")) )
132-
stop("The optional parameter `raw_or_label_headers` must be either 'raw' or 'label'.")
133-
134130
token <- sanitize_token(token)
135131
validate_field_names(fields)
136132

R/redcap-read-oneshot.R

+1-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ redcap_read_oneshot <- function(
104104
# forms
105105
# events
106106
checkmate::assert_subset( raw_or_label , c("raw", "label"))
107-
# raw_or_label_headers
107+
checkmate::assert_subset( raw_or_label_headers , c("raw", "label"))
108108
# exportCheckboxLabel
109109
# returnFormat
110110
checkmate::assert_logical(export_survey_fields, any.missing=F, len=1)
@@ -116,10 +116,6 @@ redcap_read_oneshot <- function(
116116
# verbose
117117
# config_options
118118

119-
# TODO: convert this to checkmate::assert_subset
120-
if( !(raw_or_label_headers %in% c("raw", "label")) )
121-
stop("The optional parameter `raw_or_label_headers` must be either 'raw' or 'label'.")
122-
123119
token <- sanitize_token(token)
124120
validate_field_names(fields)
125121

R/redcap-read.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ redcap_read <- function(
102102
# forms
103103
# events
104104
checkmate::assert_subset( raw_or_label , c("raw", "label"))
105-
# raw_or_label_headers
105+
checkmate::assert_subset( raw_or_label_headers , c("raw", "label"))
106106
# exportCheckboxLabel
107107
# returnFormat
108108
# export_survey_fields

tests/testthat/test-read-batch-simple.R

+88
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,94 @@ test_that("All Records -label", {
401401
expect_match(returned_object2$outcome_messages, regexp=expected_outcome_message, perl=TRUE)
402402
})
403403

404+
test_that("All Records -label header", {
405+
testthat::skip_on_cran()
406+
expected_data_frame <- structure(list(`Study ID` = c(1, 2, 3, 4, 5), `First Name` = c("Nutmeg",
407+
"Tumtum", "Marcus", "Trudy", "John Lee"), `Last Name` = c("Nutmouse",
408+
"Nutmouse", "Wood", "DAG", "Walker"), `Street, City, State, ZIP` = c("14 Rose Cottage St.\nKenning UK, 323232",
409+
"14 Rose Cottage Blvd.\nKenning UK 34243", "243 Hill St.\nGuthrie OK 73402",
410+
"342 Elm\nDuncanville TX, 75116", "Hotel Suite\nNew Orleans LA, 70115"
411+
), `Phone number` = c("(405) 321-1111", "(405) 321-2222", "(405) 321-3333",
412+
"(405) 321-4444", "(405) 321-5555"), `E-mail` = c("[email protected]",
413+
414+
), `Date of birth` = structure(c(12294, 12121, -13051, -6269,
415+
-5375), class = "Date"), `Age (years)` = c(11, 11, 80, 61, 59
416+
), Gender = c(0, 1, 1, 0, 1), `Complete?` = c(2, 2, 2, 2, 2),
417+
`Height (cm)` = c(7, 6, 180, 165, 193.04), `Weight (kilograms)` = c(1,
418+
1, 80, 54, 104), BMI = c(204.1, 277.8, 24.7, 19.8, 27.9),
419+
Comments = c("Character in a book, with some guessing", "A mouse character from a good book",
420+
"completely made up", "This record doesn't have a DAG assigned\n\nSo call up Trudy on the telephone\nSend her a letter in the mail",
421+
"Had a hand for trouble and a eye for cash\n\nHe had a gold watch chain and a black mustache"
422+
), Mugshot = c("[document]", "[document]", "[document]",
423+
"[document]", "[document]"), `Complete?_1` = c(1, 0, 2, 2,
424+
0), `Race (Select all that apply) (choice=American Indian/Alaska Native)` = c(0,
425+
0, 0, 0, 1), `Race (Select all that apply) (choice=Asian)` = c(0,
426+
0, 0, 1, 0), `Race (Select all that apply) (choice=Native Hawaiian or Other Pacific Islander)` = c(0,
427+
1, 0, 0, 0), `Race (Select all that apply) (choice=Black or African American)` = c(0,
428+
0, 1, 0, 0), `Race (Select all that apply) (choice=White)` = c(1,
429+
1, 1, 1, 0), `Race (Select all that apply) (choice=Unknown / Not Reported)` = c(0,
430+
0, 0, 0, 1), Ethnicity = c(1, 1, 0, 1, 2), `Complete?_2` = c(2,
431+
0, 2, 2, 2)), .Names = c("Study ID", "First Name", "Last Name",
432+
"Street, City, State, ZIP", "Phone number", "E-mail", "Date of birth",
433+
"Age (years)", "Gender", "Complete?", "Height (cm)", "Weight (kilograms)",
434+
"BMI", "Comments", "Mugshot", "Complete?_1", "Race (Select all that apply) (choice=American Indian/Alaska Native)",
435+
"Race (Select all that apply) (choice=Asian)", "Race (Select all that apply) (choice=Native Hawaiian or Other Pacific Islander)",
436+
"Race (Select all that apply) (choice=Black or African American)",
437+
"Race (Select all that apply) (choice=White)", "Race (Select all that apply) (choice=Unknown / Not Reported)",
438+
"Ethnicity", "Complete?_2"), row.names = c(NA, -5L), class = "data.frame", spec = structure(list(
439+
cols = structure(list(`Study ID` = structure(list(), class = c("collector_double",
440+
"collector")), `First Name` = structure(list(), class = c("collector_character",
441+
"collector")), `Last Name` = structure(list(), class = c("collector_character",
442+
"collector")), `Street, City, State, ZIP` = structure(list(), class = c("collector_character",
443+
"collector")), `Phone number` = structure(list(), class = c("collector_character",
444+
"collector")), `E-mail` = structure(list(), class = c("collector_character",
445+
"collector")), `Date of birth` = structure(list(format = ""), .Names = "format", class = c("collector_date",
446+
"collector")), `Age (years)` = structure(list(), class = c("collector_double",
447+
"collector")), Gender = structure(list(), class = c("collector_double",
448+
"collector")), `Complete?` = structure(list(), class = c("collector_double",
449+
"collector")), `Height (cm)` = structure(list(), class = c("collector_double",
450+
"collector")), `Weight (kilograms)` = structure(list(), class = c("collector_double",
451+
"collector")), BMI = structure(list(), class = c("collector_double",
452+
"collector")), Comments = structure(list(), class = c("collector_character",
453+
"collector")), Mugshot = structure(list(), class = c("collector_character",
454+
"collector")), `Complete?_1` = structure(list(), class = c("collector_double",
455+
"collector")), `Race (Select all that apply) (choice=American Indian/Alaska Native)` = structure(list(), class = c("collector_double",
456+
"collector")), `Race (Select all that apply) (choice=Asian)` = structure(list(), class = c("collector_double",
457+
"collector")), `Race (Select all that apply) (choice=Native Hawaiian or Other Pacific Islander)` = structure(list(), class = c("collector_double",
458+
"collector")), `Race (Select all that apply) (choice=Black or African American)` = structure(list(), class = c("collector_double",
459+
"collector")), `Race (Select all that apply) (choice=White)` = structure(list(), class = c("collector_double",
460+
"collector")), `Race (Select all that apply) (choice=Unknown / Not Reported)` = structure(list(), class = c("collector_double",
461+
"collector")), Ethnicity = structure(list(), class = c("collector_double",
462+
"collector")), `Complete?_2` = structure(list(), class = c("collector_double",
463+
"collector"))), .Names = c("Study ID", "First Name", "Last Name",
464+
"Street, City, State, ZIP", "Phone number", "E-mail", "Date of birth",
465+
"Age (years)", "Gender", "Complete?", "Height (cm)", "Weight (kilograms)",
466+
"BMI", "Comments", "Mugshot", "Complete?_1", "Race (Select all that apply) (choice=American Indian/Alaska Native)",
467+
"Race (Select all that apply) (choice=Asian)", "Race (Select all that apply) (choice=Native Hawaiian or Other Pacific Islander)",
468+
"Race (Select all that apply) (choice=Black or African American)",
469+
"Race (Select all that apply) (choice=White)", "Race (Select all that apply) (choice=Unknown / Not Reported)",
470+
"Ethnicity", "Complete?_2")), default = structure(list(), class = c("collector_guess",
471+
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
472+
473+
expected_warning <- "Duplicated column names deduplicated: 'Complete\\?' => 'Complete\\?_1' \\[16\\], 'Complete\\?' => 'Complete\\?_2' \\[24\\]"
474+
expected_outcome_message <- "5 records and 24 columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."
475+
476+
expect_warning(
477+
regexp = expected_warning,
478+
expect_message(
479+
regexp = expected_outcome_message,
480+
returned_object <- redcap_read(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label_headers="label")
481+
)
482+
)
483+
484+
expect_equivalent(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct") # dput(returned_object$data)
485+
expect_match(returned_object$status_codes, regexp="200", perl=TRUE)
486+
expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.")
487+
expect_true(returned_object$fields_collapsed=="", "A subset of fields was not requested.")
488+
expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
489+
expect_true(returned_object$success)
490+
})
491+
404492
test_that("Filter - numeric", {
405493
testthat::skip_on_cran()
406494
expected_data_frame <- structure(

tests/testthat/test-read-oneshot-eav.R

+18-18
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ test_that("All Records -label and DAG", {
207207
expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
208208
expect_true(returned_object$success)
209209
})
210-
test_that("All Records -label", {
210+
211+
test_that("All Records -label header", {
211212
testthat::skip_on_cran()
212213
expected_data_frame <- structure(list(record_id = 1:5, name_first = c("Nutmeg", "Tumtum",
213214
"Marcus", "Trudy", "John Lee"), name_last = c("Nutmouse", "Nutmouse",
@@ -218,22 +219,20 @@ test_that("All Records -label", {
218219
"(405) 321-4444", "(405) 321-5555"), email = c("[email protected]",
219220
220221
), dob = c("2003-08-30", "2003-03-10", "1934-04-09", "1952-11-02",
221-
"1955-04-15"), age = c(11L, 11L, 80L, 61L, 59L), sex = c("Female",
222-
"Male", "Male", "Female", "Male"), height = c(7, 6, 180, 165,
223-
193.04), weight = c(1L, 1L, 80L, 54L, 104L), bmi = c(204.1, 277.8,
224-
24.7, 19.8, 27.9), comments = c("Character in a book, with some guessing",
225-
"A mouse character from a good book", "completely made up", "This record doesn't have a DAG assigned\n\nSo call up Trudy on the telephone\nSend her a letter in the mail",
222+
"1955-04-15"), age = c(11L, 11L, 80L, 61L, 59L), sex = c(0L,
223+
1L, 1L, 0L, 1L), height = c(7, 6, 180, 165, 193.04), weight = c(1L,
224+
1L, 80L, 54L, 104L), bmi = c(204.1, 277.8, 24.7, 19.8, 27.9),
225+
comments = c("Character in a book, with some guessing", "A mouse character from a good book",
226+
"completely made up", "This record doesn't have a DAG assigned\n\nSo call up Trudy on the telephone\nSend her a letter in the mail",
226227
"Had a hand for trouble and a eye for cash\n\nHe had a gold watch chain and a black mustache"
227-
), mugshot = 7610:7614, race___1 = c(FALSE, FALSE, FALSE, FALSE,
228-
FALSE), race___2 = c(FALSE, FALSE, FALSE, FALSE, FALSE), race___3 = c(FALSE,
229-
FALSE, FALSE, FALSE, FALSE), race___4 = c(FALSE, FALSE, FALSE,
230-
FALSE, FALSE), race___5 = c(FALSE, FALSE, FALSE, FALSE, FALSE
231-
), race___6 = c(FALSE, FALSE, FALSE, FALSE, FALSE), ethnicity = c("NOT Hispanic or Latino",
232-
"NOT Hispanic or Latino", "Unknown / Not Reported", "NOT Hispanic or Latino",
233-
"Hispanic or Latino"), demographics_complete = c("Complete",
234-
"Complete", "Complete", "Complete", "Complete"), health_complete = c("Unverified",
235-
"Incomplete", "Complete", "Complete", "Incomplete"), race_and_ethnicity_complete = c("Complete",
236-
"Incomplete", "Complete", "Complete", "Complete")), .Names = c("record_id",
228+
), mugshot = 7610:7614, race___1 = c(FALSE, FALSE, FALSE,
229+
FALSE, TRUE), race___2 = c(FALSE, FALSE, FALSE, TRUE, FALSE
230+
), race___3 = c(FALSE, TRUE, FALSE, FALSE, FALSE), race___4 = c(FALSE,
231+
FALSE, TRUE, FALSE, FALSE), race___5 = c(TRUE, TRUE, TRUE,
232+
TRUE, FALSE), race___6 = c(FALSE, FALSE, FALSE, FALSE, TRUE
233+
), ethnicity = c(1L, 1L, 0L, 1L, 2L), demographics_complete = c(2L,
234+
2L, 2L, 2L, 2L), health_complete = c(1L, 0L, 2L, 2L, 0L),
235+
race_and_ethnicity_complete = c(2L, 0L, 2L, 2L, 2L)), .Names = c("record_id",
237236
"name_first", "name_last", "address", "telephone", "email", "dob",
238237
"age", "sex", "height", "weight", "bmi", "comments", "mugshot",
239238
"race___1", "race___2", "race___3", "race___4", "race___5", "race___6",
@@ -245,10 +244,10 @@ test_that("All Records -label", {
245244

246245
expect_message(
247246
regexp = expected_outcome_message,
248-
returned_object <- REDCapR:::redcap_read_oneshot_eav(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label="label", export_data_access_groups=FALSE, verbose=T)
247+
returned_object <- REDCapR:::redcap_read_oneshot_eav(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label_headers="label")
249248
)
250249

251-
expect_equal(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct") # dput(returned_object$data)
250+
expect_equivalent(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct") # dput(returned_object$data)
252251
expect_equal(returned_object$status_code, expected=200L)
253252
expect_equivalent(returned_object$raw_text, expected="") # dput(returned_object$raw_text)
254253
expect_true(returned_object$records_collapsed=="", "A subset of records was not requested.")
@@ -258,6 +257,7 @@ test_that("All Records -label", {
258257
expect_true(returned_object$success)
259258
})
260259

260+
261261
test_that("Filter - numeric", {
262262
testthat::skip_on_cran()
263263
expected_data_frame <- structure(list(record_id = 3:4, name_first = c("Marcus", "Trudy"

0 commit comments

Comments
 (0)