diff --git a/tests/test_events.py b/tests/test_events.py index ccbe155..f52ec9c 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -8,23 +8,29 @@ from custom_components.watchman.const import DOMAIN, CONF_INCLUDED_FOLDERS from custom_components.watchman.config_flow import DEFAULT_DATA +TEST_INCLUDED_FOLDERS = ["/workspaces/thewatchman/tests/input"] + + async def test_add_service(hass): """test adding and removing service events""" + @callback - def dummy_service_handler(event): # pylint: disable=unused-argument + def dummy_service_handler(event): # pylint: disable=unused-argument """dummy service handler.""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 3 assert len(hass.data[DOMAIN]["services_missing"]) == 3 - hass.services.async_register('fake', 'service1', dummy_service_handler) + hass.services.async_register("fake", "service1", dummy_service_handler) await hass.async_block_till_done() assert len(hass.data[DOMAIN]["services_missing"]) == 2 hass.services.async_remove("fake", "service1") @@ -35,12 +41,14 @@ def dummy_service_handler(event): # pylint: disable=unused-argument async def test_change_state(hass): """test change entity state events""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 3 assert len(hass.data[DOMAIN]["services_missing"]) == 3 @@ -48,29 +56,35 @@ async def test_change_state(hass): await hass.async_block_till_done() assert len(hass.data[DOMAIN]["entities_missing"]) == 2 + async def test_remove_entity(hass): """test entity removal""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 3 hass.states.async_remove("sensor.test4_avail") await hass.async_block_till_done() assert len(hass.data[DOMAIN]["entities_missing"]) == 4 + async def test_add_entity(hass): """test entity addition""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 4 # missing -> 42 diff --git a/tests/test_init.py b/tests/test_init.py index f9c5b7b..0413422 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -9,89 +9,108 @@ CONF_IGNORED_STATES, DOMAIN, CONF_INCLUDED_FOLDERS, - CONF_IGNORED_FILES + CONF_IGNORED_FILES, ) from custom_components.watchman.config_flow import DEFAULT_DATA +TEST_INCLUDED_FOLDERS = ["/workspaces/thewatchman/tests/input"] + + async def test_init(hass): """test watchman initialization""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert "watchman_data" in hass.data - assert hass.services.has_service(DOMAIN, 'report') + assert hass.services.has_service(DOMAIN, "report") async def test_missing(hass): """test missing entities detection""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 3 assert len(hass.data[DOMAIN]["services_missing"]) == 3 + async def test_ignored_state(hass): """test single ingnored state processing""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS options[CONF_IGNORED_STATES] = ["unknown"] hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 2 assert len(hass.data[DOMAIN]["services_missing"]) == 3 + async def test_multiple_ignored_states(hass): """test multiple ingnored states processing""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] - options[CONF_IGNORED_STATES] = ["unknown","missing","unavailable"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS + options[CONF_IGNORED_STATES] = ["unknown", "missing", "unavailable"] hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 0 assert len(hass.data[DOMAIN]["services_missing"]) == 0 + async def test_ignored_files(hass): """test ignored files processing""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS options[CONF_IGNORED_STATES] = [] options[CONF_IGNORED_FILES] = ["*/test_services.yaml"] hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entities_missing"]) == 3 assert len(hass.data[DOMAIN]["services_missing"]) == 0 + async def test_ignored_items(hass): """test ignored files processing""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS options[CONF_IGNORED_STATES] = [] options[CONF_IGNORED_ITEMS] = ["sensor.test1_*", "timer.*"] hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) assert len(hass.data[DOMAIN]["entity_list"]) == 3 assert len(hass.data[DOMAIN]["service_list"]) == 2 assert len(hass.data[DOMAIN]["entities_missing"]) == 2 - assert len(hass.data[DOMAIN]["services_missing"]) == 2 \ No newline at end of file + assert len(hass.data[DOMAIN]["services_missing"]) == 2 diff --git a/tests/test_report.py b/tests/test_report.py index 11292bd..5b1a1f7 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -10,95 +10,113 @@ CONF_INCLUDED_FOLDERS, CONF_IGNORED_FILES, CONF_REPORT_PATH, - CONF_COLUMNS_WIDTH + CONF_COLUMNS_WIDTH, ) from custom_components.watchman.config_flow import DEFAULT_DATA +TEST_INCLUDED_FOLDERS = ["/workspaces/thewatchman/tests/input"] + + async def test_table_default(hass, tmpdir): """test table rendering""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS options[CONF_IGNORED_STATES] = [] options[CONF_IGNORED_FILES] = [] base_report = "/workspaces/thewatchman/tests/input/test_report1.txt" - #reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current + # reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current test_report = tmpdir.join("test_report1.txt") options[CONF_REPORT_PATH] = test_report hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) - await hass.services.async_call(DOMAIN, "report", {"test_mode":True}) + await hass.services.async_call(DOMAIN, "report", {"test_mode": True}) await hass.async_block_till_done() - assert [row for row in open(test_report, encoding="utf-8")] ==\ - [row for row in open(base_report, encoding="utf-8")] + assert [row for row in open(test_report, encoding="utf-8")] == [ + row for row in open(base_report, encoding="utf-8") + ] + async def test_table_no_missing(hass, tmpdir): """test table rendering with no missing elements""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS options[CONF_IGNORED_STATES] = ["missing"] options[CONF_IGNORED_FILES] = [] base_report = "/workspaces/thewatchman/tests/input/test_report2.txt" - #reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current + # reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current test_report = tmpdir.join("test_report2.txt") options[CONF_REPORT_PATH] = test_report hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) - await hass.services.async_call(DOMAIN, "report", {"test_mode":True}) + await hass.services.async_call(DOMAIN, "report", {"test_mode": True}) await hass.async_block_till_done() - assert [row for row in open(test_report, encoding="utf-8")] ==\ - [row for row in open(base_report, encoding="utf-8")] + assert [row for row in open(test_report, encoding="utf-8")] == [ + row for row in open(base_report, encoding="utf-8") + ] + async def test_table_all_clear(hass, tmpdir): """test table rendering with no entries""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] - options[CONF_IGNORED_STATES] = ["missing","unknown","unavailable"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS + options[CONF_IGNORED_STATES] = ["missing", "unknown", "unavailable"] options[CONF_IGNORED_FILES] = [] base_report = "/workspaces/thewatchman/tests/input/test_report3.txt" - #reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current + # reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current test_report = tmpdir.join("test_report3.txt") options[CONF_REPORT_PATH] = test_report hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) - await hass.services.async_call(DOMAIN, "report", {"test_mode":True}) + await hass.services.async_call(DOMAIN, "report", {"test_mode": True}) await hass.async_block_till_done() - assert [row for row in open(test_report, encoding="utf-8")] ==\ - [row for row in open(base_report, encoding="utf-8")] + assert [row for row in open(test_report, encoding="utf-8")] == [ + row for row in open(base_report, encoding="utf-8") + ] + async def test_column_resize(hass, tmpdir): """test table rendering with narrow columns""" options = deepcopy(DEFAULT_DATA) - options[CONF_INCLUDED_FOLDERS] = ["/workspaces/thewatchman/tests/*"] + options[CONF_INCLUDED_FOLDERS] = TEST_INCLUDED_FOLDERS options[CONF_IGNORED_STATES] = [] options[CONF_IGNORED_FILES] = [] - options[CONF_COLUMNS_WIDTH] = [7,7,7] + options[CONF_COLUMNS_WIDTH] = [7, 7, 7] base_report = "/workspaces/thewatchman/tests/input/test_report4.txt" - #reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current + # reports stored here: /tmp/pytest-of-root/pytest-current/_pyloop_current test_report = tmpdir.join("test_report4.txt") options[CONF_REPORT_PATH] = test_report hass.states.async_set("sensor.test1_unknown", "unknown") hass.states.async_set("sensor.test2_missing", "missing") hass.states.async_set("sensor.test3_unavail", "unavailable") hass.states.async_set("sensor.test4_avail", "42") - config_entry = MockConfigEntry(domain='watchman', data={}, options=options, entry_id="test") + config_entry = MockConfigEntry( + domain="watchman", data={}, options=options, entry_id="test" + ) assert await async_setup_entry(hass, config_entry) - await hass.services.async_call(DOMAIN, "report", {"test_mode":True}) + await hass.services.async_call(DOMAIN, "report", {"test_mode": True}) await hass.async_block_till_done() - assert [row for row in open(test_report, encoding="utf-8")] ==\ - [row for row in open(base_report, encoding="utf-8")] + assert [row for row in open(test_report, encoding="utf-8")] == [ + row for row in open(base_report, encoding="utf-8") + ]