Skip to content

Commit

Permalink
make log tests more portable
Browse files Browse the repository at this point in the history
ref #542
  • Loading branch information
wibeasley committed Oct 13, 2024
1 parent 2560d69 commit 53945cb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 201 deletions.
2 changes: 1 addition & 1 deletion R/helpers-testing.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
retrieve_credential_testing <- function(project_tag = "simple", server_instance = "bbmc", username = NA_character_) {
retrieve_credential_testing <- function(project_tag = "simple", server_instance = "dev-2", username = NA_character_) {
checkmate::assert_character(project_tag , any.missing = FALSE, min.chars = 2, max.chars = 50)
checkmate::assert_character(server_instance , any.missing = FALSE, min.chars = 2, max.chars = 50)
checkmate::assert_character(username , any.missing = TRUE , min.chars = 2, max.chars = 50)
Expand Down
143 changes: 0 additions & 143 deletions inst/test-data/specific-redcapr/log-read/2020-08-10.R

This file was deleted.

25 changes: 0 additions & 25 deletions inst/test-data/specific-redcapr/log-read/2021-07-11-record3-user.R

This file was deleted.

31 changes: 31 additions & 0 deletions inst/test-data/specific-redcapr/log-read/2024-10-11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
structure(list(timestamp = structure(c(1728643440, 1728643380,
1728643380, 1728643320, 1728643320, 1728643260, 1728643260, 1728643260,
1728643260, 1728643200, 1728643140, 1728643140, 1728641160, 1728641160,
1728641160, 1728641160, 1728641160, 1728638100), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), username = c("unittestphifree", "unittestphifree",
"unittestphifree", "unittestphifree", "unittestphifree", "unittestphifree",
"unittestphifree", "unittestphifree", "itawilliamb", "unittestphifree",
"unittestphifree", "unittestphifree", "itawilliamb", "itawilliamb",
"itawilliamb", "itawilliamb", "itawilliamb", "itawilliamb"),
action = c("Manage/Design", "Manage/Design", "Manage/Design",
"Manage/Design", "Manage/Design", "Manage/Design", "Manage/Design",
"Manage/Design", "Edit user role", "Manage/Design", "Manage/Design",
"Manage/Design", "Update record (import) 5", "Update record (import) 4",
"Update record (import) 3", "Update record (import) 2", "Update record (import) 1",
"Edit user role"), details = c("Export Logging (API)", "Export Logging (API)",
"Export Logging (API)", "Export Logging (API)", "Export Logging (API)",
"Export Logging (API)", "Export Logging (API)", "Export Logging (API)",
"role = 'api'", "Export instruments (API)", "Export DAGs (API)",
"Export project information (API)", "mugshot = '26'", "comments = 'This record doesn't have a DAG assigned So call up Trudy on the telephone Send her a letter in the mail', mugshot = '25'",
"mugshot = '24'", "mugshot = '23'", "mugshot = '22'", "role = 'api'"
), record = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, "5", "4", "3", "2", "1", NA)), row.names = c(NA, -18L
), spec = structure(list(cols = list(timestamp = structure(list(
format = ""), class = c("collector_datetime", "collector"
)), username = structure(list(), class = c("collector_character",
"collector")), action = structure(list(), class = c("collector_character",
"collector")), details = structure(list(), class = c("collector_character",
"collector")), record = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_character",
"collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
41 changes: 9 additions & 32 deletions tests/testthat/test-log-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library(testthat)
credential <- retrieve_credential_testing()
update_expectation <- FALSE

path_expected_2020_08 <- "test-data/specific-redcapr/log-read/2020-08-10.R"
path_expected_2024_10 <- "test-data/specific-redcapr/log-read/2024-10-11.R"

test_that("smoke test", {
testthat::skip_on_cran()
Expand All @@ -18,48 +18,25 @@ test_that("smoke test", {
expect_type(returned, "list")
})

test_that("2020-08-10-api-export", {
test_that("2024-10-11", {
testthat::skip_on_cran()
expected_outcome_message <- "\\d+ rows were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."

returned_object <-
REDCapR::redcap_log_read(
redcap_uri = credential$redcap_uri,
token = credential$token,
log_begin_date = as.Date("2020-08-10"),
log_end_date = as.Date("2020-08-10"),
verbose = FALSE
)

if (update_expectation) save_expected(returned_object$data, path_expected_2020_08)
expected_data_frame <- retrieve_expected(path_expected_2020_08)

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("2021-07-11-record3-user", {
testthat::skip_on_cran()
path_expected <- "test-data/specific-redcapr/log-read/2021-07-11-record3-user.R"
expected_outcome_message <- "\\d+ rows were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."
if (credential$redcap_uri != "https://redcap-dev-2.ouhsc.edu/redcap/api/") {
testthat::skip("Skipping logging test b/c a different server.")
}

returned_object <-
REDCapR::redcap_log_read(
redcap_uri = credential$redcap_uri,
token = credential$token,
log_begin_date = as.Date("2021-07-11"),
log_end_date = as.Date("2021-07-11"),
record = as.character(3),
user = "unittestphifree",
log_begin_date = as.Date("2024-10-11"),
log_end_date = as.Date("2024-10-11"),
verbose = FALSE
)

if (update_expectation) save_expected(returned_object$data, path_expected)
expected_data_frame <- retrieve_expected(path_expected)
if (update_expectation) save_expected(returned_object$data, path_expected_2024_10)
expected_data_frame <- retrieve_expected(path_expected_2024_10)

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)
Expand Down

0 comments on commit 53945cb

Please sign in to comment.