Skip to content

Commit

Permalink
testing sankey plot
Browse files Browse the repository at this point in the history
  • Loading branch information
lindadelacombaz committed Apr 23, 2024
1 parent 9c6a7b6 commit 0f2d711
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/testthat/test-plot_sankey.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,76 @@ test_that("returns correct risk categories values", {
possible_names <- c("low", "medium", "high", "other")
expect_true(all(risk_names %in% possible_names))
})

test_that("y-axis amount is equal to loanbook amount for all the modes", {
data <- financial |>
distinct(bank_id, company_name, ep_product, .keep_all = TRUE)
modes <- c("worst_case", "best_case", "equal_weight")

test_mode <- function(mode) {
p <- plot_sankey(data, mode = mode)
expect_equal(sum(data[[paste0(mode, "_finance")]]), sum(p$data[[paste0(mode, "_finance")]]))
}
lapply(modes, test_mode)
})

test_that("each bank_id has the correct amount for all the modes", {
modes <- c("equal_weight", "best_case", "worst_case")
calculate_amount_bank_id <- function(data, mode) {
data |>
group_by(bank_id) |>
mutate(sum = sum(data[[paste0(mode, "_finance")]])) |>
distinct(bank_id, sum)
}

data <- financial |>
distinct(bank_id, company_name, ep_product, .keep_all = TRUE)
amount_bank_id <- lapply(modes, calculate_amount_bank_id, data = data)

plots <- lapply(modes, function(mode) plot_sankey(data, mode = mode))
data_plots <- lapply(plots, `[[`, "data")
amount_bank_id_plot <- lapply(seq_along(data_plots), function(i) {
calculate_amount_bank_id(data_plots[[i]], mode = modes[i])
})

all_results_equal <- lapply(seq_along(modes), function(i) {
all.equal(amount_bank_id_plot[[i]], amount_bank_id[[i]])
})

expect_true(all(unlist(lapply(all_results_equal, isTRUE))))
})

test_that("each risk category have the correct amount for all the modes", {
data <- financial |>
distinct(bank_id, company_name, ep_product, .keep_all = TRUE)

calculate_amount_risk <- function(data, mode) {
data |>
group_by(aka("emission_profile")) |>
mutate(sum = sum(data[[paste0(mode, "_finance")]])) |>
distinct(aka("emission_profile"), sum)
}

data <- financial |>
distinct(bank_id, company_name, ep_product, .keep_all = TRUE)
amount_risk <- lapply(modes, calculate_amount_risk, data = data)

plots <- lapply(modes, function(mode) plot_sankey(data, mode = mode))
data_plots <- lapply(plots, `[[`, "data")
amount_risk_plot <- lapply(seq_along(data_plots), function(i) {
calculate_amount_risk(data_plots[[i]], mode = modes[i])
})

all_results_equal <- lapply(seq_along(modes), function(i) {
all.equal(amount_risk[[i]], amount_risk_plot[[i]])
})

expect_true(all(unlist(lapply(all_results_equal, isTRUE))))
})

test_that("risk categories are in the right order", {
p <- plot_sankey(financial)
risk_categories <- unique(p$data$emission_profile)
expected_order <- c("high", "medium", "low")
expect_identical(risk_categories, expected_order)
})

0 comments on commit 0f2d711

Please sign in to comment.