140 lines
3.5 KiB
Python
140 lines
3.5 KiB
Python
![]() |
#!/usr/bin/env python3
|
||
|
"""
|
||
|
后端测试脚本
|
||
|
验证所有后端功能是否正常工作
|
||
|
"""
|
||
|
|
||
|
import os
|
||
|
import sys
|
||
|
import requests
|
||
|
from pathlib import Path
|
||
|
|
||
|
# 添加当前目录到Python路径
|
||
|
sys.path.insert(0, '.')
|
||
|
|
||
|
import os
|
||
|
os.chdir('backend')
|
||
|
from utils.markdown_parser import MarkdownParser
|
||
|
from utils.file_manager import FileManager
|
||
|
from utils.regex_processor import RegexProcessor
|
||
|
|
||
|
def test_markdown_parser():
|
||
|
"""测试Markdown解析器"""
|
||
|
print("🧪 测试Markdown解析器...")
|
||
|
parser = MarkdownParser()
|
||
|
|
||
|
# 基础测试
|
||
|
result = parser.parse('# 测试标题')
|
||
|
assert '<h1' in result['html'], "标题解析失败"
|
||
|
|
||
|
# 复杂测试
|
||
|
complex_md = """
|
||
|
# 主标题
|
||
|
|
||
|
这是一段**粗体**文本和*斜体*文本。
|
||
|
|
||
|
## 代码块
|
||
|
|
||
|
```python
|
||
|
def hello():
|
||
|
print("Hello, World!")
|
||
|
```
|
||
|
|
||
|
## 表格
|
||
|
|
||
|
| 列1 | 列2 |
|
||
|
|-----|-----|
|
||
|
| 数据1 | 数据2 |
|
||
|
|
||
|
## 数学公式
|
||
|
|
||
|
$E = mc^2$
|
||
|
"""
|
||
|
|
||
|
result = parser.parse(complex_md)
|
||
|
assert result['word_count'] > 0, "字数统计失败"
|
||
|
assert result['reading_time'] > 0, "阅读时间计算失败"
|
||
|
|
||
|
print("✅ Markdown解析器测试通过")
|
||
|
|
||
|
def test_file_manager():
|
||
|
"""测试文件管理器"""
|
||
|
print("🧪 测试文件管理器...")
|
||
|
|
||
|
# 创建测试目录
|
||
|
test_dir = Path("backend/workspace/test")
|
||
|
test_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
||
|
fm = FileManager()
|
||
|
|
||
|
# 测试写入
|
||
|
test_content = "# 测试文件\n这是测试内容"
|
||
|
fm.write_file("test/test.md", test_content)
|
||
|
|
||
|
# 测试读取
|
||
|
content = fm.read_file("test/test.md")
|
||
|
assert content == test_content, "文件读写不一致"
|
||
|
|
||
|
# 测试列表
|
||
|
files = fm.list_files("test")
|
||
|
assert len(files) > 0, "文件列表为空"
|
||
|
|
||
|
# 清理
|
||
|
import shutil
|
||
|
shutil.rmtree("workspace/test", ignore_errors=True)
|
||
|
|
||
|
print("✅ 文件管理器测试通过")
|
||
|
|
||
|
def test_regex_processor():
|
||
|
"""测试正则表达式处理器"""
|
||
|
print("🧪 测试正则表达式处理器...")
|
||
|
|
||
|
processor = RegexProcessor()
|
||
|
|
||
|
# 测试替换
|
||
|
content = "Hello World, Hello Python"
|
||
|
result = processor.replace(content, "Hello", "Hi", "gi")
|
||
|
assert result['result'] == "Hi World, Hi Python", "替换失败"
|
||
|
|
||
|
# 测试提取
|
||
|
content = "邮箱: test@example.com, 另一个: user@domain.org"
|
||
|
matches = processor.extract(content, r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', "g")
|
||
|
assert len(matches) == 2, "提取失败"
|
||
|
|
||
|
print("✅ 正则表达式处理器测试通过")
|
||
|
|
||
|
def test_api_health():
|
||
|
"""测试API健康检查"""
|
||
|
print("🧪 测试API健康检查...")
|
||
|
|
||
|
try:
|
||
|
from app import create_app
|
||
|
app = create_app()
|
||
|
with app.test_client() as client:
|
||
|
response = client.get('/api/health')
|
||
|
assert response.status_code == 200
|
||
|
data = response.get_json()
|
||
|
assert data['status'] == 'healthy'
|
||
|
print("✅ API健康检查测试通过")
|
||
|
except Exception as e:
|
||
|
print(f"❌ API健康检查测试失败: {e}")
|
||
|
|
||
|
def main():
|
||
|
"""运行所有测试"""
|
||
|
print("🚀 开始后端功能测试...")
|
||
|
|
||
|
# 确保workspace目录存在
|
||
|
Path("backend/workspace").mkdir(exist_ok=True)
|
||
|
|
||
|
try:
|
||
|
test_markdown_parser()
|
||
|
test_file_manager()
|
||
|
test_regex_processor()
|
||
|
test_api_health()
|
||
|
print("\n🎉 所有测试通过!后端功能正常")
|
||
|
except Exception as e:
|
||
|
print(f"\n❌ 测试失败: {e}")
|
||
|
sys.exit(1)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|