summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-12-26 14:22:29 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-12-26 14:22:29 -0600
commit383a4012810705a6b6617e745338e92a13b04dc0 (patch)
tree0b159c71d1488fae20a13c7c3cda2f600441e9dd /tests
parentRaise an unsupported os error in default connection (diff)
downloadlirc-383a4012810705a6b6617e745338e92a13b04dc0.zip
Clean up and add tests to lircd connection
Diffstat (limited to 'tests')
-rw-r--r--tests/test_lircd_connection.py198
1 files changed, 147 insertions, 51 deletions
diff --git a/tests/test_lircd_connection.py b/tests/test_lircd_connection.py
index 8121f48..7f8ff0a 100644
--- a/tests/test_lircd_connection.py
+++ b/tests/test_lircd_connection.py
@@ -1,80 +1,144 @@
-import time
from collections import deque
-from platform import system
-from unittest import mock
+from unittest.mock import patch
+import socket
import pytest
from lirc import LircdConnection
-from lirc.exceptions import LircdConnectionError
+from lirc.exceptions import LircdConnectionError, LircdSocketError
-@mock.patch("socket.socket")
-@mock.patch("platform.system")
-@pytest.mark.parametrize("os_name", ["Windows", "Darwin", "Linux"])
-def test_socket_and_address_have_correct_cross_platform_values(
- mocked_system, mocked_socket, os_name
+def test_close_closes_the_socket_connection(mock_connection):
+ """
+ lirc.connection.lircd_connection.LircdConnection.close
+
+ Ensure that a call to close calls the socket's close method.
+ """
+ mock_connection.close() # SUT
+
+ mock_connection._LircdConnection__socket.close.assert_called()
+
+
+def test_that_retrieving_the_address_property_retrieves_the_address(
+ mock_socket
):
- if system() != os_name:
- return
+ """
+ lirc.connection.lircd_connection.LircdConnection.address
- mocked_system.return_value = os_name
+ Ensure that that trying to retrieve the address property
+ retrieves the expected address.
+ """
+ expected_address = "/var/does/not/exist"
+ conn = LircdConnection(socket=mock_socket, address=expected_address)
- connection = LircdConnection()
+ address = conn.address # SUT
- expected_socket = connection._LircdConnection__socket
- expected_address = connection._LircdConnection__address
+ assert address == expected_address
- assert connection._LircdConnection__socket.family == expected_socket.family
- assert connection._LircdConnection__socket.type == expected_socket.type
- assert connection.address == expected_address
+def test_that_attribute_error_is_raised_on_invalid_socket():
+ """
+ lirc.connection.lircd_connection.LircdConnection.__init__
-def test_close_closes_the_socket_connection(mock_connection):
- mock_connection.close()
- mock_connection._LircdConnection__socket.close.assert_called()
+ Ensure that an error is raised when an invalid socket is passed
+ to the LircdConnection constructor.
+ """
+ with pytest.raises(AttributeError):
+ LircdConnection(socket=5) # SUT
def test_that_connection_error_is_raised_on_invalid_address():
+ """
+ lirc.connection.lircd_connection.LircdConnection.connect
+
+ Ensure that an invalid address raises an LircdConnectionError
+ when we try to connect.
+ """
with pytest.raises(LircdConnectionError):
- LircdConnection(address="invalid thing").connect()
+ LircdConnection(address="invalid thing").connect() # SUT
-def test_that_attribute_error_is_raised_on_invalid_socket():
- with pytest.raises(AttributeError):
- LircdConnection(socket=5).connect()
+def test_that_a_socket_with_an_invalid_address_raises_error(mock_socket):
+ """
+ lirc.connection.lircd_connection.LircdConnection.connect
+
+ Ensure that a LircdConnectionError is raised if cannot connect
+ to lircd at the given address (if the socket raises a FileNotFoundError).
+ """
+ mock_socket.connect.side_effect = FileNotFoundError
+
+ with pytest.raises(LircdConnectionError) as error:
+ LircdConnection(socket=mock_socket).connect() # SUT
+
+ assert "Could not connect to lircd" in str(error)
+
+
+def test_that_a_socket_raises_general_error(mock_socket):
+ """
+ lirc.connection.lircd_connection.LircdConnection.connect
+
+ Ensure that a LircdConnectionError is raised if any general
+ exception is raised by the connection to the socket, such as
+ an InterruptedError if a signal occurs.
+ """
+ mock_socket.connect.side_effect = InterruptedError("interrupted by a signal")
+
+ with pytest.raises(LircdConnectionError) as error:
+ LircdConnection(socket=mock_socket).connect() # SUT
+
+ assert "interrupted by a signal" in str(error)
@pytest.mark.parametrize("test_command", ["SEND_ONCE REMOTE KEY", ""])
-def test_that_send_adds_a_newline_to_the_end_if_not_present(
+def test_that_send_adds_a_newline_to_the_end(
mock_connection, test_command
):
- mock_connection.send(test_command)
+ """
+ lirc.connection.lircd_connection.LircdConnection.send
+
+ Ensure a newline is added to the command we pass to send()
+ if one is not present.
+ """
+ mock_connection.send(test_command) # SUT
+
mock_connection._LircdConnection__socket.sendall.assert_called_with(
f"{test_command}\n".encode("utf-8")
)
+def test_that_send_does_not_add_too_many_newlines(mock_connection):
+ """
+ lirc.connection.lircd_connection.LircdConnection.send
+
+ Ensure a newline is not added to the end of the command if
+ one is already present.
+ """
+ command = "SEND_ONCE REMOTE KEY\n"
+
+ mock_connection.send(command) # SUT
+
+ mock_connection._LircdConnection__socket.sendall.assert_called_with(
+ f"{command}".encode("utf-8")
+ )
+
+
@pytest.mark.parametrize("test_command", [5, False, 0.1, b"hello"])
def test_that_send_raises_a_type_error_for_parameters_that_are_not_strings(
mock_connection, test_command
):
+ """
+ lirc.connection.lircd_connection.LircdConnection.send
+
+ Ensure that a TypeError is raised if the parameter we passed is not
+ a string. This is to prevent errors such as when we try to add an
+ ending newline or check if the passed in arg has a newline.
+ """
with pytest.raises(TypeError) as error:
- mock_connection.send(test_command)
+ mock_connection.send(test_command) # SUT
assert "data parameter to send() must be a string" in str(error)
-@pytest.mark.parametrize("test_command", ["SEND_ONCE REMOTE KEY\n"])
-def test_that_send_sends_the_command_unaltered_to_the_socket_if_in_correct_format(
- mock_connection, test_command
-):
- mock_connection.send(test_command)
- mock_connection._LircdConnection__socket.sendall.assert_called_with(
- test_command.encode("utf-8")
- )
-
-
@pytest.mark.parametrize(
"buffer_payload, expected_item",
[("BEGIN\nSIGHUP\nEND\n".split("\n"), "BEGIN"), ([""], "")],
@@ -82,8 +146,16 @@ def test_that_send_sends_the_command_unaltered_to_the_socket_if_in_correct_forma
def test_that_readline_uses_buffer_if_items_are_present_in_it(
mock_connection, buffer_payload, expected_item
):
+ """
+ lirc.connection.lircd_connection.LircdConnection.readline
+
+ Ensure that readline uses the buffer if there are items in it.
+ """
mock_connection._LircdConnection__buffer.extend(buffer_payload)
- assert mock_connection.readline() == expected_item
+
+ line = mock_connection.readline() # SUT
+
+ assert line == expected_item
@pytest.mark.parametrize(
@@ -97,25 +169,49 @@ def test_that_readline_uses_buffer_if_items_are_present_in_it(
def test_that_readline_retrieves_data_from_socket_and_adds_to_buffer(
mock_connection, socket_payload, expected_return, expected_buffer
):
+ """
+ lirc.connection.lircd_connection.LircdConnection.readline
+
+ Ensure that when we call readline(), it adds to the buffer if we
+ retrieved more than a single line.
+ """
mock_connection._LircdConnection__socket.recv.return_value = socket_payload
- line = mock_connection.readline()
+ line = mock_connection.readline() # SUT
assert line == expected_return
assert mock_connection._LircdConnection__buffer == expected_buffer
-@pytest.mark.skip(msg="Seems to be a flaky test, need to return back to.")
-def test_that_readline_raises_timeout_error_if_no_data_from_socket():
- timeout = 0.001
- error_threshold = 0.01
- connection = LircdConnection(timeout=timeout)
+@patch("socket.socket.recv")
+def test_that_readline_raises_timeout_error(patched_recv):
+ """
+ lirc.connection.lircd_connection.LircdConnection.readline
+
+ Ensure that a TimeoutError is raised from readline() if the
+ socket times out (raises socket.timeout).
+ """
+ patched_recv.side_effect = socket.timeout
+ connection = LircdConnection()
- start_time = time.time()
with pytest.raises(TimeoutError) as error:
- connection.readline()
- end_time = time.time()
- delta = abs(start_time - end_time)
+ connection.readline() # SUT
+
+ assert "could not find any data on the socket" in str(error)
+
+
+@patch("socket.socket.recv")
+def test_that_readline_raises_lircd_socket_error(patched_recv):
+ """
+ lirc.connection.lircd_connection.LircdConnection.readline
+
+ Ensure that a general LircdSocketError is raised from readline()
+ if the socket raises `socket.error`.
+ """
+ patched_recv.side_effect = socket.error
+ connection = LircdConnection()
+
+ with pytest.raises(LircdSocketError) as error:
+ connection.readline() # SUT
- assert delta < timeout + error_threshold
- assert f"could not find any data on the socket after {timeout}" in str(error)
+ assert "An error occurred while reading from the lircd socket" in str(error)