|
| 1 | +import csv |
| 2 | +import pytest |
| 3 | + |
| 4 | +from unittest.mock import Mock |
| 5 | +from pathlib import Path |
| 6 | +from youtool.commands import VideoInfo |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def youtube_mock(mocker, mock_video_info): |
| 11 | + mock = mocker.patch("youtool.commands.video_info.YouTube") |
| 12 | + mock_instance = mock.return_value |
| 13 | + mock_instance.videos_infos = Mock(return_value=mock_video_info) |
| 14 | + return mock_instance |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_video_info(): |
| 18 | + return [ |
| 19 | + {"id": "tmrhPou85HQ", "title": "Title 1", "description": "Description 1", "published_at": "2021-01-01", "view_count": 100, "like_count": 10, "comment_count": 5}, |
| 20 | + {"id": "qoI_x9fylaw", "title": "Title 2", "description": "Description 2", "published_at": "2021-02-01", "view_count": 200, "like_count": 20, "comment_count": 10} |
| 21 | + ] |
| 22 | + |
| 23 | +def test_execute_with_ids_and_urls(youtube_mock, mocker, tmp_path, mock_video_info): |
| 24 | + ids = ["tmrhPou85HQ", "qoI_x9fylaw"] |
| 25 | + urls = ["https://www.youtube.com/watch?v=tmrhPou85HQ&ab_channel=Turicas", "https://www.youtube.com/watch?v=qoI_x9fylaw&ab_channel=PythonicCaf%C3%A9"] |
| 26 | + output_file_path = tmp_path / "output.csv" |
| 27 | + |
| 28 | + VideoInfo.execute(ids=ids, urls=urls, output_file_path=str(output_file_path), api_key="test_api_key") |
| 29 | + |
| 30 | + assert Path(output_file_path).is_file() |
| 31 | + with open(output_file_path, 'r') as f: |
| 32 | + reader = csv.DictReader(f) |
| 33 | + csv_data = list(reader) |
| 34 | + |
| 35 | + assert csv_data[0]["id"] == "tmrhPou85HQ" |
| 36 | + assert csv_data[1]["id"] == "qoI_x9fylaw" |
| 37 | + |
| 38 | +def test_execute_missing_arguments(): |
| 39 | + with pytest.raises(Exception) as exc_info: |
| 40 | + VideoInfo.execute(api_key="test_api_key") |
| 41 | + |
| 42 | + assert str(exc_info.value) == "Either 'ids' or 'urls' must be provided for the video-info command" |
| 43 | + |
| 44 | +def test_execute_with_input_file_path(youtube_mock, mocker, tmp_path, mock_video_info): |
| 45 | + input_csv_content = """video_id,video_url |
| 46 | + tmrhPou85HQ,https://www.youtube.com/watch?v=tmrhPou85HQ&ab_channel=Turicas |
| 47 | + qoI_x9fylaw,https://www.youtube.com/watch?v=qoI_x9fylaw&ab_channel=PythonicCaf%C3%A9 |
| 48 | + """ |
| 49 | + input_file_path = tmp_path / "input.csv" |
| 50 | + output_file_path = tmp_path / "output.csv" |
| 51 | + |
| 52 | + with open(input_file_path, 'w') as f: |
| 53 | + f.write(input_csv_content) |
| 54 | + |
| 55 | + VideoInfo.execute(input_file_path=str(input_file_path), output_file_path=str(output_file_path), api_key="test_api_key") |
| 56 | + |
| 57 | + assert Path(output_file_path).is_file() |
| 58 | + with open(output_file_path, 'r') as f: |
| 59 | + reader = csv.DictReader(f) |
| 60 | + csv_data = list(reader) |
| 61 | + |
| 62 | + assert csv_data[0]["id"] == "tmrhPou85HQ" |
| 63 | + assert csv_data[1]["id"] == "qoI_x9fylaw" |
| 64 | + |
| 65 | + |
| 66 | +def test_execute_with_info_columns(youtube_mock, mocker, tmp_path, mock_video_info): |
| 67 | + ids = ["tmrhPou85HQ", "qoI_x9fylaw"] |
| 68 | + output_file_path = tmp_path / "output.csv" |
| 69 | + |
| 70 | + VideoInfo.execute(ids=ids, output_file_path=str(output_file_path), api_key="test_api_key", info_columns="id,title") |
| 71 | + |
| 72 | + assert Path(output_file_path).is_file() |
| 73 | + with open(output_file_path, 'r') as f: |
| 74 | + reader = csv.DictReader(f) |
| 75 | + csv_data = list(reader) |
| 76 | + |
| 77 | + assert csv_data[0]["id"] == "tmrhPou85HQ" |
| 78 | + assert csv_data[0]["title"] == "Title 1" |
| 79 | + assert csv_data[1]["id"] == "qoI_x9fylaw" |
| 80 | + assert csv_data[1]["title"] == "Title 2" |
0 commit comments