Skip to content

Commit

Permalink
target_market_share errors if input data has unexpected columns (#…
Browse files Browse the repository at this point in the history
…268)

Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
jdhoffa and actions-user authored Jan 21, 2021
1 parent 1b62d2a commit eb59265
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# r2dii.analysis (development version)

* `target_market_share()` now errors if input `data` has an unexpected column
(@georgeharris2deg #267).

* `target_market_share()` now correctly outputs `technology_share` with
multiple loans to the same company (@georgeharris2deg #262, @ab-bbva #265).

Expand Down
45 changes: 45 additions & 0 deletions R/target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ target_market_share <- function(data,

data <- ungroup(warn_grouped(data, "Ungrouping input data."))

valid_columns <- c(
"id_loan",
"id_direct_loantaker",
"name_direct_loantaker",
"id_intermediate_parent_1",
"name_intermediate_parent_1",
"id_ultimate_parent",
"name_ultimate_parent",
"loan_size_outstanding",
"loan_size_outstanding_currency",
"loan_size_credit_limit",
"loan_size_credit_limit_currency",
"sector_classification_system",
"sector_classification_input_type",
"sector_classification_direct_loantaker",
"fi_type",
"flag_project_finance_loan",
"name_project",
"lei_direct_loantaker",
"isin_direct_loantaker",
"id_2dii",
"level",
"sector",
"sector_ald",
"name",
"name_ald",
"score",
"source",
"borderline"
)

check_valid_columns(data, valid_columns)
data <- aggregate_by_loan_id(data)

crucial_scenario <- c("scenario", "tmsr", "smsp")
Expand Down Expand Up @@ -406,3 +438,16 @@ aggregate_by_loan_id <- function(data) {
) %>%
ungroup()
}

check_valid_columns <- function(data, valid_columns) {
invalid_columns <- setdiff(names(data), valid_columns)

if (length(invalid_columns) != 0) {
abort(
glue("Loanbook has unexpected names: `{invalid_columns}`."),
class = "invalid_columns"
)
}

invisible(data)
}
15 changes: 15 additions & 0 deletions tests/testthat/test-target_market_share.R
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,21 @@ test_that("for one company with multiple loans of different size, unweighted
expect_equal(projected$production, fake_ald()$production)
})

test_that("with bad column errors with informative message (#267)", {
bad_matched <- fake_matched(
bad_column = "bad"
)

expect_error(
class = "invalid_columns",
target_market_share(
bad_matched,
fake_ald(),
fake_scenario()
)
)
})

test_that("`technology_share` outputs consistently when multiple
direct_loantakers match to a single company (#265)", {

Expand Down

0 comments on commit eb59265

Please sign in to comment.