library(testthat) credential <- retrieve_credential_testing() update_expectation <- FALSE report_id <- 12L if (credential$redcap_uri != "https://redcap-dev-2.ouhsc.edu/redcap/api/") { testthat::skip("Skip report tests on a new server") } test_that("smoke test", { testthat::skip_on_cran() returned_object <- redcap_report( redcap_uri = credential$redcap_uri, token = credential$token, report_id = report_id, verbose = FALSE ) expect_type(returned_object, "list") }) test_that("default", { testthat::skip_on_cran() path_expected <- "test-data/specific-redcapr/report/default.R" expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." returned_object <- redcap_report( redcap_uri = credential$redcap_uri, token = credential$token, report_id = report_id, verbose = FALSE ) 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=200L) expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text) expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) expect_true(returned_object$success) expect_s3_class(returned_object$data, "tbl") }) test_that("col_types", { testthat::skip_on_cran() path_expected <- "test-data/specific-redcapr/report/col_types.R" expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." col_types <- readr::cols( record_id = readr::col_integer(), height = readr::col_double(), health_complete = readr::col_integer(), address = readr::col_character(), ethnicity = readr::col_integer() ) returned_object <- REDCapR::redcap_report( redcap_uri = credential$redcap_uri, token = credential$token, report_id = report_id, col_types = col_types, verbose = FALSE ) 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=200L) expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text) expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) expect_true(returned_object$success) expect_s3_class(returned_object$data, "tbl") }) test_that("force-character-type", { testthat::skip_on_cran() path_expected <- "test-data/specific-redcapr/report/force-character-type.R" expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." returned_object <- redcap_report( redcap_uri = credential$redcap_uri, token = credential$token, report_id = report_id, guess_type = FALSE, verbose = FALSE ) 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=200L) expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text) expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) expect_true(returned_object$success) expect_s3_class(returned_object$data, "tbl") }) test_that("raw", { testthat::skip_on_cran() path_expected <- "test-data/specific-redcapr/report/raw.R" expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." returned_object <- redcap_report( redcap_uri = credential$redcap_uri, token = credential$token, report_id = report_id, raw_or_label = "raw", verbose = FALSE ) 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=200L) expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text) expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) expect_true(returned_object$success) expect_s3_class(returned_object$data, "tbl") }) test_that("export_checkbox_label", { testthat::skip_on_cran() path_expected <- "test-data/specific-redcapr/report/export_checkbox_label.R" expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\." returned_object <- redcap_report( redcap_uri = credential$redcap_uri, token = credential$token, report_id = report_id, export_checkbox_label = TRUE, raw_or_label = "label", verbose = FALSE ) 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=200L) expect_equal(returned_object$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text) expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE) expect_true(returned_object$success) expect_s3_class(returned_object$data, "tbl") }) test_that("bad token -Error", { testthat::skip_on_cran() expected_outcome_message <- "The REDCapR report operation was not successful\\." expect_message( returned_object <- redcap_report( redcap_uri = credential$redcap_uri, token = "BAD00000000000000000000000000000", report_id = report_id ), expected_outcome_message ) testthat::expect_false(returned_object$success) testthat::expect_equal(returned_object$status_code, 403L) testthat::expect_equal(returned_object$raw_text, "ERROR: You do not have permissions to use the API") }) rm(credential)