Skip to content

Commit

Permalink
tests: updated test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
MountainGod2 committed Aug 9, 2024
1 parent 5bfa98d commit cfd6a6f
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 5 deletions.
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ build-backend = "poetry.core.masonry.api"
source = ['chaturbate_poller']
relative_files = true

[tool.coverage.report]
exclude_also = [
"def __repr__",
"if self.debug:",
"if settings.DEBUG",
"raise AssertionError",
"raise NotImplementedError",
"if 0:",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
"class .*\\bProtocol\\):",
"@(abc\\.)?abstractmethod",
]

[tool.semantic_release]
version_variable = "pyproject.toml:version"
version_toml = [
Expand Down
11 changes: 8 additions & 3 deletions src/chaturbate_poller/chaturbate_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,18 @@ def __init__(
if not username or not token:
msg = "Chaturbate username and token are required."
raise ValueError(msg)

self.base_url = TESTBED_BASE_URL if testbed else DEFAULT_BASE_URL
if testbed:
self.base_url = TESTBED_BASE_URL
else:
self.base_url = DEFAULT_BASE_URL
if timeout is not None and timeout < 0:
msg = "Timeout must be a positive integer."
raise ValueError(msg)
self.timeout = timeout
self.username = username
self.token = token
self._client: httpx.AsyncClient | None = None
self.influxdb_handler = InfluxDBHandler()
self.influxdb_handler: InfluxDBHandler = InfluxDBHandler()

@property
def client(self) -> httpx.AsyncClient:
Expand Down
19 changes: 17 additions & 2 deletions tests/test_chaturbate_poller.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ def test_http_status_codes(self) -> None:
assert HttpStatusCode.GATEWAY_TIMEOUT == 504 # noqa: PLR2004


class TestChaturbateClient:
"""Tests for the ChaturbateClient."""
class TestChaturbateClientInitialization:
"""Tests for the initialization of ChaturbateClient."""

@pytest.mark.asyncio()
async def test_initialization(self) -> None:
Expand Down Expand Up @@ -263,6 +263,21 @@ async def test_initialization_failure(self) -> None:
async with ChaturbateClient(USERNAME, ""):
await asyncio.sleep(0)

@pytest.mark.asyncio()
async def test_initialization_with_invalid_timeout(self) -> None:
"""Test ChaturbateClient initialization with an 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()
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."):
async with ChaturbateClient("", ""):
pass


class TestErrorHandling:
"""Tests for error handling."""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_influxdb_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ruff: noqa: S101
"""Tests for InfluxDBHandler."""

from chaturbate_poller.influxdb_client import InfluxDBHandler


class TestInfluxDBHandler:
"""Tests for InfluxDBHandler."""

def test_flatten_dict(self) -> None:
"""Test the dictionary flattening method."""
handler = InfluxDBHandler()
nested_dict = {"level1": {"level2": "value", "level2b": {"level3": "value3"}}}
flattened_dict = handler.flatten_dict(nested_dict)
expected_dict = {"level1.level2": "value", "level1.level2b.level3": "value3"}

assert flattened_dict == expected_dict
185 changes: 185 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# ruff: noqa: S101
"""Tests for Pydantic models."""

import pytest
from pydantic import ValidationError

from chaturbate_poller.models import Event, EventData, Gender, Media, MediaType, Message, Tip, User


class TestModels:
"""Tests for the models."""

def test_user_model(self) -> None:
"""Test the User model."""
user = User(
username="example_user",
inFanclub=False,
gender=Gender.MALE,
hasTokens=True,
recentTips="none",
isMod=False,
)
assert user.username == "example_user"
assert user.in_fanclub is False
assert user.gender == Gender.MALE
assert user.has_tokens is True
assert user.recent_tips == "none"
assert user.is_mod is False

def test_invalid_user_model(self) -> None:
"""Test the User model with invalid data."""
with pytest.raises(ValidationError):
User(
username="example_user",
inFanclub="not_a_boolean", # type: ignore[arg-type]
gender=Gender.MALE,
hasTokens=True,
recentTips="none",
isMod=False,
)

def test_message_model(self) -> None:
"""Test the Message model."""
message = Message(
fromUser="example_user",
message="example message",
color="example_color",
font="example_font",
toUser="user",
bgColor="example_bg_color",
)
assert message.from_user == "example_user"
assert message.message == "example message"
assert message.color == "example_color"
assert message.font == "example_font"
assert message.to_user == "user"
assert message.bg_color == "example_bg_color"

def test_event_data_model(self) -> None:
"""Test the EventData model."""
event_data = EventData(
broadcaster="example_broadcaster",
user=User(
username="example_user",
inFanclub=False,
gender=Gender.MALE,
hasTokens=True,
recentTips="none",
isMod=False,
),
tip=Tip(
tokens=100,
message="example message",
isAnon=False,
),
media=Media(id=1, name="photoset1", type=MediaType.PHOTOS, tokens=25),
subject="example subject",
message=Message(
fromUser="example_user",
message="example message",
color="example_color",
font="example_font",
toUser="user",
bgColor="example_bg_color",
),
)
assert event_data.broadcaster == "example_broadcaster"
if event_data.user is not None:
assert event_data.user.username == "example_user"
assert event_data.user.in_fanclub is False

if event_data.tip is not None:
assert event_data.tip.tokens == 100 # noqa: PLR2004
assert event_data.tip.message == "example message"
assert event_data.tip.is_anon is False

if event_data.media is not None:
assert event_data.media.id == 1
assert event_data.media.name == "photoset1"
assert event_data.media.type == MediaType.PHOTOS
assert event_data.media.tokens == 25 # noqa: PLR2004

if event_data.message is not None:
assert event_data.message.from_user == "example_user"
assert event_data.message.message == "example message"
assert event_data.message.color == "example_color"
assert event_data.message.font == "example_font"
assert event_data.message.to_user == "user"
assert event_data.message.bg_color == "example_bg_color"

assert event_data.subject == "example subject"

def test_event_model(self) -> None:
"""Test the Event model."""
event = Event(
method="userEnter",
object=EventData(
broadcaster="example_broadcaster",
user=User(
username="example_user",
inFanclub=False,
gender=Gender.MALE,
hasTokens=True,
recentTips="none",
isMod=False,
),
),
id="UNIQUE_EVENT_ID",
)
assert event.method == "userEnter"
assert event.object.broadcaster == "example_broadcaster"
if event.object.user is not None:
assert event.object.user.username == "example_user"
assert event.id == "UNIQUE_EVENT_ID"

def test_invalid_event_model(self) -> None:
"""Test the Event model with invalid data."""
with pytest.raises(ValidationError):
Event(
method="userEnter",
object="invalid_data", # type: ignore[arg-type]
id="UNIQUE_EVENT_ID",
)

def test_tip_model(self) -> None:
"""Test the Tip model."""
tip = Tip(tokens=100, message="example message", isAnon=False)
assert tip.tokens == 100 # noqa: PLR2004
assert tip.message == "example message"
assert tip.is_anon is False

def test_invalid_tip_model(self) -> None:
"""Test the Tip model with invalid data."""
with pytest.raises(ValidationError):
Tip(tokens=-10, message="example message", isAnon=False)

def test_media_model(self) -> None:
"""Test the Media model."""
media = Media(id=1, name="photoset1", type=MediaType.PHOTOS, tokens=25)
assert media.id == 1
assert media.name == "photoset1"
assert media.type == MediaType.PHOTOS
assert media.tokens == 25 # noqa: PLR2004

def test_invalid_media_model(self) -> None:
"""Test the Media model with invalid data."""
with pytest.raises(ValidationError):
Media(id="invalid_id", name="photoset1", type=MediaType.PHOTOS, tokens=25) # type: ignore[arg-type]

def test_enum_gender(self) -> None:
"""Test the Gender enum."""
assert Gender.MALE.value == "m"
assert Gender.FEMALE.value == "f"
assert Gender.TRANS.value == "t"
assert Gender.COUPLE.value == "c"

def test_enum_media_type(self) -> None:
"""Test the MediaType enum."""
assert MediaType.PHOTOS.value == "photos"
assert MediaType.VIDEOS.value == "videos"

def test_invalid_enum(self) -> None:
"""Test an invalid enum value."""
with pytest.raises(ValueError, match="'invalid_gender' is not a valid Gender"):
Gender("invalid_gender")

0 comments on commit cfd6a6f

Please sign in to comment.