Skip to content

Commit 9f894b7

Browse files
committed
- Add test for channel_info command;
- Add update channel_info file; - fix test_base
1 parent dbe578e commit 9f894b7

3 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎tests/commands/test_base.py‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import argparse
33
import pytest
44

5-
from io import StringIO
6-
from datetime import datetime
75
from pathlib import Path
86
from unittest.mock import MagicMock, patch, mock_open
97
from youtool.commands import Command
@@ -65,7 +63,7 @@ def test_data_from_csv_valid(mock_csv_file):
6563
with patch('pathlib.Path.is_file', return_value=True):
6664
with patch('builtins.open', mock_open(read_data=mock_csv_file)):
6765
data_column_name = "URL"
68-
file_path = Path("tests/commands/csv_valid.csv")
66+
file_path = Path("tests/resources/csv_valid.csv")
6967
result = Command.data_from_csv(file_path, data_column_name)
7068
assert len(result) == 2
7169
assert result[0] == "http://example.com"
@@ -80,10 +78,10 @@ def test_data_from_csv_file_not_found():
8078
def test_data_from_csv_column_not_found(mock_csv_file):
8179
with patch('pathlib.Path.is_file', return_value=True):
8280
with patch('builtins.open', mock_open(read_data=mock_csv_file)):
83-
file_path = Path("tests/commands/csv_column_not_found.csv")
81+
file_path = Path("tests/resources/csv_column_not_found.csv")
8482
with pytest.raises(Exception) as exc_info:
8583
Command.data_from_csv(file_path, "NonExistentColumn")
86-
assert "Column NonExistentColumn not found on tests/commands/csv_column_not_found.csv" in str(exc_info.value), "Exception message should contain column not found error"
84+
assert "Column NonExistentColumn not found on tests/resources/csv_column_not_found.csv" in str(exc_info.value)
8785

8886

8987
@pytest.fixture
@@ -98,13 +96,13 @@ def test_data_to_csv_with_output_file_path(tmp_path, sample_data):
9896

9997
result_path = Command.data_to_csv(sample_data, str(output_file_path))
10098

101-
assert result_path == str(output_file_path), "The returned path should match the provided output file path"
102-
assert output_file_path.exists(), "The output file should exist"
99+
assert result_path == str(output_file_path)
100+
assert output_file_path.exists()
103101
with output_file_path.open('r') as f:
104102
reader = csv.DictReader(f)
105103
rows = list(reader)
106-
assert len(rows) == 2, "There should be two rows in the output CSV"
107-
assert rows[0]["id"] == "123" and rows[1]["id"] == "456", "The IDs should match the sample data"
104+
assert len(rows) == 2
105+
assert rows[0]["id"] == "123" and rows[1]["id"] == "456"
108106

109107
def test_data_to_csv_without_output_file_path(sample_data):
110108
csv_content = Command.data_to_csv(sample_data)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
3+
from unittest.mock import patch, Mock, call
4+
5+
from youtool.commands.channel_info import ChannelInfo, YouTube
6+
7+
8+
def test_filter_fields():
9+
channel_info = {
10+
'channel_id': '123456',
11+
'channel_name': 'Test Channel',
12+
'subscribers': 1000,
13+
'videos': 50,
14+
'category': 'Tech'
15+
}
16+
17+
info_columns = ['channel_id', 'channel_name', 'subscribers']
18+
filtered_info = ChannelInfo.filter_fields(channel_info, info_columns)
19+
20+
expected_result = {
21+
'channel_id': '123456',
22+
'channel_name': 'Test Channel',
23+
'subscribers': 1000
24+
}
25+
26+
assert filtered_info == expected_result, f"Expected {expected_result}, but got {filtered_info}"
27+
28+
29+
def test_channel_ids_from_urls_and_usernames(mocker):
30+
urls = ["https://www.youtube.com/@Turicas/featured", "https://www.youtube.com/c/PythonicCaf%C3%A9"]
31+
usernames = ["Turicas", "PythonicCafe"]
32+
33+
ids_from_urls_mock = "id_from_url"
34+
ids_from_usernames_mock = "id_from_username"
35+
youtube_mock = mocker.patch("youtool.commands.channel_info.YouTube")
36+
37+
channel_id_from_url_mock = Mock(return_value=ids_from_urls_mock)
38+
channel_id_from_username_mock = Mock(return_value=ids_from_usernames_mock)
39+
channels_infos_mock = Mock(return_value=[])
40+
41+
youtube_mock.return_value.channel_id_from_url = channel_id_from_url_mock
42+
youtube_mock.return_value.channel_id_from_username = channel_id_from_username_mock
43+
youtube_mock.return_value.channels_infos = channels_infos_mock
44+
45+
ChannelInfo.execute(urls=urls, usernames=usernames)
46+
47+
channel_id_from_url_mock.assert_has_calls(
48+
[call(url) for url in urls]
49+
)
50+
channel_id_from_username_mock.assert_has_calls(
51+
[call(username) for username in usernames]
52+
)
53+
channels_infos_mock.assert_called_once_with([ids_from_urls_mock, ids_from_usernames_mock])

‎youtool/commands/channel_info.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ def execute(cls: Self, **kwargs) -> str:
108108
] + [
109109
youtube.channel_id_from_username(username) for username in (usernames or []) if username
110110
]
111-
channel_ids = [channel_id for channel_id in channels_ids if channel_id]
111+
channel_ids = list(
112+
set([channel_id for channel_id in channels_ids if channel_id])
113+
)
112114

113115
return cls.data_to_csv(
114116
data=[
@@ -117,4 +119,4 @@ def execute(cls: Self, **kwargs) -> str:
117119
) for channel_info in (youtube.channels_infos(channel_ids) or [])
118120
],
119121
output_file_path=output_file_path
120-
)
122+
)

0 commit comments

Comments
 (0)