Skip to content

Commit 839e131

Browse files
authored
ci: Add unit tests for parse_videos and FileUpdater (#47)
1 parent 52fd2c3 commit 839e131

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

Diff for: action.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ def update(readme_path: str, comment_tag: str, replace_content: str):
258258
title_color=args.title_color,
259259
stats_color=args.stats_color,
260260
youtube_api_key=args.youtube_api_key,
261-
show_duration=args.show_duration == "true",
262261
theme_context_light=json.loads(args.theme_context_light),
263262
theme_context_dark=json.loads(args.theme_context_dark),
263+
show_duration=args.show_duration == "true",
264264
)
265265

266266
video_content = video_parser.parse_videos()

Diff for: tests/test_action.py

+86-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
from action import VideoParser
1+
import os
2+
import re
3+
4+
from action import FileUpdater, VideoParser
5+
6+
video_parser = VideoParser(
7+
base_url="https://ytcards.demolab.com/",
8+
channel_id="UCipSxT7a3rn81vGLw9lqRkg",
9+
max_videos=6,
10+
card_width=250,
11+
background_color="#0d1117",
12+
title_color="#ffffff",
13+
stats_color="#dedede",
14+
youtube_api_key="",
15+
theme_context_light={},
16+
theme_context_dark={},
17+
show_duration=False,
18+
)
219

320

421
def test_parse_iso8601_duration():
@@ -8,11 +25,73 @@ def test_parse_iso8601_duration():
825
assert VideoParser.parse_iso8601_duration("P1DT2H10M10S") == 94210
926

1027

11-
def test_parse_video():
12-
"""TODO: Test parsing a video from feedparser video object"""
13-
pass
28+
def test_parse_videos():
29+
videos = video_parser.parse_videos()
30+
31+
assert len(videos.splitlines()) == 6
1432

33+
line_regex = r"^\[!\[.*\]\(.* \"(.*)\"\)\]\(.*\)$"
34+
assert all(re.match(line_regex, line) for line in videos.splitlines())
1535

16-
def test_parse_videos():
17-
"""TODO: Test parsing a list of videos with feedparser"""
18-
pass
36+
assert "https://ytcards.demolab.com/?id=" in videos
37+
assert "title=" in videos
38+
assert "timestamp=" in videos
39+
assert "background_color=" in videos
40+
assert "title_color=" in videos
41+
assert "stats_color=" in videos
42+
assert "width=" in videos
43+
44+
45+
def test_parse_videos_with_theme_context():
46+
video_parser._theme_context_light = {
47+
"background_color": "#ffffff",
48+
"title_color": "#000000",
49+
"stats_color": "#000000",
50+
}
51+
video_parser._theme_context_dark = {
52+
"background_color": "#000000",
53+
"title_color": "#ffffff",
54+
"stats_color": "#ffffff",
55+
}
56+
videos = video_parser.parse_videos()
57+
58+
assert len(videos.splitlines()) == 6
59+
60+
line_regex = (
61+
r"^\[!\[.*\]\(.* \"(.*)\"\)\]\(.*\#gh-dark-mode-only\)"
62+
r"\[!\[.*\]\(.* \"(.*)\"\)\]\(.*\#gh-light-mode-only\)$"
63+
)
64+
assert all(re.match(line_regex, line) for line in videos.splitlines())
65+
66+
67+
def test_update_file():
68+
path = "./tests/README.md"
69+
# create a file to test with
70+
with open(path, "w+") as f:
71+
f.write(
72+
"Test Before\n"
73+
"\n"
74+
"<!-- BEGIN YOUTUBE-CARDS -->\n"
75+
"<!-- END YOUTUBE-CARDS -->\n"
76+
"\n"
77+
"Test After\n"
78+
)
79+
try:
80+
# update the file
81+
FileUpdater.update(path, "YOUTUBE-CARDS", "A\nB\nC")
82+
# read the file and assert the contents
83+
with open(path, "r") as f:
84+
assert f.read() == (
85+
"Test Before\n"
86+
"\n"
87+
"<!-- BEGIN YOUTUBE-CARDS -->\n"
88+
"A\n"
89+
"B\n"
90+
"C\n"
91+
"<!-- END YOUTUBE-CARDS -->\n"
92+
"\n"
93+
"Test After\n"
94+
)
95+
finally:
96+
# remove the file
97+
os.remove(path)

0 commit comments

Comments
 (0)