Skip to content

Commit

Permalink
fixes tests; adds tests to cover all of plotting, scenario_inputs and…
Browse files Browse the repository at this point in the history
… data_loading
  • Loading branch information
Fox Sebastian (BNSSG CCG) authored and Fox Sebastian (BNSSG CCG) committed Jul 23, 2024
1 parent 9faaed1 commit 6498ad6
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 4 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,4 @@ importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(utils,stack)
importFrom(utils,tail)
importFrom(utils,write.csv)
6 changes: 4 additions & 2 deletions R/fct_plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ plot_trust_icb_proportions <- function(ics_code) {
nudge_x = 1,
color = "black",
show.legend = FALSE,
size = 4
size = 4,
seed = 123
) +
scale_fill_brewer(palette = "Set1") +
labs(
Expand All @@ -178,6 +179,7 @@ plot_trust_icb_proportions <- function(ics_code) {
}

#' @importFrom rlang sym
#' @import ggplot2
plot_hold_message <- function() {
p <- tibble::tibble(
x = 1,
Expand Down Expand Up @@ -209,7 +211,7 @@ plot_hold_message <- function() {
panel.border = element_rect(
colour = "black",
fill = NA,
size = 1
linewidth = 1
)
)

Expand Down
10 changes: 9 additions & 1 deletion R/fct_scenario_inputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,13 @@ update_predictions_and_plot_r <- function(prediction_custom_scenario, model_outp
r$performance_plot <- plot_hold_message()
}
}

invisible(r)
}

#' @importFrom tidyr pivot_wider
#' @importFrom purrr lmap
#' @importFrom dplyr mutate select filter bind_rows
#' @importFrom stats setNames
update_observed_time_period_predictions <- function(model_outputs, r) {
# all available data in long format
Expand Down Expand Up @@ -512,7 +517,8 @@ important_variables <- function(model_permutation_importance,
return(important_metrics)
}

#' @importFrom utils head tail
#' @importFrom dplyr filter mutate arrange
#' @importFrom rlang sym
update_custom_tables <- function(input_table, model_permutation_importance, performance_metrics, r) {

# character vector of the most important variables based on the selected
Expand All @@ -531,6 +537,8 @@ update_custom_tables <- function(input_table, model_permutation_importance, perf
)
) |>
arrange(!!sym("metric"))

invisible(r)
}


Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions tests/testthat/_snaps/fct_plotting/ggplot-hold-message.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/fct_scenario_inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# update_prediction_and_plot_r

Code
update_predictions_and_plot_r(prediction_custom_scenario = "testing",
model_outputs = load_model_object("wf"), scenario_name = "incorrect_name",
performance_metrics = "Proportion of attended GP appointments (over 4 weeks wait time)",
r = list(ics_cd = ics_code, ics_data = all_ics_data, scenario_data = scenario_data))
Condition
Error in `match.arg()`:
! 'arg' should be one of "last_known", "percent", "linear", "custom"

---

Code
custom_test <- update_predictions_and_plot_r(prediction_custom_scenario = "testing",
model_outputs = load_model_object("wf"), scenario_name = scenario,
performance_metrics = "Proportion of attended GP appointments (over 4 weeks wait time)",
r = list(ics_cd = ics_code, ics_data = all_ics_data, scenario_data = scenario_data,
predictions = predictions))
Condition
Error in `session$sendModal()`:
! attempt to apply non-function

59 changes: 59 additions & 0 deletions tests/testthat/test-fct_data_loading.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,62 @@ test_that("dimensions of ics_data function", {
label = "more than one row of data for BNSSG"
)
})

test_that("narrow age bands are replaced by broad age bands using replace_narrow_age_bands", {

narrow_age_bands <- c("0-9",
"10-19",
"20-29",
"30-39",
"40-49",
"50-59",
"60-69",
"70-79",
"80-89",
"90+")

broad_age_bands <- c("0-29", "30-59", "60+")

demand_timeseries <- tibble(
metric = paste(
"Proportion of population in age band ",
narrow_age_bands
),
numerator = sample(1:10, 10, replace = TRUE),
denominator = sample(50:70, 10, replace = TRUE)
)
narrow_demand_timeseries <- demand_timeseries |>
replace_narrow_age_bands()

expect_true(
all(
purrr::map_lgl(
broad_age_bands,
~ any(grepl(.x, unique(narrow_demand_timeseries$metric)))
)
),
info = "each of the age bands are in the broad age band version of population data"
)

expect_true(
!all(
purrr::map_lgl(
narrow_age_bands,
~ any(grepl(.x, unique(narrow_demand_timeseries$metric)))
)
),
info = "none of the age bands are in the narrow age band version of population data"
)


})



test_that("testing errors", {
expect_error(
ics_data("QUY", domain_type = "performance"),
"domain_type needs to be one of 'Performance', 'Demand' or 'Capacity'"
)
})

15 changes: 15 additions & 0 deletions tests/testthat/test-fct_plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ test_that("ggplot images are consistent", {
p
)
})

test_that("pie chart works", {

vdiffr::expect_doppelganger(
"Acute to trust proportions pie chart is consistent",
plot_trust_icb_proportions("QR1")
)
})

test_that("hold message image works", {
vdiffr::expect_doppelganger(
"ggplot hold message",
plot_hold_message()
)
})
Loading

0 comments on commit 6498ad6

Please sign in to comment.