summaryrefslogtreecommitdiffstats
path: root/tests/test_lircd_connection.py
blob: 8121f4808072af681327d3c54b9c8fa65424754d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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._LircdConnection__socket.family == expected_socket.family
    assert connection._LircdConnection__socket.type == expected_socket.type
    assert connection.address == expected_address


def test_close_closes_the_socket_connection(mock_connection):
    mock_connection.close()
    mock_connection._LircdConnection__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._LircdConnection__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._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"), ([""], "")],
)
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._LircdConnection__socket.recv.return_value = socket_payload

    line = mock_connection.readline()

    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)

    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)