diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2021-04-15 13:34:37 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2021-04-15 13:34:37 -0500 |
| commit | bd067ba87b458f440a59d1c4668840dcb9e9a789 (patch) | |
| tree | 7182daa5ea473594e7c5fb73de2075d2afcf424c /tests | |
| parent | Add napoleon extension settings (diff) | |
| download | lirc-bd067ba87b458f440a59d1c4668840dcb9e9a789.zip | |
Fix send_once issue where signal is sent twice
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_client.py | 34 | ||||
| -rw-r--r-- | tests/test_default_connection.py | 43 | ||||
| -rw-r--r-- | tests/test_lircd_connection.py | 10 | ||||
| -rw-r--r-- | tests/test_reply_packet_parser.py | 124 |
4 files changed, 122 insertions, 89 deletions
diff --git a/tests/test_client.py b/tests/test_client.py index f4edf10..237a038 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,3 +1,5 @@ +from unittest import mock + import pytest from lirc import Client, LircdConnection @@ -18,7 +20,9 @@ def test_that_custom_connections_can_be_used(mock_socket): assert client._Client__connection == connection -def test_that_custom_connection_that_is_not_a_lircd_connection_raises_error(): +def test_that_custom_connection_that_is_not_a_lircd_connection_raises_error( + mock_connection, +): """ lirc.Client.__init__ @@ -26,11 +30,29 @@ def test_that_custom_connection_that_is_not_a_lircd_connection_raises_error(): LircdConnection raise a TypeError. """ with pytest.raises(TypeError) as error: - Client(connection=Client()) # SUT + Client(connection=Client(connection=mock_connection)) # SUT assert "must be an instance of `LircdConnection`" in str(error) +def test_that_no_connection_passed_in_creates_default_one(): + """ + lirc.Client.__init__ + + Ensure that creating the Client without specifying the + connection as a keyword argument creates a default one. + """ + + class MockedLircdConnection(LircdConnection): + def __new__(cls, *args, **kwargs): + return mock.Mock(spec=cls) + + with mock.patch("lirc.client.LircdConnection", MockedLircdConnection): + c = Client() + + assert isinstance(c._Client__connection, MockedLircdConnection) + + def test_that_close_closes_the_socket(mock_client_and_connection): """ lirc.Client.close @@ -47,7 +69,7 @@ def test_that_close_closes_the_socket(mock_client_and_connection): @pytest.mark.parametrize( "client_command, args, lircd_command", [ - ("send_once", {"remote": "REMOTE", "key": "KEY"}, "SEND_ONCE REMOTE KEY 1"), + ("send_once", {"remote": "REMOTE", "key": "KEY"}, "SEND_ONCE REMOTE KEY 0"), ( "send_once", {"remote": "REMOTE", "key": "KEY", "repeat_count": 5}, @@ -126,9 +148,9 @@ def test_unsuccessful_command_raises_custom_exception(mock_client_and_connection ) with pytest.raises(LircdCommandFailureError) as error: - client.send_once('remote', 'key') + client.send_once("remote", "key") - assert 'The `SEND_ONCE remote key 1` command sent to lircd failed: []' in str(error) + assert "command sent to lircd failed: []" in str(error) def test_last_send_start_remote_and_key_is_used(mock_client_and_connection): @@ -144,7 +166,7 @@ def test_last_send_start_remote_and_key_is_used(mock_client_and_connection): connection._LircdConnection__socket.recv.return_value = ( b"BEGIN\nCOMMAND\nSUCCESS\nEND\n" ) - client.send_start('remote', 'key') + client.send_start("remote", "key") client.send_stop() # SUT diff --git a/tests/test_default_connection.py b/tests/test_default_connection.py index bc4fa09..8488dd3 100644 --- a/tests/test_default_connection.py +++ b/tests/test_default_connection.py @@ -1,29 +1,32 @@ -from unittest.mock import patch import socket +from unittest.mock import patch + +import pytest from lirc.connection.default_connection import DefaultConnection from lirc.exceptions import UnsupportedOperatingSystemError -import pytest - -@pytest.mark.parametrize("os, expected_socket, expected_address", [ - ( - "Windows", - socket.socket(socket.AF_INET, socket.SOCK_STREAM), - ("localhost", 8765) - ), - ( - "Linux", - socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), - "/var/run/lirc/lircd" - ), - ( - "Darwin", - socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), - "/opt/run/var/run/lirc/lircd" - ) -]) +@pytest.mark.parametrize( + "os, expected_socket, expected_address", + [ + ( + "Windows", + socket.socket(socket.AF_INET, socket.SOCK_STREAM), + ("localhost", 8765), + ), + ( + "Linux", + socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), + "/var/run/lirc/lircd", + ), + ( + "Darwin", + socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), + "/opt/run/var/run/lirc/lircd", + ), + ], +) @patch("platform.system") def test_default_connection_has_correct_os_specific_socket_and_address( patched_system, os, expected_socket, expected_address diff --git a/tests/test_lircd_connection.py b/tests/test_lircd_connection.py index 7f8ff0a..7221215 100644 --- a/tests/test_lircd_connection.py +++ b/tests/test_lircd_connection.py @@ -1,6 +1,6 @@ +import socket from collections import deque from unittest.mock import patch -import socket import pytest @@ -19,9 +19,7 @@ def test_close_closes_the_socket_connection(mock_connection): mock_connection._LircdConnection__socket.close.assert_called() -def test_that_retrieving_the_address_property_retrieves_the_address( - mock_socket -): +def test_that_retrieving_the_address_property_retrieves_the_address(mock_socket): """ lirc.connection.lircd_connection.LircdConnection.address @@ -90,9 +88,7 @@ def test_that_a_socket_raises_general_error(mock_socket): @pytest.mark.parametrize("test_command", ["SEND_ONCE REMOTE KEY", ""]) -def test_that_send_adds_a_newline_to_the_end( - mock_connection, test_command -): +def test_that_send_adds_a_newline_to_the_end(mock_connection, test_command): """ lirc.connection.lircd_connection.LircdConnection.send diff --git a/tests/test_reply_packet_parser.py b/tests/test_reply_packet_parser.py index 3eadd28..304bb74 100644 --- a/tests/test_reply_packet_parser.py +++ b/tests/test_reply_packet_parser.py @@ -1,7 +1,7 @@ import pytest -from lirc.reply_packet_parser import ReplyPacketParser from lirc.exceptions import LircdInvalidReplyPacketError +from lirc.reply_packet_parser import ReplyPacketParser def test_starting_state_of_reply_packet_parser(): @@ -26,15 +26,7 @@ def test_parsing_version_reply_packet(): is parsed correctly. """ parser = ReplyPacketParser() - packet = [ - "BEGIN\n", - "VERSION\n", - "SUCCESS\n", - "DATA\n", - "1\n", - "0.10.1\n", - "END\n" - ] + packet = ["BEGIN\n", "VERSION\n", "SUCCESS\n", "DATA\n", "1\n", "0.10.1\n", "END\n"] for line in packet: parser.feed(line) # SUT @@ -57,9 +49,10 @@ def test_parsing_remotes_reply_packet(): "LIST\n", "SUCCESS\n", "DATA\n", - "1\n", + "2\n", "some-remote\n", - "END\n" + "some-other-remote\n", + "END\n", ] for line in packet: @@ -67,7 +60,7 @@ def test_parsing_remotes_reply_packet(): assert parser.success assert parser.is_finished - assert parser.data == ["some-remote"] + assert parser.data == ["some-remote", "some-other-remote"] def test_handling_of_sighup_packet(): @@ -110,7 +103,7 @@ def test_error_with_send_once(): "DATA", "1", "hardware does not support sending", - "END" + "END", ] for line in packet: @@ -137,50 +130,53 @@ def test_no_line_passed_to_feed(): assert parser.data == [] -@pytest.mark.parametrize("packet", [ - [ - "BEGIN\n", - "SIGHUP\n", - "invalid-expected-end\n", - ], - [ - "BEGIN\n", - "VERSION\n", - "SUCCESS\n", - "DATA\n", - "1\n", - "0.10.1\n", - "invalid-expected-end\n" - ], - ["NOTBEGIN"], - [ - "BEGIN\n", - "VERSION\n", - "not-SUCCESS-or-ERROR\n", - "DATA\n", - "1\n", - "0.10.1\n", - "END\n" - ], +@pytest.mark.parametrize( + "packet", [ - "BEGIN\n", - "VERSION\n", - "SUCCESS\n", - "not-DATA-or-END\n", - "1\n", - "0.10.1\n", - "END\n" - ], - [ - "BEGIN\n", - "VERSION\n", - "SUCCESS\n", - "DATA\n", - "not-a-line-count-left-number\n", - "0.10.1\n", - "END\n" + [ + "BEGIN\n", + "SIGHUP\n", + "invalid-expected-end\n", + ], + [ + "BEGIN\n", + "VERSION\n", + "SUCCESS\n", + "DATA\n", + "1\n", + "0.10.1\n", + "invalid-expected-end\n", + ], + ["NOTBEGIN"], + [ + "BEGIN\n", + "VERSION\n", + "not-SUCCESS-or-ERROR\n", + "DATA\n", + "1\n", + "0.10.1\n", + "END\n", + ], + [ + "BEGIN\n", + "VERSION\n", + "SUCCESS\n", + "not-DATA-or-END\n", + "1\n", + "0.10.1\n", + "END\n", + ], + [ + "BEGIN\n", + "VERSION\n", + "SUCCESS\n", + "DATA\n", + "not-a-line-count-left-number\n", + "0.10.1\n", + "END\n", + ], ], -]) +) def test_invalid_end_line_raises_error(packet): """ lirc.reply_packet_parser.ReplyPacketParser.feed @@ -192,3 +188,19 @@ def test_invalid_end_line_raises_error(packet): with pytest.raises(LircdInvalidReplyPacketError): for line in packet: parser.feed(line) # SUT + + +@pytest.mark.parametrize("line", [None, "", False]) +def test_command_state_raises_exception_on_empty_line(line): + """ + lirc.reply_packet_parser.ReplyPacketParser.__command + + Ensure that the command state raises an error when + an empty (or falsey) line is passed. This scenario + is unlikely to happen in practical use since the .feed() + method simply returns if we recieved an empty line. + """ + parser = ReplyPacketParser() + + with pytest.raises(LircdInvalidReplyPacketError): + parser._ReplyPacketParser__command(line) |