diff --git a/src/chaturbate_poller/__main__.py b/src/chaturbate_poller/__main__.py index 1e197180..f9370e29 100644 --- a/src/chaturbate_poller/__main__.py +++ b/src/chaturbate_poller/__main__.py @@ -20,7 +20,7 @@ load_dotenv() -def parse_arguments() -> argparse.Namespace: +def parse_arguments() -> argparse.Namespace: # pragma: no cover """Parse command-line arguments.""" parser = argparse.ArgumentParser(description="Poll events from Chaturbate.") parser.add_argument("--version", action="version", version=f"chaturbate_poller {__version__}") @@ -67,7 +67,7 @@ async def start_polling( url = str(response.next_url) -def main() -> None: +def main() -> None: # pragma: no cover """Run the main function within an asyncio event loop.""" args = parse_arguments() with suppress(KeyboardInterrupt): @@ -78,5 +78,5 @@ def main() -> None: ) -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover main() diff --git a/tests/test___main__.py b/tests/test___main__.py new file mode 100644 index 00000000..1a258f31 --- /dev/null +++ b/tests/test___main__.py @@ -0,0 +1,18 @@ +# ruff: noqa: S101 +"""Tests for the __main__ module.""" + +import asyncio +from contextlib import suppress + +import pytest + +from chaturbate_poller.__main__ import start_polling + + +def test_start_polling() -> None: + """Test the start_polling function.""" + with ( + suppress(KeyboardInterrupt), + pytest.raises(ValueError, match="Unauthorized access. Verify the username and token."), + ): + asyncio.run(start_polling("username", "token", 10, testbed=False, verbose=False)) diff --git a/tests/test_chaturbate_poller.py b/tests/test_chaturbate_poller.py index f8b2c950..8b79b576 100644 --- a/tests/test_chaturbate_poller.py +++ b/tests/test_chaturbate_poller.py @@ -198,9 +198,30 @@ def test_backoff_handler(self, caplog) -> None: # noqa: ANN001 def test_giveup_handler(self, caplog) -> None: # noqa: ANN001 """Test the giveup handler.""" caplog.set_level(logging.ERROR) - # Providing required key "tries" in the details dict - giveup_handler({"tries": 1, "target": lambda x: x, "args": (), "kwargs": {}, "elapsed": 0}) - assert "Giving up after 1 tries" in caplog.text + giveup_handler( + { # type: ignore[typeddict-item] + "tries": 6, + "exception": HTTPStatusError( + message="Server Error", + request=Request("GET", "https://error.url.com"), + response=Response(500, json={"status": "Unknown error"}), + ), + } + ) + assert "Giving up after 6 tries due to server error code 500: Unknown error" in caplog.text + + def test_giveup_handler_no_exception(self, caplog) -> None: # noqa: ANN001 + """Test the giveup handler with no exception.""" + caplog.set_level(logging.ERROR) + giveup_handler( + { # type: ignore[typeddict-item] + "tries": 6, + } + ) + assert ( + "Giving up after 6 tries due to server error code None: No response available" + in caplog.text + ) class TestConstants: @@ -271,6 +292,14 @@ async def test_initialization_with_invalid_timeout(self) -> None: async with ChaturbateClient(USERNAME, TOKEN, timeout=invalid_timeout): # type: ignore[arg-type] pass + @pytest.mark.asyncio() + async def test_initialization_with_negative_timeout(self) -> None: + """Test ChaturbateClient initialization with negative timeout.""" + negative_timeout = -1 + with pytest.raises(ValueError, match="Timeout must be a positive integer."): + async with ChaturbateClient(USERNAME, TOKEN, timeout=negative_timeout): + pass + @pytest.mark.asyncio() async def test_initialization_with_missing_env_variables(self) -> None: """Test ChaturbateClient initialization with missing environment variables."""