test_that("energy balance is consistent", { eb <- energy_balance(farm_green, inputs = c(diesel = "diesel", n = "n_synthetic"), output_energy = "output_energy", yield = "yield_kg") expect_equal(eb$input_energy, farm_green$diesel * 56.31 + farm_green$n_synthetic * 60.6) expect_equal(eb$energy_use_efficiency, farm_green$output_energy / eb$input_energy) expect_error(energy_balance(farm_green, c(gold = "diesel"), "output_energy"), "No energy coefficient") }) test_that("nutrients and depletion cost", { expect_equal(fertilizer_nutrients(urea = 100, dap = 100, mop = 100), data.frame(n = 64, p2o5 = 46, k2o = 60)) expect_equal(nutrient_price(4.6, 46), 10) nb <- nutrient_balance(100, 50, 20, 80, 60, 100) expect_equal(c(nb$n_balance, nb$p_balance, nb$k_balance), c(20, -10, -80)) sc <- soil_depletion_cost(nb$n_balance, nb$p_balance, nb$k_balance, 10, 20, 5) expect_equal(sc$total, 10 * 20 + 80 * 5) }) test_that("El Serafy user cost", { r <- el_serafy(100, reserves = 900, extraction = 100, r = 0.1) expect_equal(r$life, 9) expect_equal(r$user_cost, 100 / 1.1^10) expect_equal(r$user_cost + r$true_income, 100) expect_equal(el_serafy(100, 10, 0)$user_cost, 0) }) test_that("groundwater account classifies stages", { g <- groundwater_account(c(50, 80, 95, 150), 100, price = 2) expect_equal(g$category, c("safe", "semi_critical", "critical", "over_exploited")) expect_equal(g$overdraft_cost, c(0, 0, 0, 100)) }) test_that("ecosystem service value and sensitivity", { esv <- ecosystem_service_value(c(cropland = 10, wetland = 1)) expect_equal(esv$total, 10 * 5567 + 25682) s <- esv_sensitivity(c(cropland = 10, wetland = 1)) expect_equal(sum(s$cs), 1) expect_error(ecosystem_service_value(c(moon = 1))) }) test_that("green value added and ANS", { g <- green_value_added(1000, 100, data.frame(a = 20, b = 30), 50, 10) expect_equal(g$nva, 900) expect_equal(g$green_va, 900 - 50 - 50 + 10) a <- adjusted_net_savings(300, 100, 40, co2_damage = 20, income = 1000) expect_equal(a$ans, 220) expect_equal(a$ans_pct, 22) }) test_that("green farm account is coherent", { acc <- green_farm_account(farm_green, carbon_price = 1500, nutrient_prices = c(11.65, 58.7, 29.3), water_price = 2, rice_area = "rice_area") expect_s3_class(acc, "green_account") a <- acc$accounts expect_equal(a$green_income, a$conventional_income - a$ghg_cost - a$soil_cost - a$groundwater_cost + a$ecosystem_services) s <- summary(acc, by = farm_green$crop) expect_equal(nrow(s), 3) expect_equal(sum(s$green_income), sum(a$green_income)) expect_output(print(acc), "Green farm accounts") expect_output(print(s), "Summary") zero <- green_farm_account(farm_green) expect_equal(zero$accounts$green_income, zero$accounts$conventional_income) })