diff --git a/tests/testthat/test-write_read_equality.R b/tests/testthat/test-write_read_equality.R index 079554dd..65a09c9f 100644 --- a/tests/testthat/test-write_read_equality.R +++ b/tests/testthat/test-write_read_equality.R @@ -41,9 +41,6 @@ test_that("Writing then reading returns identical data.frame 1", { expect_equal(object = getwd(), curr_wd) }) - - - test_that("Writing then reading returns identical data.frame 2", { curr_wd <- getwd() @@ -112,65 +109,79 @@ test_that("Writing then reading returns identical data.frame 2", { unlink(fileName, recursive = TRUE, force = TRUE) }) - - - - - - test_that("Writing then reading rowNames, colNames combinations", { - fileName <- file.path(tempdir(), "tmp.xlsx") + fileName <- tempfile(fileext = ".xlsx") curr_wd <- getwd() - - ## rowNames = colNames = TRUE - write.xlsx(mtcars, file = fileName, overwrite = TRUE, row.names = TRUE) + mt <- utils::head(mtcars) # don't need the whole thing + + expect_warning( + write.xlsx(mt, file = tempfile(), row.names = TRUE, overwrite = TRUE), + "Please use 'rowNames' instead of 'row.names'" + ) + + # write the row and column names for testing + write.xlsx(mt, file = fileName, overwrite = TRUE, rowNames = TRUE, colNames = TRUE) + + # rowNames = colNames = TRUE + # Row names = first column + # Col names = first row x <- read.xlsx(fileName, sheet = 1, rowNames = TRUE) - expect_equal(object = x, expected = mtcars, check.attributes = TRUE) + expect_equal(x, mt) - ## rowNames = TRUE, colNames = FALSE - write.xlsx(mtcars, file = fileName, overwrite = TRUE, rowNames = TRUE, colNames = FALSE) + # rowNames = TRUE, colNames = FALSE + # Row names = first column + # Col names = X1, X2, etc + + # need to create an expected output + y <- as.data.frame(rbind(colnames(mt), as.matrix(mt))) + colnames(y) <- c(make.names(seq_along(mt))) x <- read.xlsx(fileName, sheet = 1, rowNames = TRUE, colNames = FALSE) - expect_equal(object = x, expected = mtcars, check.attributes = FALSE) - expect_equal(object = rownames(x), expected = rownames(mtcars)) + expect_equal(x, y) - ## rowNames = FALSE, colNames = TRUE - write.xlsx(mtcars, file = fileName, overwrite = TRUE, rowNames = FALSE, colNames = TRUE) + # rowNames = FALSE, colNames = TRUE + # Row names = "" + # Cl names = first row + y2 <- cbind(row.names(mt), mt) + colnames(y2)[1] <- "" + row.names(y2) <- NULL x <- read.xlsx(fileName, sheet = 1, rowNames = FALSE, colNames = TRUE) - expect_equal(object = x, expected = mtcars, check.attributes = FALSE) - - ## rowNames = FALSE, colNames = FALSE - write.xlsx(mtcars, file = fileName, overwrite = TRUE, rowNames = FALSE, colNames = FALSE) + expect_equal(x, y2) + + # rowNames = FALSE, colNames = FALSE + # Row names = "" + # Col names = X1, X2, etc + y3 <- cbind(row.names(y), y) + colnames(y3) <- make.names(seq_along(y3)) + row.names(y3) <- NULL x <- read.xlsx(fileName, sheet = 1, rowNames = FALSE, colNames = FALSE) - expect_equal(object = x, expected = mtcars, check.attributes = FALSE) + expect_equal(x, y3) - expect_equal(object = getwd(), curr_wd) + + # Check wd + expect_equal(getwd(), curr_wd) + unlink(fileName, recursive = TRUE, force = TRUE) }) - - - - - test_that("Writing then reading returns identical data.frame 3", { + op <- options() + options(openxlsx.dateFormat = "yyyy-mm-dd") + on.exit(options(op), add = TRUE) ## data - genDf <- function() { - data.frame( - "Date" = Sys.Date() - 0:4, - "Logical" = c(TRUE, FALSE, TRUE, TRUE, FALSE), - "Currency" = -2:2, - "Accounting" = -2:2, - "hLink" = "https://CRAN.R-project.org/", - "Percentage" = seq(-1, 1, length.out = 5), - "TinyNumber" = runif(5) / 1E9, stringsAsFactors = FALSE - ) - } - - df <- genDf() + df <- data.frame( + Date = as.Date("2021-05-21") - 0:4, + Logical = c(TRUE, FALSE, TRUE, TRUE, FALSE), + Currency = -2:2, + Accounting = -2:2, + hLink = "https://CRAN.R-project.org/", + Percentage = seq.int(-1, 1, length.out = 5), + TinyNumber = runif(5) / 1E9, + stringsAsFactors = FALSE + ) class(df$Currency) <- "currency" class(df$Accounting) <- "accounting" @@ -178,44 +189,38 @@ test_that("Writing then reading returns identical data.frame 3", { class(df$Percentage) <- "percentage" class(df$TinyNumber) <- "scientific" - options("openxlsx.dateFormat" = "yyyy-mm-dd") - - fileName <- file.path(tempdir(), "allClasses.xlsx") + fileName <- tempfile("allClasses", fileext = ".xlsx") write.xlsx(df, file = fileName, overwrite = TRUE) - ## rows, cols combinations rows <- 1:4 cols <- c(1, 3, 5) - x <- read.xlsx(xlsxFile = fileName, detectDates = TRUE, rows = rows, cols = cols) - expect_equal(object = x, expected = genDf()[sort((rows - 1)[(rows - 1) <= nrow(df)]), sort(cols[cols <= ncol(df)])], check.attributes = FALSE) - + x <- read.xlsx(fileName, detectDates = TRUE, rows = rows, cols = cols) + exp <- df[sort((rows - 1)[(rows - 1) <= nrow(df)]), sort(cols[cols <= ncol(df)])] + expect_equal(x, exp) rows <- 1:4 cols <- 1:9 x <- read.xlsx(xlsxFile = fileName, detectDates = TRUE, rows = rows, cols = cols) - expect_equal(object = x, expected = genDf()[sort((rows - 1)[(rows - 1) <= nrow(df)]), sort(cols[cols <= ncol(df)])], check.attributes = FALSE) - + exp <- df[sort((rows - 1)[(rows - 1) <= nrow(df)]), sort(cols[cols <= ncol(df)])] + expect_equal(x, exp) rows <- 1:200 cols <- c(5, 99, 2) x <- read.xlsx(xlsxFile = fileName, detectDates = TRUE, rows = rows, cols = cols) - expect_equal(object = x, expected = genDf()[sort((rows - 1)[(rows - 1) <= nrow(df)]), sort(cols[cols <= ncol(df)])], check.attributes = FALSE) + exp <- df[sort((rows - 1)[(rows - 1) <= nrow(df)]), sort(cols[cols <= ncol(df)])] + expect_equal(x, exp) rows <- 1000:900 cols <- c(5, 99, 2) suppressWarnings(x <- read.xlsx(xlsxFile = fileName, detectDates = TRUE, rows = rows, cols = cols)) - expect_equal(object = x, expected = NULL, check.attributes = FALSE) + expect_identical(x, NULL) unlink(fileName, recursive = TRUE, force = TRUE) }) - - - - test_that("Writing then reading returns identical data.frame 4", { ## data @@ -266,7 +271,6 @@ test_that("Writing then reading returns identical data.frame 5", { - test_that("Special characters in sheet names", { tf <- tempfile(fileext = ".xlsx") diff --git a/tests/testthat/test-write_xlsx_vector_args.R b/tests/testthat/test-write_xlsx_vector_args.R index dbd49ba3..75eda293 100644 --- a/tests/testthat/test-write_xlsx_vector_args.R +++ b/tests/testthat/test-write_xlsx_vector_args.R @@ -3,11 +3,11 @@ context("write.xlsx vector arguments") test_that("Writing then reading returns identical data.frame 1", { tmp_file <- file.path(tempdir(), "xlsx_vector_args.xlsx") - + df1 <- data.frame(1:2) df2 <- data.frame(1:3) x <- list(df1, df2) - + write.xlsx( file = tmp_file, x = x, @@ -16,50 +16,34 @@ test_that("Writing then reading returns identical data.frame 1", { zoom = c(50, 90), tabColour = c("red", "blue") ) - + wb <- loadWorkbook(tmp_file) - - expect_equal(object = getSheetNames(tmp_file), expected = c("a", "b")) - expect_equal(object = names(wb), expected = c("a", "b")) - - expect_true(object = grepl('rgb="FFFF0000"', wb$worksheets[[1]]$sheetPr)) - expect_true(object = grepl('rgb="FF0000FF"', wb$worksheets[[2]]$sheetPr)) - - expect_true(object = grepl('zoomScale="50"', wb$worksheets[[1]]$sheetViews)) - expect_true(object = grepl('zoomScale="90"', wb$worksheets[[2]]$sheetViews)) - - expect_true(object = grepl('showGridLines="0"', wb$worksheets[[1]]$sheetViews)) - expect_true(object = grepl('showGridLines="1"', wb$worksheets[[2]]$sheetViews)) - + + expect_equal(getSheetNames(tmp_file), expected = c("a", "b")) + expect_equal(names(wb), expected = c("a", "b")) + + expect_true(grepl('rgb="FFFF0000"', wb$worksheets[[1]]$sheetPr)) + expect_true(grepl('rgb="FF0000FF"', wb$worksheets[[2]]$sheetPr)) + + expect_true(grepl('zoomScale="50"', wb$worksheets[[1]]$sheetViews)) + expect_true(grepl('zoomScale="90"', wb$worksheets[[2]]$sheetViews)) + + expect_true(grepl('showGridLines="0"', wb$worksheets[[1]]$sheetViews)) + expect_true(grepl('showGridLines="1"', wb$worksheets[[2]]$sheetViews)) + expect_equal(read.xlsx(tmp_file, sheet = 1), df1) expect_equal(read.xlsx(tmp_file, sheet = 2), df2) - + unlink(tmp_file, recursive = TRUE, force = TRUE) }) test_that("write.xlsx() passes withFilter and colWidths [151]", { df <- data.frame(x = 1, b = 2) - tf1 <- tempfile("file_1_", fileext = ".xlsx") - tf2 <- tempfile("file_2_", fileext = ".xlsx") - on.exit(file.remove(tf1, tf2), add = TRUE) - - # undebug(write.xlsx) - # withFilter default should be FALSE when asTable is FALSE - write.xlsx(df, tf1) - write.xlsx(df, tf2, withFilter = TRUE, colWidths = 15) - - x <- loadWorkbook(tf1) - y <- loadWorkbook(tf2) - - expect_equal( - x$worksheets[[1]]$autoFilter, - character() - ) - expect_equal( - y$worksheets[[1]]$autoFilter, - "" - ) + x <- buildWorkbook(df) + y <- buildWorkbook(df, withFilter = TRUE, colWidths = 15) + expect_equal(x$worksheets[[1]]$autoFilter, character()) + expect_equal(y$worksheets[[1]]$autoFilter, "") expect_equal(x$colWidths, list(list())) expect_equal( @@ -70,57 +54,53 @@ test_that("write.xlsx() passes withFilter and colWidths [151]", { test_that("write.xlsx() correctly passes default asTable and withFilters", { df <- data.frame(x = 1, b = 2) - tf1 <- tempfile(fileext = ".xlsx") - tf2 <- tempfile(fileext = ".xlsx") - on.exit(file.remove(tf1, tf2), add = TRUE) # asTable = TRUE >> writeDataTable >> withFilter = TRUE # asTable = FALSE >> writeData >> withFilter = FALSE - write.xlsx(df, tf1, asTable = FALSE) - write.xlsx(df, tf2, asTable = TRUE) + x <- buildWorkbook(df, asTable = FALSE) + y <- buildWorkbook(df, asTable = TRUE) - x <- loadWorkbook(tf1) - y <- loadWorkbook(tf2) + # Save the workbook + tf <- tempfile(fileext = ".xlsx") + saveWorkbook(y, tf) + y2 <- loadWorkbook(tf) + expect_identical(x$worksheets[[1]]$autoFilter, character()) + + # not autoFilter for tables -- not named in buildWorkbook expect_equal( - x$worksheets[[1]]$autoFilter, - character() + y$worksheets[[1]]$tableParts, + structure("", tableName = "Table3") ) - # not autoFilter for tables expect_equal( - y$worksheets[[1]]$tableParts, + y2$worksheets[[1]]$tableParts, structure("", tableName = c(`A1:B2` = "Table3")) ) + + file.remove(tf) }) test_that("write.xlsx() correctly handles colWidths", { x <- data.frame(a = 1, b = 2, c = 3) - file <- tempfile("write_xlsx_", fileext = ".xlsx") - on.exit(if (file.exists(file)) file.remove(file), add = TRUE) zero3 <- rep("0", 3) # No warning when passing "auto" - expect_warning(write.xlsx(rep_len(list(x), 3), file, colWidths = "auto"), NA) + expect_warning(buildWorkbook(rep_len(list(x), 3), colWidths = "auto"), NA) # single value is repeated for all columns - write.xlsx(rep_len(list(x), 3), file, colWidths = 13) - - expect_equal( - loadWorkbook(file)$colWidths, - rep_len(list(structure(c(`1` = "13", `2` = "13", `3` = "13"), hidden = zero3)), 3) - ) - - # sets are repated - write.xlsx(rep_len(list(x), 3), file, colWidths = list(c(10, 20, 30))) + wb <- buildWorkbook(rep_len(list(x), 3), colWidths = 13) + exp <- rep_len(list(structure(c(`1` = "13", `2` = "13", `3` = "13"), hidden = zero3)), 3) + expect_equal(wb$colWidths, exp) - expect_equal( - loadWorkbook(file)$colWidths, - rep_len(list(structure(c(`1` = "10", `2` = "20", `3` = "30"), hidden = zero3)), 3) - ) + # sets are repeated + wb <- buildWorkbook(rep_len(list(x), 3), colWidths = list(c(10, 20, 30))) + exp <- rep_len(list(structure(c(`1` = "10", `2` = "20", `3` = "30"), hidden = zero3)), 3) + expect_equal(wb$colWidths, exp) # 3 distinct sets - write.xlsx(rep_len(list(x), 3), file, + wb <- buildWorkbook( + rep_len(list(x), 3), colWidths = list( c(10, 20, 30), c(100, 200, 300), @@ -128,7 +108,7 @@ test_that("write.xlsx() correctly handles colWidths", { )) expect_equal( - loadWorkbook(file)$colWidths, + wb$colWidths, list( structure(c(`1` = "10", `2` = "20", `3` = "30"), hidden = zero3), structure(c(`1` = "100", `2` = "200", `3` = "300"), hidden = zero3),