summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-12-13 21:28:20 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-12-13 21:28:20 -0600
commit21d9993eef8c68ad0f6e32ef0c0390202c2a34dd (patch)
tree75d128a78e478041ef13c2289be306d46f2da122 /tests
parentIgnore .venv (diff)
parentClarify readme (diff)
downloadlirc-21d9993eef8c68ad0f6e32ef0c0390202c2a34dd.zip
Merge branch 'dev' for 0.2.0 release
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py18
-rw-r--r--tests/test_client.py68
-rw-r--r--tests/test_lirc.py135
-rw-r--r--tests/test_lircd_connection.py118
-rw-r--r--tests/test_reply_packet_parser.py66
5 files changed, 266 insertions, 139 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 01e2460..4529cd7 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -3,14 +3,24 @@ from unittest import mock
import pytest
-from lirc import Lirc
+from lirc import Client, LircdConnection
@pytest.fixture
def mock_socket():
- return mock.Mock(spec=socket.socket)
+ return mock.MagicMock(spec=socket.socket)
@pytest.fixture
-def mock_lirc(mock_socket):
- return Lirc(socket=mock_socket)
+def mock_connection(mock_socket):
+ return LircdConnection(socket=mock_socket)
+
+
+@pytest.fixture
+def mock_client(mock_connection):
+ return Client(connection=mock_connection)
+
+
+@pytest.fixture
+def mock_client_and_connection(mock_connection):
+ return Client(connection=mock_connection), mock_connection
diff --git a/tests/test_client.py b/tests/test_client.py
new file mode 100644
index 0000000..523119b
--- /dev/null
+++ b/tests/test_client.py
@@ -0,0 +1,68 @@
+import pytest
+
+from lirc import Client, LircdConnection
+
+
+def test_that_custom_connections_can_be_used(mock_socket):
+ connection = LircdConnection(socket=mock_socket)
+ client = Client(connection=connection)
+
+ assert client._Client__connection == connection
+
+
+def test_that_custom_connection_that_is_not_a_lircd_connection_raises_error():
+ with pytest.raises(ValueError) as error:
+ Client(connection=Client())
+
+ assert "must be an instance of `LircdConnection`" in str(error)
+
+
+def test_that_close_closes_the_socket(mock_client_and_connection):
+ client, connection = mock_client_and_connection
+
+ client.close()
+
+ connection.socket.close.assert_called()
+
+
+@pytest.mark.parametrize(
+ "client_command, args, lircd_command",
+ [
+ ("send", {"remote": "REMOTE", "key": "KEY"}, "SEND_ONCE REMOTE KEY 1"),
+ (
+ "send",
+ {"remote": "REMOTE", "key": "KEY", "repeat_count": 5},
+ "SEND_ONCE REMOTE KEY 5",
+ ),
+ ("start_repeat", {"remote": "REMOTE", "key": "KEY"}, "SEND_START REMOTE KEY"),
+ ("stop_repeat", {}, "SEND_STOP "),
+ ("stop_repeat", {"remote": "REMOTE", "key": "KEY"}, "SEND_STOP REMOTE KEY"),
+ ("list_remotes", {}, "LIST"),
+ ("list_remote_keys", {"remote": "REMOTE"}, "LIST REMOTE"),
+ ("start_logging", {"path": "PATH"}, "SET_INPUTLOG PATH"),
+ ("stop_logging", {}, "SET_INPUTLOG"),
+ ("version", {}, "VERSION"),
+ ("driver_option", {"key": "KEY", "value": "VALUE"}, "DRV_OPTION KEY VALUE"),
+ (
+ "simulate",
+ {"remote": "REMOTE", "key": "KEY"},
+ "SIMULATE 0000000000000000 01 KEY REMOTE",
+ ),
+ (
+ "simulate",
+ {"remote": "REMOTE", "key": "KEY", "repeat_count": 4, "keycode": 53},
+ "SIMULATE 0000000000000053 04 KEY REMOTE",
+ ),
+ ("set_transmitters", {"transmitters": 5}, "SET_TRANSMITTERS 5"),
+ ("set_transmitters", {"transmitters": [10, 1, 1]}, "SET_TRANSMITTERS 513"),
+ ],
+)
+def test_that_client_commands_send_the_correct_command(
+ mock_client_and_connection, client_command, args, lircd_command
+):
+ client, connection = mock_client_and_connection
+ connection.socket.recv.return_value = b"BEGIN\nCOMMAND\nSUCCESS\nEND\n"
+
+ getattr(client, client_command)(**args)
+
+ connection.socket.sendall.assert_called_with((lircd_command + "\n").encode("utf-8"))
diff --git a/tests/test_lirc.py b/tests/test_lirc.py
deleted file mode 100644
index 348e785..0000000
--- a/tests/test_lirc.py
+++ /dev/null
@@ -1,135 +0,0 @@
-import os
-
-import pytest
-
-from lirc import (
- InvalidReplyPacketFormatError,
- Lirc,
- LircResponse,
- LircSocketTimeoutError,
-)
-
-
-def test_default_lirc_init(mock_socket):
- TIMEOUT = 5
- mock_lirc = Lirc(socket=mock_socket, socket_timeout=TIMEOUT)
-
- mock_socket.connect.assert_called_with(mock_lirc.DEFAULT_SOCKET_PATH)
- mock_socket.settimeout.assert_called_with(TIMEOUT)
-
-
-def test_lirc_custom_path(mock_socket):
- CUSTOM_PATH = "CUSTOM"
- Lirc(socket=mock_socket, socket_path=CUSTOM_PATH)
-
- mock_socket.connect.assert_called_with(CUSTOM_PATH)
-
-
-def test_custom_path_lirc_init(mock_socket):
- TEST_SOCKET_PATH = "test_path"
- Lirc(socket=mock_socket, socket_path=TEST_SOCKET_PATH)
- mock_socket.connect.assert_called_with(TEST_SOCKET_PATH)
-
-
-@pytest.mark.parametrize(
- "reply_packet", [b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n"]
-)
-def test_read_reply_packet(mock_lirc, reply_packet):
- mock_lirc._Lirc__socket.recv.return_value = reply_packet
- retrieved_packet = mock_lirc._Lirc__read_reply_packet()
- mock_lirc._Lirc__socket.recv.assert_called_with(256)
- assert reply_packet == retrieved_packet.encode(mock_lirc.ENCODING)
-
-
-@pytest.mark.skipif(
- os.environ.get("LIRC_RUN_LONG_TESTS") is None, reason="Test takes too long"
-)
-@pytest.mark.parametrize("reply_packet", [b"INVALID PACKET"])
-def test_read_reply_packet_socket_timeout(mock_lirc, reply_packet):
- with pytest.raises(LircSocketTimeoutError):
- mock_lirc._Lirc__socket.recv.return_value = reply_packet
- mock_lirc._Lirc__read_reply_packet()
-
-
-@pytest.mark.parametrize(
- "reply_packet, command, success, data, exception",
- [
- (
- "BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n",
- "SEND_ONCE remote key",
- True,
- [],
- None,
- ),
- ("INVALID PACKET", "", False, [], None),
- (
- "BEGIN\nSEND_ONCE remote key\nERROR\nINVALID\n",
- "SEND_ONCE remote key",
- False,
- [],
- InvalidReplyPacketFormatError,
- ),
- (
- "BEGIN\nSEND_ONCE remote key\nERROR\nDATA\n1\nunknown remote: remote\n",
- "SEND_ONCE remote key",
- False,
- ["unknown remote: remote"],
- None,
- ),
- (
- "BEGIN\nSEND_ONCE remote key\nSUCCESS\nDATA\n3\nraa\nblah\nbhal\n",
- "SEND_ONCE remote key",
- True,
- ["raa", "blah", "bhal"],
- None,
- ),
- ],
-)
-def test_parse_reply_packet(mock_lirc, reply_packet, command, success, data, exception):
- if exception:
- with pytest.raises(exception):
- mock_lirc._Lirc__parse_reply_packet(reply_packet)
- else:
- response = mock_lirc._Lirc__parse_reply_packet(reply_packet)
- assert response.success == success
- assert response.command == command
- assert response.data == data
-
-
-@pytest.mark.parametrize(
- "reply_packet, repeat_count",
- [
- (b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n", 1),
- (b"BEGIN\nSEND_ONCE remote key\nERROR\nEND\n", 1),
- (b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n", 5),
- ],
-)
-def test_send_once(mock_lirc, reply_packet, repeat_count):
- REMOTE = "remote"
- KEY = "key"
- COMMAND = f"SEND_ONCE {REMOTE} {KEY}"
- SUCCESS = True if "SUCCESS" in reply_packet.decode(mock_lirc.ENCODING) else False
-
- mock_lirc._Lirc__socket.recv.return_value = reply_packet
- response = mock_lirc.send_once(KEY, REMOTE, repeat_count=repeat_count)
- mock_lirc._Lirc__socket.sendall.assert_called_with(
- (COMMAND + "\n").encode(mock_lirc.ENCODING)
- )
-
- if repeat_count > 1:
- assert type(response) == list
- assert len(response) == repeat_count
-
- for r in response:
- _ensure_lirc_response(r, COMMAND, SUCCESS)
- else:
- _ensure_lirc_response(response, COMMAND, SUCCESS)
-
-
-def _ensure_lirc_response(
- response: LircResponse, command: str, success: bool, data: list = []
-):
- assert type(response) == LircResponse
- assert response.command == command
- assert response.success == success
- assert response.data == data
diff --git a/tests/test_lircd_connection.py b/tests/test_lircd_connection.py
new file mode 100644
index 0000000..6c8679c
--- /dev/null
+++ b/tests/test_lircd_connection.py
@@ -0,0 +1,118 @@
+import time
+from collections import deque
+from platform import system
+from unittest import mock
+
+import pytest
+
+from lirc import LircdConnection
+from lirc.exceptions import LircdConnectionError
+
+
+@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
+):
+ if system() != os_name:
+ return
+
+ mocked_system.return_value = os_name
+
+ connection = LircdConnection()
+
+ expected_socket = connection._LircdConnection__socket
+ expected_address = connection._LircdConnection__address
+
+ assert connection.socket.family == expected_socket.family
+ assert connection.socket.type == expected_socket.type
+ assert connection.address == expected_address
+
+
+def test_close_closes_the_socket_connection(mock_connection):
+ mock_connection.close()
+ mock_connection.socket.close.assert_called()
+
+
+def test_that_connection_error_is_raised_on_invalid_address():
+ with pytest.raises(LircdConnectionError):
+ LircdConnection(address="invalid thing").connect()
+
+
+def test_that_attribute_error_is_raised_on_invalid_socket():
+ with pytest.raises(AttributeError):
+ LircdConnection(socket=5).connect()
+
+
+@pytest.mark.parametrize("test_command", ["SEND_ONCE REMOTE KEY", ""])
+def test_that_send_adds_a_newline_to_the_end_if_not_present(
+ mock_connection, test_command
+):
+ mock_connection.send(test_command)
+ mock_connection.socket.sendall.assert_called_with(
+ f"{test_command}\n".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
+):
+ with pytest.raises(TypeError) as error:
+ mock_connection.send(test_command)
+
+ 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.socket.sendall.assert_called_with(test_command.encode("utf-8"))
+
+
+@pytest.mark.parametrize(
+ "buffer_payload, expected_item",
+ [("BEGIN\nSIGHUP\nEND\n".split("\n"), "BEGIN"), ([""], "")],
+)
+def test_that_readline_uses_buffer_if_items_are_present_in_it(
+ mock_connection, buffer_payload, expected_item
+):
+ mock_connection._LircdConnection__buffer.extend(buffer_payload)
+ assert mock_connection.readline() == expected_item
+
+
+@pytest.mark.parametrize(
+ "socket_payload, expected_return, expected_buffer",
+ [
+ (b"BEGIN\nTEST\nEND\n", "BEGIN", deque(["TEST", "END"])),
+ (b"BEGIN\nTEST\nEND", "BEGIN", deque(["TEST", "END"])),
+ (b"", "", deque()),
+ ],
+)
+def test_that_readline_retrieves_data_from_socket_and_adds_to_buffer(
+ mock_connection, socket_payload, expected_return, expected_buffer
+):
+ mock_connection.socket.recv.return_value = socket_payload
+
+ line = mock_connection.readline()
+
+ assert line == expected_return
+ assert mock_connection._LircdConnection__buffer == expected_buffer
+
+
+def test_that_readline_raises_timeout_error_if_no_data_from_socket():
+ timeout = 0.001
+ error_threshold = 0.01
+ connection = LircdConnection(timeout=timeout)
+
+ start_time = time.time()
+ with pytest.raises(TimeoutError) as error:
+ connection.readline()
+ end_time = time.time()
+ delta = abs(start_time - end_time)
+
+ assert delta < timeout + error_threshold
+ assert f"could not find any data on the socket after {timeout}" in str(error)
diff --git a/tests/test_reply_packet_parser.py b/tests/test_reply_packet_parser.py
new file mode 100644
index 0000000..86ae311
--- /dev/null
+++ b/tests/test_reply_packet_parser.py
@@ -0,0 +1,66 @@
+# import pytest
+#
+# from lirc import (
+# LircdCommandFailureError,
+# LircdInvalidReplyPacketError,
+# LircdSocketTimeoutError,
+# )
+# from lirc.reply_packet_parser import ReplyPacketParser
+# from lirc.utils import ENCODING
+#
+#
+# @pytest.mark.parametrize(
+# "reply_packet", [b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n"]
+# )
+# def test_read_reply_packet(mock_socket, reply_packet):
+# mock_socket.recv.return_value = reply_packet
+# parser = ReplyPacketParser(mock_socket)
+# parser.read()
+#
+# retrieved_packet = parser.feed()
+#
+# mock_socket.recv.assert_called_with(ReplyPacketParser.BUFFER_LENGTH)
+# assert reply_packet == retrieved_packet.encode(ENCODING)
+#
+#
+# @pytest.mark.skip(reason="Not working")
+# @pytest.mark.parametrize("reply_packet", [b"INVALID PACKET"])
+# def test_read_reply_packet_socket_timeout(mock_lirc, reply_packet):
+# with pytest.raises(LircdSocketTimeoutError):
+# mock_lirc.socket.recv.return_value = reply_packet
+# ReplyPacketParser.read(mock_lirc.socket)
+#
+#
+# @pytest.mark.parametrize(
+# "reply_packet, data, exception",
+# [
+# (b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n", [], None,),
+# # ("INVALID PACKET", [], LircdInvalidReplyPacketError),
+# (
+# b"BEGIN\nSEND_ONCE remote key\nERROR\nINVALID\n",
+# [],
+# LircdInvalidReplyPacketError,
+# ),
+# (
+# b"BEGIN\nSEND_ONCE remote key\nERROR\nDATA\n1\nunknown remote: remote\n",
+# ["unknown remote: remote"],
+# LircdCommandFailureError,
+# ),
+# (
+# b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nDATA\n3\nraa\nblah\nbhal\n",
+# ["raa", "blah", "bhal"],
+# None,
+# ),
+# ],
+# )
+# def test_parse_reply_packet(mock_socket, reply_packet, data, exception):
+# mock_socket.recv.return_value = reply_packet
+# parser = ReplyPacketParser(mock_socket)
+#
+# if exception:
+# with pytest.raises(exception):
+# parser.read()
+# parser.feed()
+# else:
+# parser.read()
+# assert parser.feed() == data