SpeechRecognition/test/test_transcode_routes.py

256 lines
8.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
测试转码路由
"""
import pytest
from pathlib import Path
import tempfile
import os
import sys
project_root = Path(__file__).parent.parent.absolute()
sys.path.insert(0, str(project_root))
os.chdir(project_root)
from main import create_app
class TestConvertRoute:
"""测试视频转码路由"""
def test_convert_missing_path_parameter(self):
"""测试缺少 path 参数时返回错误"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/convert')
assert response.status_code == 400
data = response.get_json()
assert data['status'] == 'error'
assert 'path' in data['message']
def test_convert_with_empty_path(self):
"""测试 path 为空时返回错误"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/convert?path=')
assert response.status_code == 400
data = response.get_json()
assert data['status'] == 'error'
def test_convert_file_not_found(self, temp_dirs):
"""测试文件不存在时返回 404"""
app = create_app()
app.config['TESTING'] = True
app.config['INPUT_DIR'] = str(temp_dirs['input'])
app.config['OUTPUT_DIR'] = str(temp_dirs['output'])
with app.test_client() as client:
response = client.get('/api/convert?path=nonexistent.mp4')
assert response.status_code == 404
data = response.get_json()
assert data['status'] == 'error'
assert '不存在' in data['message']
def test_convert_response_format(self, temp_dirs):
"""测试转码响应格式"""
app = create_app()
app.config['TESTING'] = True
app.config['INPUT_DIR'] = str(temp_dirs['input'])
app.config['OUTPUT_DIR'] = str(temp_dirs['output'])
with app.test_client() as client:
response = client.get('/api/convert?path=test.mp4')
# 文件不存在时返回 404
assert response.status_code in [200, 404, 500]
data = response.get_json()
assert 'status' in data
assert 'message' in data
class TestGetVidUrlRoute:
"""测试获取视频 URL 路由"""
def test_getvidurl_missing_path_parameter(self):
"""测试缺少 path 参数时返回错误"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/getVidUrl')
assert response.status_code == 404
data = response.get_json()
assert data['status'] == 'error'
def test_getvidurl_with_empty_path(self):
"""测试 path 为空时返回错误"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/getVidUrl?path=')
# Path('') 在布尔检查中为 True但后续检查会返回 404
assert response.status_code == 404
data = response.get_json()
assert data['status'] == 'error'
def test_getvidurl_video_not_found(self, temp_dirs):
"""测试视频不存在时返回 404"""
app = create_app()
app.config['TESTING'] = True
app.config['OUTPUT_DIR'] = str(temp_dirs['output'])
# 创建转码输出目录
transcode_dir = Path(temp_dirs['output']) / 'vid_h264'
transcode_dir.mkdir(parents=True, exist_ok=True)
with app.test_client() as client:
response = client.get('/api/getVidUrl?path=nonexistent.mp4')
assert response.status_code == 404
data = response.get_json()
assert data['status'] == 'error'
def test_getvidurl_with_valid_video(self, temp_dirs):
"""测试获取存在的视频 URL"""
# 使用 conftest 的 app fixture
from flask import Flask
import tempfile
# 创建临时输出目录
temp_output = tempfile.mkdtemp()
app = Flask(__name__)
app.config['TESTING'] = True
app.config['OUTPUT_DIR'] = temp_output
# 注册转码路由
from app.transcode.routes import register_transcode_routes
register_transcode_routes(app)
# 创建模拟转码视频文件
transcode_dir = Path(temp_output) / 'vid_h264'
transcode_dir.mkdir(parents=True, exist_ok=True)
video_file = transcode_dir / 'test_h264.mp4'
video_file.touch()
with app.test_client() as client:
response = client.get('/api/getVidUrl?path=test.mp4')
assert response.status_code == 200
data = response.get_json()
assert data['status'] == 'success'
assert 'data' in data
assert 'url' in data['data']
assert 'test_h264.mp4' in data['data']['url']
assert 'localhost:8086' in data['data']['url']
class TestTranscodeCore:
"""测试转码核心功能"""
def test_transcode_core_import(self):
"""测试转码核心模块可以导入"""
from app.transcode.core import convert_to_h264
assert convert_to_h264 is not None
def test_convert_to_h264_file_not_found(self, temp_dirs):
"""测试文件不存在时抛出异常"""
from app.transcode.core import convert_to_h264
with pytest.raises(FileNotFoundError):
convert_to_h264(
input_root=temp_dirs['input'],
vid_full_name='nonexistent.mp4',
output_root=temp_dirs['output']
)
def test_convert_to_h264_output_path_format(self, temp_dirs):
"""测试输出路径格式"""
from app.transcode.core import convert_to_h264
# 创建一个假的输入文件
input_file = temp_dirs['input'] / 'test.mp4'
input_file.touch()
# 由于需要 ffmpeg这里只测试路径处理逻辑
# 实际转码会失败,但可以验证路径处理
try:
output_path = convert_to_h264(
input_root=temp_dirs['input'],
vid_full_name='test.mp4',
output_root=temp_dirs['output']
)
# 如果成功,验证输出路径格式
assert 'test_h264.mp4' in output_path
except Exception as e:
# 预期可能因为 ffmpeg 问题失败
assert 'ffmpeg' in str(e).lower() or 'returncode' in str(e).lower()
class TestTranscodeOutputDirectory:
"""测试转码输出目录"""
def test_transcode_output_dir_created(self, temp_dirs):
"""测试转码输出目录自动创建"""
from flask import Flask
# 创建新 app 并配置 OUTPUT_DIR
app = Flask(__name__)
app.config['TESTING'] = True
app.config['OUTPUT_DIR'] = str(temp_dirs['output'])
# 触发路由注册以创建输出目录
from app.transcode.routes import register_transcode_routes
register_transcode_routes(app)
transcode_dir = Path(temp_dirs['output']) / 'vid_h264'
assert transcode_dir.exists()
assert transcode_dir.is_dir()
def test_transcode_output_dir_with_main_app(self):
"""测试主应用创建时输出目录也会创建"""
# 使用 create_app 会自动注册路由并创建输出目录
app = create_app()
# 验证路由已注册
rules = [rule.rule for rule in app.url_map.iter_rules()]
assert '/api/convert' in rules
assert '/api/getVidUrl' in rules
class TestVideoFormats:
"""测试支持的视频格式"""
def test_mp4_format(self):
"""测试 MP4 格式支持"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/convert?path=test.mp4')
# 文件不存在,但路由应该能处理
assert response.status_code in [404, 500]
def test_avi_format(self):
"""测试 AVI 格式支持"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/convert?path=test.avi')
assert response.status_code in [404, 500]
def test_mkv_format(self):
"""测试 MKV 格式支持"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/convert?path=test.mkv')
assert response.status_code in [404, 500]
def test_mov_format(self):
"""测试 MOV 格式支持"""
app = create_app()
app.config['TESTING'] = True
with app.test_client() as client:
response = client.get('/api/convert?path=test.mov')
assert response.status_code in [404, 500]
if __name__ == '__main__':
pytest.main([__file__, '-v'])