import pytest
from etl.epc_clean.epc_attributes.WallAttributes import WallAttributes
from etl.epc_clean.tests.test_data.test_wall_attributes_cases import wall_cases


class TestWallAttributes:

    @pytest.fixture
    def wall_attr(self):
        def _wall_attr(description):
            return WallAttributes(description)

        return _wall_attr

    def test_thermal_transmittance(self, wall_attr):
        description = 'average thermal transmittance -4.67 w/m-¦k'
        wa = wall_attr(description)
        result = wa.process()
        assert result['thermal_transmittance'] == 4.67
        assert result['thermal_transmittance_unit'] == 'w/m-¦k'

    def test_wall_types(self, wall_attr):
        description = 'solid brick system built'
        wa = wall_attr(description)
        result = wa.process()
        assert result['is_solid_brick'] is True
        assert result['is_system_built'] is True
        assert result['is_timber_frame'] is False

    def test_insulation_thickness(self, wall_attr):
        description = 'additional insulation'
        wa = wall_attr(description)
        result = wa.process()
        assert result['insulation_thickness'] == 'above average'

    def test_insulation_type(self, wall_attr):
        description = 'internal insulation'
        wa = wall_attr(description)
        result = wa.process()
        assert result['internal_insulation'] is True
        assert result['external_insulation'] is False

    @pytest.mark.parametrize(
        "test_case",
        wall_cases
    )
    def test_wall_attributes(self, test_case):
        expected_result = test_case.copy()
        del expected_result["original_description"]
        result = WallAttributes(test_case['original_description']).process()
        # Some of the expected_result test data was produced before some attributes were added to the code
        # base so we need to filter out some of the keys. The test is still valid
        result = {k: v for k, v in result.items() if v}
        expected_result = {k: v for k, v in expected_result.items() if v}
        if not result:
            raise Exception("Something went wong")
        # Ensure the output ordering is correct
        assert sorted(result.items()) == sorted(expected_result.items())

    def test_empty_str(self):
        assert WallAttributes("").process() == {
            'thermal_transmittance': None, 'thermal_transmittance_unit': None, 'is_cavity_wall': False,
            'is_filled_cavity': False, 'is_solid_brick': False, 'is_system_built': False, 'is_timber_frame': False,
            'is_granite_or_whinstone': False, 'is_as_built': False, 'is_cob': False, 'is_assumed': False,
            'is_sandstone_or_limestone': False, 'insulation_thickness': 'none', 'external_insulation': False,
            'internal_insulation': False, "is_park_home": False
        }
