From c64a072ac7b66ab43242250f66d4c12306e9e4b6 Mon Sep 17 00:00:00 2001 From: CJ Yetman - RMI Date: Sun, 10 Mar 2024 11:46:32 +0100 Subject: [PATCH 1/2] add tests for reading multiple CSVs with `read_portfolio_csv()` --- tests/testthat/test-read_portfolio_csv.R | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/testthat/test-read_portfolio_csv.R b/tests/testthat/test-read_portfolio_csv.R index efeddb3..1aac9fe 100644 --- a/tests/testthat/test-read_portfolio_csv.R +++ b/tests/testthat/test-read_portfolio_csv.R @@ -200,3 +200,56 @@ test_that("deals with a portfolio CSV with no header and only one row of data", result <- read_portfolio_csv(csv_file) expect_equal(unlist(result), unlist(portfolio_min)) }) + +test_that("reads and combines multiple, proper portfolio CSVs correctly", { + csv_file <- withr::local_tempfile(fileext = ".csv") + readr::write_csv(portfolio_min, file = csv_file) + + result <- read_portfolio_csv(c(csv_file, csv_file)) + expect_true(names(result)[[1L]] == "filepath") + expect_true(nrow(result) == 2L) + expect_equal(result[ , -1L], rbind(portfolio_min, portfolio_min), ignore_attr = TRUE) +}) + +test_that("reads and combines multiple portfolio CSVs correctly when one is not proper", { + csv_file <- withr::local_tempfile(fileext = ".csv") + csv_file2 <- withr::local_tempfile(fileext = ".csv") + readr::write_csv(portfolio_min, file = csv_file) + readr::write_csv(portfolio_min[0, ], file = csv_file2) + + result <- read_portfolio_csv(c(csv_file, csv_file2)) + expect_true(names(result)[[1L]] == "filepath") + expect_true(nrow(result) == 1L) + expect_equal(result[ , -1L], portfolio_min, ignore_attr = TRUE) +}) + +test_that("reads and combines multiple, proper portfolio CSVs correctly into a list", { + csv_file <- withr::local_tempfile(fileext = ".csv") + csv_file <- gsub("//", "/", csv_file) + readr::write_csv(portfolio_min, file = csv_file) + + result <- read_portfolio_csv(c(csv_file, csv_file), combine = FALSE) + expect_type(result, "list") + expect_equal(length(result), 2L) + expect_named(result) + expect_equal(names(result), c(csv_file, csv_file)) + expect_equal(result[[1L]], portfolio_min, ignore_attr = TRUE) + expect_equal(result[[2L]], portfolio_min, ignore_attr = TRUE) +}) + +test_that("reads and combines multiple portfolio CSVs correctly into a list when one is not proper", { + csv_file <- withr::local_tempfile(fileext = ".csv") + csv_file2 <- withr::local_tempfile(fileext = ".csv") + csv_file <- gsub("//", "/", csv_file) + csv_file2 <- gsub("//", "/", csv_file2) + readr::write_csv(portfolio_min, file = csv_file) + readr::write_csv(portfolio_min[0, ], file = csv_file2) + + result <- read_portfolio_csv(c(csv_file, csv_file2), combine = FALSE) + expect_type(result, "list") + expect_equal(length(result), 2L) + expect_named(result) + expect_equal(names(result), c(csv_file, csv_file2)) + expect_equal(result[[1L]], portfolio_min, ignore_attr = TRUE) + expect_equal(result[[2L]], NA) +}) From 20e6be17047a8abb5eace78a1b85d23d86311c98 Mon Sep 17 00:00:00 2001 From: CJ Yetman - RMI Date: Sun, 10 Mar 2024 12:01:45 +0100 Subject: [PATCH 2/2] just check filename to avoid weird temp paths on Windows --- tests/testthat/test-read_portfolio_csv.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-read_portfolio_csv.R b/tests/testthat/test-read_portfolio_csv.R index 1aac9fe..2bdcdcf 100644 --- a/tests/testthat/test-read_portfolio_csv.R +++ b/tests/testthat/test-read_portfolio_csv.R @@ -207,6 +207,7 @@ test_that("reads and combines multiple, proper portfolio CSVs correctly", { result <- read_portfolio_csv(c(csv_file, csv_file)) expect_true(names(result)[[1L]] == "filepath") + expect_equal(basename(result[[1L]]), basename(c(csv_file, csv_file))) expect_true(nrow(result) == 2L) expect_equal(result[ , -1L], rbind(portfolio_min, portfolio_min), ignore_attr = TRUE) }) @@ -219,6 +220,7 @@ test_that("reads and combines multiple portfolio CSVs correctly when one is not result <- read_portfolio_csv(c(csv_file, csv_file2)) expect_true(names(result)[[1L]] == "filepath") + expect_equal(basename(result[[1L]]), basename(csv_file)) expect_true(nrow(result) == 1L) expect_equal(result[ , -1L], portfolio_min, ignore_attr = TRUE) }) @@ -232,7 +234,7 @@ test_that("reads and combines multiple, proper portfolio CSVs correctly into a l expect_type(result, "list") expect_equal(length(result), 2L) expect_named(result) - expect_equal(names(result), c(csv_file, csv_file)) + expect_equal(basename(names(result)), basename(c(csv_file, csv_file))) expect_equal(result[[1L]], portfolio_min, ignore_attr = TRUE) expect_equal(result[[2L]], portfolio_min, ignore_attr = TRUE) }) @@ -249,7 +251,7 @@ test_that("reads and combines multiple portfolio CSVs correctly into a list when expect_type(result, "list") expect_equal(length(result), 2L) expect_named(result) - expect_equal(names(result), c(csv_file, csv_file2)) + expect_equal(basename(names(result)), basename(c(csv_file, csv_file2))) expect_equal(result[[1L]], portfolio_min, ignore_attr = TRUE) expect_equal(result[[2L]], NA) })