Skip to content

Commit

Permalink
remove unnecessary asyncio.Tasks from tests (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Sep 21, 2020
1 parent 88ed733 commit 17fefb9
Show file tree
Hide file tree
Showing 34 changed files with 318 additions and 362 deletions.
36 changes: 15 additions & 21 deletions test/core_tests/telegram_queue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def async_telegram_received_cb(device):
group_address=GroupAddress("1/2/3"),
)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue.process_telegram_incoming(telegram))
xknx.telegram_queue.process_telegram_incoming(telegram)
)
telegram_received_callback.assert_called_once_with(telegram)

Expand Down Expand Up @@ -144,7 +144,7 @@ async def async_telegram_received_cb(device):
group_address=GroupAddress("1/2/3"),
)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue.process_telegram_incoming(telegram))
xknx.telegram_queue.process_telegram_incoming(telegram)
)
telegram_received_callback.assert_not_called()

Expand All @@ -170,7 +170,7 @@ def test_process_to_device(self, devices_by_ga_mock):
group_address=GroupAddress("1/2/3"),
)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue.process_telegram_incoming(telegram))
xknx.telegram_queue.process_telegram_incoming(telegram)
)

devices_by_ga_mock.assert_called_once_with(GroupAddress("1/2/3"))
Expand All @@ -197,7 +197,7 @@ async def async_telegram_received_cb(device):
group_address=GroupAddress("1/2/3"),
)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue.process_telegram_incoming(telegram))
xknx.telegram_queue.process_telegram_incoming(telegram)
)
telegram_received_callback.assert_called_once_with(telegram)
devices_by_ga_mock.assert_not_called()
Expand All @@ -221,15 +221,15 @@ def test_outgoing(self, logger_warning_mock, if_mock):

# log a warning if there is no KNXIP interface instanciated
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue.process_telegram_outgoing(telegram))
xknx.telegram_queue.process_telegram_outgoing(telegram)
)
logger_warning_mock.assert_called_once_with("No KNXIP interface defined")
if_mock.send_telegram.assert_not_called()

# if we have an interface send the telegram
xknx.knxip_interface = if_mock
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue.process_telegram_outgoing(telegram))
xknx.telegram_queue.process_telegram_outgoing(telegram)
)
if_mock.send_telegram.assert_called_once_with(telegram)

Expand All @@ -254,9 +254,8 @@ async def process_exception():
)

xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue._process_all_telegrams())
)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())

logging_error_mock.assert_called_once_with(
"Error while processing telegram %s",
CouldNotParseTelegram(
Expand Down Expand Up @@ -291,9 +290,7 @@ def test_process_all_telegrams(

xknx.telegrams.put_nowait(telegram_in)
xknx.telegrams.put_nowait(telegram_out)
res = self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue._process_all_telegrams())
)
res = self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())

self.assertIsNone(res)
self.assertEqual(process_telegram_incoming_mock.call_count, 1)
Expand Down Expand Up @@ -321,9 +318,8 @@ async def async_telegram_received_cb(device):
group_address=GroupAddress("1/2/3"),
)
xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue._process_all_telegrams())
)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())

telegram_received_callback.assert_called_with(telegram)

#
Expand Down Expand Up @@ -351,9 +347,8 @@ async def async_telegram_received_cb(device):
group_address=GroupAddress("1/2/3"),
)
xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue._process_all_telegrams())
)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())

telegram_received_callback.assert_called_with(telegram)

#
Expand Down Expand Up @@ -381,7 +376,6 @@ async def async_telegram_received_cb(device):
group_address=GroupAddress("1/2/3"),
)
xknx.telegrams.put_nowait(telegram)
self.loop.run_until_complete(
asyncio.Task(xknx.telegram_queue._process_all_telegrams())
)
self.loop.run_until_complete(xknx.telegram_queue._process_all_telegrams())

telegram_received_callback.assert_not_called()
8 changes: 4 additions & 4 deletions test/devices_tests/action_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_execute_base_action(self):
xknx = XKNX(loop=self.loop)
action = ActionBase(xknx)
with patch("logging.Logger.info") as mock_info:
self.loop.run_until_complete(asyncio.Task(action.execute()))
self.loop.run_until_complete(action.execute())
mock_info.assert_called_with("Execute not implemented for %s", "ActionBase")

def test_execute_action(self):
Expand All @@ -79,7 +79,7 @@ def test_execute_action(self):
fut = asyncio.Future()
fut.set_result(None)
mock_do.return_value = fut
self.loop.run_until_complete(asyncio.Task(action.execute()))
self.loop.run_until_complete(action.execute())
mock_do.assert_called_with("on")

def test_execute_action_callback(self):
Expand All @@ -92,7 +92,7 @@ async def async_callback():
callback()

action = ActionCallback(xknx, async_callback)
self.loop.run_until_complete(asyncio.Task(action.execute()))
self.loop.run_until_complete(action.execute())
callback.assert_called_with()

def test_execute_unknown_device(self):
Expand All @@ -101,7 +101,7 @@ def test_execute_unknown_device(self):

action = Action(xknx, target="Light1", method="on")
with patch("logging.Logger.warning") as logger_warning_mock:
self.loop.run_until_complete(asyncio.Task(action.execute()))
self.loop.run_until_complete(action.execute())
logger_warning_mock.assert_called_once_with(
"Unknown device %s witin action %s.", action.target, action
)
39 changes: 18 additions & 21 deletions test/devices_tests/binary_sensor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def test_process(self):
telegram_on = Telegram(
group_address=GroupAddress("1/2/3"), payload=DPTBinary(1)
)
self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
self.loop.run_until_complete(binaryinput.process(telegram_on))

self.assertEqual(binaryinput.state, True)

telegram_off = Telegram(
group_address=GroupAddress("1/2/3"), payload=DPTBinary(0)
)
self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_off)))
self.loop.run_until_complete(binaryinput.process(telegram_off))
self.assertEqual(binaryinput.state, False)

binaryinput2 = BinarySensor(xknx, "TestInput", "1/2/4")
Expand All @@ -56,7 +56,7 @@ def test_process(self):
telegram_off2 = Telegram(
group_address=GroupAddress("1/2/4"), payload=DPTBinary(0)
)
self.loop.run_until_complete(asyncio.Task(binaryinput2.process(telegram_off2)))
self.loop.run_until_complete(binaryinput2.process(telegram_off2))
self.assertEqual(binaryinput2.state, False)

def test_process_reset_after(self):
Expand All @@ -70,7 +70,7 @@ def test_process_reset_after(self):
group_address=GroupAddress("1/2/3"), payload=DPTBinary(1)
)

self.loop.run_until_complete(asyncio.Task(binaryinput.process(telegram_on)))
self.loop.run_until_complete(binaryinput.process(telegram_on))
self.loop.run_until_complete(asyncio.sleep(reset_after_ms * 2 / 1000))
self.assertEqual(binaryinput.state, False)

Expand All @@ -91,7 +91,7 @@ def test_process_action(self):
telegram_on = Telegram(
group_address=GroupAddress("1/2/3"), payload=DPTBinary(1)
)
self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_on)))
self.loop.run_until_complete(binary_sensor.process(telegram_on))
# process outgoing telegram from queue
self.loop.run_until_complete(switch.process(xknx.telegrams.get_nowait()))

Expand All @@ -101,7 +101,7 @@ def test_process_action(self):
telegram_off = Telegram(
group_address=GroupAddress("1/2/3"), payload=DPTBinary(0)
)
self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram_off)))
self.loop.run_until_complete(binary_sensor.process(telegram_off))
self.loop.run_until_complete(switch.process(xknx.telegrams.get_nowait()))

self.assertEqual(binary_sensor.state, False)
Expand Down Expand Up @@ -129,17 +129,16 @@ def test_process_action_ignore_internal_state(self):
"asyncio.sleep", new_callable=AsyncMock
) as mock_sleep:
mock_time.return_value = 1599076123.0
self.loop.run_until_complete(
asyncio.Task(binary_sensor.process(telegram_on))
)
self.loop.run_until_complete(binary_sensor.process(telegram_on))

self.loop.run_until_complete(
xknx.devices.process(xknx.telegrams.get_nowait())
)

self.assertEqual(binary_sensor.state, True)
self.assertEqual(switch.state, True)

self.loop.run_until_complete(asyncio.Task(switch.set_off()))
self.loop.run_until_complete(switch.set_off())
self.loop.run_until_complete(
xknx.devices.process(xknx.telegrams.get_nowait())
)
Expand All @@ -148,9 +147,8 @@ def test_process_action_ignore_internal_state(self):
self.assertEqual(binary_sensor.state, True)
# add a second to the time to avoid double tap behaviour here
mock_time.return_value += BinarySensor.DEFAULT_CONTEXT_TIMEOUT
self.loop.run_until_complete(
asyncio.Task(binary_sensor.process(telegram_on))
)
self.loop.run_until_complete(binary_sensor.process(telegram_on))

self.loop.run_until_complete(
xknx.devices.process(xknx.telegrams.get_nowait())
)
Expand All @@ -164,7 +162,7 @@ def test_process_wrong_payload(self):
binary_sensor = BinarySensor(xknx, "Warning", group_address_state="1/2/3")
telegram = Telegram(GroupAddress("1/2/3"), payload=DPTArray((0x1, 0x2, 0x3)))
with self.assertRaises(CouldNotParseTelegram):
self.loop.run_until_complete(asyncio.Task(binary_sensor.process(telegram)))
self.loop.run_until_complete(binary_sensor.process(telegram))

#
# TEST SWITCHED ON
Expand All @@ -176,9 +174,8 @@ def test_is_on(self):
self.assertFalse(binaryinput.is_on())
self.assertTrue(binaryinput.is_off())
# pylint: disable=protected-access
self.loop.run_until_complete(
asyncio.Task(binaryinput._set_internal_state(True))
)
self.loop.run_until_complete(binaryinput._set_internal_state(True))

self.assertTrue(binaryinput.is_on())
self.assertFalse(binaryinput.is_off())

Expand All @@ -190,9 +187,8 @@ def test_is_off(self):
xknx = XKNX(loop=self.loop)
binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
# pylint: disable=protected-access
self.loop.run_until_complete(
asyncio.Task(binaryinput._set_internal_state(False))
)
self.loop.run_until_complete(binaryinput._set_internal_state(False))

self.assertFalse(binaryinput.is_on())
self.assertTrue(binaryinput.is_off())

Expand All @@ -214,7 +210,8 @@ async def async_after_update_callback(device):
switch.register_device_updated_cb(async_after_update_callback)

telegram = Telegram(group_address=GroupAddress("1/2/3"), payload=DPTBinary(1))
self.loop.run_until_complete(asyncio.Task(switch.process(telegram)))
self.loop.run_until_complete(switch.process(telegram))

after_update_callback.assert_called_with(switch)

#
Expand Down

0 comments on commit 17fefb9

Please sign in to comment.