Add tests for workday sensor (#31832)

* Workday Fix

* fix pylint errors

* Update binary_sensor.py

* Update test_binary_sensor.py

Added tests to match the document new configuration examples
This commit is contained in:
Paul Enright 2020-03-10 02:53:06 -05:00 committed by GitHub
parent 11eee43fc7
commit ac9c9377c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,26 @@ class TestWorkdaySetup:
}
}
self.config_example1 = {
"binary_sensor": {
"platform": "workday",
"country": "US",
"workdays": ["mon", "tue", "wed", "thu", "fri"],
"excludes": ["sat", "sun"],
}
}
self.config_example2 = {
"binary_sensor": {
"platform": "workday",
"country": "DE",
"province": "BW",
"workdays": ["mon", "wed", "fri"],
"excludes": ["sat", "sun", "holiday"],
"add_holidays": ["2020-02-24"],
}
}
self.config_tomorrow = {
"binary_sensor": {"platform": "workday", "country": "DE", "days_offset": 1}
}
@ -229,6 +249,43 @@ class TestWorkdaySetup:
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to a Presidents day to test Holiday on a Work day - Jan 20th, 2020
# Presidents day Feb 17th 2020 is mon.
@patch(FUNCTION_PATH, return_value=date(2020, 2, 17))
def test_config_example1_holiday(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_example1)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "on"
# Freeze time to test tue - Feb 18th, 2020
@patch(FUNCTION_PATH, return_value=date(2020, 2, 18))
def test_config_example2_tue(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_example2)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
# Freeze time to test mon, but added as holiday - Feb 24th, 2020
@patch(FUNCTION_PATH, return_value=date(2020, 2, 24))
def test_config_example2_add_holiday(self, mock_date):
"""Test if public holidays are reported correctly."""
with assert_setup_component(1, "binary_sensor"):
setup_component(self.hass, "binary_sensor", self.config_example2)
self.hass.start()
entity = self.hass.states.get("binary_sensor.workday_sensor")
assert entity.state == "off"
def test_day_to_string(self):
"""Test if day_to_string is behaving correctly."""
assert binary_sensor.day_to_string(0) == "mon"