Skip to content

Commit

Permalink
reintroduce repeat_instance
Browse files Browse the repository at this point in the history
ref #530
  • Loading branch information
wibeasley committed Sep 6, 2024
1 parent 32e8aa5 commit e9f19d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
12 changes: 6 additions & 6 deletions R/redcap-file-download-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#' Required
#' @param event The name of the event where the file is saved in REDCap.
#' Optional
# @param repeat_instance (only for projects with repeating instruments/events)
# The repeat instance number of the repeating event (if longitudinal) or the
# repeating instrument (if classic or longitudinal). Default value is '1'.
# Optional
#' @param repeat_instance (only for projects with repeating instruments/events)
#' The repeat instance number of the repeating event (if longitudinal) or the
#' repeating instrument (if classic or longitudinal). Default value is '1'.
#' Optional
#' @param verbose A boolean value indicating if `message`s should be printed
#' to the R console during the operation. Optional.
#' @param config_options A list of options passed to [httr::POST()].
Expand Down Expand Up @@ -125,7 +125,7 @@ redcap_file_download_oneshot <- function(
record,
field,
event = "",
# repeat_instance = NULL,
repeat_instance = NULL,
verbose = TRUE,
config_options = NULL,
handle_httr = NULL
Expand Down Expand Up @@ -158,7 +158,7 @@ redcap_file_download_oneshot <- function(

if (0L < nchar(event)) post_body$event <- event

# if (!is.null(repeat_instance)) post_body$repeat_instance <- repeat_instance
if (!is.null(repeat_instance)) post_body$repeat_instance <- repeat_instance

# This is the first of two important lines in the function.
# It retrieves the information from the server and stores it in RAM.
Expand Down
76 changes: 40 additions & 36 deletions tests/testthat/test-file-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ test_that("file in repeating instrument", {
testthat::skip_on_cran()
credential_repeating <- retrieve_credential_testing(3181L)
on.exit({
base::unlink(returned_object_1$file_name)
base::unlink(returned_object_2$file_name)
base::unlink(returned_object_1_1$file_name)
base::unlink(returned_object_2_1$file_name)
})

# start_time <- Sys.time() - lubridate::seconds(1) #Knock off a second in case there's small time imprecisions
Expand All @@ -346,73 +346,77 @@ test_that("file in repeating instrument", {
expected_outcome_message <- '^(Preparing to download the file `.+\\.jpg`\\.|.+; name=".+\\.jpg" successfully downloaded in \\d+(\\.\\d+\\W|\\W)seconds\\, and saved as .+\\.jpg)'
# expected_outcome_message <- ".+"

# ---- first record --------------------------
# ---- first record, first instance --------------------------
suppressMessages({
returned_object_1 <-
returned_object_1_1 <-
redcap_file_download_oneshot(
record = 1L,
field = field,
repeat_instance = "1",
redcap_uri = credential_repeating$redcap_uri,
token = credential_repeating$token,
verbose = TRUE
)
})

Sys.sleep(delay_after_download_file)
info_actual_1 <- file.info(returned_object_1$file_name)
expect_true(file.exists(returned_object_1$file_name), "The downloaded file should exist.")
info_actual_1_1 <- file.info(returned_object_1_1$file_name)
expect_true(file.exists(returned_object_1_1$file_name), "The downloaded file should exist.")

#Test the values of the returned object.
expect_true(returned_object_1$success)
expect_equal(returned_object_1$status_code, expected=200L)
expect_match(returned_object_1$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_equal(returned_object_1$records_affected_count, 1L)
expect_equal(returned_object_1$affected_ids, "1")
expect_true(returned_object_1$elapsed_seconds>0, "The `elapsed_seconds` should be a positive number.")
expect_equal(returned_object_1$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object_1$raw_text)
expect_equal(returned_object_1$file_name, "levon-and-barry.jpg", label="The name of the downloaded file should be correct.")
expect_true(returned_object_1_1$success)
expect_equal(returned_object_1_1$status_code, expected=200L)
expect_match(returned_object_1_1$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_equal(returned_object_1_1$records_affected_count, 1L)
expect_equal(returned_object_1_1$affected_ids, "1")
expect_true(returned_object_1_1$elapsed_seconds>0, "The `elapsed_seconds` should be a positive number.")
expect_equal(returned_object_1_1$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object_1$raw_text)
expect_equal(returned_object_1_1$file_name, "levon-and-barry.jpg", label="The name of the downloaded file should be correct.")

#Test the values of the file.
expect_equal(info_actual_1$size, expected=info_expected_1_1$size, label="The size of the downloaded file should match.")
expect_false(info_actual_1$isdir, "The downloaded file should not be a directory.")
expect_equal(info_actual_1_1$size, expected=info_expected_1_1$size, label="The size of the downloaded file should match.")
expect_false(info_actual_1_1$isdir, "The downloaded file should not be a directory.")
# expect_equal(as.character(info_actual$mode), expected=as.character(info_expected$mode), label="The mode/permissions of the downloaded file should match.")
expect_true(start_time <= info_actual_1$mtime, label="The downloaded file's modification time should not precede this function's start time.")
# expect_true(start_time <= info_actual_1$ctime, label="The downloaded file's last change time should not precede this function's start time.")
expect_true(start_time <= info_actual_1$atime, label="The downloaded file's last access time should not precede this function's start time.")
expect_true(start_time <= info_actual_1_1$mtime, label="The downloaded file's modification time should not precede this function's start time.")
# expect_true(start_time <= info_actual_1_1$ctime, label="The downloaded file's last change time should not precede this function's start time.")
expect_true(start_time <= info_actual_1_1$atime, label="The downloaded file's last access time should not precede this function's start time.")

# ---- second record --------------------------
# ---- first record, second instance --------------------------

# ---- second record, first instance --------------------------
suppressMessages({
returned_object_2 <-
returned_object_2_1 <-
redcap_file_download_oneshot(
record = 2L,
field = field,
repeat_instance = "1",
redcap_uri = credential_repeating$redcap_uri,
token = credential_repeating$token,
verbose = TRUE
)
})

Sys.sleep(delay_after_download_file)
info_actual_2 <- file.info(returned_object_2$file_name)
expect_true(file.exists(returned_object_2$file_name), "The downloaded file should exist.")
info_actual_2_1 <- file.info(returned_object_2_1$file_name)
expect_true(file.exists(returned_object_2_1$file_name), "The downloaded file should exist.")

#Test the values of the returned object.
expect_true(returned_object_2$success)
expect_equal(returned_object_2$status_code, expected=200L)
expect_match(returned_object_2$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_equal(returned_object_2$records_affected_count, 1L)
expect_equal(returned_object_2$affected_ids, "2")
expect_true(returned_object_2$elapsed_seconds>0, "The `elapsed_seconds` should be a positive number.")
expect_equal(returned_object_2$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object_1$raw_text)
expect_equal(returned_object_2$file_name, "mugshot-2.jpg", label="The name of the downloaded file should be correct.")
expect_true(returned_object_2_1$success)
expect_equal(returned_object_2_1$status_code, expected=200L)
expect_match(returned_object_2_1$outcome_message, regexp=expected_outcome_message, perl=TRUE)
expect_equal(returned_object_2_1$records_affected_count, 1L)
expect_equal(returned_object_2_1$affected_ids, "2")
expect_true(returned_object_2_1$elapsed_seconds>0, "The `elapsed_seconds` should be a positive number.")
expect_equal(returned_object_2_1$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object_1$raw_text)
expect_equal(returned_object_2_1$file_name, "mugshot-2.jpg", label="The name of the downloaded file should be correct.")

#Test the values of the file.
expect_equal(info_actual_2$size, expected=info_expected_2_1$size, label="The size of the downloaded file should match.")
expect_false(info_actual_2$isdir, "The downloaded file should not be a directory.")
expect_equal(info_actual_2_1$size, expected=info_expected_2_1$size, label="The size of the downloaded file should match.")
expect_false(info_actual_2_1$isdir, "The downloaded file should not be a directory.")
# expect_equal(as.character(info_actual$mode), expected=as.character(info_expected$mode), label="The mode/permissions of the downloaded file should match.")
expect_true(start_time <= info_actual_2$mtime, label="The downloaded file's modification time should not precede this function's start time.")
# expect_true(start_time <= info_actual_2$ctime, label="The downloaded file's last change time should not precede this function's start time.")
expect_true(start_time <= info_actual_2$atime, label="The downloaded file's last access time should not precede this function's start time.")
expect_true(start_time <= info_actual_2_1$mtime, label="The downloaded file's modification time should not precede this function's start time.")
# expect_true(start_time <= info_actual_2_1$ctime, label="The downloaded file's last change time should not precede this function's start time.")
expect_true(start_time <= info_actual_2_1$atime, label="The downloaded file's last access time should not precede this function's start time.")
})

test_that("download file conflict -Error", {
Expand Down

0 comments on commit e9f19d4

Please sign in to comment.