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

feat: add unit tests. #3

Merged
merged 3 commits into from
Jun 22, 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
37 changes: 37 additions & 0 deletions .github/workflows/test-and-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Python Tests and Coverage

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
cd youtube-playlist-downloader
poetry install
- name: Run tests with pytest and coverage
run: |
cd youtube-playlist-downloader
poetry run pytest tests --cov=. --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./youtube-playlist-downloader/coverage.xml
flags: unittests
fail_ci_if_error: true
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ repos:
- id: debug-statements
- id: detect-private-key

- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 7.1.0
hooks:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
This Python script allows you to download information about playlists created by a YouTube account, including both private and public playlists.
This is useful for backing up your playlists or for analyzing the videos in your playlists.

[![codecov](https://codecov.io/gh/siavashyj/toolbox/branch/main/graph/badge.svg)](https://codecov.io/gh/siavashyj/toolbox)

## Features

- Authenticates with the YouTube API using OAuth 2.0
Expand Down Expand Up @@ -78,6 +80,14 @@ My Favorite Song: https://www.youtube.com/watch?v=dQw4w9WgXcQ

Contributions to this project are welcome. Please feel free to submit a Pull Request.

## Unit Tests

To run the unit tests, use the following command:
```
poetry run pytest
```


## License

This project is licensed under the MIT License - see the LICENSE file for details.
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ black = "^23.3.0"
isort = "^5.12.0"
flake8 = "^6.0.0"

[tool.poetry.group.dev.dependencies]
pytest-cov = "^5.0.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Expand Down
Empty file.
106 changes: 106 additions & 0 deletions youtube-playlist-downloader/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from unittest.mock import Mock, mock_open, patch

import pytest
from googleapiclient.discovery import Resource

from main import get_authenticated_service, get_playlist_items, get_playlists, main


@pytest.fixture
def mock_youtube_service():
mock_service = Mock(spec=Resource)
mock_playlists = Mock()
mock_playlists.list.return_value.execute.return_value = {
"items": [{"id": "playlist1", "snippet": {"title": "Test Playlist"}}],
"nextPageToken": None,
}

mock_service.playlists = Mock()
mock_service.playlists.return_value = mock_playlists

mock_playlist_items = Mock()
mock_playlist_items.list.return_value.execute.return_value = {
"items": [{"snippet": {"title": "Test Video", "resourceId": {"videoId": "video1"}}}],
"nextPageToken": None,
}
mock_service.playlistItems = Mock()
mock_service.playlistItems.return_value = mock_playlist_items

return mock_service


def test_get_authenticated_service():
with patch("main.build") as mock_build, patch("main.InstalledAppFlow.from_client_secrets_file") as mock_flow, patch(
"builtins.open", new_callable=mock_open
) as _, patch("pickle.dump") as _, patch("pickle.load") as mock_load, patch("os.path.exists", return_value=False):

mock_credentials = Mock()
mock_flow.return_value.run_local_server.return_value = mock_credentials
mock_build.return_value = Mock(spec=Resource)
mock_load.return_value = mock_credentials

result = get_authenticated_service()

assert isinstance(result, Mock)
mock_build.assert_called_once_with("youtube", "v3", credentials=mock_credentials)


def test_get_playlists(mock_youtube_service):
result = get_playlists(mock_youtube_service)

assert len(result) == 3
assert result[0]["id"] == "playlist1"
assert result[0]["snippet"]["title"] == "Test Playlist"


def test_get_playlists_without_defaults(mock_youtube_service):
result = get_playlists(mock_youtube_service, get_liked=False, get_watch_list=False)

assert len(result) == 1
assert result[0]["id"] == "playlist1"
assert result[0]["snippet"]["title"] == "Test Playlist"


def test_get_playlist_items(mock_youtube_service):
result = get_playlist_items(mock_youtube_service, "playlist1")

assert len(result) == 1
assert result[0]["snippet"]["title"] == "Test Video"
assert result[0]["snippet"]["resourceId"]["videoId"] == "video1"


@patch("main.get_authenticated_service")
@patch("main.get_playlists")
@patch("main.get_playlist_items")
@patch("builtins.open", new_callable=mock_open)
@patch("os.path.exists", return_value=False)
@patch("os.makedirs")
def test_main(
mock_makedirs,
mock_exists,
mock_file,
mock_get_playlist_items,
mock_get_playlists,
mock_get_authenticated_service,
):
mock_youtube = Mock(spec=Resource)
mock_get_authenticated_service.return_value = mock_youtube
mock_get_playlists.return_value = [{"id": "playlist1", "snippet": {"title": "Test Playlist"}}]
mock_get_playlist_items.return_value = [{"snippet": {"title": "Test Video", "resourceId": {"videoId": "video1"}}}]

main()

assert mock_file.call_count == 2 # Two files are created per playlist
mock_file().write.assert_any_call("Test Video: https://www.youtube.com/watch?v=video1\n")
mock_file().write.assert_any_call("https://www.youtube.com/watch?v=video1\n")


@pytest.mark.parametrize(
"get_liked,get_watch_list,expected_calls",
[(True, True, 3), (True, False, 2), (False, True, 2), (False, False, 1)],
)
def test_get_playlists_with_options(mock_youtube_service, get_liked, get_watch_list, expected_calls):
result = get_playlists(mock_youtube_service, get_liked=get_liked, get_watch_list=get_watch_list)

assert mock_youtube_service.playlists.return_value.list.call_count == expected_calls
assert len(result) == expected_calls
Loading