From 9f129c419d7de654dc9282ec1db9a18ead37bf9a Mon Sep 17 00:00:00 2001 From: Natalie Goulett Date: Sat, 22 Mar 2025 20:02:45 -0400 Subject: [PATCH 1/2] Added tests of dimensions and contents for lists of single and multiple data frames. Modified to only use temp directories and current dependencies. --- tests/testthat/test-excel_to_list.R | 104 ++++++++++++++++++++++++++++ tests/testthat/test-list_to_excel.R | 102 +++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 tests/testthat/test-excel_to_list.R create mode 100644 tests/testthat/test-list_to_excel.R diff --git a/tests/testthat/test-excel_to_list.R b/tests/testthat/test-excel_to_list.R new file mode 100644 index 00000000..78b54098 --- /dev/null +++ b/tests/testthat/test-excel_to_list.R @@ -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)) + } + +}) diff --git a/tests/testthat/test-list_to_excel.R b/tests/testthat/test-list_to_excel.R new file mode 100644 index 00000000..088a6540 --- /dev/null +++ b/tests/testthat/test-list_to_excel.R @@ -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]]) + +}) + + + From 2b96528d331955ce3d5b158f592c8b1c16a4c06d Mon Sep 17 00:00:00 2001 From: Natalie Goulett Date: Wed, 26 Mar 2025 16:51:12 -0400 Subject: [PATCH 2/2] Deleted test comment. --- R/REDCap_tokens.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/REDCap_tokens.R b/R/REDCap_tokens.R index c9beff3d..0e3c9197 100644 --- a/R/REDCap_tokens.R +++ b/R/REDCap_tokens.R @@ -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)