""" 测试 main.py 入口和 Flask 应用创建 """ import pytest from pathlib import Path import tempfile import sys import os # 添加项目根目录到路径 project_root = Path(__file__).parent.parent.absolute() sys.path.insert(0, str(project_root)) os.chdir(project_root) from main import create_app from app.settings import config class TestAppCreation: """测试应用创建""" def test_create_app_returns_flask_app(self): """测试 create_app 返回 Flask 应用实例""" app = create_app() assert app is not None assert app.name == 'main' def test_create_app_loads_config(self): """测试应用加载全局配置""" app = create_app() assert 'MAX_CONTENT_LENGTH' in app.config assert 'SEND_FILE_MAX_AGE_DEFAULT' in app.config assert 'INPUT_DIR' in app.config assert 'OUTPUT_DIR' in app.config assert 'TASK_TIMEOUT' in app.config assert 'API_PORT' in app.config assert 'VIDEO_PORT' in app.config def test_create_app_config_values(self): """测试配置值正确性""" app = create_app() assert app.config['MAX_CONTENT_LENGTH'] == 500 * 1024 * 1024 assert app.config['SEND_FILE_MAX_AGE_DEFAULT'] == 300 assert app.config['TASK_TIMEOUT'] == 600 assert app.config['API_PORT'] == 5000 assert app.config['VIDEO_PORT'] == 8086 def test_create_app_registers_cors(self): """测试应用注册了 CORS""" app = create_app() # 检查是否注册了 OPTIONS 路由 rules = [rule.rule for rule in app.url_map.iter_rules()] assert '/config' in rules class TestIndexRoute: """测试根路由""" def test_index_route_exists(self): """测试根路由存在""" app = create_app() with app.test_client() as client: response = client.get('/') assert response.status_code == 200 def test_index_route_returns_success(self): """测试根路由返回成功响应""" app = create_app() with app.test_client() as client: response = client.get('/') data = response.get_json() assert data is not None assert data['status'] == 'success' assert 'API 服务运行中' in data['message'] def test_index_route_response_format(self): """测试根路由响应格式""" app = create_app() with app.test_client() as client: response = client.get('/') data = response.get_json() assert 'status' in data assert 'message' in data assert 'timestamp' in data assert 'data' in data class TestASRRoutes: """测试 ASR 路由注册""" def test_asr_routes_registered(self): """测试 ASR 路由已注册""" app = create_app() rules = [rule.rule for rule in app.url_map.iter_rules()] assert '/api/recognize' in rules assert '/api/result' in rules def test_asr_recognize_route_methods(self): """测试 ASR 识别路由方法""" app = create_app() for rule in app.url_map.iter_rules(): if rule.rule == '/api/recognize': assert rule.methods is not None and 'GET' in rule.methods def test_asr_result_route_methods(self): """测试 ASR 结果路由方法""" app = create_app() for rule in app.url_map.iter_rules(): if rule.rule == '/api/result': assert rule.methods is not None and 'GET' in rule.methods class TestTranscodeRoutes: """测试转码路由注册""" def test_transcode_routes_registered(self): """测试转码路由已注册""" app = create_app() rules = [rule.rule for rule in app.url_map.iter_rules()] assert '/api/convert' in rules assert '/api/getVidUrl' in rules def test_transcode_convert_route_methods(self): """测试转码路由方法""" app = create_app() for rule in app.url_map.iter_rules(): if rule.rule == '/api/convert': assert rule.methods is not None and 'GET' in rule.methods def test_transcode_getvidurl_route_methods(self): """测试获取视频 URL 路由方法""" app = create_app() for rule in app.url_map.iter_rules(): if rule.rule == '/api/getVidUrl': assert rule.methods is not None and 'GET' in rule.methods class TestCORS: """测试 CORS 配置""" def test_cors_options_route_exists(self): """测试 CORS OPTIONS 路由存在""" app = create_app() with app.test_client() as client: response = client.options('/config') assert response.status_code == 200 def test_cors_headers_in_response(self): """测试响应包含 CORS 头""" app = create_app() with app.test_client() as client: response = client.get('/', headers={'Origin': 'http://test.com'}) assert 'Access-Control-Allow-Origin' in response.headers assert 'Access-Control-Allow-Methods' in response.headers assert 'Access-Control-Allow-Headers' in response.headers assert 'Access-Control-Allow-Credentials' in response.headers class TestConfigModule: """测试配置模块""" def test_config_exists(self): """测试配置存在""" assert config is not None assert isinstance(config, dict) def test_config_required_keys(self): """测试配置包含必需的键""" required_keys = [ 'MAX_CONTENT_LENGTH', 'SEND_FILE_MAX_AGE_DEFAULT', 'INPUT_DIR', 'OUTPUT_DIR', 'TASK_TIMEOUT', 'API_PORT', 'VIDEO_PORT' ] for key in required_keys: assert key in config, f"配置缺少必需的键:{key}" def test_config_port_values(self): """测试端口配置值""" assert isinstance(config['API_PORT'], int) assert isinstance(config['VIDEO_PORT'], int) assert config['API_PORT'] > 0 assert config['VIDEO_PORT'] > 0 def test_config_timeout_value(self): """测试超时配置值""" assert isinstance(config['TASK_TIMEOUT'], int) assert config['TASK_TIMEOUT'] > 0 if __name__ == '__main__': pytest.main([__file__, '-v'])