diff options
| -rw-r--r-- | lirc/lirc.py | 8 | ||||
| -rw-r--r-- | tests/test_lirc.py | 47 |
2 files changed, 37 insertions, 18 deletions
diff --git a/lirc/lirc.py b/lirc/lirc.py index 3671c3c..d59a076 100644 --- a/lirc/lirc.py +++ b/lirc/lirc.py @@ -1,7 +1,7 @@ +import platform import socket import threading from itertools import islice -from platform import system from typing import List, Union from lirc.exceptions import ( @@ -49,7 +49,7 @@ class Lirc: self.__address = ( address if address is not None - else globals()[f"DEFAULT_ADDRESS_{system().upper()}"] + else globals()[f"DEFAULT_ADDRESS_{platform.system().upper()}"] ) self.__socket.settimeout(self.__timeout) @@ -67,10 +67,10 @@ class Lirc: :param lircd_socket: The socket passed in to the Lirc constructor. """ - if isinstance(lircd_socket, socket.socket): + if lircd_socket: return lircd_socket - if system() == "Windows": + if platform.system() == "Windows": return socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) diff --git a/tests/test_lirc.py b/tests/test_lirc.py index 4c69ac6..960ad33 100644 --- a/tests/test_lirc.py +++ b/tests/test_lirc.py @@ -1,4 +1,6 @@ import os +import socket +from unittest import mock import pytest @@ -8,27 +10,44 @@ from lirc import ( LircResponse, LircSocketTimeoutError, ) +from lirc.lirc import ( + DEFAULT_ADDRESS_DARWIN, + DEFAULT_ADDRESS_LINUX, + DEFAULT_ADDRESS_WINDOWS, +) + +@mock.patch("socket.socket") +@mock.patch("platform.system") +@pytest.mark.parametrize( + "os_name, expected_address, expected_socket_parameters", + [ + ("Windows", DEFAULT_ADDRESS_WINDOWS, (socket.AF_INET, socket.SOCK_STREAM)), + ("Darwin", DEFAULT_ADDRESS_DARWIN, (socket.AF_UNIX, socket.SOCK_STREAM)), + ("Linux", DEFAULT_ADDRESS_LINUX, (socket.AF_UNIX, socket.SOCK_STREAM)), + ], +) +def test_default_lirc_initialization( + mocked_system, mocked_socket, os_name, expected_address, expected_socket_parameters +): + mocked_system.return_value = os_name -# TODO: test this per OS! -def test_default_lirc_init(mock_socket): - TIMEOUT = 5 - Lirc(socket=mock_socket, timeout=TIMEOUT) - # mock_socket.connect.assert_called_with(mock_lirc.DEFAULT_SOCKET_PATH) - mock_socket.settimeout.assert_called_with(TIMEOUT) + lirc = Lirc() + lirc_socket = lirc._Lirc__socket + mocked_socket.assert_called_once_with(*expected_socket_parameters) + lirc_socket.connect.assert_called_with(expected_address) + lirc_socket.settimeout.assert_called_with(5.0) -def test_lirc_custom_path(mock_socket): - CUSTOM_PATH = "CUSTOM" - Lirc(socket=mock_socket, address=CUSTOM_PATH) - mock_socket.connect.assert_called_with(CUSTOM_PATH) +def test_custom_lirc_initialization(mock_socket): + CUSTOM_ADDRESS = "CUSTOM" + CUSTOM_TIMEOUT = 10.0 + Lirc(socket=mock_socket, address=CUSTOM_ADDRESS, timeout=CUSTOM_TIMEOUT) -def test_custom_path_lirc_init(mock_socket): - TEST_SOCKET_PATH = "test_path" - Lirc(socket=mock_socket, address=TEST_SOCKET_PATH) - mock_socket.connect.assert_called_with(TEST_SOCKET_PATH) + mock_socket.connect.assert_called_with(CUSTOM_ADDRESS) + mock_socket.settimeout.assert_called_with(CUSTOM_TIMEOUT) @pytest.mark.parametrize( |