From fdab5dd4b0a3d336dc2376da1106f18786f607c4 Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Fri, 17 Jan 2025 16:50:44 -0800 Subject: [PATCH] test: remove potential side effects of test infrastructure --- .../test_compliance_service/infrastructure.py | 34 +++++++++++++------ .../test_compliance_service_class.py | 12 +++---- .../test_compliance_service_static.py | 20 +++-------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/bc_obps/reporting/tests/service/test_compliance_service/infrastructure.py b/bc_obps/reporting/tests/service/test_compliance_service/infrastructure.py index fe79f132ff..ade1f7e79b 100644 --- a/bc_obps/reporting/tests/service/test_compliance_service/infrastructure.py +++ b/bc_obps/reporting/tests/service/test_compliance_service/infrastructure.py @@ -142,18 +142,31 @@ def build(cls): @classmethod def pare_data_single_product_flaring(cls): # Pare down build data to data needed for this test - ReportProductEmissionAllocation.objects.exclude(emission_category_id=1).delete() - ReportProduct.objects.exclude(product_id=1).delete() - ReportEmission.objects.exclude(gas_type_id=1).delete() + t = cls.build() + t.allocation_2.delete() + t.allocation_3.delete() + t.allocation_4.delete() + t.allocation_5.delete() + t.allocation_6.delete() + t.report_product_2.delete() + t.report_product_3.delete() + t.report_emission_2.delete() + t.report_emission_3.delete() + t.report_emission_4.delete() + return t @classmethod def pare_data_remove_reporting_only(cls): # Pare down build data to data needed for this test - ReportProductEmissionAllocation.objects.filter(emission_category__in=[5, 12]).delete() - ReportEmission.objects.filter(gas_type_id=3).delete() + t = cls.build() + t.allocation_4.delete() + t.allocation_5.delete() + t.report_emission_4.delete() + return t @classmethod def reporting_year_2025(cls): + t = cls.build() reporting_year = make_recipe( "reporting.tests.utils.reporting_year", reporting_year=2025, @@ -161,12 +174,11 @@ def reporting_year_2025(cls): reporting_window_end='2026-12-31 16:00:00-08', report_due_date='2025-05-31 16:59:59.999-07', ) - report = Report.objects.get(reporting_year_id=2024) - report.reporting_year = reporting_year - report.save() + Report.objects.filter(pk=t.report_1.id).update(reporting_year=reporting_year) + return t @classmethod def new_entrant(cls): - operation = Operation.objects.get(name='test sfo') - operation.registration_purpose = 'New Entrant Operation' - operation.save() + t = cls.build() + Operation.objects.filter(pk=t.operation_1.id).update(registration_purpose='New Entrant Operation') + return t diff --git a/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_class.py b/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_class.py index b22a51a025..b8684e254f 100644 --- a/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_class.py +++ b/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_class.py @@ -7,8 +7,7 @@ class TestComplianceSummaryServiceClass(TestCase): def test_compliance_summary_only_flaring_single_product(self): # Assertion values from compliance_class_manual_calcs.xlsx sheet 1 - build_data = ComplianceTestInfrastructure.build() - ComplianceTestInfrastructure.pare_data_single_product_flaring() + build_data = ComplianceTestInfrastructure.pare_data_single_product_flaring() result = ComplianceService.get_calculated_compliance_data(build_data.report_version_1.id) @@ -19,8 +18,7 @@ def test_compliance_summary_only_flaring_single_product(self): def test_with_industrial_process_emissions(self): # Assertion values from compliance_class_manual_calcs.xlsx sheet 2 - build_data = ComplianceTestInfrastructure.build() - ComplianceTestInfrastructure.pare_data_remove_reporting_only() + build_data = ComplianceTestInfrastructure.pare_data_remove_reporting_only() result = ComplianceService.get_calculated_compliance_data(build_data.report_version_1.id) @@ -46,8 +44,7 @@ def test_compliance_summary_with_all_data(self): def test_compliance_summary_2025_period(self): # Assertion values from compliance_class_manual_calcs.xlsx sheet 4 - build_data = ComplianceTestInfrastructure.build() - ComplianceTestInfrastructure.reporting_year_2025() + build_data = ComplianceTestInfrastructure.reporting_year_2025() result = ComplianceService.get_calculated_compliance_data(build_data.report_version_1.id) @@ -59,8 +56,7 @@ def test_compliance_summary_2025_period(self): assert result.credited_emissions == 0 def test_new_entrant(self): - build_data = ComplianceTestInfrastructure.build() - ComplianceTestInfrastructure.new_entrant() + build_data = ComplianceTestInfrastructure.new_entrant() ## TESTS ## result = ComplianceService.get_calculated_compliance_data(build_data.report_version_1.id) diff --git a/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_static.py b/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_static.py index 1303a991ff..a3902751c6 100644 --- a/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_static.py +++ b/bc_obps/reporting/tests/service/test_compliance_service/test_compliance_service_static.py @@ -15,18 +15,10 @@ def test_get_regulatory_values_by_naics_code(self): report_version_1 = make_recipe("reporting.tests.utils.report_version") report_version_2 = make_recipe("reporting.tests.utils.report_version") # Override parent values auto-generated by recipe - operation = Operation.objects.get(pk=report_version_1.report.operation.id) - report = Report.objects.get(pk=report_version_1.report_id) - operation.naics_code_id = 1 - operation.save() - report.reporting_year_id = 2024 - report.save() - operation = Operation.objects.get(pk=report_version_2.report.operation.id) - report = Report.objects.get(pk=report_version_2.report_id) - operation.naics_code_id = 22 - operation.save() - report.reporting_year_id = 2024 - report.save() + Operation.objects.filter(pk=report_version_1.report.operation.id).update(naics_code_id=1) + Report.objects.filter(pk=report_version_1.report_id).update(reporting_year_id=2024) + Operation.objects.filter(pk=report_version_2.report.operation.id).update(naics_code_id=22) + Report.objects.filter(pk=report_version_2.report_id).update(reporting_year_id=2024) ## TESTS ## # Test service function returns correct values @@ -251,9 +243,7 @@ def test_get_reporting_only_allocated(self): emission_category=EmissionCategory.objects.get(pk=1), allocated_quantity=Decimal('12.0'), ) - rp = ReportProduct.objects.get(pk=fog_product_allocation.report_product_id) - rp.product_id = 39 - rp.save() + ReportProduct.objects.filter(pk=fog_product_allocation.report_product_id).update(product_id=39) # Correctly aggregates reporting-only emissions when there is a fog product reporting_only_with_fog_for_test = ComplianceService.get_reporting_only_allocated(