Skip to content

Commit

Permalink
Improve docs and tests (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore authored Nov 12, 2023
1 parent 3473136 commit 6dd4b28
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
11 changes: 9 additions & 2 deletions R/emissions_profile_any_add_values_to_categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#'
#' @family pre-processing helpers
#'
#' @return The input data frame with the additional column
#' `values_to_categorize`.
#' @return The input data frame with the additional columns `grouped_by` and
#' `values_to_categorize` and one row per benchmark per company.
#'
#' @export
#'
Expand All @@ -23,10 +23,17 @@
#' inputs <- read_csv(toy_emissions_profile_upstream_products())
#' inputs |> emissions_profile_any_add_values_to_categorize()
emissions_profile_any_add_values_to_categorize <- function(data) {
check_emissions_profile_any_add_values_to_categorize(data)

benchmarks <- set_names(epa_benchmarks(data), flat_benchmarks(data))
map_df(benchmarks, ~ add_rank(data, .x), .id = "grouped_by")
}

check_emissions_profile_any_add_values_to_categorize <- function(data) {
crucial <- c(aka("tsector"), aka("xunit"), aka("isic"), aka("co2footprint"))
walk(crucial, \(pattern) check_matches_name(data, pattern))
}

rank_proportion <- function(x) {
rank(x) / length(x)
}
Expand Down
4 changes: 2 additions & 2 deletions man/emissions_profile_any_add_values_to_categorize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,50 @@ test_that("works with any 'co2-like' dataset", {
co2 <- example_inputs()
expect_no_error(emissions_profile_any_add_values_to_categorize(co2))
})

test_that("adds columns `grouped_by` and `values_to_categorize`", {
co2 <- example_products()

out <- emissions_profile_any_add_values_to_categorize(co2)

new_names <- c("grouped_by", "values_to_categorize")
expect_equal(setdiff(names(out), names(co2)), new_names)
})

test_that("with one company, adds one row per benchmark per company", {
co2 <- example_products()

out <- emissions_profile_any_add_values_to_categorize(co2)

number_of_benchmarks <- length(flat_benchmarks(co2))
expect_equal(nrow(out), number_of_benchmarks)
})

test_that("with two companies, adds one row per benchmark per company", {
co2 <- example_products(!!aka("id") := c("a", "b"))

out <- emissions_profile_any_add_values_to_categorize(co2)

number_of_benchmarks <- length(flat_benchmarks(co2))
expect_equal(nrow(out), 2 * number_of_benchmarks)
})

test_that("without crucial columns errors gracefully", {
co2 <- example_products()

crucial <- aka("tsector")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)

crucial <- aka("xunit")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)

crucial <- aka("isic")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)

crucial <- aka("co2footprint")
bad <- select(co2, -all_of(crucial))
expect_error(emissions_profile_any_add_values_to_categorize(bad), crucial)
})

0 comments on commit 6dd4b28

Please sign in to comment.