Skip to content

Commit 6db0448

Browse files
committed
- Add test for video_livechat command
1 parent bc96643 commit 6db0448

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import csv
2+
import pytest
3+
4+
from io import StringIO
5+
from datetime import datetime
6+
from unittest.mock import Mock
7+
from youtool.commands import VideoLiveChat
8+
9+
10+
def test_video_livechat(mocker):
11+
youtube_mock = mocker.patch("youtool.commands.video_livechat.YouTube")
12+
video_id = "video_id_mock"
13+
14+
expected_result = [
15+
{column: "data" for column in VideoLiveChat.CHAT_MESSAGE_COLUMNS}
16+
]
17+
18+
csv_file = StringIO()
19+
csv_writer = csv.DictWriter(csv_file, fieldnames=expected_result[0].keys())
20+
csv_writer.writeheader()
21+
csv_writer.writerows(expected_result)
22+
23+
videos_livechat_mock = Mock(return_value=expected_result)
24+
youtube_mock.return_value.video_livechat = videos_livechat_mock
25+
result = VideoLiveChat.execute(id=video_id)
26+
27+
videos_livechat_mock.assert_called_once_with(video_id)
28+
29+
assert result == csv_file.getvalue()
30+
31+
32+
def test_video_livechat_with_file_output(mocker, tmp_path):
33+
youtube_mock = mocker.patch("youtool.commands.video_livechat.YouTube")
34+
video_id = "video_id_mock"
35+
36+
expected_result = [
37+
{column: "data" for column in VideoLiveChat.CHAT_MESSAGE_COLUMNS}
38+
]
39+
40+
csv_file = StringIO()
41+
csv_writer = csv.DictWriter(csv_file, fieldnames=expected_result[0].keys())
42+
csv_writer.writeheader()
43+
csv_writer.writerows(expected_result)
44+
45+
timestamp = datetime.now().strftime("%f")
46+
output_file_name = f"output_{timestamp}.csv"
47+
output_file_path = tmp_path / output_file_name
48+
49+
videos_livechat_mock = Mock(return_value=expected_result)
50+
youtube_mock.return_value.video_livechat = videos_livechat_mock
51+
52+
result_file_path = VideoLiveChat.execute(id=video_id, output_file_path=output_file_path)
53+
54+
with open(result_file_path, "r") as result_csv_file:
55+
result_csv = result_csv_file.read()
56+
57+
videos_livechat_mock.assert_called_once_with(video_id)
58+
59+
assert result_csv.replace("\r", "") == csv_file.getvalue().replace("\r", "")

0 commit comments

Comments
 (0)