8
8
9
9
@pytest .fixture
10
10
def youtube_mock (mocker , mock_video_info ):
11
+ """Fixture to mock the YouTube instance and its videos_infos method."""
11
12
mock = mocker .patch ("youtool.commands.video_info.YouTube" )
12
13
mock_instance = mock .return_value
13
14
mock_instance .videos_infos = Mock (return_value = mock_video_info )
14
15
return mock_instance
15
16
16
17
@pytest .fixture
17
18
def mock_video_info ():
19
+ """Fixture to return mock video information."""
18
20
return [
19
21
{"id" : "tmrhPou85HQ" , "title" : "Title 1" , "description" : "Description 1" , "published_at" : "2021-01-01" , "view_count" : 100 , "like_count" : 10 , "comment_count" : 5 },
20
22
{"id" : "qoI_x9fylaw" , "title" : "Title 2" , "description" : "Description 2" , "published_at" : "2021-02-01" , "view_count" : 200 , "like_count" : 20 , "comment_count" : 10 }
21
23
]
22
24
23
25
def test_execute_with_ids_and_urls (youtube_mock , mocker , tmp_path , mock_video_info ):
26
+ """Test the execute method with provided video IDs and URLs.
27
+
28
+ This test verifies that the execute method can handle both video IDs and URLs,
29
+ and correctly writes the video information to the output CSV file.
30
+ """
24
31
ids = ["tmrhPou85HQ" , "qoI_x9fylaw" ]
25
32
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
33
output_file_path = tmp_path / "output.csv"
@@ -36,12 +43,25 @@ def test_execute_with_ids_and_urls(youtube_mock, mocker, tmp_path, mock_video_in
36
43
assert csv_data [1 ]["id" ] == "qoI_x9fylaw"
37
44
38
45
def test_execute_missing_arguments ():
46
+ """Test the execute method raises an exception when missing required arguments.
47
+
48
+ This test verifies that the execute method raises an exception if neither
49
+ video IDs nor URLs are provided.
50
+
51
+ Raises:
52
+ Exception: If neither 'ids' nor 'urls' is provided.
53
+ """
39
54
with pytest .raises (Exception ) as exc_info :
40
55
VideoInfo .execute (api_key = "test_api_key" )
41
56
42
57
assert str (exc_info .value ) == "Either 'ids' or 'urls' must be provided for the video-info command"
43
58
44
59
def test_execute_with_input_file_path (youtube_mock , mocker , tmp_path , mock_video_info ):
60
+ """Test the execute method with an input CSV file containing video URLs and IDs.
61
+
62
+ This test verifies that the execute method can read video URLs and IDs from
63
+ an input CSV file and correctly writes the video information to the output CSV file.
64
+ """
45
65
input_csv_content = """video_id,video_url
46
66
tmrhPou85HQ,https://www.youtube.com/watch?v=tmrhPou85HQ&ab_channel=Turicas
47
67
qoI_x9fylaw,https://www.youtube.com/watch?v=qoI_x9fylaw&ab_channel=PythonicCaf%C3%A9
@@ -64,6 +84,12 @@ def test_execute_with_input_file_path(youtube_mock, mocker, tmp_path, mock_video
64
84
65
85
66
86
def test_execute_with_info_columns (youtube_mock , mocker , tmp_path , mock_video_info ):
87
+ """Test the execute method with specified info columns.
88
+
89
+ This test verifies that the execute method can filter the video information
90
+ based on specified columns and correctly writes the filtered information
91
+ to the output CSV file.
92
+ """
67
93
ids = ["tmrhPou85HQ" , "qoI_x9fylaw" ]
68
94
output_file_path = tmp_path / "output.csv"
69
95
0 commit comments