|
| 1 | +from common.exceptions import InternalSystemError |
| 2 | +from model_bakery.baker import make, make_recipe, prepare_recipe |
| 3 | +import pytest |
| 4 | +from reporting.models.configuration_element import ConfigurationElement |
| 5 | +from reporting.service.report_validation.report_validation_tags import ValidationTags |
| 6 | +from reporting.service.report_validation.validators import reporting_configuration_validator |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.django_db |
| 10 | +class TestReportingConfigurationValidator: |
| 11 | + |
| 12 | + validator_under_test = reporting_configuration_validator |
| 13 | + |
| 14 | + def setup_method(self): |
| 15 | + self.report_version = make_recipe( |
| 16 | + "reporting.tests.utils.report_version", report__reporting_year__reporting_year=2018 |
| 17 | + ) |
| 18 | + self.configuration = make_recipe( |
| 19 | + "reporting.tests.utils.configuration", valid_from="2018-01-01", valid_to="2018-12-31" |
| 20 | + ) |
| 21 | + |
| 22 | + def test_has_the_proper_configuration(self): |
| 23 | + assert self.validator_under_test.TAGS == [ValidationTags.REPORT_VALIDATION, ValidationTags.ON_SUBMIT] |
| 24 | + |
| 25 | + def test_raises_if_no_configuration_element_present(self): |
| 26 | + report_methodology = make_recipe( |
| 27 | + "reporting.tests.utils.report_methodology", |
| 28 | + report_version=self.report_version, |
| 29 | + ) |
| 30 | + |
| 31 | + with pytest.raises( |
| 32 | + InternalSystemError, |
| 33 | + match=f"Missing configuration elements for report methodology IDs: {report_methodology.id}", |
| 34 | + ): |
| 35 | + self.validator_under_test.validate(self.report_version) |
| 36 | + |
| 37 | + def test_raises_if_configuration_doesnt_match_reporting_year(self): |
| 38 | + report_methodology = make_recipe( |
| 39 | + "reporting.tests.utils.report_methodology", |
| 40 | + report_version=self.report_version, |
| 41 | + ) |
| 42 | + |
| 43 | + wrong_year_confguration = make_recipe( |
| 44 | + "reporting.tests.utils.configuration", valid_from="2011-01-01", valid_to="2012-12-31" |
| 45 | + ) |
| 46 | + make( |
| 47 | + ConfigurationElement, |
| 48 | + activity=report_methodology.report_emission.report_source_type.report_activity.activity, |
| 49 | + source_type=report_methodology.report_emission.report_source_type.source_type, |
| 50 | + gas_type=report_methodology.report_emission.gas_type, |
| 51 | + methodology=report_methodology.methodology, |
| 52 | + valid_from=wrong_year_confguration, |
| 53 | + valid_to=wrong_year_confguration, |
| 54 | + ) |
| 55 | + |
| 56 | + with pytest.raises( |
| 57 | + InternalSystemError, |
| 58 | + match=f"Missing configuration elements for report methodology IDs: {report_methodology.id}", |
| 59 | + ): |
| 60 | + self.validator_under_test.validate(self.report_version) |
| 61 | + |
| 62 | + def test_raises_if_configuration_doesnt_match_activity(self): |
| 63 | + report_methodology = make_recipe( |
| 64 | + "reporting.tests.utils.report_methodology", |
| 65 | + report_version=self.report_version, |
| 66 | + ) |
| 67 | + wrong_activity = make_recipe("reporting.tests.utils.activity") |
| 68 | + |
| 69 | + make( |
| 70 | + ConfigurationElement, |
| 71 | + activity=wrong_activity, |
| 72 | + source_type=report_methodology.report_emission.report_source_type.source_type, |
| 73 | + gas_type=report_methodology.report_emission.gas_type, |
| 74 | + methodology=report_methodology.methodology, |
| 75 | + valid_from=self.configuration, |
| 76 | + valid_to=self.configuration, |
| 77 | + ) |
| 78 | + |
| 79 | + with pytest.raises( |
| 80 | + InternalSystemError, |
| 81 | + match=f"Missing configuration elements for report methodology IDs: {report_methodology.id}", |
| 82 | + ): |
| 83 | + self.validator_under_test.validate(self.report_version) |
| 84 | + |
| 85 | + def test_raises_if_reported_fields_not_allowed_in_configuration_element(self): |
| 86 | + report_methodology = make_recipe( |
| 87 | + "reporting.tests.utils.report_methodology", |
| 88 | + report_version=self.report_version, |
| 89 | + json_data={"allowedField": "test data", "unexpectedField": 123}, |
| 90 | + ) |
| 91 | + |
| 92 | + make( |
| 93 | + ConfigurationElement, |
| 94 | + activity=report_methodology.report_emission.report_source_type.report_activity.activity, |
| 95 | + source_type=report_methodology.report_emission.report_source_type.source_type, |
| 96 | + gas_type=report_methodology.report_emission.gas_type, |
| 97 | + methodology=report_methodology.methodology, |
| 98 | + valid_from=self.configuration, |
| 99 | + valid_to=self.configuration, |
| 100 | + reporting_fields=[prepare_recipe("reporting.tests.utils.reporting_field", slug="allowedField")], |
| 101 | + ) |
| 102 | + |
| 103 | + with pytest.raises( |
| 104 | + InternalSystemError, |
| 105 | + match=f"ReportMethodology ID {report_methodology.id} has reporting fields" |
| 106 | + " {'unexpectedField'} which are not in the allowed fields {'allowedField'} of its matching configuration element.", |
| 107 | + ): |
| 108 | + self.validator_under_test.validate(self.report_version) |
| 109 | + |
| 110 | + def test_passes_if_no_report_methodlogy_for_that_report_version(self): |
| 111 | + assert self.validator_under_test.validate(self.report_version) == {} |
| 112 | + |
| 113 | + def test_passes_with_fieldunits_field_reported(self): |
| 114 | + report_methodology = make_recipe( |
| 115 | + "reporting.tests.utils.report_methodology", |
| 116 | + report_version=self.report_version, |
| 117 | + json_data={ |
| 118 | + "allowedField": "test data", |
| 119 | + "allowedFieldFieldUnits": "test units", |
| 120 | + "anotherAllowedReportingField": 111, |
| 121 | + }, |
| 122 | + ) |
| 123 | + |
| 124 | + allowed_fields = [ |
| 125 | + prepare_recipe("reporting.tests.utils.reporting_field", slug="allowedField"), |
| 126 | + prepare_recipe("reporting.tests.utils.reporting_field", slug="anotherAllowedReportingField"), |
| 127 | + prepare_recipe("reporting.tests.utils.reporting_field", slug="unreportedField"), |
| 128 | + ] |
| 129 | + |
| 130 | + make( |
| 131 | + ConfigurationElement, |
| 132 | + activity=report_methodology.report_emission.report_source_type.report_activity.activity, |
| 133 | + source_type=report_methodology.report_emission.report_source_type.source_type, |
| 134 | + gas_type=report_methodology.report_emission.gas_type, |
| 135 | + methodology=report_methodology.methodology, |
| 136 | + valid_from=self.configuration, |
| 137 | + valid_to=self.configuration, |
| 138 | + reporting_fields=allowed_fields, |
| 139 | + ) |
| 140 | + |
| 141 | + assert self.validator_under_test.validate(self.report_version) == {} |
0 commit comments