Skip to content

Commit 0382303

Browse files
committed
add docstrings
1 parent 5aa10d7 commit 0382303

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

tests/commands/test_base.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ def execute(cls, **kwargs):
2121

2222
@pytest.fixture
2323
def subparsers():
24+
"""Fixture to create subparsers for argument parsing."""
2425
parser = argparse.ArgumentParser()
2526
return parser.add_subparsers()
2627

2728

2829
def test_generate_parser(subparsers):
30+
"""Test to verify the parser generation.
31+
32+
This test checks if the `generate_parser` method correctly generates a parser
33+
for the command and sets the appropriate properties
34+
"""
2935
parser = TestCommand.generate_parser(subparsers)
3036

3137
assert parser is not None, "Parser should not be None"
@@ -34,6 +40,11 @@ def test_generate_parser(subparsers):
3440

3541

3642
def test_parse_arguments(subparsers):
43+
"""Test to verify argument parsing.
44+
45+
This test checks if the `parse_arguments` method correctly adds the command's
46+
arguments to the parser and sets the default function to the command's execute method.
47+
"""
3748
subparsers_mock = MagicMock(spec=subparsers)
3849

3950
TestCommand.parse_arguments(subparsers_mock)
@@ -45,6 +56,11 @@ def test_parse_arguments(subparsers):
4556

4657

4758
def test_command():
59+
"""Test to verify that the `execute` method is implemented.
60+
61+
This test ensures that if a command does not implement the `execute` method,
62+
a `NotImplementedError` is raised.
63+
"""
4864
class MyCommand(Command):
4965
pass
5066

@@ -54,6 +70,7 @@ class MyCommand(Command):
5470

5571
@pytest.fixture
5672
def mock_csv_file():
73+
"""Fixture to provide mock CSV content for tests."""
5774

5875
csv_content = """URL
5976
http://example.com
@@ -62,6 +79,14 @@ def mock_csv_file():
6279
return csv_content
6380

6481
def test_data_from_csv_valid(mock_csv_file):
82+
"""Test to verify reading data from a valid CSV file.
83+
84+
This test checks if the `data_from_csv` method correctly reads data from a valid CSV file
85+
and returns the expected list of URLs.
86+
87+
Args:
88+
mock_csv_file (str): The mock CSV file content.
89+
"""
6590
with patch('pathlib.Path.is_file', return_value=True):
6691
with patch('builtins.open', mock_open(read_data=mock_csv_file)):
6792
data_column_name = "URL"
@@ -72,6 +97,11 @@ def test_data_from_csv_valid(mock_csv_file):
7297
assert result[1] == "http://example2.com"
7398

7499
def test_data_from_csv_file_not_found():
100+
"""Test to verify behavior when the specified column is not found in the CSV file.
101+
102+
This test checks if the `data_from_csv` method raises an exception when the specified
103+
column does not exist in the CSV file.
104+
"""
75105
with patch('pathlib.Path.is_file', return_value=False):
76106
file_path = Path("/fake/path/not_found.csv")
77107
with pytest.raises(FileNotFoundError):
@@ -88,12 +118,18 @@ def test_data_from_csv_column_not_found(mock_csv_file):
88118

89119
@pytest.fixture
90120
def sample_data():
121+
"""Fixture to provide sample data for tests."""
91122
return [
92123
{"id": "123", "name": "Channel One"},
93124
{"id": "456", "name": "Channel Two"}
94125
]
95126

96127
def test_data_to_csv_with_output_file_path(tmp_path, sample_data):
128+
"""Test to verify writing data to a CSV file with an output file path specified.
129+
130+
This test checks if the `data_to_csv` method correctly writes the sample data to
131+
a CSV file when an output file path is provided.
132+
"""
97133
output_file_path = tmp_path / "output.csv"
98134

99135
result_path = Command.data_to_csv(sample_data, str(output_file_path))
@@ -107,13 +143,24 @@ def test_data_to_csv_with_output_file_path(tmp_path, sample_data):
107143
assert rows[0]["id"] == "123" and rows[1]["id"] == "456", "The IDs should match the sample data"
108144

109145
def test_data_to_csv_without_output_file_path(sample_data):
146+
"""Test to verify writing data to a CSV format without an output file path specified.
147+
148+
This test checks if the `data_to_csv` method correctly returns the CSV content
149+
as a string when no output file path is provided.
150+
"""
110151
csv_content = Command.data_to_csv(sample_data)
111152

112153
assert "id,name" in csv_content
113154
assert "123,Channel One" in csv_content
114155
assert "456,Channel Two" in csv_content
115156

116157
def test_data_to_csv_output(tmp_path):
158+
"""
159+
Test to verify the content of the output CSV file.
160+
161+
This test checks if the `data_to_csv` method writes the expected content
162+
to the output CSV file.
163+
"""
117164
output_file_path = tmp_path / "output.csv"
118165

119166
data = [

tests/commands/test_channel_id.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@
88

99
@pytest.fixture
1010
def csv_file(tmp_path):
11+
"""Fixture to create a temporary CSV file with a single YouTube channel URL."""
12+
1113
csv_content = "channel_url\nhttps://www.youtube.com/@Turicas/featured\n"
1214
csv_file = tmp_path / "urls.csv"
1315
csv_file.write_text(csv_content)
1416
return csv_file
1517

1618
@pytest.fixture
1719
def youtube_api_mock():
20+
"""Fixture to mock the YouTube API.
21+
22+
This fixture mocks the `YouTube` class and its `channel_id_from_url` method
23+
to return a channel ID based on the URL.
24+
"""
1825
with patch("youtool.commands.channel_id.YouTube") as mock:
1926
mock.return_value.channel_id_from_url.side_effect = lambda url: f"channel-{url}"
2027
yield mock
2128

2229
def test_channels_ids_csv_preparation(youtube_api_mock):
30+
"""Fixture to mock the YouTube API.
31+
32+
This fixture mocks the `YouTube` class and its `channel_id_from_url` method
33+
to return a channel ID based on the URL.
34+
"""
2335
urls = ["https://www.youtube.com/@Turicas/featured", "https://www.youtube.com/c/PythonicCaf%C3%A9"]
2436
api_key = "test_api_key"
2537
id_column_name = "custom_id_column"
@@ -40,16 +52,29 @@ def test_channels_ids_csv_preparation(youtube_api_mock):
4052

4153

4254
def test_resolve_urls_with_direct_urls():
43-
# Tests whether the function returns the directly given list of URLs.
55+
"""Test to verify resolving URLs when provided directly.
56+
57+
This test checks if the `resolve_urls` method of the `ChannelId` class correctly
58+
returns the given list of URLs when provided directly.
59+
"""
4460
urls = ["https://www.youtube.com/@Turicas/featured"]
4561
result = ChannelId.resolve_urls(urls, None, None)
4662
assert result == urls
4763

4864
def test_resolve_urls_with_file_path(csv_file):
65+
"""Test to verify resolving URLs from a CSV file.
66+
67+
This test checks if the `resolve_urls` method of the `ChannelId` class correctly
68+
reads URLs from a given CSV file.
69+
"""
4970
result = ChannelId.resolve_urls(None, csv_file, "channel_url")
5071
assert result == ["https://www.youtube.com/@Turicas/featured"]
5172

5273
def test_resolve_urls_raises_exception():
53-
# Tests whether the function throws an exception when neither urls nor urls_file_path are provided.
74+
"""Test to verify exception raising when no URLs are provided.
75+
76+
This test checks if the `resolve_urls` method of the `ChannelId` class raises an exception
77+
when neither direct URLs nor a file path are provided.
78+
"""
5479
with pytest.raises(Exception, match="Either 'username' or 'url' must be provided for the channel-id command"):
5580
ChannelId.resolve_urls(None, None, None)

tests/test_cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"command", COMMANDS
1212
)
1313
def test_missing_api_key(monkeypatch: pytest.MonkeyPatch, command: Command):
14+
"""Test to verify behavior when the YouTube API key is missing.
15+
16+
This test ensures that when the YouTube API key is not set, running any command
17+
from the youtool CLI results in an appropriate error message and exit code.
18+
"""
1419
monkeypatch.delenv('YOUTUBE_API_KEY', raising=False)
1520
cli_path = "youtool/cli.py"
1621
command = ["python", cli_path, command.name]

0 commit comments

Comments
 (0)