Skip to content

Commit

Permalink
add tests for remove_if_exists() and save_if_exists()
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ Yetman - RMI authored and CJ Yetman - RMI committed Apr 26, 2024
1 parent 1efba41 commit 94ea435
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/testthat/test-remove_if_exists.R
Original file line number Diff line number Diff line change
@@ -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))
})
40 changes: 40 additions & 0 deletions tests/testthat/test-save_if_exists.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit 94ea435

Please sign in to comment.