Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
do not allow grouping by crucial variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobvjk committed Apr 17, 2024
1 parent 39e292e commit fc70905
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
24 changes: 24 additions & 0 deletions R/aggregate_alignment_loanbook_exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,29 @@ validate_input_data_aggregate_alignment_loanbook_exposure <- function(data,
)
)

if (!is.null(.by)) {
if (
any(
.by %in% c(
group_vars,
"name_abcd",
"activity_unit",
"scenario_source",
"alignment_metric",
"id_loan",
"loan_size_outstanding",
"loan_size_outstanding_currency",
"name_abcd",
"sector"
)
)
) {
stop(
"It is not possible to group by the critical columns of the `data` and
`matched` inputs. Please check your .by argument!"
)
}
}

invisible()
}
17 changes: 16 additions & 1 deletion tests/testthat/test-aggregate_alignment_loanbook_exposure.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ test_matched_dot_by <- tibble::tribble(
)
# styler: on

test_that("net aggregate results with grouped with bad .by returns ", {
test_that("net aggregate results with bad .by returns error", {
expect_error(
{
test_data_company_net %>%
Expand All @@ -145,6 +145,21 @@ test_that("net aggregate results with grouped with bad .by returns ", {
)
})

test_that("net aggregate results with .by arg as crucial variable returns error", {
expect_error(
{
test_data_company_net %>%
aggregate_alignment_loanbook_exposure(
matched = test_matched_dot_by,
level = test_level_net,
.by = "loan_size_outstanding"
)
},
regexp = "It is not possible to group by the critical columns of the `data` and
`matched` inputs."
)
})

test_that("net aggregate results with .by specified returns results for each group", {
n_groups <- length(unique(test_matched_dot_by$foo))

Expand Down

0 comments on commit fc70905

Please sign in to comment.