Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update ChaturbateClient timeout parameter name to api_timeout #21

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/chaturbate_poller/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_arguments() -> argparse.Namespace: # pragma: no cover
async def start_polling( # pylint: disable=too-many-arguments # noqa: PLR0913 # pragma: no cover
username: str,
token: str,
timeout: int,
api_timeout: int,
event_handler: EventHandler,
*,
testbed: bool,
Expand All @@ -65,7 +65,7 @@ async def start_polling( # pylint: disable=too-many-arguments # noqa: PLR0913
)
return

async with ChaturbateClient(username, token, timeout=timeout, testbed=testbed) as client:
async with ChaturbateClient(username, token, timeout=api_timeout, testbed=testbed) as client:
url = None
while True:
response = await client.fetch_events(url)
Expand Down
8 changes: 4 additions & 4 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_start_polling_verbose(mocker) -> None: # noqa: ANN001
assert logging.getLogger().level == logging.DEBUG


@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_fetch_events_returns_none(mocker) -> None: # noqa: ANN001
"""Test start_polling when fetch_events returns None."""
mocker.patch.dict("os.environ", {"CB_USERNAME": "testuser", "CB_TOKEN": "testtoken"})
Expand All @@ -40,7 +40,7 @@ async def test_fetch_events_returns_none(mocker) -> None: # noqa: ANN001
await start_polling(
username="testuser",
token="testtoken", # noqa: S106
timeout=10,
api_timeout=10,
event_handler=mocker.Mock(),
testbed=False,
verbose=False,
Expand All @@ -49,7 +49,7 @@ async def test_fetch_events_returns_none(mocker) -> None: # noqa: ANN001
mock_fetch_events.assert_called_once()


@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_missing_username_or_token(mocker, caplog) -> None: # noqa: ANN001
"""Test start_polling when username or token is missing."""
mocker.patch.dict("os.environ", {"CB_USERNAME": "", "CB_TOKEN": ""})
Expand All @@ -58,7 +58,7 @@ async def test_missing_username_or_token(mocker, caplog) -> None: # noqa: ANN00
await start_polling(
username="",
token="",
timeout=10,
api_timeout=10,
testbed=False,
verbose=False,
event_handler=mocker.Mock(),
Expand Down
72 changes: 36 additions & 36 deletions tests/test_chaturbate_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _setup_logging() -> None:
logging.getLogger().setLevel(logging.DEBUG)


@pytest.fixture()
@pytest.fixture
def http_client_mock(mocker: MockerFixture) -> MockerFixture:
"""Fixture for mocking the httpx.AsyncClient.get method.

Expand All @@ -166,7 +166,7 @@ def http_client_mock(mocker: MockerFixture) -> MockerFixture:
return mocker.patch("httpx.AsyncClient.get")


@pytest.fixture()
@pytest.fixture
def chaturbate_client() -> ChaturbateClient:
"""Fixture for creating a ChaturbateClient instance.

Expand Down Expand Up @@ -253,51 +253,51 @@ def test_http_status_codes(self) -> None:
class TestChaturbateClientInitialization:
"""Tests for the initialization of ChaturbateClient."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_initialization(self) -> None:
"""Test successful initialization of ChaturbateClient with default settings."""
async with ChaturbateClient(USERNAME, TOKEN) as client:
assert client.username == USERNAME
assert client.token == TOKEN

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_initialization_with_timeout(self) -> None:
"""Test ChaturbateClient initialization with custom timeout."""
timeout = API_TIMEOUT
async with ChaturbateClient(USERNAME, TOKEN, timeout=timeout) as client:
assert client.timeout == timeout

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_initialization_with_testbed(self) -> None:
"""Test ChaturbateClient initialization with testbed base URL."""
async with ChaturbateClient(USERNAME, TOKEN, testbed=True) as client:
assert client.base_url == TESTBED_BASE_URL

@pytest.mark.parametrize(("username", "token"), [("", TOKEN), (USERNAME, ""), ("", "")])
@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_initialization_failure(self, username: str, token: str) -> None:
"""Test ChaturbateClient initialization failure with missing username or token."""
with pytest.raises(ValueError, match="Chaturbate username and token are required."):
async with ChaturbateClient(username, token):
await asyncio.sleep(0)

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_initialization_with_invalid_timeout(self) -> None:
"""Test ChaturbateClient initialization with invalid timeout."""
invalid_timeout = "invalid_timeout"
with pytest.raises(TypeError):
async with ChaturbateClient(USERNAME, TOKEN, timeout=invalid_timeout): # type: ignore[arg-type]
pass

@pytest.mark.asyncio()
@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()
@pytest.mark.asyncio
async def test_initialization_with_missing_env_variables(self) -> None:
"""Test ChaturbateClient initialization with missing environment variables."""
with pytest.raises(ValueError, match="Chaturbate username and token are required."):
Expand Down Expand Up @@ -387,15 +387,15 @@ def test_detailed_formatter(self) -> None:
class TestClientLifecycle:
"""Tests for the client lifecycle."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_client_as_context_manager(self) -> None:
"""Test client as a context manager."""
async with ChaturbateClient(USERNAME, TOKEN) as client:
assert isinstance(
client.client, AsyncClient
), "Client should be an instance of AsyncClient during context management."

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_client_closed_correctly(self, chaturbate_client: ChaturbateClient) -> None:
"""Test client is closed correctly."""
async with chaturbate_client:
Expand All @@ -408,14 +408,14 @@ async def test_client_closed_correctly(self, chaturbate_client: ChaturbateClient
class TestMiscellaneous:
"""Miscellaneous tests."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_chaturbate_client_initialization(self) -> None:
"""Test ChaturbateClient initialization."""
async with ChaturbateClient(USERNAME, TOKEN) as client:
assert client.username == USERNAME
assert client.token == TOKEN

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_timeout_handling(
self,
http_client_mock, # noqa: ANN001
Expand All @@ -428,7 +428,7 @@ async def test_timeout_handling(
with pytest.raises(TimeoutException):
await chaturbate_client.fetch_events(TEST_URL)

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_fetch_events_influxdb_error(self, mocker, chaturbate_client) -> None: # noqa: ANN001
"""Test fetch_events method when InfluxDB write fails."""
mocker.patch.object(
Expand All @@ -439,7 +439,7 @@ async def test_fetch_events_influxdb_error(self, mocker, chaturbate_client) -> N
with pytest.raises(ValueError, match="Unauthorized access. Verify the username and token."):
await chaturbate_client.fetch_events()

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_fetch_events_timeout(self, mocker, chaturbate_client) -> None: # noqa: ANN001
"""Test fetch_events method when a timeout occurs."""
mocker.patch.object(
Expand All @@ -449,7 +449,7 @@ async def test_fetch_events_timeout(self, mocker, chaturbate_client) -> None: #
with pytest.raises(ReadTimeout):
await chaturbate_client.fetch_events()

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_fetch_events_unhandled_exception(self, mocker) -> None: # noqa: ANN001
"""Test fetch_events method handles unhandled exceptions properly."""
mocker.patch("httpx.AsyncClient.get", side_effect=Exception("Unhandled error"))
Expand All @@ -462,20 +462,20 @@ async def test_fetch_events_unhandled_exception(self, mocker) -> None: # noqa:
class TestURLConstruction:
"""Tests for URL construction."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_url_construction(self, chaturbate_client: ChaturbateClient) -> None:
"""Test URL construction."""
url = chaturbate_client._construct_url() # noqa: SLF001
assert url == TEST_URL, "URL should be correctly constructed."

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_url_construction_with_timeout(self, chaturbate_client: ChaturbateClient) -> None:
"""Test URL construction with timeout."""
chaturbate_client.timeout = 10
url = chaturbate_client._construct_url() # noqa: SLF001
assert url == f"{TEST_URL}?timeout=10", "URL should be correctly constructed with timeout."

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_url_construction_with_timeout_zero(
self, chaturbate_client: ChaturbateClient
) -> None:
Expand All @@ -484,7 +484,7 @@ async def test_url_construction_with_timeout_zero(
url = chaturbate_client._construct_url() # noqa: SLF001
assert url == TEST_URL, "URL should be correctly constructed without timeout."

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_url_construction_with_timeout_none(
self, chaturbate_client: ChaturbateClient
) -> None:
Expand All @@ -497,7 +497,7 @@ async def test_url_construction_with_timeout_none(
class TestMessageFormatting:
"""Tests for message formatting."""

@pytest.fixture()
@pytest.fixture
def example_user(self) -> User:
"""Return an example User object."""
return User(
Expand All @@ -509,17 +509,17 @@ def example_user(self) -> User:
isMod=False,
)

@pytest.fixture()
@pytest.fixture
def media_photos(self) -> Media:
"""Return an example Media object."""
return Media(id=1, name="photoset1", type=MediaType.PHOTOS, tokens=25)

@pytest.fixture()
@pytest.fixture
def tip_example(self) -> Tip:
"""Return an example Tip object."""
return Tip(tokens=100, message="example message", isAnon=False)

@pytest.fixture()
@pytest.fixture
def message_example(self) -> Message:
"""Return an example Message object."""
return Message(
Expand All @@ -531,7 +531,7 @@ def message_example(self) -> Message:
bgColor="example_bg_color",
)

@pytest.mark.asyncio()
@pytest.mark.asyncio
@pytest.mark.parametrize(
("event_method", "event_data_func", "expected_message"),
[
Expand Down Expand Up @@ -613,7 +613,7 @@ async def test_events( # noqa: PLR0913
formatted_message = await format_message(event)
assert formatted_message == expected_message

@pytest.mark.asyncio()
@pytest.mark.asyncio
@pytest.mark.parametrize(
("tip_message", "is_anon", "expected_suffix"),
[
Expand Down Expand Up @@ -648,7 +648,7 @@ async def test_tip_variants(
formatted_message = await format_message(event)
assert formatted_message == f"example_user {expected_suffix}"

@pytest.mark.asyncio()
@pytest.mark.asyncio
@pytest.mark.parametrize(
("event_method", "expected_message"),
[
Expand Down Expand Up @@ -827,14 +827,14 @@ def test_enum_media_type(self) -> None:
class TestHTTPClient:
"""Tests for the HTTP client."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_http_client_request_success(self) -> None:
"""Test HTTP client request success."""
async with AsyncClient() as client:
response = await client.get("https://example.com")
assert response.status_code == 200 # noqa: PLR2004

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_http_client_request_failure(self) -> None:
"""Test HTTP client request failure."""
async with AsyncClient() as client:
Expand All @@ -845,7 +845,7 @@ async def test_http_client_request_failure(self) -> None:
class TestFormatMessages:
"""Tests for message formatting."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_format_message_tip_with_message(self) -> None:
"""Test formatting a tip with a message."""
message = await format_message(
Expand All @@ -868,7 +868,7 @@ async def test_format_message_tip_with_message(self) -> None:
)
assert message == "example_user tipped 100 tokens with message: 'example message'"

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_format_message_tip_without_message(self) -> None:
"""Test formatting a tip without a message."""
message = await format_message(
Expand All @@ -895,7 +895,7 @@ async def test_format_message_tip_without_message(self) -> None:
class TestEventFormatting:
"""Tests for event formatting."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_format_event_tip_with_message(self) -> None:
"""Test formatting a tip event with a message."""
formatted_event = await format_message(
Expand All @@ -918,7 +918,7 @@ async def test_format_event_tip_with_message(self) -> None:
)
assert formatted_event == ("example_user tipped 100 tokens with message: 'example message'")

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_format_event_tip_without_message(self) -> None:
"""Test formatting a tip event without a message."""
formatted_event = await format_message(
Expand Down Expand Up @@ -964,7 +964,7 @@ def test_format_log(self) -> None:
class TestEventFetching:
"""Tests for fetching events."""

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_fetch_events_undefined_json(
self,
chaturbate_client: ChaturbateClient,
Expand All @@ -978,7 +978,7 @@ async def test_fetch_events_undefined_json(
with pytest.raises(ValidationError, match="1 validation error for EventsAPIResponse"):
await chaturbate_client.fetch_events(TEST_URL)

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_unauthorized_access(
self,
http_client_mock, # noqa: ANN001
Expand All @@ -994,7 +994,7 @@ async def test_unauthorized_access(
with pytest.raises(ValueError, match="Unauthorized access. Verify the username and token."):
await chaturbate_client.fetch_events(TEST_URL)

@pytest.mark.asyncio()
@pytest.mark.asyncio
async def test_http_status_error(
self,
http_client_mock, # noqa: ANN001
Expand Down
Loading