Skip to content

Commit

Permalink
add single value checks to prep_traj
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhoffa committed Feb 22, 2024
1 parent 66e44fb commit 4c625e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
8 changes: 6 additions & 2 deletions R/prep_trajectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ prep_trajectory <- function(data,
span_5yr = FALSE,
value_col = "percentage_of_initial_production_by_scope") {

check_prep_trajectory(data, value_col = value_col)
env <- list(data = substitute(data))
check_prep_trajectory(data, value_col = value_col, env = env)

data <- data %>%
prep_common() %>%
Expand All @@ -47,9 +48,12 @@ prep_trajectory <- function(data,
data
}

check_prep_trajectory <- function(data, value_col) {
check_prep_trajectory <- function(data, value_col, env) {
stopifnot(is.data.frame(data))
crucial <- c(common_crucial_market_share_columns(), value_col)
hint_if_missing_names(abort_if_missing_names(data, crucial), "market_share")
enforce_single_value <- c("sector", "technology", "region", "scenario_source")
abort_if_multiple(data, enforce_single_value, env = env)

invisible(data)
}
9 changes: 0 additions & 9 deletions tests/testthat/test-plot_trajectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ test_that("outputs default axis labels", {
expect_equal(p$labels$y, "value")
})

test_that("the errors message includes the name of the user's data", {
# Keep even if already tested in qplot_. Non-standard evaluation is fragile
bad_region <- head(market_share, 2L) %>%
mutate(region = c("a", "b")) %>%
prep_trajectory()

expect_error(plot_trajectory(bad_region), "bad_region")
})

test_that("By default doesn't center the Y axis", {
data <- example_market_share() %>%
prep_trajectory(convert_label = identity, span_5yr = FALSE)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-prep_trajectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ test_that("handles span_5yr correctly", {
out <- prep_trajectory(example_market_share(), span_5yr = TRUE)
expect_true(all(out$year <= min(out$year) + 5))
})

test_that("the errors message includes the name of the user's data", {
# Keep even if already tested in qplot_. Non-standard evaluation is fragile
bad_region <- head(market_share, 2L) %>%
mutate(region = c("a", "b"))

expect_error(prep_trajectory(bad_region), "bad_region")
})

0 comments on commit 4c625e3

Please sign in to comment.