diff --git a/.Rbuildignore b/.Rbuildignore index 5163d0b..33144e6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1 +1,6 @@ +.dockerignore +.github/ +.lintr ^LICENSE\.md$ +Dockerfile +docker-compose.yml diff --git a/.lintr b/.lintr new file mode 100644 index 0000000..9d708ae --- /dev/null +++ b/.lintr @@ -0,0 +1,4 @@ +linters: all_linters() +exclusions: list( + "tests/testthat.R" + ) diff --git a/DESCRIPTION b/DESCRIPTION index 19bed38..bc448c3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: workflow.portfolio.parsing Title: Reads, cleans, and reexports portfolios for PACTA -Version: 0.1.0.9000 +Version: 0.1.0.9001 Authors@R: c( person( @@ -27,14 +27,18 @@ RoxygenNote: 7.3.1 Imports: digest, dplyr, + jsonlite, jsonvalidate (>= 1.4.0), logger, + methods, pacta.portfolio.import, uuid Remotes: RMI-PACTA/pacta.portfolio.import, ropensci/jsonvalidate Suggests: + rlang, testthat (>= 3.0.0), + tibble, withr Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 4559433..0cfd8bb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand +export(export_portfolio) export(process_directory) export(reexport_portfolio) diff --git a/R/export_portfolio.R b/R/export_portfolio.R index 8c32093..7f03a67 100644 --- a/R/export_portfolio.R +++ b/R/export_portfolio.R @@ -1,4 +1,17 @@ -#' @ export +#' export a PACTA portfolio in a standard format +#' +#' This function takes a pacta portfolio as a data frame, and exports it to a +#' csv file in `output_directory`. optionally (default on), it can validate the +#' metadata that will be attached to this file export. +#' +#' @param portfolio_data data frame with the portfolio data +#' @param group_data list or data frame with the group data +#' @param output_directory character with the directory where the file will be +#' @param validate logical, should the output be validated against the schema? +#' +#' @return portfolio metadata (as nested list) for exported file, pramirly +#' called for side effect of writing file to disk. +#' @export export_portfolio <- function( portfolio_data, group_data, @@ -21,7 +34,7 @@ export_portfolio <- function( if (length(missing_cols)) { logger::log_warn( "Missing columns detected in portfolio data: ", - missing_cols, + missing_cols ) stop("Missing columns detected in portfolio data.") } @@ -45,7 +58,7 @@ export_portfolio <- function( ) logger::log_trace("Writing portfolio data to file: ", output_filepath) - write.csv( + utils::write.csv( x = portfolio_data, file = output_filepath, row.names = FALSE, @@ -75,7 +88,7 @@ export_portfolio <- function( logger::log_trace("Validating output.") schema_serialize( object = list(portfolio_metadata), - reference = "#/items/properties/portfolios" + reference = "#/items/properties/portfolios" # nolint: nonportable_path_linter ) } else { logger::log_trace("Skipping JSON validation.") diff --git a/R/process_directory.R b/R/process_directory.R index 4e7bd6c..841eaf2 100644 --- a/R/process_directory.R +++ b/R/process_directory.R @@ -1,7 +1,19 @@ +#' re-export a directory of PACTA portfolios in a standard format +#' +#' This function takes a directory containing pacta portfolios as a file, and +#' exports them to a csv file in `output_directory`. optionally (default on), +#' it can validate the metadata that will be attached to this file export. +#' +#' @param input_directory path to directory with input files +#' @param output_directory character with the directory where the file will be +#' @param validate logical, should the output be validated against the schema? +#' +#' @return portfolio metadata (as nested list) for exported files, primarily +#' called for side effect of writing files to disk. #' @export process_directory <- function( - input_directory = "/mnt/input", - output_directory = "/mnt/output", + input_directory = "/mnt/input", # nolint: nonportable_path_linter + output_directory = "/mnt/output", # nolint: nonportable_path_linter validate = TRUE ) { # Get the list of files in the directory diff --git a/R/reexport_portfolio.R b/R/reexport_portfolio.R index 3523564..f73cdb5 100644 --- a/R/reexport_portfolio.R +++ b/R/reexport_portfolio.R @@ -1,3 +1,15 @@ +#' re-export a PACTA portfolio in a standard format +#' +#' This function takes a pacta portfolio as a file, and exports it to a +#' csv file in `output_directory`. optionally (default on), it can validate the +#' metadata that will be attached to this file export. +#' +#' @param input_filepath path to input file +#' @param output_directory character with the directory where the file will be +#' @param validate logical, should the output be validated against the schema? +#' +#' @return portfolio metadata (as nested list) for exported file, primarily +#' called for side effect of writing file to disk. #' @export reexport_portfolio <- function( input_filepath, @@ -93,7 +105,7 @@ reexport_portfolio <- function( logger::log_trace("Validating output.") schema_serialize( object = file_summary, - reference = "#/items" + reference = "#/items" # nolint: nonportable_path_linter ) } else { logger::log_trace("Skipping JSON validation.") diff --git a/R/system_info.R b/R/system_info.R index 9bca97d..e027d3c 100644 --- a/R/system_info.R +++ b/R/system_info.R @@ -1,11 +1,11 @@ get_system_info <- function() { logger::log_trace("Getting system information") - package <- getPackageName() - version <- as.character(packageVersion(package)) + package <- methods::getPackageName() + version <- as.character(utils::packageVersion(package)) logger::log_trace("Package: ", package, " version: ", version) raw_deps <- trimws( strsplit( - x = packageDescription(package)[["Imports"]], + x = utils::packageDescription(package)[["Imports"]], split = ",", fixed = TRUE )[[1L]] @@ -23,7 +23,7 @@ get_system_info <- function() { FUN = function(x) { list( package = x, - version = as.character(packageVersion(x)) + version = as.character(utils::packageVersion(x)) ) } ) diff --git a/man/export_portfolio.Rd b/man/export_portfolio.Rd new file mode 100644 index 0000000..231d463 --- /dev/null +++ b/man/export_portfolio.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/export_portfolio.R +\name{export_portfolio} +\alias{export_portfolio} +\title{export a PACTA portfolio in a standard format} +\usage{ +export_portfolio(portfolio_data, group_data, output_directory, validate = TRUE) +} +\arguments{ +\item{portfolio_data}{data frame with the portfolio data} + +\item{group_data}{list or data frame with the group data} + +\item{output_directory}{character with the directory where the file will be} + +\item{validate}{logical, should the output be validated against the schema?} +} +\value{ +portfolio metadata (as nested list) for exported file, pramirly +called for side effect of writing file to disk. +} +\description{ +This function takes a pacta portfolio as a data frame, and exports it to a +csv file in \code{output_directory}. optionally (default on), it can validate the +metadata that will be attached to this file export. +} diff --git a/man/process_directory.Rd b/man/process_directory.Rd new file mode 100644 index 0000000..b9b3da3 --- /dev/null +++ b/man/process_directory.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/process_directory.R +\name{process_directory} +\alias{process_directory} +\title{re-export a directory of PACTA portfolios in a standard format} +\usage{ +process_directory( + input_directory = "/mnt/input", + output_directory = "/mnt/output", + validate = TRUE +) +} +\arguments{ +\item{input_directory}{path to directory with input files} + +\item{output_directory}{character with the directory where the file will be} + +\item{validate}{logical, should the output be validated against the schema?} +} +\value{ +portfolio metadata (as nested list) for exported files, primarily +called for side effect of writing files to disk. +} +\description{ +This function takes a directory containing pacta portfolios as a file, and +exports them to a csv file in \code{output_directory}. optionally (default on), +it can validate the metadata that will be attached to this file export. +} diff --git a/man/reexport_portfolio.Rd b/man/reexport_portfolio.Rd new file mode 100644 index 0000000..21f0401 --- /dev/null +++ b/man/reexport_portfolio.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/reexport_portfolio.R +\name{reexport_portfolio} +\alias{reexport_portfolio} +\title{re-export a PACTA portfolio in a standard format} +\usage{ +reexport_portfolio(input_filepath, output_directory, validate = TRUE) +} +\arguments{ +\item{input_filepath}{path to input file} + +\item{output_directory}{character with the directory where the file will be} + +\item{validate}{logical, should the output be validated against the schema?} +} +\value{ +portfolio metadata (as nested list) for exported file, primarily +called for side effect of writing file to disk. +} +\description{ +This function takes a pacta portfolio as a file, and exports it to a +csv file in \code{output_directory}. optionally (default on), it can validate the +metadata that will be attached to this file export. +} diff --git a/tests/testthat/foo.csv b/tests/testthat/foo.csv new file mode 100644 index 0000000..e69de29 diff --git a/tests/testthat/helper-expect_conditions.R b/tests/testthat/helper-expect_conditions.R new file mode 100644 index 0000000..f745700 --- /dev/null +++ b/tests/testthat/helper-expect_conditions.R @@ -0,0 +1,50 @@ +# check for multiple conditions in a function's output. +# matches by regexp as documented in expect_warning, etc. +expect_multiple_conditions <- function( + object, + error = NULL, + warning = NULL, + message = NULL +) { + if (length(error) > 0L) { + testthat::expect_error( + object = { + expect_multiple_conditions( + object = object, + message = message, + warning = warning, + error = error[-1L] + ) + }, + regexp = error[[1L]] + ) + return(invisible(NULL)) + } else if (length(warning) > 0L) { + testthat::expect_warning( + object = { + out <- expect_multiple_conditions( + object = object, + message = message, + warning = warning[-1L], + error = error + ) + }, + regexp = warning[[1L]] + ) + } else if (length(message) > 0L) { + testthat::expect_message( + object = { + out <- expect_multiple_conditions( + object = object, + message = message[-1L], + warning = warning, + error = error + ) + }, + regexp = message[[1L]] + ) + } else { + out <- {{ object }} + } + return(invisible(out)) +} diff --git a/tests/testthat/helper-portfolios.R b/tests/testthat/helper-portfolios.R new file mode 100644 index 0000000..41875d9 --- /dev/null +++ b/tests/testthat/helper-portfolios.R @@ -0,0 +1,32 @@ +# Utility functions +change_colnames <- function(x, colnames) { + colnames(x) <- colnames + return(x) +} + +# Groups +empty_groups <- data.frame() + +simple_groups <- tibble::tribble( + ~investor_name, ~portfolio_name, + "Simple Investor", "Simple Portfolio" +) + +# Portfolios +simple_portfolio <- tibble::tribble( + ~isin, ~market_value, ~currency, + "GB0007980591", 10000L, "USD" +) + +simple_portfolio_all_columns <- dplyr::select( + .data = dplyr::mutate( + .data = simple_portfolio, + portfolio_name = "Simple Portfolio", + investor_name = "Simple Investor" + ), + investor_name, + portfolio_name, + isin, + market_value, + currency +) diff --git a/tests/testthat/helper-simple_output.R b/tests/testthat/helper-simple_output.R index 600b718..279be39 100644 --- a/tests/testthat/helper-simple_output.R +++ b/tests/testthat/helper-simple_output.R @@ -1,16 +1,24 @@ -simple_portfolio_hash <- digest::digest( - object = testthat::test_path( - "testdata", "portfolios", "output_simple.csv" - ), - file = TRUE, - algo = "md5" -) +simple_portfolio_hash <- local({ + ff <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = simple_portfolio, + file = ff, + row.names = FALSE, + na = "", + fileEncoding = "UTF-8" + ) + digest::digest( + object = ff, + file = TRUE, + algo = "md5" + ) +}) expect_simple_portfolio_file <- function(filepath) { # Checking that output file exists. testthat::expect_true(file.exists(filepath)) # Checking that output file has correct hash. - testthat::expect_equal( + testthat::expect_identical( digest::digest( object = filepath, file = TRUE, @@ -19,9 +27,9 @@ expect_simple_portfolio_file <- function(filepath) { simple_portfolio_hash ) - file_contents <- read.csv(filepath) + file_contents <- read.csv(filepath, stringsAsFactors = FALSE) # Check that output file has correct column names. - testthat::expect_equal( + testthat::expect_identical( colnames(file_contents), c( "isin", @@ -30,15 +38,15 @@ expect_simple_portfolio_file <- function(filepath) { ) ) # Check that output file has correct column types. - testthat::expect_equal(class(file_contents[["isin"]]), "character") + testthat::expect_type(file_contents[["isin"]], "character") testthat::expect_in( class(file_contents[["market_value"]]), c("numeric", "integer") ) - testthat::expect_equal(class(file_contents[["currency"]]), "character") + testthat::expect_type(file_contents[["currency"]], "character") # Check file encoding - testthat::expect_equal( + testthat::expect_identical( pacta.portfolio.import::guess_file_encoding(filepath), "ascii" ) @@ -66,15 +74,15 @@ expect_simple_export_portfolio <- function( ) # Check investor and portfolio names - # Note that expect_equal() works for comparing NULL - testthat::expect_equal(metadata[["investor_name"]], investor_name) - testthat::expect_equal(metadata[["portfolio_name"]], portfolio_name) + # Note that expect_identical() works for comparing NULL + testthat::expect_identical(metadata[["investor_name"]], investor_name) + testthat::expect_identical(metadata[["portfolio_name"]], portfolio_name) # Checking that output file has correct number of rows. - testthat::expect_equal(metadata[["output_rows"]], 1L) + testthat::expect_identical(metadata[["output_rows"]], 1L) # read file (should be small) # check that metadata row count is same as actual file_content_rows <- expect_simple_portfolio_file(output_filepath) - testthat::expect_equal( + testthat::expect_identical( metadata[["output_rows"]], file_content_rows ) @@ -121,7 +129,7 @@ expect_simple_reexport <- function( testthat::expect_null(metadata[["warnings"]]) testthat::expect_null(metadata[["errors"]]) - observed_groups <- groups[0, ] + observed_groups <- groups[0L, ] # for each entry in the metadata for (x in metadata[["portfolios"]]) { # check that the output file exists @@ -160,7 +168,7 @@ expect_simple_reexport <- function( "investor_name" %in% names(groups) && "portfolio_name" %in% names(groups) ) { - testthat::expect_equal( + testthat::expect_identical( nrow(groups[ groups[["investor_name"]] == x[["investor_name"]] & groups[["portfolio_name"]] == x[["portfolio_name"]], @@ -172,36 +180,36 @@ expect_simple_reexport <- function( portfolio_name = x[["portfolio_name"]] ) } else if ("investor_name" %in% names(groups)) { - testthat::expect_equal( + testthat::expect_identical( nrow(groups[ groups[["investor_name"]] == x[["investor_name"]], ]), 1L ) - testthat::expect_equal(x[["portfolio_name"]], NULL) + testthat::expect_null(x[["portfolio_name"]]) this_group <- data.frame( investor_name = x[["investor_name"]] ) } else if ("portfolio_name" %in% names(groups)) { - testthat::expect_equal( + testthat::expect_identical( nrow(groups[ groups[["portfolio_name"]] == x[["portfolio_name"]], ]), 1L ) - testthat::expect_equal(x[["investor_name"]], NULL) + testthat::expect_null(x[["investor_name"]]) this_group <- data.frame( portfolio_name = x[["portfolio_name"]] ) } else { - testthat::expect_equal(x[["investor_name"]], NULL) - testthat::expect_equal(x[["portfolio_name"]], NULL) + testthat::expect_null(x[["investor_name"]]) + testthat::expect_null(x[["portfolio_name"]]) this_group <- data.frame() } observed_groups <- dplyr::bind_rows(observed_groups, this_group) } # Test that all observed groups are the expected groups - testthat::expect_equal( + testthat::expect_identical( dplyr::arrange(observed_groups, !!!rlang::syms(colnames(groups))), dplyr::arrange(groups, !!!rlang::syms(colnames(groups))) ) diff --git a/tests/testthat/test-export.R b/tests/testthat/test-export.R index 92cf623..ed00bc2 100644 --- a/tests/testthat/test-export.R +++ b/tests/testthat/test-export.R @@ -3,17 +3,8 @@ old_threshold <- logger::log_threshold() withr::defer(logger::log_threshold(old_threshold)) logger::log_threshold("FATAL") -# establish testing tempdir -test_dir <- tempdir() -withr::defer(unlink(test_dir)) - -empty_groups <- data.frame() -simple_portfolio <- tibble::tribble( - ~isin, ~market_value, ~currency, - "GB0007980591", 10000, "USD" -) - test_that("exporting a file works, no grouping", { + test_dir <- withr::local_tempdir() metadata <- export_portfolio( portfolio_data = simple_portfolio, group_data = empty_groups, @@ -23,9 +14,10 @@ test_that("exporting a file works, no grouping", { }) test_that("exporting works, against reordered columns", { - reordered_portfolio <- tibble::tribble( - ~isin, ~currency, ~market_value, - "GB0007980591", "USD", 10000 + test_dir <- withr::local_tempdir() + reordered_portfolio <- dplyr::select( + .data = simple_portfolio, + market_value, isin, currency ) metadata <- export_portfolio( portfolio_data = reordered_portfolio, @@ -36,12 +28,13 @@ test_that("exporting works, against reordered columns", { }) test_that("exporting works, against extra columns", { - extra_cols_portfolio <- tibble::tribble( - ~isin, ~currency, ~market_value, ~foo, - "GB0007980591", "USD", 10000, "foo" + test_dir <- withr::local_tempdir() + extra_cols_portfolio <- dplyr::mutate( + .data = simple_portfolio, + foo = "bar" ) expect_warning( - metadata <- export_portfolio( + metadata <- export_portfolio( # nolint: implicit_assignment_linter portfolio_data = extra_cols_portfolio, group_data = empty_groups, output_directory = test_dir @@ -52,9 +45,10 @@ test_that("exporting works, against extra columns", { }) test_that("exporting fails when missing columns", { - missing_cols_portfolio <- tibble::tribble( - ~isin, ~currency, - "GB0007980591", "USD" + test_dir <- withr::local_tempdir() + missing_cols_portfolio <- dplyr::select( + .data = simple_portfolio, + -market_value ) expect_error( export_portfolio( @@ -67,10 +61,7 @@ test_that("exporting fails when missing columns", { }) test_that("exporting a file works, simple grouping", { - simple_groups <- tibble::tribble( - ~investor_name, ~portfolio_name, - "Simple Investor", "Simple Portfolio" - ) + test_dir <- withr::local_tempdir() metadata <- export_portfolio( portfolio_data = simple_portfolio, group_data = simple_groups, @@ -85,13 +76,14 @@ test_that("exporting a file works, simple grouping", { }) test_that("exporting a file works, port name grouping", { - simple_groups <- tibble::tribble( + test_dir <- withr::local_tempdir() + port_groups <- tibble::tribble( ~portfolio_name, "Simple Portfolio" ) metadata <- export_portfolio( portfolio_data = simple_portfolio, - group_data = simple_groups, + group_data = port_groups, output_directory = test_dir ) expect_simple_export_portfolio( @@ -103,13 +95,14 @@ test_that("exporting a file works, port name grouping", { }) test_that("exporting a file works, investor name grouping", { - simple_groups <- tibble::tribble( + test_dir <- withr::local_tempdir() + investor_group <- tibble::tribble( ~investor_name, "Simple Investor" ) metadata <- export_portfolio( portfolio_data = simple_portfolio, - group_data = simple_groups, + group_data = investor_group, output_directory = test_dir ) expect_simple_export_portfolio( diff --git a/tests/testthat/test-process_directory.R b/tests/testthat/test-process_directory.R index a9a6aa1..8a2901c 100644 --- a/tests/testthat/test-process_directory.R +++ b/tests/testthat/test-process_directory.R @@ -3,12 +3,6 @@ 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" -) - json_validator <- jsonvalidate::json_schema[["new"]]( schema = system.file( "extdata", "schema", "parsedPortfolio_0-1-0.json", @@ -19,37 +13,34 @@ json_validator <- jsonvalidate::json_schema[["new"]]( ) test_that("Processing a directory with a single file works.", { - test_file <- testthat::test_path( - "testdata", "portfolios", "simple.csv" + test_dir <- withr::local_tempdir() + input_dir <- withr::local_tempdir(pattern = "input") + test_file <- withr::local_tempfile(fileext = ".csv", tmpdir = input_dir) + write.csv( + x = simple_portfolio, + file = test_file, + row.names = FALSE, + quote = FALSE ) 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 + output_directory = test_dir ) expect_simple_reexport( - output_dir = output_dir, + output_dir = test_dir, metadata = metadata[[1L]], groups = empty_groups, input_digest = filehash, - input_filename = "foo.csv", + input_filename = basename(test_file), input_entries = 1L ) - metadata_file <- file.path(output_dir, "processed_portfolios.json") + metadata_file <- file.path(test_dir, "processed_portfolios.json") expect_true(file.exists(metadata_file)) expect_true(json_validator[["validate"]]( json = metadata_file, @@ -58,50 +49,65 @@ test_that("Processing a directory with a single file works.", { }) test_that("Processing a directory with a multiple files works.", { - test_file <- testthat::test_path( - "testdata", "portfolios", "simple.csv" + test_dir <- withr::local_tempdir() + input_dir <- withr::local_tempdir(pattern = "input") + test_file <- withr::local_tempfile( + fileext = ".csv", + tmpdir = input_dir, + pattern = "portfolio1" + ) + write.csv( + x = simple_portfolio, + file = test_file, + row.names = FALSE, + quote = FALSE ) 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") + + test_file2 <- withr::local_tempfile( + fileext = ".csv", + tmpdir = input_dir, + pattern = "portfolio2" ) - file.copy( - from = test_file, - to = file.path(input_dir, "foo2.csv") + write.csv( + x = simple_portfolio_all_columns, + file = test_file2, + row.names = FALSE, + quote = FALSE ) + filehash2 <- digest::digest( + object = test_file2, + file = TRUE, + algo = "md5" + ) + metadata <- process_directory( input_directory = input_dir, - output_directory = output_dir + output_directory = test_dir ) + expect_simple_reexport( - output_dir = output_dir, + output_dir = test_dir, metadata = metadata[[1L]], groups = empty_groups, input_digest = filehash, - input_filename = "foo1.csv", + input_filename = basename(test_file), input_entries = 1L ) expect_simple_reexport( - output_dir = output_dir, + output_dir = test_dir, metadata = metadata[[2L]], - groups = empty_groups, - input_digest = filehash, - input_filename = "foo2.csv", + groups = simple_groups, + input_digest = filehash2, + input_filename = basename(test_file2), input_entries = 1L ) - metadata_file <- file.path(output_dir, "processed_portfolios.json") - expect_true(file.exists(file.path(output_dir, "processed_portfolios.json"))) + metadata_file <- file.path(test_dir, "processed_portfolios.json") + expect_true(file.exists(file.path(test_dir, "processed_portfolios.json"))) expect_true(json_validator[["validate"]]( json = metadata_file, verbose = TRUE diff --git a/tests/testthat/test-reexport-columns.R b/tests/testthat/test-reexport-columns.R index 501792c..0d389f4 100644 --- a/tests/testthat/test-reexport-columns.R +++ b/tests/testthat/test-reexport-columns.R @@ -3,52 +3,213 @@ old_threshold <- logger::log_threshold() withr::defer(logger::log_threshold(old_threshold)) logger::log_threshold("FATAL") -# establish testing tempdir -test_dir <- tempdir() -withr::defer(unlink(test_dir)) - -simple_groups <- tibble::tribble( - ~investor_name, ~portfolio_name, - "Simple Investor", "Simple Portfolio" -) +test_that("re-exporting with extra columns works", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = dplyr::mutate( + .data = simple_portfolio_all_columns, + foo = "bar" + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + metadata <- reexport_portfolio( # nolint: implicit_assignment_linter + input_filepath = test_file, + output_directory = test_dir + ) + expect_simple_reexport( + output_dir = test_dir, + metadata = metadata, + groups = simple_groups, + input_digest = filehash, + input_filename = basename(test_file), + input_entries = 1L + ) +}) +test_that("re-exporting fails with missing columns - investor_name", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = dplyr::select( + .data = simple_portfolio_all_columns, + -investor_name + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + testthat::expect_warning( + testthat::expect_warning( + metadata <- reexport_portfolio( # nolint: implicit_assignment_linter + input_filepath = test_file, + output_directory = test_dir + ), + "^No portfolio data detected in file.$" + ), + "^Object could not be validated against schema.$" + ) + expect_reexport_failure( + metadata = metadata, + input_filename = basename(test_file), + input_digest = filehash + ) +}) -files_to_test <- c( - # "simple_all-columns_extra_columns.csv", #TODO: enable this test - # "simple_all-columns_reordered.csv", #TODO: enable this test - "simple_extra_columns.csv" #, - # "simple_investorname.csv", #TODO: enable this test - # "simple_missing-currency.csv", #TODO: enable this test - # "simple_missing-isin.csv", #TODO: enable this test - # "simple_missing-marketvalue.csv", #TODO: enable this test - # "simple_portfolioname.csv", #TODO: enable this test - # "simple_reordered.csv" #TODO: enable this test -) +test_that("re-exporting fails with missing columns - portfolio_name", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = dplyr::select( + .data = simple_portfolio_all_columns, + -portfolio_name + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + testthat::expect_warning( + testthat::expect_warning( + metadata <- reexport_portfolio( # nolint: implicit_assignment_linter + input_filepath = test_file, + output_directory = test_dir + ), + "^No portfolio data detected in file.$" + ), + "^Object could not be validated against schema.$" + ) + expect_reexport_failure( + metadata = metadata, + input_filename = basename(test_file), + input_digest = filehash + ) +}) -for (filename in files_to_test) { - test_that(paste("re-exporting fails with missing columns -", filename), { - test_file <- testthat::test_path( - "testdata", "portfolios", "columns", filename - ) - filehash <- digest::digest( - object = test_file, - file = TRUE, - algo = "md5" - ) +test_that("re-exporting fails with missing columns - currency", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = dplyr::select( + .data = simple_portfolio_all_columns, + -currency + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + testthat::expect_warning( testthat::expect_warning( testthat::expect_warning( - metadata <- reexport_portfolio( - input_filepath = test_file, - output_directory = test_dir + testthat::expect_warning( + metadata <- reexport_portfolio( # nolint: implicit_assignment_linter + input_filepath = test_file, + output_directory = test_dir + ), + regexp = "^No portfolio data detected in file.$" ), - "^No portfolio data detected in file.$" + regexp = "^Object could not be validated against schema.$" ), - "^Object could not be validated against schema.$" + regexp = "column could not be determined" + ), + regexp = "column could not be determined" + ) + expect_reexport_failure( + metadata = metadata, + input_filename = basename(test_file), + input_digest = filehash + ) +}) + +test_that("re-exporting fails with missing columns - isin", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = dplyr::select( + .data = simple_portfolio_all_columns, + -isin + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + expect_multiple_conditions( + metadata <- reexport_portfolio( # nolint: implicit_assignment_linter + input_filepath = test_file, + output_directory = test_dir + ), + warning = c( + "^No portfolio data detected in file.$", + "^Object could not be validated against schema.$", + "column could not be determined", + "column could not be determined" ) - expect_reexport_failure( - metadata = metadata, - input_filename = basename(test_file), - input_digest = filehash + ) + expect_reexport_failure( + metadata = metadata, + input_filename = basename(test_file), + input_digest = filehash + ) +}) + +test_that("re-exporting fails with missing columns - market_value", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = dplyr::select( + .data = simple_portfolio_all_columns, + -market_value + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + expect_multiple_conditions( + metadata <- reexport_portfolio( # nolint: implicit_assignment_linter + input_filepath = test_file, + output_directory = test_dir + ), + warning = c( + "^No portfolio data detected in file.$", + "^Object could not be validated against schema.$", + "column could not be determined", + "column could not be determined" ) - }) -} + ) + expect_reexport_failure( + metadata = metadata, + input_filename = basename(test_file), + input_digest = filehash + ) +}) diff --git a/tests/testthat/test-reexport-headers.R b/tests/testthat/test-reexport-headers.R index c13e3dc..90dd49f 100644 --- a/tests/testthat/test-reexport-headers.R +++ b/tests/testthat/test-reexport-headers.R @@ -3,52 +3,139 @@ old_threshold <- logger::log_threshold() withr::defer(logger::log_threshold(old_threshold)) logger::log_threshold("FATAL") -# establish testing tempdir -test_dir <- tempdir() -withr::defer(unlink(test_dir)) - -simple_groups <- tibble::tribble( - ~investor_name, ~portfolio_name, - "Simple Investor", "Simple Portfolio" -) - -files_to_test <- c( - "simple_all-columns_headers-camelcase.csv", - "simple_all-columns_headers-demo.csv", - "simple_all-columns_headers-dot.csv", - # "simple_all-columns_headers-doublepadded.csv", #TODO: enable this test - "simple_all-columns_headers-mixed.csv", - # "simple_all-columns_headers-none.csv", - "simple_all-columns_headers-nosep-lowercase.csv", - "simple_all-columns_headers-nosep-uppercase.csv", - "simple_all-columns_headers-padded.csv", - "simple_all-columns_headers-quoted.csv", - "simple_all-columns_headers-space.csv", - "simple_all-columns_headers-underscore.csv"#, - # "simple_headers-none.csv" +colnames_to_test <- list( + camelcase = c( + "investorName", "portfolioName", "isin", "marketValue", "currency" + ), + demo = c( + "Investor.Name", "Portfolio.Name", "ISIN", "MarketValue", "Currency" + ), + dot = c( + "Investor.Name", "Portfolio.Name", "ISIN", "Market.Value", "Currency" + ), + doublepadded = c( + " Investor.Name ", " Portfolio.Name ", " ISIN ", + " MarketValue ", " Currency " + ), + mixed = c( + "INVESTOR.NAME", "PortfolioName", "isin", "market_value", " Currency" + ), + lowercase_nosep = c( + "investorname", "portfolioname", "isin", "marketvalue", "currency" + ), + uppercase_nosep = c( + "INVESTORNAME", "PORTFOLIONAME", "ISIN", "MARKETVALUE", "CURRENCY" + ), + padded = c( + " Investor.Name ", " Portfolio.Name ", " ISIN ", + " MarketValue ", " Currency " + ), + quoted = c( + "\"investor_name\"", "\"portfolio_name\"", "\"isin\"", + "\"market_value\"", "\"currency\"" + ), + space = c( + "Investor Name", "Portfolio Name", "ISIN", "Market Value", "Currency" + ), + underscore = c( + "investor_name", "portfolio_name", "isin", "market_value", "currency" + ) ) -for (filename in files_to_test) { - test_that(paste("re-exporting robust header formats -", filename), { - test_file <- testthat::test_path( - "testdata", "portfolios", "headers", filename - ) - filehash <- digest::digest( - object = test_file, - file = TRUE, - algo = "md5" - ) - metadata <- reexport_portfolio( - input_filepath = test_file, - output_directory = test_dir - ) - expect_simple_reexport( - output_dir = test_dir, - metadata = metadata, - groups = simple_groups, - input_digest = filehash, - input_filename = basename(test_file), - input_entries = 1L - ) - }) +for (x in seq_along(colnames_to_test)) { + testthat::test_that( + desc = paste( # nolint: indentation_linter + "re-exporting robust header formats -", + names(colnames_to_test)[x] + ), + code = { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = change_colnames( + x = simple_portfolio_all_columns, + colnames = colnames_to_test[[x]] + ), + file = test_file, + row.names = FALSE, + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + metadata <- reexport_portfolio( + input_filepath = test_file, + output_directory = test_dir + ) + expect_simple_reexport( + output_dir = test_dir, + metadata = metadata, + groups = simple_groups, + input_digest = filehash, + input_filename = basename(test_file), + input_entries = 1L + ) + }) } + +test_that("re-exporting robust header formats - no headers simple", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.table( + x = simple_portfolio, + file = test_file, + row.names = FALSE, + col.names = FALSE, + sep = ",", + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + metadata <- reexport_portfolio( + input_filepath = test_file, + output_directory = test_dir + ) + expect_simple_reexport( + output_dir = test_dir, + metadata = metadata, + groups = empty_groups, + input_digest = filehash, + input_filename = basename(test_file), + input_entries = 1L + ) +}) + +test_that("re-exporting robust header formats - no headers all_columns", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.table( + x = simple_portfolio_all_columns, + file = test_file, + row.names = FALSE, + col.names = FALSE, + sep = ",", + quote = FALSE + ) + filehash <- digest::digest( + object = test_file, + file = TRUE, + algo = "md5" + ) + metadata <- reexport_portfolio( + input_filepath = test_file, + output_directory = test_dir + ) + expect_simple_reexport( + output_dir = test_dir, + metadata = metadata, + groups = simple_groups, + input_digest = filehash, + input_filename = basename(test_file), + input_entries = 1L + ) +}) diff --git a/tests/testthat/test-reexport.R b/tests/testthat/test-reexport.R index 2b5bdc0..8c2683f 100644 --- a/tests/testthat/test-reexport.R +++ b/tests/testthat/test-reexport.R @@ -7,15 +7,14 @@ logger::log_threshold("FATAL") test_dir <- tempdir() withr::defer(unlink(test_dir)) -empty_groups <- data.frame() -simple_groups <- tibble::tribble( - ~investor_name, ~portfolio_name, - "Simple Investor", "Simple Portfolio" -) - test_that("re-exporting simple file works.", { - test_file <- testthat::test_path( - "testdata", "portfolios", "simple.csv" + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = simple_portfolio, + file = test_file, + row.names = FALSE, + quote = FALSE ) filehash <- digest::digest( object = test_file, @@ -36,32 +35,44 @@ test_that("re-exporting simple file works.", { ) }) -test_that("re-exporting simple exported file yields same file.", { - test_file <- testthat::test_path( - "testdata", "portfolios", "output_simple.csv" - ) - filehash <- digest::digest( - object = test_file, - file = TRUE, - algo = "md5" +test_that("re-exporting exported file yields same file.", { + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = simple_portfolio_all_columns, + file = test_file, + row.names = FALSE, + quote = FALSE + ) + metadata_input <- reexport_portfolio( + input_filepath = test_file, + output_directory = test_dir ) metadata <- reexport_portfolio( - input_filepath = test_file, + input_filepath = file.path( + test_dir, + metadata_input[["portfolios"]][[1L]][["output_filename"]] + ), output_directory = test_dir ) expect_simple_reexport( output_dir = test_dir, metadata = metadata, groups = empty_groups, - input_digest = filehash, - input_filename = basename(test_file), + input_digest = metadata_input[["portfolios"]][[1L]][["output_md5"]], + input_filename = metadata_input[["portfolios"]][[1L]][["output_filename"]], input_entries = 1L ) }) test_that("re-exporting simple file with all columns works", { - test_file <- testthat::test_path( - "testdata", "portfolios", "simple_all-columns.csv" + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + write.csv( + x = simple_portfolio_all_columns, + file = test_file, + row.names = FALSE, + quote = FALSE ) filehash <- digest::digest( object = test_file, @@ -83,18 +94,26 @@ test_that("re-exporting simple file with all columns works", { }) test_that("re-exporting empty file fails.", { - skip() #TODO: enable this test - test_file <- testthat::test_path( - "testdata", "portfolios", "simple_all-columns_empty.csv" - ) + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + file.create(test_file) + expect_identical(as.integer(file.size(test_file)), 0L) filehash <- digest::digest( object = test_file, file = TRUE, algo = "md5" ) - metadata <- reexport_portfolio( - input_filepath = test_file, - output_directory = test_dir + expect_multiple_conditions( + { + metadata <- reexport_portfolio( + input_filepath = test_file, + output_directory = test_dir + ) + }, + warning = c( + "No portfolio data detected in file.", + "Object could not be validated against schema." + ) ) expect_reexport_failure( metadata = metadata, @@ -104,8 +123,19 @@ test_that("re-exporting empty file fails.", { }) test_that("re-exporting multiportfolio file with all columns works", { - test_file <- testthat::test_path( - "testdata", "portfolios", "multi_simple_all-columns_portfolioname.csv" + test_dir <- withr::local_tempdir() + test_file <- withr::local_tempfile(fileext = ".csv") + groups <- tibble::tribble( + ~investor_name, ~portfolio_name, + "Simple Investor", "Portfolio A", + "Simple Investor", "Portfolio B" + ) + multi_port <- dplyr::cross_join(groups, simple_portfolio) + write.csv( + x = multi_port, + file = test_file, + row.names = FALSE, + quote = FALSE ) filehash <- digest::digest( object = test_file, @@ -116,11 +146,6 @@ test_that("re-exporting multiportfolio file with all columns works", { input_filepath = test_file, output_directory = test_dir ) - groups <- tibble::tribble( - ~investor_name, ~portfolio_name, - "Simple Investor", "Portfolio A", - "Simple Investor", "Portfolio B" - ) expect_simple_reexport( output_dir = test_dir, metadata = metadata, diff --git a/tests/testthat/testdata/portfolios/README.md b/tests/testthat/testdata/portfolios/README.md deleted file mode 100644 index 6bf991a..0000000 --- a/tests/testthat/testdata/portfolios/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# Sample Portfolios - -This diectory contains sample portfolios, which do contain real ISINs. -These ISINs are from company websites: - -| ISIN | Company | Notes | Source | -|---|---|---|---| -| AT0000720008 | A1 Group | | [a1.group](https://a1.group/investor-relations/) | -| AU000000RIO1 | Rio Tinto | Australian Securities Exchange | [riotinto.com](https://www.riotinto.com/en/invest/shareholder-information/share-price/share-series) | -| DE0005545503 | 1&1 | | [1und1.ag](https://www.1und1.ag/investor-relations-en) | -| DE0007231326 | Sixt | Common Stock | [sixt.com](https://about.sixt.com/en/investor-relations/) | -| DE0007231334 | Sixt | Common Stock | [sixt.com](https://about.sixt.com/en/investor-relations/) | -| GB0007188757 | Rio Tinto | London Stock Exchange | [riotinto.com](https://www.riotinto.com/en/invest/shareholder-information/share-price/share-series) | -| GB0007980591 | BP | Ordinary Share | [bp.com](https://www.bp.com/en/global/corporate/investors/shareholder-and-dividend-information/share-listing-information.html) | -| GB00BP6MXD84 | Shell | Shell plc Amsterdam | [shell.com](https://www.shell.com/investors/information-for-shareholders/share-information.html) | -| US0556221044 | BP | ADS | [bp.com](https://www.bp.com/en/global/corporate/investors/shareholder-and-dividend-information/share-listing-information.html) | -| US7672041008 | Rio Tinto | New York Stock Exchange | [riotinto.com](https://www.riotinto.com/en/invest/shareholder-information/share-price/share-series) | -| US7802593050 | Shell | ADS | [shell.com](https://www.shell.com/investors/information-for-shareholders/share-information.html) | diff --git a/tests/testthat/testdata/portfolios/columns/simple_all-columns_extra_columns.csv b/tests/testthat/testdata/portfolios/columns/simple_all-columns_extra_columns.csv deleted file mode 100644 index 19d667e..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_all-columns_extra_columns.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,portfolio_name,isin,market_value,currency,foo -Simple Investor,Simple Portfolio,GB0007980591,10000,USD,bar diff --git a/tests/testthat/testdata/portfolios/columns/simple_all-columns_reordered.csv b/tests/testthat/testdata/portfolios/columns/simple_all-columns_reordered.csv deleted file mode 100644 index d187dc7..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_all-columns_reordered.csv +++ /dev/null @@ -1,2 +0,0 @@ -currency,market_value,isin,portfolio_name,investor_name -USD,10000,GB0007980591,Simple Portfolio,Simple Investor diff --git a/tests/testthat/testdata/portfolios/columns/simple_extra_columns.csv b/tests/testthat/testdata/portfolios/columns/simple_extra_columns.csv deleted file mode 100644 index 9bb3220..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_extra_columns.csv +++ /dev/null @@ -1,2 +0,0 @@ -isin,market_value,currency,foo -GB0007980591,10000,USD,bar diff --git a/tests/testthat/testdata/portfolios/columns/simple_investorname.csv b/tests/testthat/testdata/portfolios/columns/simple_investorname.csv deleted file mode 100644 index ddc3d9c..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_investorname.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,isin,market_value,currency -Simple Investor,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/columns/simple_missing-currency.csv b/tests/testthat/testdata/portfolios/columns/simple_missing-currency.csv deleted file mode 100644 index 280a0b3..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_missing-currency.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,portfolio_name,isin,market_value -Simple Investor,Simple Portfolio,GB0007980591,10000 diff --git a/tests/testthat/testdata/portfolios/columns/simple_missing-isin.csv b/tests/testthat/testdata/portfolios/columns/simple_missing-isin.csv deleted file mode 100644 index 8dfcc18..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_missing-isin.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,portfolio_name,market_value,currency -Simple Investor,Simple Portfolio,10000,USD diff --git a/tests/testthat/testdata/portfolios/columns/simple_missing-marketvalue.csv b/tests/testthat/testdata/portfolios/columns/simple_missing-marketvalue.csv deleted file mode 100644 index d0ef2bb..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_missing-marketvalue.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,portfolio_name,isin,currency -Simple Investor,Simple Portfolio,GB0007980591,USD diff --git a/tests/testthat/testdata/portfolios/columns/simple_portfolioname.csv b/tests/testthat/testdata/portfolios/columns/simple_portfolioname.csv deleted file mode 100644 index ff433a9..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_portfolioname.csv +++ /dev/null @@ -1,2 +0,0 @@ -portfolio_name,isin,market_value,currency -Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/columns/simple_reordered.csv b/tests/testthat/testdata/portfolios/columns/simple_reordered.csv deleted file mode 100644 index 513322e..0000000 --- a/tests/testthat/testdata/portfolios/columns/simple_reordered.csv +++ /dev/null @@ -1,2 +0,0 @@ -currency,market_value,isin -USD,10000,GB0007980591 diff --git a/tests/testthat/testdata/portfolios/generate_portfolios.R b/tests/testthat/testdata/portfolios/generate_portfolios.R deleted file mode 100644 index c33df13..0000000 --- a/tests/testthat/testdata/portfolios/generate_portfolios.R +++ /dev/null @@ -1,355 +0,0 @@ -# Script to be run in-place (where the R file is found) -# Generates test files for the workflow.portfolio.parsing package - -library(dplyr) - -logger::log_info("Loading test data.") -simple_portfolio <- read.csv( - file = "output_simple.csv", - stringsAsFactors = FALSE -) - -logger::log_info("Generating test data.") -simple_portfolio_all_columns <- simple_portfolio %>% - mutate( - portfolio_name = "Simple Portfolio", - investor_name = "Simple Investor" - ) %>% - select( - investor_name, - portfolio_name, - isin, - market_value, - currency - ) - -logger::log_info("Writing simple full test file.") -simple_portfolio_all_columns %>% - write.csv( - file = "simple_all-columns.csv", - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing minimal simple test file.") -simple_portfolio_all_columns %>% - select(-investor_name, -portfolio_name) %>% - write.csv( - file = "simple.csv", - row.names = FALSE, - quote = FALSE - ) - -change_colnames <- function(x, colnames) { - colnames(x) <- colnames - return(x) -} - -#### Playing with headers - -if (!dir.exists("headers")) { - dir.create("headers") -} - -logger::log_info("Writing test file with underscores in headers.") -simple_portfolio_all_columns %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-underscore.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing test file with no headers.") -simple_portfolio_all_columns %>% - write.table( - file = file.path("headers", "simple_all-columns_headers-none.csv"), - row.names = FALSE, - col.names = FALSE, - sep = ",", - quote = FALSE - ) - -logger::log_info("Writing test file with no headers.") -simple_portfolio_all_columns %>% - select(-investor_name, -portfolio_name) %>% - write.table( - file = file.path("headers", "simple_headers-none.csv"), - row.names = FALSE, - col.names = FALSE, - sep = ",", - quote = FALSE - ) - -# Names as in Mock Portfolio on CTM (mixed dots and camelCase) -logger::log_info("Writing test file with headers in demo format.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "Investor.Name", "Portfolio.Name", "ISIN", "MarketValue", "Currency" - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-demo.csv"), - row.names = FALSE, - quote = FALSE - ) - -# Dot separated -logger::log_info("Writing test file with dots in headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "Investor.Name", "Portfolio.Name", "ISIN", "Market.Value", "Currency" - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-dot.csv"), - row.names = FALSE, - quote = FALSE - ) - -# space separated -logger::log_info("Writing test file with spaces in headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "Investor Name", "Portfolio Name", "ISIN", "Market Value", "Currency" - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-space.csv"), - row.names = FALSE, - quote = FALSE - ) - -# camelcase -logger::log_info("Writing test file with camelCase headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "investorName", "portfolioName", "isin", "marketValue", "currency" - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-camelcase.csv"), - row.names = FALSE, - quote = FALSE - ) - -# lowercase -logger::log_info("Writing test file with lowercase headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "investorname", "portfolioname", "isin", "marketvalue", "currency" - ) - ) %>% - write.csv( - file = file.path( - "headers", "simple_all-columns_headers-nosep-lowercase.csv" - ), - row.names = FALSE, - quote = FALSE - ) - -# uppercase separated -logger::log_info("Writing test file with uppercase headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "INVESTORNAME", "PORTFOLIONAME", "ISIN", "MARKETVALUE", "CURRENCY" - ) - ) %>% - write.csv( - file = file.path( - "headers", "simple_all-columns_headers-nosep-uppercase.csv" - ), - row.names = FALSE, - quote = FALSE - ) - -# space padded -logger::log_info("Writing test file with padded headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - " Investor.Name ", " Portfolio.Name ", " ISIN ", - " MarketValue ", " Currency " - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-padded.csv"), - row.names = FALSE, - quote = FALSE - ) - -# double space padded -logger::log_info("Writing test file with double padded headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - " Investor.Name ", " Portfolio.Name ", " ISIN ", - " MarketValue ", " Currency " - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-doublepadded.csv"), - row.names = FALSE, - quote = FALSE - ) - -# quoted heades -logger::log_info("Writing test file with quoted headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "\"investor_name\"", "\"portfolio_name\"", "\"isin\"", - "\"market_value\"", "\"currency\"" - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-quoted.csv"), - row.names = FALSE, - quote = FALSE - ) - -# Mix of formats -logger::log_info("Writing test file with mixed format headers.") -simple_portfolio_all_columns %>% - change_colnames( - colnames = c( - "INVESTOR.NAME", "PortfolioName", "isin", "market_value", " Currency" - ) - ) %>% - write.csv( - file = file.path("headers", "simple_all-columns_headers-mixed.csv"), - row.names = FALSE, - quote = FALSE - ) - - -#### Playing with column order - -if (!dir.exists("columns")) { - dir.create("columns") -} - -logger::log_info("Writing test file with reordered columns.") -simple_portfolio_all_columns %>% - select(rev(everything())) %>% - write.csv( - file = file.path("columns", "simple_all-columns_reordered.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing minimal test file with reordered columns.") -simple_portfolio_all_columns %>% - select(rev(everything())) %>% - select(-investor_name, -portfolio_name) %>% - write.csv( - file = file.path("columns", "simple_reordered.csv"), - row.names = FALSE, - quote = FALSE - ) - -#### Playing with extra columns -logger::log_info("Writing test file with extra columns.") -simple_portfolio_all_columns %>% - mutate(foo = "bar") %>% - write.csv( - file = file.path("columns", "simple_all-columns_extra_columns.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing minimal test file with extra columns.") -simple_portfolio_all_columns %>% - mutate(foo = "bar") %>% - select(-investor_name, -portfolio_name) %>% - write.csv( - file = file.path("columns", "simple_extra_columns.csv"), - row.names = FALSE, - quote = FALSE - ) - -#### Playing with missing columns -logger::log_info("Writing test file with missing column: investor_name.") -simple_portfolio_all_columns %>% - select(-investor_name) %>% - write.csv( - file = file.path("columns", "simple_portfolioname.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing test file with missing column: portfolio_name.") -simple_portfolio_all_columns %>% - select(-portfolio_name) %>% - write.csv( - file = file.path("columns", "simple_investorname.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing test file with missing column: currency.") -simple_portfolio_all_columns %>% - select(-currency) %>% - write.csv( - file = file.path("columns", "simple_missing-currency.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing test file with missing column: market_value.") -simple_portfolio_all_columns %>% - select(-market_value) %>% - write.csv( - file = file.path("columns", "simple_missing-marketvalue.csv"), - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing test file with missing column: isin.") -simple_portfolio_all_columns %>% - select(-isin) %>% - write.csv( - file = file.path("columns", "simple_missing-isin.csv"), - row.names = FALSE, - quote = FALSE - ) - -#### Playing with missing rows -logger::log_info("Writing test file with no data.") -simple_portfolio_all_columns %>% - slice(0) %>% - write.csv( - file = "simple_all-columns_empty.csv", - row.names = FALSE, - quote = FALSE - ) - -logger::log_info("Writing minimal test file with no data.") -simple_portfolio_all_columns %>% - slice(0) %>% - select(-investor_name, -portfolio_name) %>% - write.csv( - file = "simple_all-columns_empty.csv", - row.names = FALSE, - quote = FALSE - ) - -#### TODO: Playing with encodings - -#### TODO: Playing with missing values -#### TODO: Playing with invalid values -#### TODO: Playing with column types -#### TODO: Playing with Excel exports (BOMs) -#### TODO: Playing with EOL markers (\r\n, \r, \n) -#### TODO: Playing with NA strings (NA, N/A, NULL, "", -, /, etc.) -#### TODO: Playing with rownames (export from R write.csv default) - - -#### TODO: Playing with multiple portfolios in a file - -logger::log_info("Done.") diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-camelcase.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-camelcase.csv deleted file mode 100644 index c128369..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-camelcase.csv +++ /dev/null @@ -1,2 +0,0 @@ -investorName,portfolioName,isin,marketValue,currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-demo.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-demo.csv deleted file mode 100644 index 886c9ed..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-demo.csv +++ /dev/null @@ -1,2 +0,0 @@ -Investor.Name,Portfolio.Name,ISIN,MarketValue,Currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-dot.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-dot.csv deleted file mode 100644 index 33224a3..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-dot.csv +++ /dev/null @@ -1,2 +0,0 @@ -Investor.Name,Portfolio.Name,ISIN,Market.Value,Currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-doublepadded.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-doublepadded.csv deleted file mode 100644 index e90b6f8..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-doublepadded.csv +++ /dev/null @@ -1,2 +0,0 @@ - Investor.Name , Portfolio.Name , ISIN , MarketValue , Currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-mixed.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-mixed.csv deleted file mode 100644 index fd4accd..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-mixed.csv +++ /dev/null @@ -1,2 +0,0 @@ -INVESTOR.NAME,PortfolioName,isin,market_value, Currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-none.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-none.csv deleted file mode 100644 index 41b6ca7..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-none.csv +++ /dev/null @@ -1 +0,0 @@ -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-nosep-lowercase.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-nosep-lowercase.csv deleted file mode 100644 index 5720aa2..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-nosep-lowercase.csv +++ /dev/null @@ -1,2 +0,0 @@ -investorname,portfolioname,isin,marketvalue,currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-nosep-uppercase.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-nosep-uppercase.csv deleted file mode 100644 index f55e7b8..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-nosep-uppercase.csv +++ /dev/null @@ -1,2 +0,0 @@ -INVESTORNAME,PORTFOLIONAME,ISIN,MARKETVALUE,CURRENCY -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-padded.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-padded.csv deleted file mode 100644 index 56c4ae3..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-padded.csv +++ /dev/null @@ -1,2 +0,0 @@ - Investor.Name , Portfolio.Name , ISIN , MarketValue , Currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-quoted.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-quoted.csv deleted file mode 100644 index 59fc203..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-quoted.csv +++ /dev/null @@ -1,2 +0,0 @@ -"investor_name","portfolio_name","isin","market_value","currency" -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-space.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-space.csv deleted file mode 100644 index a2f54d1..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-space.csv +++ /dev/null @@ -1,2 +0,0 @@ -Investor Name,Portfolio Name,ISIN,Market Value,Currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-underscore.csv b/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-underscore.csv deleted file mode 100644 index dfe10fa..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_all-columns_headers-underscore.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,portfolio_name,isin,market_value,currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/headers/simple_headers-none.csv b/tests/testthat/testdata/portfolios/headers/simple_headers-none.csv deleted file mode 100644 index ca54d4f..0000000 --- a/tests/testthat/testdata/portfolios/headers/simple_headers-none.csv +++ /dev/null @@ -1 +0,0 @@ -GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/multi_simple_all-columns_portfolioname.csv b/tests/testthat/testdata/portfolios/multi_simple_all-columns_portfolioname.csv deleted file mode 100644 index cfba126..0000000 --- a/tests/testthat/testdata/portfolios/multi_simple_all-columns_portfolioname.csv +++ /dev/null @@ -1,3 +0,0 @@ -Investor.Name,Portfolio.Name,ISIN,MarketValue,Currency -Simple Investor,Portfolio A,GB0007980591,10000,USD -Simple Investor,Portfolio B,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/output_simple.csv b/tests/testthat/testdata/portfolios/output_simple.csv deleted file mode 100644 index a24b23a..0000000 --- a/tests/testthat/testdata/portfolios/output_simple.csv +++ /dev/null @@ -1,2 +0,0 @@ -"isin","market_value","currency" -"GB0007980591",10000,"USD" diff --git a/tests/testthat/testdata/portfolios/simple.csv b/tests/testthat/testdata/portfolios/simple.csv deleted file mode 100644 index 157e6f9..0000000 --- a/tests/testthat/testdata/portfolios/simple.csv +++ /dev/null @@ -1,2 +0,0 @@ -isin,market_value,currency -GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/simple_all-columns.csv b/tests/testthat/testdata/portfolios/simple_all-columns.csv deleted file mode 100644 index dfe10fa..0000000 --- a/tests/testthat/testdata/portfolios/simple_all-columns.csv +++ /dev/null @@ -1,2 +0,0 @@ -investor_name,portfolio_name,isin,market_value,currency -Simple Investor,Simple Portfolio,GB0007980591,10000,USD diff --git a/tests/testthat/testdata/portfolios/simple_all-columns_empty.csv b/tests/testthat/testdata/portfolios/simple_all-columns_empty.csv deleted file mode 100644 index 1b6e07b..0000000 --- a/tests/testthat/testdata/portfolios/simple_all-columns_empty.csv +++ /dev/null @@ -1 +0,0 @@ -isin,market_value,currency