From df65c04f1504f4f9956bcae49edefafc7137c6e7 Mon Sep 17 00:00:00 2001 From: ryanzomorrodi Date: Sat, 28 Dec 2024 14:16:58 -0600 Subject: [PATCH] Initial revamping of test suit --- tests/testthat/helper-setup.R | 1 + tests/testthat/test-connection.R | 20 ++++--- tests/testthat/test-conversion.R | 72 +++++++++++------------- tests/testthat/test-file.R | 67 ++++++++++++++++++++++ tests/testthat/test-io.R | 97 -------------------------------- tests/testthat/test-run.R | 34 ----------- 6 files changed, 114 insertions(+), 177 deletions(-) create mode 100644 tests/testthat/test-file.R delete mode 100644 tests/testthat/test-io.R delete mode 100644 tests/testthat/test-run.R diff --git a/tests/testthat/helper-setup.R b/tests/testthat/helper-setup.R index 41cee59..ff6f66f 100644 --- a/tests/testthat/helper-setup.R +++ b/tests/testthat/helper-setup.R @@ -1,2 +1,3 @@ sas_username <- "u63378825" test_number <- ceiling(runif(1, 0, 10000000)) # helps make sure CI tests don't interfere with one another +sas_connect() \ No newline at end of file diff --git a/tests/testthat/test-connection.R b/tests/testthat/test-connection.R index f697a6c..8039885 100644 --- a/tests/testthat/test-connection.R +++ b/tests/testthat/test-connection.R @@ -1,15 +1,21 @@ test_that("test connection", { + skip_on_cran() skip_if_offline() - expect_no_error(sas_connect()) + "connected connection" + expect_no_error(chk_connection()) - expect_no_error(sas_connect("oda")) + "disconnection" + expect_message(sas_disconnect(), "SAS Connection terminated.") + expect_null(sas_get_session()) - expect_no_error(sas_get_session()) + "disconnected connection" + expect_error(chk_connection(), "No active SAS session.") - expect_no_error(sas_disconnect()) + "connection" + expect_message(sas_connect(), "SAS Connection established.") + expect_s3_class(sas_get_session(), c("saspy.sasbase.SASsession", "python.builtin.object")) - expect_error(sas_disconnect()) - - expect_error(sas_get_session()) + "specifying connection config" + expect_message(sas_connect("oda"), "SAS Connection established.") }) diff --git a/tests/testthat/test-conversion.R b/tests/testthat/test-conversion.R index c2197e6..4439b43 100644 --- a/tests/testthat/test-conversion.R +++ b/tests/testthat/test-conversion.R @@ -1,42 +1,36 @@ -test_that("SAS to R data.frame", { +test_that("Back and forth", { + skip_on_cran() skip_if_offline() - sas_connect() - - iris_sas <- sas_to_r("iris", "sashelp") - - # processing to get SAS and R data frames the same - data(iris) - names(iris_sas) <- c("Species", "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width") - iris_sas <- iris_sas[c(2:5, 1)] - iris <- iris[order(iris$Species, iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, iris$Petal.Width), ] - iris_sas <- iris_sas[order(iris_sas$Species, iris_sas$Sepal.Length, iris_sas$Sepal.Width, iris_sas$Petal.Length, iris_sas$Petal.Width), ] - iris_sas[1:4] <- iris_sas[1:4] / 10 - iris$Species <- as.character(iris$Species) - iris_sas$Species <- tolower(iris_sas$Species) - rownames(iris) <- NULL - rownames(iris_sas) <- NULL - - expect_equal(iris, iris_sas) - - sas_disconnect() -}) - -test_that("R to SAS data.frame", { - skip_if_offline() - - sas_connect() - - data(iris) - r_to_sas(iris, "iris") - iris_sas <- sas_to_r("iris") - - # processing to get SAS and R data frames the same - attr(iris_sas, "pandas.index") <- NULL - iris$Species <- as.character(iris$Species) - - expect_equal(iris, iris_sas) - - sas_disconnect() - + df_all <- data.frame( + a = c(1, 2.5, NA), + b = c(1:2, NA), + c = c(T, F, NA), + d = c("a", "b", NA), + e = factor(c("a", "b", NA)), + f = as.Date("2015-12-09") + c(1:2, NA), + g = as.POSIXct("2015-12-09 10:51:34.5678", tz = "UTC") + c(1:2, NA), + h = I(as.list(c(1:2, NA))), + i = I(list(list(1, 2:3), list(4:6), list(NA))) + ) + + "list columns: R to SAS" + expect_error(r_to_sas(df_all, "df"), "must only have logical, integer, double, factor, character, POSIXct, or Date class columns") + + "valid columns: R to SAS" + df <- df_all[!sapply(df_all, is.list)] + expect_equal(r_to_sas(df, "df"), df) + + "rownames: R to SAS" + rownames(df) <- paste("row", 1:3) + expect_warning(r_to_sas(df, "df"), "rownames will not be transferred as a column") + rownames(df) <- NULL + + "back to R check" + df_from_sas <- sas_to_r("df") + df_from_sas$f <- as.Date(as.POSIXct(df_from_sas$f, tz = "UTC")) + df_from_sas$g <- as.POSIXct(df_from_sas$g, tz = "UTC") + df$c <- as.double(df$c) + df$e <- as.character(df$e) + expect_equal(df_from_sas, df) }) diff --git a/tests/testthat/test-file.R b/tests/testthat/test-file.R new file mode 100644 index 0000000..7c52c42 --- /dev/null +++ b/tests/testthat/test-file.R @@ -0,0 +1,67 @@ +test_that("uploading and downloading to SAS", { + skip_on_cran() + skip_if_offline() + skip_if(sas_get_session()$sascfg$name != "oda") + + local_path <- tempfile(pattern="temp", fileext=".sas") + local_name <- basename(local_path) + sas_path <- paste0("~/", local_name) + + "no file exists upload" + expect_error(sas_file_upload(local_path, sas_path), "must specify an existing file") + + "file exists upload" + cat("PROC MEANS DATA = sashelp.cars; RUN;", file = local_path) + expect_true(sas_file_upload(local_path, sas_path)) + expect_true(sas_file_exists(sas_path)) + + "file download" + file.remove(local_path) + expect_true(sas_file_download(sas_path, local_path)) + expect_true(file.exists(local_path)) +}) + +test_that("removing file from SAS", { + skip_on_cran() + skip_if_offline() + skip_if(sas_get_session()$sascfg$name != "oda") + + local_path <- tempfile(pattern="temp", fileext=".sas") + local_name <- basename(local_path) + sas_path <- paste0("~/", local_name) + + "no file exists removal" + expect_warning(sas_file_remove(sas_path)) + + "file exists removal" + cat("PROC MEANS DATA = sashelp.cars; RUN;", file = local_path) + sas_file_upload(local_path, sas_path) + expect_true(sas_file_remove(sas_path)) + expect_false(sas_file_exists(sas_path)) +}) + + +test_that("copying file on SAS", { + skip_on_cran() + skip_if_offline() + skip_if(sas_get_session()$sascfg$name != "oda") + + local_path <- tempfile(pattern="temp", fileext=".sas") + local_name <- basename(local_path) + sas_path <- paste0("~/", local_name) + sas_copy_path <- gsub(".sas", "_copy.sas", sas_path, fixed = TRUE) + + "no file exists copy" + expect_warning(sas_file_copy(sas_path, sas_copy_path)) + + "file exists copy" + cat("PROC MEANS DATA = sashelp.cars; RUN;", file = local_path) + sas_file_upload(local_path, sas_path) + expect_true(sas_file_copy(sas_path, sas_copy_path)) + expect_true(sas_file_exists(sas_path)) + expect_true(sas_file_exists(sas_copy_path)) + + # clean up + sas_file_remove(sas_path) + sas_file_remove(sas_copy_path) +}) \ No newline at end of file diff --git a/tests/testthat/test-io.R b/tests/testthat/test-io.R deleted file mode 100644 index 349bff6..0000000 --- a/tests/testthat/test-io.R +++ /dev/null @@ -1,97 +0,0 @@ -test_that("uploading files to SAS", { - skip_if_offline() - - local_path <- paste0("test", test_number, ".sas") - sas_path <- paste("/home", sas_username, local_path, sep = "/") - - "no connection + no file" - expect_error(sas_upload(local_path, sas_path)) - - "no connection + file" - cat("PROC MEANS DATA = sashelp.cars;\nRUN;", file = local_path) - expect_error(sas_upload(local_path, sas_path)) - - sas_connect() - - "connection + file" - expect_no_error(sas_upload(local_path, sas_path)) - - "connection + no file" - file.remove(local_path) - expect_error(sas_upload(local_path, sas_path)) - - sas_remove(sas_path) - sas_disconnect() -}) - -test_that("download files from SAS", { - skip_if_offline() - - local_path <- paste0("test", test_number, ".sas") - sas_path <- paste("/home", sas_username, local_path, sep = "/") - - "no connection + no file" - expect_error(sas_download(sas_path, local_path)) - - "no connection + file" - cat("PROC MEANS DATA = sashelp.cars;\nRUN;", file = local_path) - sas_connect() - sas_upload(local_path, sas_path) - sas_disconnect() - expect_error(sas_download(sas_path, local_path)) - - sas_connect() - - "connection + file" - expect_no_error(sas_download(sas_path, local_path)) - sas_remove(sas_path) - - "connection + no file" - expect_error(sas_download(sas_path, local_path)) - - sas_disconnect() - file.remove(local_path) -}) - -test_that("removing files from SAS", { - skip_if_offline() - - local_path <- paste0("test", test_number, ".sas") - sas_path <- paste("/home", sas_username, local_path, sep = "/") - - "no connection + no file" - expect_error(sas_remove(sas_path)) - - "no connection + file" - cat("PROC MEANS DATA = sashelp.cars;\nRUN;", file = local_path) - sas_connect() - sas_upload(local_path, sas_path) - sas_disconnect() - expect_error(sas_remove(sas_path)) - - sas_connect() - - "connection + file" - expect_no_error(sas_remove(sas_path)) - - "connection + no file" - expect_error(sas_remove(sas_path)) - - sas_disconnect() - file.remove(local_path) -}) - -test_that("listing files", { - skip_if_offline() - - sas_path <- paste0("/home/", sas_username) - - "no connection" - expect_error(sas_list(sas_path)) - - "connection" - sas_connect() - expect_no_error(sas_list(sas_path)) - - sas_disconnect() -}) diff --git a/tests/testthat/test-run.R b/tests/testthat/test-run.R deleted file mode 100644 index 98b9e35..0000000 --- a/tests/testthat/test-run.R +++ /dev/null @@ -1,34 +0,0 @@ -test_that("Run SAS code from string", { - skip_if_offline() - - expect_error(sas_run_string("PROC MEANS DATA = sashelp.cars;\nRUN;")) - - sas_connect() - - expect_s3_class(sas_run_string("PROC MEANS DATA = sashelp.cars;\nRUN;"), "sas_widget") - - sas_disconnect() -}) - -test_that("Run SAS code from file", { - skip_if_offline() - - local_path <- "test.sas" - output_path <- "test.html" - cat("PROC MEANS DATA = sashelp.cars;\nRUN;", file = local_path) - - expect_error(sas_run_file(local_path)) - - sas_connect() - expect_s3_class(sas_run_file(local_path), "sas_widget") - - expect_no_error(sas_run_file(local_path, output_path)) - - output_paths <- c(output_path, gsub("html", "log", output_path)) - expect_true(all(output_paths %in% list.files())) - - expect_error(sas_run_file(local_path, output_path)) - - sas_disconnect() - file.remove(c(local_path, output_paths)) -})