tests/testthat/test-rd_export.R

skip_if_not_installed("openxlsx")

test_that("rd_export writes an xlsx file without password and returns success message", {
  df <- head(covican$data, 3)
  path <- tempfile(fileext = ".xlsx")

  expect_message(
    rd_export(queries = df, path = path),
    regexp = "successfully created"
  )

  expect_true(file.exists(path))

  df_read <- openxlsx::read.xlsx(path, sheet = 1, detectDates = TRUE)

  # Convert both to character for a robust comparison
  df_chr     <- as.data.frame(lapply(df, as.character), stringsAsFactors = FALSE)
  df_read_chr <- as.data.frame(lapply(df_read, as.character), stringsAsFactors = FALSE)

  expect_equal(names(df_read), names(df))
  expect_equal(nrow(df_read), nrow(df))
})


test_that("rd_export warns if links detected but no column argument and no 'Link' column", {
  df_links <- data.frame(
    urlcol = c("https://example.com/a", "no-link"),
    note = "x",
    stringsAsFactors = FALSE
  )
  path <- tempfile(fileext = ".xlsx")

  expect_warning(
    rd_export(queries = df_links, path = path, column = NULL),
    regexp = "Links were detected in the dataset"
  )

  expect_true(file.exists(path))
})

test_that("rd_export errors when 'column' argument is provided but column doesn't exist", {
  df <- head(covican$data, 2)
  path <- tempfile(fileext = ".xlsx")

  expect_error(
    rd_export(queries = df, column = "nonexistent_col", path = path),
    regexp = "The specified column for hyperlinks does not exist"
  )
  expect_false(file.exists(path))
})

test_that("rd_export accepts existing 'column' argument and does not issue link-detection warning", {
  df <- data.frame(
    id = 1:2,
    Link = c("https://x", "https://y"),
    stringsAsFactors = FALSE
  )
  path <- tempfile(fileext = ".xlsx")

  expect_message(
    rd_export(queries = df, column = "Link", path = path),
    regexp = "successfully created"
  )

  expect_true(file.exists(path))
  df_read <- openxlsx::read.xlsx(path, sheet = 1)
  expect_equal(as.data.frame(df_read, stringsAsFactors = FALSE), df)
})

test_that("rd_export saves with password and returns password-protection message", {
  df <- head(covican$data, 2)
  path <- tempfile(fileext = ".xlsx")
  pw <- "secret_pw"

  expect_message(
    rd_export(queries = df, path = path, password = pw),
    regexp = "protected from edits by a password"
  )

  expect_true(file.exists(path))

  df_read <- openxlsx::read.xlsx(path, sheet = 1)

  # --- Normalisation helpers ---
  # Convert Excel serials to Date if column was Date originally
  fix_dates <- function(read_col, original_col) {
    if (inherits(original_col, "Date") && is.numeric(read_col)) {
      as.Date(read_col, origin = "1899-12-30")
    } else {
      read_col
    }
  }

  # Apply to all columns
  for (nm in names(df)) {
    df_read[[nm]] <- fix_dates(df_read[[nm]], df[[nm]])
  }

  # Convert both to character for comparison (ignore factors/levels)
  df_chr      <- as.data.frame(lapply(df, as.character), stringsAsFactors = FALSE)
  df_read_chr <- as.data.frame(lapply(df_read, as.character), stringsAsFactors = FALSE)

  expect_equal(df_read_chr, df_chr)
})


test_that("rd_export stops when there is nothing to export or the folder doesn't exist", {
  expect_error(rd_export(queries = NULL), "There are no queries to export")
  expect_error(rd_export(queries = head(covican$data, 2), path = file.path(tempfile(), "q.xlsx")), "does not exist")
})

test_that("rd_export stops with invalid sheet names and with a folder as path", {
  df <- head(covican$data, 2)
  expect_error(rd_export(queries = df, path = tempfile(fileext = ".xlsx"), sheet_name = "a/b"), class = "REDCapDM_error_argument")
  expect_error(rd_export(queries = df, path = tempfile(fileext = ".xlsx"), sheet_name = strrep("x", 32)), class = "REDCapDM_error_argument")
  expect_error(rd_export(queries = df, path = tempdir()), class = "REDCapDM_error_file")

  path <- tempfile(fileext = ".xlsx")
  expect_message(rd_export(queries = df, path = path, sheet_name = strrep("x", 31)), "successfully created")
  expect_equal(openxlsx::getSheetNames(path), strrep("x", 31))
})

test_that("rd_export accepts the data frame of queries as its first argument and explains a wrong project", {
  q <- suppressMessages(rd_query(covican, variables = "copd", expression = "is.na(x)", event = "baseline_visit_arm_1"))
  path <- tempfile(fileext = ".xlsx")
  expect_message(rd_export(q$queries, path = path), "successfully created")
  expect_equal(nrow(openxlsx::read.xlsx(path)), nrow(q$queries))
  expect_error(rd_export(project = "queries", path = tempfile(fileext = ".xlsx")), class = "REDCapDM_error_argument")
})

test_that("rd_export writes the links of a factor column and keeps the format of the dates", {
  queries <- data.frame(
    Identifier = c("1", "2"), Date = as.Date(c("2024-02-29", "2024-03-01")),
    Time = as.POSIXct(c("2024-02-29 10:30:00", "2024-03-01 08:00:00"), tz = "UTC"),
    Link = factor(c("https://redcap.example.org/a", "https://redcap.example.org/b"))
  )
  path <- tempfile(fileext = ".xlsx")
  suppressMessages(rd_export(queries = queries, column = "Link", path = path))

  out <- openxlsx::read.xlsx(path, detectDates = TRUE)
  expect_equal(out$Link, as.character(queries$Link))
  # (read as dates: the cells keep their date format)
  expect_equal(as.Date(out$Date), queries$Date)

  files <- tempfile()
  utils::unzip(path, exdir = files)
  rels <- paste(readLines(list.files(files, "sheet1.xml.rels", recursive = TRUE, full.names = TRUE), warn = FALSE), collapse = "")
  expect_true(all(vapply(as.character(queries$Link), grepl, logical(1), x = rels, fixed = TRUE)))
})


test_that("export defaults and precedence preserve the selected queries", {
  skip_if_not_installed("mockery")
  withr::local_options(REDCapDM.quiet = TRUE)

  q <- data.frame(
    Identifier = c("1", "2"),
    Link = factor(c("https://example/a", "https://example/b"))
  )
  saved <- list()
  export <- rd_export

  mockery::stub(
    export, "openxlsx::saveWorkbook",
    function(wb, file, overwrite = FALSE, ...) {
      saved$workbook <<- wb
      saved$path <<- file
      saved$overwrite <<- overwrite
      invisible(TRUE)
    }
  )

  expect_warning(
    export(project = list(queries = q), queries = data.frame(wrong = 1)),
    "provided twice"
  )

  out <- openxlsx::read.xlsx(saved$workbook)
  expect_equal(out$Identifier, c("1", "2"), ignore_attr = TRUE)
  expect_equal(out$Link, as.character(q$Link))
  expect_match(saved$path, "example\\.xlsx$")
  expect_true(saved$overwrite)
})

Try the REDCapDM package in your browser

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

REDCapDM documentation built on Nov. 27, 2026, 9:06 a.m.