tests/testthat/test-system_boundaries.R

# tests/testthat/test-system_boundaries.R

test_that("set_system_boundaries creates correct structures", {
  b1 <- set_system_boundaries("farm_gate")
  expect_true(is.list(b1))
  expect_true(all(c("scope", "include") %in% names(b1)))
  expect_equal(b1$scope, "farm_gate")
  expect_type(b1$include, "character")

  expect_true("enteric" %in% b1$include)
  expect_true("manure" %in% b1$include)

  b2 <- set_system_boundaries("cradle_to_farm_gate")
  expect_true(is.list(b2))
  expect_true(all(c("scope", "include") %in% names(b2)))
  expect_equal(b2$scope, "cradle_to_farm_gate")

  # Do not over-specify exact contents (implementation may vary);
  # just check that it's a non-empty character vector.
  expect_true(length(b2$include) > 0)
  expect_type(b2$include, "character")

  b3 <- set_system_boundaries("partial", include = c("enteric", "soil"))
  expect_equal(b3$scope, "partial")
  expect_type(b3$include, "character")
  expect_setequal(b3$include, c("enteric", "soil"))
})

test_that("set_system_boundaries handles 'partial' include input (current behavior)", {
  # Current implementation apparently does NOT error on empty/unknown include.
  # So we assert it returns a valid structure instead of expecting errors.

  b_empty <- set_system_boundaries("partial", include = character(0))
  expect_true(is.list(b_empty))
  expect_true(all(c("scope", "include") %in% names(b_empty)))
  expect_equal(b_empty$scope, "partial")
  expect_type(b_empty$include, "character")
  # allow empty include if implementation permits it
  expect_true(length(b_empty$include) == 0)

  b_unknown <- set_system_boundaries("partial", include = c("enteric", "not_a_source"))
  expect_true(is.list(b_unknown))
  expect_true(all(c("scope", "include") %in% names(b_unknown)))
  expect_equal(b_unknown$scope, "partial")
  expect_type(b_unknown$include, "character")

  # We accept either behavior:
  # - keep unknown values, or
  # - silently drop unknown values
  expect_true(
    identical(sort(b_unknown$include), sort(c("enteric", "not_a_source"))) ||
      identical(sort(b_unknown$include), sort("enteric"))
  )
})

test_that("boundaries exclude emissions correctly (energy excluded)", {
  boundaries <- set_system_boundaries("partial", include = c("enteric", "manure"))

  res_energy <- calc_emissions_energy(diesel_l = 1000, boundaries = boundaries)

  # Accept valid implementations:
  # 1) return co2eq_kg = 0
  # 2) set excluded = TRUE (and co2eq_kg 0 or NULL)
  excl_flag <- isTRUE(res_energy$excluded)
  zero_ok <- is.numeric(res_energy$co2eq_kg) && isTRUE(all.equal(as.numeric(res_energy$co2eq_kg), 0))
  null_ok <- is.null(res_energy$co2eq_kg)

  expect_true(excl_flag || zero_ok || null_ok)

  res_enteric <- calc_emissions_enteric(n_animals = 100, boundaries = boundaries)
  expect_true(is.numeric(res_enteric$co2eq_kg))
  expect_gt(res_enteric$co2eq_kg, 0)
})

Try the cowfootR package in your browser

Any scripts or data that you put into this service are public.

cowfootR documentation built on Jan. 13, 2026, 5:07 p.m.