Skip to content

Commit

Permalink
Merge pull request #5 from MountainGod2/dev
Browse files Browse the repository at this point in the history
tests: updated and expanded test coverage
  • Loading branch information
MountainGod2 authored Aug 11, 2024
2 parents 1daf4e7 + c6ef0b0 commit f3d0673
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/chaturbate_poller/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}")
Expand Down Expand Up @@ -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):
Expand All @@ -78,5 +78,5 @@ def main() -> None:
)


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
main()
18 changes: 18 additions & 0 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
@@ -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))
35 changes: 32 additions & 3 deletions tests/test_chaturbate_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit f3d0673

Please sign in to comment.