From 0ff5d315426718b59060695acb232570eb2fd4f3 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Thu, 4 Aug 2022 20:57:37 +0200 Subject: [PATCH] Test get length command (#140) --- tests/model/test_command.py | 60 +++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/tests/model/test_command.py b/tests/model/test_command.py index bdbd6c6..98d71c4 100644 --- a/tests/model/test_command.py +++ b/tests/model/test_command.py @@ -7,12 +7,68 @@ import pytest from aiovlc.client import Client -from aiovlc.exceptions import AuthError, CommandError -from aiovlc.model.command import Info, Password, Status +from aiovlc.exceptions import AuthError, CommandError, CommandParseError +from aiovlc.model.command import GetLength, Info, Password, Status # pylint: disable=unused-argument +@pytest.mark.parametrize( + "read, length", + [(b"372\r\n> ", 372), (b"\r\n> ", 0)], +) +async def test_get_length( + transport: tuple[AsyncMock, AsyncMock], + client_connected: Client, + read: list[bytes], + length: int, +) -> None: + """Test the get length command.""" + mock_reader, mock_writer = transport + mock_reader.readuntil.return_value = read + + command = GetLength() + output = await command.send(client_connected) + + assert mock_writer.write.call_count == 1 + assert mock_writer.write.call_args == call(b"get_length\n") + assert mock_reader.readuntil.call_count == 1 + assert output + assert output.length == length + + +@pytest.mark.parametrize( + "read, error, error_message", + [ + (b"> ", CommandParseError, "Could not get length."), + ( + b"unexpected\r\n> ", + CommandParseError, + "Could not get length.", + ), + ], +) +async def test_get_length_error( + transport: tuple[AsyncMock, AsyncMock], + client_connected: Client, + read: list[bytes], + error: Type[Exception], + error_message: str, +) -> None: + """Test the get length command errors.""" + mock_reader, mock_writer = transport + mock_reader.readuntil.return_value = read + + command = GetLength() + with pytest.raises(error) as err: + await command.send(client_connected) + + assert str(err.value) == error_message + assert mock_writer.write.call_count == 1 + assert mock_writer.write.call_args == call(b"get_length\n") + assert mock_reader.readuntil.call_count == 1 + + async def test_info_command( transport: tuple[AsyncMock, AsyncMock], client_connected: Client,