Skip to content

Commit

Permalink
improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dummylabs committed Jun 12, 2022
1 parent 4c6e0e4 commit 2875220
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 53 deletions.
34 changes: 24 additions & 10 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -35,42 +41,50 @@ 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
hass.states.async_set("sensor.test1_unknown", "available")
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
Expand Down
51 changes: 35 additions & 16 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
assert len(hass.data[DOMAIN]["services_missing"]) == 2
72 changes: 45 additions & 27 deletions tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<test_name>_pyloop_current
# reports stored here: /tmp/pytest-of-root/pytest-current/<test_name>_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/<test_name>_pyloop_current
# reports stored here: /tmp/pytest-of-root/pytest-current/<test_name>_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/<test_name>_pyloop_current
# reports stored here: /tmp/pytest-of-root/pytest-current/<test_name>_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/<test_name>_pyloop_current
# reports stored here: /tmp/pytest-of-root/pytest-current/<test_name>_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")
]

0 comments on commit 2875220

Please sign in to comment.