import unittest

from nose.tools import assert_almost_equal

from helix.calculators.pressure_coefficient_calculator import PressureCoefficientCalculator
from helix.constants.module_type import ModuleType
from helix.constants.system_type import SystemType
from helix.models.sql.sites import Site
from helix.user_values import UserValues


class PressureCoefficientCalculatorTest(unittest.TestCase):
    def setUp(self):
        self.site = Site(
            building_height=0,
            building_width=0,
            building_length=0,
            parapet_height=0,
            wind_speed=0,
            exposure_category='C',
            ballast_block_weight=0,
            max_psf=0,
            system_type=SystemType.dualTilt.value,
            module_type=ModuleType.Cell128.value,
            spectral_response=0,
            seismic_importance_factor=1,
        )
        self.subject = PressureCoefficientCalculator(UserValues(None, self.site))

    def test_L_B_when_building_dimensions_are_zero(self):
        self.site.building_height = 0
        self.site.building_width = 0
        self.site.building_length = 0

        assert_almost_equal(self.subject.L_B(), 1.549193338, 2)

    def test_L_B_when_limited_by_building_height(self):
        self.site.building_height = 35
        self.site.building_width = 250
        self.site.building_length = 250

        assert_almost_equal(self.subject.L_B(), 35.000, 2)

    def test_L_B_when_limited_by_width(self):
        self.site.building_height = 35
        self.site.building_width = 80
        self.site.building_length = 75

        assert_almost_equal(self.subject.L_B(), 21.166, 2)

    def test_L_B_when_limited_by_length(self):
        self.site.building_height = 80
        self.site.building_width = 90
        self.site.building_length = 100

        assert_almost_equal(self.subject.L_B(), 35.777, 2)

    def test_L_B_when_building_height_less_than_15(self):
        self.site.building_height = 12
        self.site.building_width = 250
        self.site.building_length = 250

        assert_almost_equal(self.subject.L_B(), 15.000, 2)

    def test_parapet_factor_when_parapet_height_over_building_height_greater_than_point_2(self):
        self.site.building_height = 20
        self.site.parapet_height = 10
        assert_almost_equal(self.subject.parapet_factor(), 1.12, 2)

        self.site.building_height = 0
        assert_almost_equal(self.subject.parapet_factor(), 1.12, 2)

    def test_parapet_factor_when_parapet_height_over_building_height_less_than_point_2(self):
        self.site.building_height = 20
        self.site.parapet_height = 1
        assert_almost_equal(self.subject.parapet_factor(), 0.94, 2)

        self.site.building_height = 0
        assert_almost_equal(self.subject.parapet_factor(), 0.96, 2)

        self.site.parapet_height = 0
        assert_almost_equal(self.subject.parapet_factor(), 0.88, 2)
