Fix youtube-transcript-api call structure and make test assertions environment-robust

- Instantiate YouTubeTranscriptApi class and call list() as required by v1.2.4
- Decorate async database fixture with pytest_asyncio.fixture
- Replace hardcoded base URL string assertion in test_summarizer.py with dynamized summarizer.BASE_URL value to support proxy profiles

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Gan, Jimmy
2026-06-04 10:38:50 +08:00
parent 7f4de7e8d2
commit 85fd40acf8
3 changed files with 7 additions and 6 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ Transcript:
def download_transcript(video_id: str) -> str: def download_transcript(video_id: str) -> str:
try: try:
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id) transcript_list = YouTubeTranscriptApi().list(video_id)
# Try to find manual transcripts in preferred languages # Try to find manual transcripts in preferred languages
try: try:
+2 -1
View File
@@ -1,5 +1,6 @@
import os import os
import pytest import pytest
import pytest_asyncio
import asyncio import asyncio
import aiosqlite import aiosqlite
import sys import sys
@@ -34,7 +35,7 @@ def mock_db_path(tmp_path, monkeypatch):
return test_db_file return test_db_file
@pytest.fixture @pytest_asyncio.fixture
async def test_db(mock_db_path): async def test_db(mock_db_path):
# Initialize the tables # Initialize the tables
await db.init_db() await db.init_db()
+4 -4
View File
@@ -30,7 +30,7 @@ async def test_download_transcript_success():
mock_trans = MockTranscript() mock_trans = MockTranscript()
mock_list = MockTranscriptList(mock_trans) mock_list = MockTranscriptList(mock_trans)
with patch("youtube_transcript_api.YouTubeTranscriptApi.list_transcripts", return_value=mock_list): with patch("youtube_transcript_api.YouTubeTranscriptApi.list", return_value=mock_list):
text = summarizer.download_transcript("test_vid") text = summarizer.download_transcript("test_vid")
assert text == "Welcome to this video tutorial. Today we are learning about Svelte 5 and runes." assert text == "Welcome to this video tutorial. Today we are learning about Svelte 5 and runes."
@@ -58,7 +58,7 @@ async def test_summarize_video_success(test_db):
} }
# Patch both transcript api and httpx AsyncClient post call # Patch both transcript api and httpx AsyncClient post call
with patch("youtube_transcript_api.YouTubeTranscriptApi.list_transcripts", return_value=mock_list), \ with patch("youtube_transcript_api.YouTubeTranscriptApi.list", return_value=mock_list), \
patch("httpx.AsyncClient.post", new_callable=AsyncMock, return_value=mock_response) as mock_post: patch("httpx.AsyncClient.post", new_callable=AsyncMock, return_value=mock_response) as mock_post:
summary = await summarizer.summarize_video("summarize_vid") summary = await summarizer.summarize_video("summarize_vid")
@@ -66,7 +66,7 @@ async def test_summarize_video_success(test_db):
# Verify API request details # Verify API request details
mock_post.assert_called_once() mock_post.assert_called_once()
url = mock_post.call_args[0][0] url = mock_post.call_args[0][0]
assert "bytecatcode.org" in url assert summarizer.BASE_URL in url
headers = mock_post.call_args[1]["headers"] headers = mock_post.call_args[1]["headers"]
assert "x-api-key" in headers assert "x-api-key" in headers
@@ -105,7 +105,7 @@ async def test_summarize_all_pending(test_db):
"content": [{"text": "Fresh summary"}] "content": [{"text": "Fresh summary"}]
} }
with patch("youtube_transcript_api.YouTubeTranscriptApi.list_transcripts", return_value=mock_list), \ with patch("youtube_transcript_api.YouTubeTranscriptApi.list", return_value=mock_list), \
patch("httpx.AsyncClient.post", new_callable=AsyncMock, return_value=mock_response) as mock_post: patch("httpx.AsyncClient.post", new_callable=AsyncMock, return_value=mock_response) as mock_post:
await summarizer.summarize_all_pending() await summarizer.summarize_all_pending()