tests/testthat/test-instrument-repeating.R

library(testthat)

update_expectation  <- FALSE
credential_repeating    <- retrieve_credential_testing("repeating-instruments")

test_that("Smoke Test", {
  testthat::skip_on_cran()
  expect_message(
    returned_object <-
      redcap_instrument_repeating(
        redcap_uri  = credential_repeating$redcap_uri,
        token       = credential_repeating$token
      )
  )
})
test_that("simple repeating", {
  testthat::skip_on_cran()
  path_expected <- "test-data/specific-redcapr/instrument-repeating/simple.R"
  expected_outcome_message <- "\\d+ repeating event-instrument metadata records were read from REDCap in \\d\\.\\d seconds\\.  The http status code was 200\\.(\\n)?"

  returned_object <-
    redcap_instrument_repeating(
      redcap_uri  = credential_repeating$redcap_uri,
      token       = credential_repeating$token,
      verbose     = FALSE
    )

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

  expect_identical(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
  expect_identical(returned_object$status_code, expected=200L)
  expect_identical(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("longitudinal repeating", {
  testthat::skip_on_cran()
  # Add credential for longitudinal project with repeating events here!
  credential    <- retrieve_credential_testing(project_tag = "longitudinal-with-repeating-instrument")
  path_expected_explicit    <- "test-data/specific-redcapr/instrument-repeating/longitudinal-explicit.R"
  path_expected_default     <- "test-data/specific-redcapr/instrument-repeating/longitudinal-default.R"
  expected_outcome_message_explicit  <- "\\d+ repeating event-instrument metadata records were read from REDCap in \\d\\.\\d seconds\\.  The http status code was 200\\.(\\n)?"
  expected_outcome_message_default   <- "\\d+ event instrument metadata records were read from REDCap in \\d\\.\\d seconds\\.  The http status code was 200\\.(\\n)?"

  returned_object_explicit <-
    redcap_instrument_repeating(
      redcap_uri  = credential$redcap_uri,
      token       = credential$token,
      verbose     = FALSE
    )

  if (update_expectation) save_expected(returned_object_explicit$data, path_expected_explicit)
  expected_data_frame_explicit <- retrieve_expected(path_expected_explicit)

  expect_identical(   returned_object_explicit$data, expected=expected_data_frame_explicit, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
  expect_identical(   returned_object_explicit$status_code, expected=200L)
  expect_identical(   returned_object_explicit$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text)
  expect_match(   returned_object_explicit$outcome_message, regexp=expected_outcome_message_explicit, perl=TRUE)
  expect_true(    returned_object_explicit$success)
  expect_s3_class(returned_object_explicit$data, "tbl")

  returned_object_default <-
    redcap_event_instruments(
      redcap_uri  = credential$redcap_uri,
      token       = credential$token,
      verbose     = FALSE
    )

  if (update_expectation) save_expected(returned_object_default$data, path_expected_default)
  expected_data_frame_default <- retrieve_expected(path_expected_default)

  expect_identical(   returned_object_default$data, expected=expected_data_frame_default, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
  expect_identical(   returned_object_default$status_code, expected=200L)
  expect_identical(   returned_object_default$raw_text, expected="", ignore_attr = TRUE) # dput(returned_object$raw_text)
  expect_match(   returned_object_default$outcome_message, regexp=expected_outcome_message_default, perl=TRUE)
  expect_true(    returned_object_default$success)
  expect_s3_class(returned_object_default$data, "tbl")
})
test_that("no repeating", {
  testthat::skip_on_cran()
  credential    <- retrieve_credential_testing("metadata-write")
  expected_outcome_message <- "ERROR: You cannot export repeating instruments and events because the project does not contain any repeating instruments and events(\\n)?"

  returned_object <-
    redcap_instrument_repeating(
      redcap_uri  = credential$redcap_uri,
      token       = credential$token,
      verbose     = FALSE
    )

  expected_data_frame <- tibble::tibble()

  expect_identical(returned_object$data, expected=expected_data_frame, label="The returned data.frame should be correct", ignore_attr = TRUE) # dput(returned_object$data)
  expect_identical(returned_object$status_code, expected=400L)
  expect_identical(returned_object$raw_text, expected="ERROR: You cannot export repeating instruments and events because the project does not contain any repeating instruments and events", ignore_attr = TRUE) # dput(returned_object$raw_text)
  expect_match(returned_object$outcome_message, regexp=expected_outcome_message, perl=TRUE)
  expect_false(returned_object$success)
  expect_s3_class(returned_object$data, "tbl")
})
test_that("bad token -Error", {
  testthat::skip_on_cran()
  expected_outcome_message <- "ERROR: You do not have permissions to use the API"

  returned_object <-
    redcap_instrument_repeating(
      redcap_uri  = credential_repeating$redcap_uri,
      token       = "BAD00000000000000000000000000000",
      verbose     = FALSE
    )

  testthat::expect_false(returned_object$success)
  testthat::expect_identical(returned_object$status_code, 403L)
  testthat::expect_identical(returned_object$raw_text, expected_outcome_message)
})

rm(credential_repeating)

Try the REDCapR package in your browser

Any scripts or data that you put into this service are public.

REDCapR documentation built on Aug. 20, 2026, 5:10 p.m.