test_that("GWP values are correct", { expect_equal(get_gwp("AR5")[["N2O"]], 265) expect_equal(get_gwp("AR6")[["CH4"]], 27) expect_equal(get_gwp("AR4")[["CH4"]], 25) }) test_that("N2O follows IPCC Tier 1 arithmetic", { r <- n2o_nitrogen(100) expect_equal(r$direct, 100 * 0.01 * 44 / 28) expect_equal(r$volatilisation, 100 * 0.11 * 0.01 * 44 / 28) expect_equal(r$leaching, 100 * 0.24 * 0.011 * 44 / 28) expect_equal(r$total, r$direct + r$volatilisation + r$leaching) expect_equal(n2o_nitrogen(100, indirect = FALSE)$total, r$direct) expect_error(n2o_nitrogen(-1)) }) test_that("CO2 from urea, lime and energy", { expect_equal(co2_urea(1000), 1000 * 0.2 * 44 / 12) expect_equal(co2_lime(1000, 1000), 2000 * 0.125 * 44 / 12) expect_equal(co2_energy(diesel = 10, electricity = 100), 26.8 + 71.6) }) test_that("rice methane scaling factors apply", { base <- ch4_rice(1, 100, ef_baseline = 1) expect_equal(base, 100) expect_equal(ch4_rice(1, 100, "multiple_drainage", ef_baseline = 1), 55) expect_equal(ch4_rice(1, 100, "upland", ef_baseline = 1), 0) expect_equal(ch4_rice(1, 100, oa_rate = 1, oa_type = "straw_short", ef_baseline = 1), 100 * 2^0.59) }) test_that("ghg_inventory adds up", { inv <- ghg_inventory(farm_green, rice_area = "rice_area") expect_equal(nrow(inv), nrow(farm_green)) parts <- inv[, c("n2o", "urea", "lime", "rice_ch4", "energy", "other")] expect_equal(inv$total, rowSums(parts)) expect_true(all(inv$rice_ch4[farm_green$crop != "rice"] == 0)) expect_error(ghg_inventory(farm_green, urea = "nope"), "not found") cf <- carbon_footprint(inv$total, farm_green$yield_kg, farm_green$area_ha) expect_named(cf, c("per_output", "per_ha")) })