Skip to content

Commit cc384b2

Browse files
committed
test: emission allocation validator
1 parent 62a8483 commit cc384b2

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

bc_obps/reporting/service/report_validation/validators/report_emission_allocation_validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def validate(report_version: ReportVersion) -> dict[str, ReportValidationError]:
2828

2929
for fr in facility_reports:
3030
emission_data = ReportEmissionAllocationService.get_emission_allocation_data(report_version.id, fr.facility_id)
31-
if emission_data.allocation_methodology != "Not applicable":
31+
if emission_data.allocation_methodology != "Not Applicable":
3232
for category in emission_data.report_product_emission_allocations:
3333
total_allocated = sum(product.allocated_quantity for product in category.products)
3434
if total_allocated != category.emission_total:
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from django.test import TestCase
2+
from model_bakery.baker import make_recipe, make
3+
import pytest
4+
from reporting.models.report_operation import ReportOperation
5+
from reporting.service.report_validation.report_validation_error import (
6+
ReportValidationError,
7+
Severity,
8+
)
9+
from reporting.service.report_validation.validators.report_emission_allocation_validator import (
10+
validate,
11+
)
12+
from reporting.tests.service.test_report_activity_save_service.infrastructure import TestInfrastructure
13+
14+
15+
@pytest.mark.django_db
16+
class TestReportEmissionAllocationValidator(TestCase):
17+
FLARING_CATEGORY_ID = 1
18+
WOODY_BIOMASS_CATEGORY_ID = 10
19+
20+
def setUp(self):
21+
# Arrange: sets up the report elements necessary to allocate emissions
22+
self.test_infrastructure = TestInfrastructure.build()
23+
24+
make(
25+
ReportOperation,
26+
report_version=self.test_infrastructure.report_version,
27+
activities=[self.test_infrastructure.activity],
28+
)
29+
30+
activity_source_type = self.test_infrastructure.make_activity_source_type(
31+
source_type__json_key="sourceTypeWithUnitAndFuel",
32+
has_unit=True,
33+
has_fuel=True,
34+
)
35+
36+
report_activity = self.test_infrastructure.make_report_activity()
37+
report_source_type = make_recipe(
38+
"reporting.tests.utils.report_source_type",
39+
activity_source_type_base_schema=activity_source_type,
40+
source_type=activity_source_type.source_type,
41+
report_activity=report_activity,
42+
report_version=self.test_infrastructure.report_version,
43+
json_data={"test_report_source_type": "yes"},
44+
)
45+
report_fuel = make_recipe(
46+
"reporting.tests.utils.report_fuel",
47+
report_source_type=report_source_type,
48+
report_version=self.test_infrastructure.report_version,
49+
json_data={"test_report_unit": True},
50+
report_unit=None,
51+
)
52+
53+
report_emission = make_recipe(
54+
"reporting.tests.utils.report_emission",
55+
report_fuel=report_fuel,
56+
report_source_type=report_source_type,
57+
report_version=self.test_infrastructure.report_version,
58+
json_data={"equivalentEmission": 100},
59+
)
60+
61+
report_emission.emission_categories.set([self.FLARING_CATEGORY_ID])
62+
63+
self.report_product = make_recipe(
64+
"reporting.tests.utils.report_product",
65+
id=3,
66+
report_version=self.test_infrastructure.report_version,
67+
facility_report=self.test_infrastructure.facility_report,
68+
product_id=1, # "BC-specific refinery complexity throughput"
69+
)
70+
71+
def test_no_errors_when_allocations_match_totals(self):
72+
report_emission_allocation = make_recipe(
73+
"reporting.tests.utils.report_emission_allocation",
74+
report_version=self.test_infrastructure.report_version,
75+
facility_report=self.test_infrastructure.facility_report,
76+
allocation_methodology="Other",
77+
allocation_other_methodology_description="test",
78+
)
79+
# allocate all emissions
80+
make_recipe(
81+
"reporting.tests.utils.report_product_emission_allocation",
82+
report_emission_allocation=report_emission_allocation,
83+
report_version=self.test_infrastructure.report_version,
84+
report_product=self.report_product,
85+
emission_category_id=self.FLARING_CATEGORY_ID,
86+
allocated_quantity=100,
87+
)
88+
result = validate(self.test_infrastructure.report_version)
89+
assert result == {}
90+
91+
def test_no_errors_when_allocation_methodology_not_applicable(self):
92+
report_emission_allocation = make_recipe(
93+
"reporting.tests.utils.report_emission_allocation",
94+
report_version=self.test_infrastructure.report_version,
95+
facility_report=self.test_infrastructure.facility_report,
96+
allocation_methodology="Not Applicable",
97+
)
98+
# allocate only some of the emissions
99+
make_recipe(
100+
"reporting.tests.utils.report_product_emission_allocation",
101+
report_emission_allocation=report_emission_allocation,
102+
report_version=self.test_infrastructure.report_version,
103+
report_product=self.report_product,
104+
emission_category_id=self.FLARING_CATEGORY_ID,
105+
allocated_quantity=50,
106+
)
107+
result = validate(self.test_infrastructure.report_version)
108+
assert result == {}
109+
110+
def test_errors_when_allocations_do_not_match_totals(self):
111+
report_emission_allocation = make_recipe(
112+
"reporting.tests.utils.report_emission_allocation",
113+
report_version=self.test_infrastructure.report_version,
114+
facility_report=self.test_infrastructure.facility_report,
115+
allocation_methodology="Other",
116+
allocation_other_methodology_description="test",
117+
)
118+
# allocate only some of the emissions
119+
make_recipe(
120+
"reporting.tests.utils.report_product_emission_allocation",
121+
report_emission_allocation=report_emission_allocation,
122+
report_version=self.test_infrastructure.report_version,
123+
report_product=self.report_product,
124+
emission_category_id=self.FLARING_CATEGORY_ID,
125+
allocated_quantity=50,
126+
)
127+
result = validate(self.test_infrastructure.report_version)
128+
assert result == {
129+
f"facility_{self.test_infrastructure.facility_report.facility_id}_category_{self.FLARING_CATEGORY_ID}_allocation_mismatch": ReportValidationError(
130+
Severity.ERROR,
131+
f"Allocated emissions for {self.test_infrastructure.facility_report.facility_name} in 'Flaring emissions' category do not match reported emissions.",
132+
)
133+
}

0 commit comments

Comments
 (0)