|
""" |
|
Test podcast download functionality |
|
ๆต่ฏๆญๅฎขไธ่ฝฝๅ่ฝ |
|
""" |
|
|
|
import pytest |
|
import asyncio |
|
import os |
|
from pathlib import Path |
|
from typing import Dict, Any |
|
|
|
from src.tools.download_tools import download_apple_podcast_tool, download_xyz_podcast_tool |
|
from src.services.podcast_download_service import PodcastDownloadService |
|
from src.interfaces.podcast_downloader import PodcastPlatform |
|
|
|
|
|
class TestPodcastDownload: |
|
"""Test podcast download integration""" |
|
|
|
@pytest.mark.asyncio |
|
async def test_apple_podcast_info_extraction(self, podcast_download_service: PodcastDownloadService): |
|
"""Test Apple podcast information extraction""" |
|
print("\n๐ Testing Apple Podcast info extraction...") |
|
|
|
|
|
test_url = "https://podcasts.apple.com/us/podcast/the-tim-ferriss-show/id863897795" |
|
|
|
try: |
|
|
|
can_handle = podcast_download_service.can_handle_url(test_url) |
|
assert can_handle, "Should be able to handle Apple Podcast URL" |
|
|
|
|
|
podcast_info = await podcast_download_service.extract_podcast_info(test_url) |
|
|
|
assert podcast_info is not None |
|
assert podcast_info.platform == PodcastPlatform.APPLE |
|
assert podcast_info.title is not None |
|
assert len(podcast_info.title) > 0 |
|
|
|
print(f"โ
Successfully extracted Apple Podcast info:") |
|
print(f" Title: {podcast_info.title}") |
|
print(f" Platform: {podcast_info.platform}") |
|
print(f" Episode ID: {podcast_info.episode_id}") |
|
|
|
except Exception as e: |
|
print(f"โ Apple Podcast info extraction failed: {str(e)}") |
|
pytest.skip(f"Apple Podcast info extraction failed: {str(e)}") |
|
|
|
@pytest.mark.asyncio |
|
async def test_xiaoyuzhou_podcast_info_extraction(self, podcast_download_service: PodcastDownloadService): |
|
"""Test XiaoYuZhou podcast information extraction""" |
|
print("\n๐ต Testing XiaoYuZhou Podcast info extraction...") |
|
|
|
|
|
test_url = "https://www.xiaoyuzhoufm.com/episode/example123" |
|
|
|
try: |
|
|
|
can_handle = podcast_download_service.can_handle_url(test_url) |
|
assert can_handle, "Should be able to handle XiaoYuZhou Podcast URL" |
|
|
|
|
|
try: |
|
podcast_info = await podcast_download_service.extract_podcast_info(test_url) |
|
|
|
assert podcast_info is not None |
|
assert podcast_info.platform == PodcastPlatform.XIAOYUZHOU |
|
|
|
print(f"โ
Successfully extracted XiaoYuZhou Podcast info:") |
|
print(f" Title: {podcast_info.title}") |
|
print(f" Platform: {podcast_info.platform}") |
|
print(f" Episode ID: {podcast_info.episode_id}") |
|
|
|
except Exception as e: |
|
print(f"โ ๏ธ XiaoYuZhou info extraction failed (expected for test URL): {str(e)}") |
|
|
|
except Exception as e: |
|
print(f"โ XiaoYuZhou platform detection failed: {str(e)}") |
|
|
|
@pytest.mark.asyncio |
|
async def test_apple_podcast_download_simulation(self, temp_dir: str): |
|
"""Test Apple podcast download simulation (without actual download)""" |
|
print("\n๐ Testing Apple Podcast download simulation...") |
|
|
|
|
|
test_url = "https://podcasts.apple.com/us/podcast/the-tim-ferriss-show/id863897795" |
|
|
|
try: |
|
|
|
result = await download_apple_podcast_tool(test_url) |
|
|
|
print(f"๐ Download tool result:") |
|
print(f" Status: {result.get('status', 'unknown')}") |
|
print(f" Original URL: {result.get('original_url', 'N/A')}") |
|
|
|
if result.get("status") == "success": |
|
print(f" Audio file path: {result.get('audio_file_path', 'N/A')}") |
|
print("โ
Apple Podcast download simulation successful") |
|
else: |
|
print(f" Error: {result.get('error_message', 'Unknown error')}") |
|
print("โ ๏ธ Apple Podcast download simulation failed (might be network-related)") |
|
|
|
except Exception as e: |
|
print(f"โ Apple Podcast download test failed: {str(e)}") |
|
pytest.skip(f"Apple Podcast download test failed: {str(e)}") |
|
|
|
@pytest.mark.asyncio |
|
async def test_xiaoyuzhou_podcast_download_simulation(self, temp_dir: str): |
|
"""Test XiaoYuZhou podcast download simulation""" |
|
print("\n๐ต Testing XiaoYuZhou Podcast download simulation...") |
|
|
|
|
|
test_url = "https://www.xiaoyuzhoufm.com/episode/example123" |
|
|
|
try: |
|
|
|
result = await download_xyz_podcast_tool(test_url) |
|
|
|
print(f"๐ Download tool result:") |
|
print(f" Status: {result.get('status', 'unknown')}") |
|
print(f" Original URL: {result.get('original_url', 'N/A')}") |
|
|
|
if result.get("status") == "success": |
|
print(f" Audio file path: {result.get('audio_file_path', 'N/A')}") |
|
print("โ
XiaoYuZhou Podcast download simulation successful") |
|
else: |
|
print(f" Error: {result.get('error_message', 'Unknown error')}") |
|
print("โ ๏ธ XiaoYuZhou Podcast download simulation failed (expected for test URL)") |
|
|
|
except Exception as e: |
|
print(f"โ XiaoYuZhou Podcast download test failed: {str(e)}") |
|
|
|
|
|
@pytest.mark.asyncio |
|
async def test_supported_platforms(self, podcast_download_service: PodcastDownloadService): |
|
"""Test supported platforms detection""" |
|
print("\n๐ Testing supported platforms...") |
|
|
|
platforms = podcast_download_service.get_supported_platforms() |
|
|
|
assert PodcastPlatform.APPLE in platforms |
|
assert PodcastPlatform.XIAOYUZHOU in platforms |
|
|
|
print(f"โ
Supported platforms: {[p.value for p in platforms]}") |
|
|
|
@pytest.mark.asyncio |
|
async def test_url_validation(self, podcast_download_service: PodcastDownloadService): |
|
"""Test URL validation""" |
|
print("\n๐ Testing URL validation...") |
|
|
|
test_cases = [ |
|
("https://podcasts.apple.com/us/podcast/test", True, "Apple Podcast URL"), |
|
("https://www.xiaoyuzhoufm.com/episode/test", True, "XiaoYuZhou URL"), |
|
("https://example.com/podcast", False, "Generic URL"), |
|
("invalid-url", False, "Invalid URL"), |
|
] |
|
|
|
for url, expected, description in test_cases: |
|
result = podcast_download_service.can_handle_url(url) |
|
assert result == expected, f"URL validation failed for {description}: {url}" |
|
print(f"โ
{description}: {'โ' if result else 'โ'}") |
|
|
|
def test_download_tools_initialization(self): |
|
"""Test download tools initialization""" |
|
print("\n๐ง Testing download tools initialization...") |
|
|
|
|
|
assert download_apple_podcast_tool is not None |
|
assert download_xyz_podcast_tool is not None |
|
|
|
print("โ
Download tools initialized successfully") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
pytest.main([__file__, "-v", "-s"]) |