-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for
remove_if_exists()
and save_if_exists()
- Loading branch information
CJ Yetman - RMI
authored and
CJ Yetman - RMI
committed
Apr 26, 2024
1 parent
1efba41
commit 94ea435
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |