Skip to content

Commit

Permalink
feat(app): Process directory
Browse files Browse the repository at this point in the history
Update process directory and add initial tests
  • Loading branch information
AlexAxthelm committed Jan 30, 2024
1 parent 25f82a2 commit 0c623d1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
5 changes: 4 additions & 1 deletion R/process_directory.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ process_directory <- function(
# Get the list of files in the directory
files <- list.files(input_directory, full.names = TRUE)
# Process each file
all_summaries <- list()
for (file in files) {
reexport_portfolio(
portfolio_summary <- reexport_portfolio(
input_filepath = file,
output_directory = output_directory
)
all_summaries <- c(all_summaries, list(portfolio_summary))
}
logger::log_info("Done processing directory.")
return(all_summaries)
}
89 changes: 89 additions & 0 deletions tests/testthat/test-process_directory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# suppress log messages during testing.
old_threshold <- logger::log_threshold()
withr::defer(logger::log_threshold(old_threshold))
logger::log_threshold("FATAL")

empty_groups <- data.frame()
simple_groups <- tibble::tribble(
~investor_name, ~portfolio_name,
"Simple Investor", "Simple Portfolio"
)

test_that("Processing a directory with a single file works.", {
test_file <- testthat::test_path(
"testdata", "portfolios", "simple.csv"
)
filehash <- digest::digest(
object = test_file,
file = TRUE,
algo = "md5"
)
input_dir <- tempfile("input")
dir.create(input_dir)
withr::defer(unlink(input_dir))
output_dir <- tempfile("output")
dir.create(output_dir)
withr::defer(unlink(output_dir))
file.copy(
from = test_file,
to = file.path(input_dir, "foo.csv")
)
metadata <- process_directory(
input_directory = input_dir,
output_directory = output_dir
)
expect_simple_reexport(
output_dir = output_dir,
metadata = metadata[[1]],
groups = empty_groups,
input_digest = filehash,
input_filename = "foo.csv",
input_entries = 1
)
})

test_that("Processing a directory with a multiple files works.", {
test_file <- testthat::test_path(
"testdata", "portfolios", "simple.csv"
)
filehash <- digest::digest(
object = test_file,
file = TRUE,
algo = "md5"
)
input_dir <- tempfile("input")
dir.create(input_dir)
withr::defer(unlink(input_dir))
output_dir <- tempfile("output")
dir.create(output_dir)
withr::defer(unlink(output_dir))
file.copy(
from = test_file,
to = file.path(input_dir, "foo1.csv")
)
file.copy(
from = test_file,
to = file.path(input_dir, "foo2.csv")
)
metadata <- process_directory(
input_directory = input_dir,
output_directory = output_dir
)
expect_simple_reexport(
output_dir = output_dir,
metadata = metadata[[1]],
groups = empty_groups,
input_digest = filehash,
input_filename = "foo1.csv",
input_entries = 1
)
expect_simple_reexport(
output_dir = output_dir,
metadata = metadata[[2]],
groups = empty_groups,
input_digest = filehash,
input_filename = "foo2.csv",
input_entries = 1
)
})

Check warning on line 89 in tests/testthat/test-process_directory.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-process_directory.R,line=89,col=1,[trailing_blank_lines_linter] Trailing blank lines are superfluous.

0 comments on commit 0c623d1

Please sign in to comment.