diff --git a/tests/testthat/test-remove_if_exists.R b/tests/testthat/test-remove_if_exists.R new file mode 100644 index 0000000..a05228a --- /dev/null +++ b/tests/testthat/test-remove_if_exists.R @@ -0,0 +1,9 @@ +test_that("removes the specified object if it exists", { + test_obj <- 1L + remove_if_exists(test_obj) + expect_false(exists("test_obj")) +}) + +test_that("does not error if the object does not exist", { + expect_no_error(remove_if_exists(test_obj)) +}) diff --git a/tests/testthat/test-save_if_exists.R b/tests/testthat/test-save_if_exists.R new file mode 100644 index 0000000..ce25e23 --- /dev/null +++ b/tests/testthat/test-save_if_exists.R @@ -0,0 +1,40 @@ +test_that("properly saves a RDS if the object exists", { + portfolio_name <- "test" + df <- data.frame(portfolio_name = portfolio_name) + save_name <- withr::local_file("test.rds") + + save_if_exists(df = df, portfolio_name = portfolio_name, save_name = save_name) + + expect_true(file.exists(save_name)) + expect_equal(df, readRDS(save_name)) +}) + +test_that("properly saves a CSV if the object exists", { + portfolio_name <- "test" + df <- data.frame(portfolio_name = portfolio_name) + save_name <- withr::local_file("test.csv") + + save_if_exists( + df = df, + portfolio_name = portfolio_name, + save_name = save_name, + csv_or_rds = "csv" + ) + + expect_true(file.exists(save_name)) + expect_equal(df, read.csv(save_name)) +}) + +test_that("does nothing if the object is not a data frame", { + portfolio_name <- "test" + df <- 1L + save_name <- "test.rds" + + save_if_exists( + df = df, + portfolio_name = portfolio_name, + save_name = save_name + ) + + expect_false(file.exists(save_name)) +})