From fb4135e43ece716396e6723510c3e7eddb06c85a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 24 Jul 2020 14:28:15 -0500 Subject: Start refactor of tests --- tests/conftest.py | 5 +++ tests/test_lirc.py | 85 ++------------------------------------- tests/test_lirc_response.py | 0 tests/test_reply_packet_parser.py | 73 +++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 81 deletions(-) create mode 100644 tests/test_lirc_response.py create mode 100644 tests/test_reply_packet_parser.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index 01e2460..de842cf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,3 +14,8 @@ def mock_socket(): @pytest.fixture def mock_lirc(mock_socket): return Lirc(socket=mock_socket) + + +@pytest.fixture +def mock_lirc_and_socket(mock_lirc): + return (mock_lirc, mock_lirc._Lirc__socket) diff --git a/tests/test_lirc.py b/tests/test_lirc.py index 960ad33..f94651b 100644 --- a/tests/test_lirc.py +++ b/tests/test_lirc.py @@ -1,15 +1,9 @@ -import os import socket from unittest import mock import pytest -from lirc import ( - InvalidReplyPacketFormatError, - Lirc, - LircResponse, - LircSocketTimeoutError, -) +from lirc import Lirc, LircResponse from lirc.lirc import ( DEFAULT_ADDRESS_DARWIN, DEFAULT_ADDRESS_LINUX, @@ -50,71 +44,6 @@ def test_custom_lirc_initialization(mock_socket): mock_socket.settimeout.assert_called_with(CUSTOM_TIMEOUT) -@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._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", [ @@ -127,9 +56,6 @@ 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._Lirc__encoding) else False - ) mock_lirc._Lirc__socket.recv.return_value = reply_packet response = mock_lirc.send_once(KEY, REMOTE, repeat_count=repeat_count) @@ -142,15 +68,12 @@ def test_send_once(mock_lirc, reply_packet, repeat_count): assert len(response) == repeat_count for r in response: - _ensure_lirc_response(r, COMMAND, SUCCESS) + _ensure_lirc_response(r, COMMAND) else: - _ensure_lirc_response(response, COMMAND, SUCCESS) + _ensure_lirc_response(response, COMMAND) -def _ensure_lirc_response( - response: LircResponse, command: str, success: bool, data: list = [] -): +def _ensure_lirc_response(response: LircResponse, command: str, data: list = []): assert type(response) == LircResponse assert response.command == command - assert response.success == success assert response.data == data diff --git a/tests/test_lirc_response.py b/tests/test_lirc_response.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_reply_packet_parser.py b/tests/test_reply_packet_parser.py new file mode 100644 index 0000000..ff3c67c --- /dev/null +++ b/tests/test_reply_packet_parser.py @@ -0,0 +1,73 @@ +import os + +import pytest + +from lirc import InvalidReplyPacketFormatError, LircSocketTimeoutError + + +@pytest.mark.parametrize( + "reply_packet", [b"BEGIN\nSEND_ONCE remote key\nSUCCESS\nEND\n"] +) +def test_read_reply_packet(mock_lirc_and_socket, reply_packet): + mock_lirc, mock_socket = mock_lirc_and_socket + mock_socket.recv.return_value = reply_packet + + retrieved_packet = mock_lirc._Lirc__read_reply_packet() + + mock_socket.recv.assert_called_with(256) + assert reply_packet == retrieved_packet.encode(mock_lirc._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 -- cgit v1.2.3