Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoulSKY committed Aug 30, 2024
1 parent 58c74fb commit af814dc
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 124 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[report]
omit =
*/tests/*
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ docstring-code-format = false
docstring-code-line-length = "dynamic"

[lint.per-file-ignores]
"tests/*" = ["F811", "SLF001", "S101", "ARG001", "PT011"]
"tests/*" = ["F811", "SLF001", "S101", "ARG001", "PT011", "PLR2004"]
"docs/*" = ["INP001"]
"examples/*" = ["INP001", "T201", "D401"]
30 changes: 27 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Contains the setup code that runs before the tests in this package."""
"""Contains fixtures and utility functions."""

import os
import time
from datetime import UTC, datetime
from threading import Thread

import pytest
from dotenv import load_dotenv
from pyngrok import ngrok

from ytnoti import YouTubeNotifier
from ytnoti import Channel, Timestamp, Video, YouTubeNotifier

CALLBACK_URL = "http://localhost:8000"

Expand All @@ -30,4 +31,27 @@ def notifier() -> YouTubeNotifier:

time.sleep(0.1)

return noti
yield noti

noti.stop()


def get_channel() -> Channel:
"""Create a mock channel."""
return Channel(
id="mock_channel_id",
name="Mock Channel",
url="https://www.youtube.com/channel/mock_channel")

def get_video() -> Video:
"""Create a mock video."""
return Video(
id="mock_video_id",
title="Mock Video",
url="https://www.youtube.com/watch?v=mock_video",
timestamp=Timestamp(
published=datetime(2023, 1, 1, 12, 0, 0, tzinfo=UTC),
updated=datetime(2023, 1, 1, 13, 0, 0, tzinfo=UTC)
),
channel=get_channel()
)
27 changes: 1 addition & 26 deletions tests/history/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
"""Contains the utility functions for testing."""

from datetime import UTC, datetime

from ytnoti import Channel, Timestamp, Video


def get_channel() -> Channel:
"""Create a mock channel."""
return Channel(
id="mock_channel_id",
name="Mock Channel",
url="https://www.youtube.com/channel/mock_channel")

def get_video() -> Video:
"""Create a mock video."""
return Video(
id="mock_video_id",
title="Mock Video",
url="https://www.youtube.com/watch?v=mock_video",
timestamp=Timestamp(
published=datetime(2023, 1, 1, 12, 0, 0, tzinfo=UTC),
updated=datetime(2023, 1, 1, 13, 0, 0, tzinfo=UTC)
),
channel=get_channel()
)
"""Contains tests for history class."""
2 changes: 1 addition & 1 deletion tests/history/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from tests.history import get_video
from tests import get_video
from ytnoti.models.history import FileVideoHistory

NUM_VIDEOS = 100
Expand Down
16 changes: 15 additions & 1 deletion tests/history/test_inmemory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

from tests.history import get_video
from tests import get_video
from ytnoti import NotificationKind, YouTubeNotifier
from ytnoti.models.history import InMemoryVideoHistory

CACHE_SIZE = 100
Expand Down Expand Up @@ -50,3 +51,16 @@ async def test_add(history: InMemoryVideoHistory) -> None:

assert len(history._video_ids) == CACHE_SIZE
assert video.id not in history._video_ids


@pytest.mark.asyncio
async def test_get_kind() -> None:
"""Test getting the kind."""
notifier = YouTubeNotifier()
video = get_video()
video.timestamp.published = video.timestamp.updated

assert await notifier._get_kind(video) == NotificationKind.UPLOAD

await notifier._video_history.add(video)
assert await notifier._get_kind(video) == NotificationKind.UPLOAD
Loading

0 comments on commit af814dc

Please sign in to comment.