Skip to content

Commit

Permalink
emissions_profile*() uses co2$values_to_categorize if present (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore authored Nov 13, 2023
1 parent 6dd4b28 commit f45790c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions R/emissions_profile_any_add_values_to_categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
emissions_profile_any_add_values_to_categorize <- function(data) {
check_emissions_profile_any_add_values_to_categorize(data)

if (hasName(data, "values_to_categorize")) {
return(data)
}

benchmarks <- set_names(epa_benchmarks(data), flat_benchmarks(data))
map_df(benchmarks, ~ add_rank(data, .x), .id = "grouped_by")
}
Expand Down
51 changes: 50 additions & 1 deletion tests/testthat/test-emissions_profile_any_at_product_level.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ test_that("if the 'isic' column hasn't 4 digits throws an errors ", {
})

test_that("a 0-row `co2` yields an error", {
companies <- example_companies()
products <- example_products()
expect_error(
emissions_profile_any_at_product_level(companies[0L, ], products),
"companies.*can't have 0-row"
Expand All @@ -156,8 +158,9 @@ test_that("a 0-row `co2` yields an error", {
)
})


test_that("a 0-row `companies` yields an error", {
companies <- example_companies()
inputs <- example_inputs()
expect_error(
emissions_profile_any_at_product_level(companies[0L, ], inputs),
"companies.*can't have 0-row"
Expand Down Expand Up @@ -283,3 +286,49 @@ test_that("`*rowid` columns are passed through inputs with duplicates", {
expect_true(hasName(out, "companies_rowid"))
expect_true(hasName(out, "products_rowid"))
})

test_that("with products, uses `co2$values_to_categorize` if present (#603)", {
companies <- example_companies()
co2 <- example_products(!!aka("uid") := c("a", "b"))
co2[extract_name(co2, aka("co2footprint"))] <- 1:2

lacks_values_to_categorize <- !hasName(co2, "values_to_categorize")
stopifnot(lacks_values_to_categorize)
out1 <- emissions_profile_any_at_product_level(companies, co2)
using_computed_values <- unique(out1$risk_category)

pre_computed <- emissions_profile_any_add_values_to_categorize(co2)
has_values_to_categorize <- hasName(pre_computed, "values_to_categorize")
stopifnot(has_values_to_categorize)

yields_a_different_risk_category <- 999
pre_computed$values_to_categorize <- yields_a_different_risk_category
out2 <- emissions_profile_any_at_product_level(companies, pre_computed)
using_pre_computed_values <- unique(out2$risk_category)

expect_false(identical(using_computed_values, using_pre_computed_values))
})

test_that("with inputs, uses `co2$values_to_categorize` if present (#603)", {
companies <- example_companies()
co2 <- example_inputs(!!aka("uid") := c("a", "b"))
# This is wrong: names(example_inputs(!!aka("co2footprint") := 1:2))
# It yields two columns: `co2_footprint` AND `input_co2_footprint`
co2[extract_name(co2, aka("co2footprint"))] <- 1:2

lacks_values_to_categorize <- !hasName(co2, "values_to_categorize")
stopifnot(lacks_values_to_categorize)
out1 <- emissions_profile_any_at_product_level(companies, co2)
using_computed_values <- unique(out1$risk_category)

pre_computed <- emissions_profile_any_add_values_to_categorize(co2)
has_values_to_categorize <- hasName(pre_computed, "values_to_categorize")
stopifnot(has_values_to_categorize)

yields_a_different_risk_category <- 999
pre_computed$values_to_categorize <- yields_a_different_risk_category
out2 <- emissions_profile_any_at_product_level(companies, pre_computed)
using_pre_computed_values <- unique(out2$risk_category)

expect_false(identical(using_computed_values, using_pre_computed_values))
})

0 comments on commit f45790c

Please sign in to comment.