Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion R/REDCap_tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#' @keywords Token Functions
#' @export
set_project_token <- function(project, ask = TRUE) {
# Hello - nat
project <- assert_blank_project(project)
token_name <- get_REDCap_token_name(project)
is_a_test <- is_test_project(project)
Expand Down
104 changes: 104 additions & 0 deletions tests/testthat/test-excel_to_list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Tests for excel_to_list()

# Note: We use .rdata files to store class information. The list_to_excel() will
# export all columns as 'text' class, which are imported as 'character' class,
# so we will convert the expected objects' columns to character class.

test_that("Excel elements match list's", {

# Create temp directory and temp file
temp_dir <- tempdir()
temp_file <- file.path(temp_dir, "mtcarsReport.xlsx")

# Export mtcars list of one df named "sheet_name"
mtcars_list <- list(sheet_name = mtcars)
REDCapSync:::list_to_excel(
mtcars_list,
dir = temp_dir,
file_name = "mtcarsReport"
)

# Import mtcars list from temp file
mtcars_import <- REDCapSync:::excel_to_list(temp_file)

# Convert expected list df columns to character class
mtcars_char_df <- as.data.frame(
lapply(
mtcars,
as.character
),
stringsAsFactors = FALSE)
mtcars_list_char <- list(sheet_name = mtcars_char_df)

# Test that imported list matches exported list containing character vectors
expect_equal(mtcars_import, mtcars_list_char)

# Test that number of rows and columns match
expect_equal(
nrow(mtcars_import$sheet_name),
nrow(mtcars_list_char$sheet_name)
)
expect_equal(
ncol(mtcars_import$sheet_name),
ncol(mtcars_list_char$sheet_name)
)

# Test that column names match
expect_equal(
names(mtcars_import$sheet_name),
names(mtcars_list_char$sheet_name)
)

})


test_that("Lists with multiple elements import correctly", {

# Set up temp directory and temp file 2
temp_dir <- tempdir()
temp_file_2 <- file.path(temp_dir, "testReport.xlsx")

# Export list with multiple data.frames (sheets)
test_list <- list(
mtcars_sheet = mtcars,
iris_sheet = iris,
tooth_sheet = ToothGrowth
)
REDCapSync:::list_to_excel(
test_list,
dir = temp_dir,
file_name = "testReport"
)

# Read in test report Excel file
test_list_import <- REDCapSync:::excel_to_list(temp_file_2)

# Convert each vector in each data.frame to character class
expected_list <- lapply(test_list, function(df) {
as.data.frame(lapply(df, as.character), stringsAsFactors = FALSE)
})

# Test that data.frame (sheet) names match
expect_equal(names(test_list_import), names(expected_list))

# Compare each imported sheet with respective exported data.frame
for (sheet_name in names(expected_list)) {
imported_df <- imported_list[[sheet_name]]
expected_df <- expected_list[[sheet_name]]

# Check number of rows and columns match
expect_equal(nrow(imported_df), nrow(expected_df),
info = paste("Row mismatch in sheet:", sheet_name))
expect_equal(ncol(imported_df), ncol(expected_df),
info = paste("Column mismatch in sheet:", sheet_name))

# Check column names match
expect_equal(names(imported_df), names(expected_df),
info = paste("Column names mismatch in sheet:", sheet_name))

# Check content matches
expect_equal(imported_df, expected_df,
info = paste("Data mismatch in sheet:", sheet_name))
}

})
102 changes: 102 additions & 0 deletions tests/testthat/test-list_to_excel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Test for list_to_excel() from RosyUtils

# Note: list_to_excel() treats all variables as character. All columns are
# converted to character class before comparing actual vs. expected. .rdata
# files are typically used to store character class data.


test_that("list_to_excel() creates Excel file", {

# Required packages
library(openxlsx) # For reading reports

# Create a temporary directory and file name
temp_dir <- tempdir()
temp_file <- file.path(temp_dir, "testReport.xlsx")

# Create sample list of data frames
df1 <- data.frame(
age = c(34, 7, 101),
full_name = c("Anita Break", "Gene Poole", "Gus Undheit")
)
df2 <- data.frame(
diagnosis = c("Chronic Fatigue", "Huntington's Disease", "Rhinitis"),
first_visit_year = c(2005, 2018, 1967)
)
test_list <- list(Sheet1 = df1, Sheet2 = df2)

# Export as Excel file
list_to_excel(
test_list,
dir = temp_dir,
file_name = "testReport"
)

# Verify report is created
expect_true(file.exists(temp_file))

# Check if the file contains the expected sheets
output_sheets <- openxlsx::getSheetNames(temp_file)
expect_equal(sort(output_sheets), sort(names(test_list)))

# Check if the data in the file matches the input data
df1_read <- openxlsx::read.xlsx(temp_file, sheet = "Sheet1")
df2_read <- openxlsx::read.xlsx(temp_file, sheet = "Sheet2")

# Convert df1 and df2 to character type because the Excel columns are read
# as character type
df1_char <- df1
df1_char$age <- as.character(df1_char$age)

df2_char <- df2
df2_char$first_visit_year <- as.character(df2_char$first_visit_year)

# Compare the read data to expected data.frames with character vectors
expect_equal(df1_read, df1_char)
expect_equal(df2_read, df2_char)

unlink(temp_file)
})


# Test creating separate Excel files
test_that("list_to_excel() creates multiple Excel files", {

# Required packages
library(openxlsx) # For reading reports

# Create temporary directory and file names
temp_dir <- tempdir()
temp_files <- file.path(
temp_dir,
c("testReports_Sheet1.xlsx", "testReports_Sheet2.xlsx")
)

# Export as Excel files with separate = TRUE
list_to_excel(
test_list,
dir = temp_dir,
file_name = "testReports",
separate = TRUE
)

# Check if Excel files exist
expect_true(all(file.exists(temp_files)))

# Verify that files can be read
wb_read_1 <- openxlsx::read.xlsx(temp_files[1])
wb_read_2 <- openxlsx::read.xlsx(temp_files[2])

# Convert original list's data.frame to character class only
test_list_char <- lapply(test_list, function(df) {
as.data.frame(lapply(df, as.character))
})

# Compare original data frames with imported sheets
expect_equal(wb_read_1, test_list_char[[1]])
expect_equal(wb_read_2, test_list_char[[2]])

})