Skip to content

Commit

Permalink
Merge pull request #25 from RMI-PACTA/test/fix-broken-r-checks
Browse files Browse the repository at this point in the history
Test/fix broken r checks
  • Loading branch information
AlexAxthelm authored Apr 8, 2024
2 parents e6af733 + 2a4bb7a commit 286a8e3
Show file tree
Hide file tree
Showing 49 changed files with 724 additions and 658 deletions.
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
.dockerignore
.github/
.lintr
^LICENSE\.md$
Dockerfile
docker-compose.yml
4 changes: 4 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters: all_linters()
exclusions: list(
"tests/testthat.R"
)
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(export_portfolio)
export(process_directory)
export(reexport_portfolio)
21 changes: 17 additions & 4 deletions R/export_portfolio.R
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.")
}
Expand All @@ -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,
Expand Down Expand Up @@ -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.")
Expand Down
16 changes: 14 additions & 2 deletions R/process_directory.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 13 additions & 1 deletion R/reexport_portfolio.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.")
Expand Down
8 changes: 4 additions & 4 deletions R/system_info.R
Original file line number Diff line number Diff line change
@@ -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]]
Expand All @@ -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))
)
}
)
Expand Down
26 changes: 26 additions & 0 deletions man/export_portfolio.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/process_directory.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/reexport_portfolio.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added tests/testthat/foo.csv
Empty file.
50 changes: 50 additions & 0 deletions tests/testthat/helper-expect_conditions.R
Original file line number Diff line number Diff line change
@@ -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))
}
32 changes: 32 additions & 0 deletions tests/testthat/helper-portfolios.R
Original file line number Diff line number Diff line change
@@ -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
)
Loading

0 comments on commit 286a8e3

Please sign in to comment.