Skip to content

Commit

Permalink
Smoketests for some cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k committed Jan 12, 2020
1 parent e36f185 commit 4d65547
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tests/aio/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import threading
import time
from unittest import mock

import pytest

Expand Down Expand Up @@ -72,7 +73,10 @@ async def async_cleanup():
break
time.sleep(1)

yield controller
with mock.patch.object(controller, "load_data", lambda x: None):
with mock.patch("homekit.aio.__main__.Controller") as c:
c.return_value = controller
yield controller

httpd.shutdown()

Expand Down Expand Up @@ -141,7 +145,10 @@ async def async_cleanup():
event_loop.run_until_complete(async_cleanup())
request.addfinalizer(cleanup)

yield controller
with mock.patch.object(controller, "load_data", lambda x: None):
with mock.patch("homekit.aio.__main__.Controller") as c:
c.return_value = controller
yield controller

httpd.shutdown()

Expand Down
65 changes: 65 additions & 0 deletions tests/aio/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Test the AIO CLI variant."""

import json
from unittest import mock

import pytest

from homekit.aio.__main__ import main


# Without this line you would have to mark your async tests with @pytest.mark.asyncio
pytestmark = pytest.mark.asyncio


async def test_help():
with mock.patch("sys.stdout") as stdout:
with pytest.raises(SystemExit):
await main(["-h"])
printed = stdout.write.call_args[0][0]

assert printed.startswith("usage: ")
assert "discover_ip" in printed


async def test_get_accessories(pairing):
with mock.patch("sys.stdout") as stdout:
await main(["get_accessories", "-f", "pairing.json", "-a", "alias"])
printed = stdout.write.call_args_list[0][0][0]
assert printed.startswith("1.2: >accessory-information")

with mock.patch("sys.stdout") as stdout:
await main(["get_accessories", "-f", "pairing.json", "-a", "alias", "-o", "json"])
printed = stdout.write.call_args_list[0][0][0]
accessories = json.loads(printed)
assert accessories[0]["aid"] == 1
assert accessories[0]["services"][0]["iid"] == 2
assert accessories[0]["services"][0]["characteristics"][0]["iid"] == 3


async def test_get_characteristic(pairing):
with mock.patch("sys.stdout") as stdout:
await main(["get_characteristics", "-f", "pairing.json", "-a", "alias", "-c", "1.10"])
printed = stdout.write.call_args_list[0][0][0]
assert json.loads(printed) == {"1.10": {"value": False}}


async def test_put_characteristic(pairing):
with mock.patch("sys.stdout"):
await main(["put_characteristics", "-f", "pairing.json", "-a", "alias", "-c", "1.10", "true"])

characteristics = await pairing.get_characteristics([
(1, 10),
])
assert characteristics[(1, 10)] == {'value': True}


async def test_list_pairings(pairing):
with mock.patch("sys.stdout") as stdout:
await main(["list_pairings", "-f", "pairing.json", "-a", "alias"])
printed = "".join(write[0][0] for write in stdout.write.call_args_list)
assert printed == (
"Pairing Id: decc6fa3-de3e-41c9-adba-ef7409821bfc\n"
"\tPublic Key: 0xd708df2fbf4a8779669f0ccd43f4962d6d49e4274f88b1292f822edc3bcf8ed8\n"
"\tPermissions: 1 (admin)\n"
)

0 comments on commit 4d65547

Please sign in to comment.