284 lines
8.7 KiB
Markdown
284 lines
8.7 KiB
Markdown
# 正则路径替换功能测试总结
|
||
|
||
## 📊 测试统计
|
||
|
||
- **总测试数**: 46 个
|
||
- **测试状态**: ✅ 全部通过
|
||
- **执行时间**: ~0.4 秒
|
||
- **覆盖范围**: 正则编译、路径替换、预处理、边界情况、性能测试
|
||
|
||
## 🎯 测试文件
|
||
|
||
`tests/test_regex_path_replacement.py` - 正则路径替换功能的全面测试套件
|
||
|
||
## 📝 测试分类
|
||
|
||
### 1. TestCompileRegexRules (7 个测试)
|
||
测试正则规则编译功能
|
||
|
||
- ✅ `test_compile_simple_pattern` - 简单正则模式编译
|
||
- ✅ `test_compile_multiple_patterns` - 多个正则模式编译
|
||
- ✅ `test_compile_empty_pattern_skipped` - 跳过空模式
|
||
- ✅ `test_compile_missing_pattern_skipped` - 跳过缺失模式
|
||
- ✅ `test_compile_invalid_regex_skipped` - 跳过无效正则表达式
|
||
- ✅ `test_compile_empty_replacement` - 空替换字符串
|
||
- ✅ `test_compile_missing_replacement` - 缺失替换字符串(默认为空)
|
||
|
||
**关键测试点**:
|
||
- 编译过程容错性(跳过无效规则)
|
||
- 边界情况处理(空/缺失值)
|
||
|
||
### 2. TestApplyCompiledRulesToPaths (5 个测试)
|
||
测试应用已编译的正则规则到路径
|
||
|
||
- ✅ `test_apply_single_rule` - 应用单个规则
|
||
- ✅ `test_apply_multiple_rules_in_order` - 按顺序应用多个规则
|
||
- ✅ `test_apply_no_rules` - 没有规则时返回原路径
|
||
- ✅ `test_apply_no_match` - 规则不匹配时保持原路径
|
||
- ✅ `test_apply_partial_match` - 部分路径匹配
|
||
|
||
**关键测试点**:
|
||
- 规则顺序执行
|
||
- 链式替换(第一个规则的输出作为第二个规则的输入)
|
||
- 部分匹配处理
|
||
|
||
### 3. TestApplyRegexRulesToPaths (17 个测试)
|
||
测试完整的路径正则替换流程
|
||
|
||
#### 基础替换
|
||
- ✅ `test_simple_replacement` - 简单字符串替换
|
||
- ✅ `test_windows_path_replacement` - Windows 路径替换
|
||
- ✅ `test_unc_path_replacement` - UNC 网络路径替换
|
||
|
||
#### 高级正则功能
|
||
- ✅ `test_case_sensitive_replacement` - 大小写敏感替换
|
||
- ✅ `test_case_insensitive_replacement` - 大小写不敏感替换(`(?i)` 标志)
|
||
- ✅ `test_regex_special_characters` - 正则特殊字符处理
|
||
- ✅ `test_capture_group_replacement` - 捕获组替换 `\1`
|
||
- ✅ `test_multiple_capture_groups` - 多个捕获组交换位置
|
||
|
||
#### 实用场景
|
||
- ✅ `test_delete_pattern` - 删除匹配内容(替换为空)
|
||
- ✅ `test_multiple_matches_in_path` - 路径中多次匹配
|
||
- ✅ `test_chained_replacements` - 链式替换(NAS 路径转换)
|
||
- ✅ `test_url_encoding_path` - URL 编码路径处理
|
||
- ✅ `test_unicode_path` - Unicode 路径支持
|
||
|
||
#### 边界情况
|
||
- ✅ `test_empty_rules_list` - 空规则列表
|
||
- ✅ `test_empty_paths_list` - 空路径列表
|
||
|
||
**关键测试点**:
|
||
- 各种路径格式(Windows、Linux、UNC、URL 编码)
|
||
- 正则高级特性(捕获组、标志)
|
||
- 国际化支持(Unicode)
|
||
|
||
### 4. TestPreprocessPlaylistText (7 个测试)
|
||
测试预处理播放列表文本(含正则替换)
|
||
|
||
- ✅ `test_preprocess_with_replacements` - 带替换的预处理
|
||
- ✅ `test_preprocess_removes_comments` - 移除注释
|
||
- ✅ `test_preprocess_empty_text` - 空文本处理
|
||
- ✅ `test_preprocess_with_blank_lines` - 处理空行
|
||
- ✅ `test_preprocess_real_world_scenario` - **真实场景:NAS 路径转换**
|
||
- ✅ `test_preprocess_with_compiled_rules` - 使用预编译规则
|
||
- ✅ `test_preprocess_preserves_order` - 保持顺序
|
||
|
||
**关键测试点**:
|
||
- 完整的播放列表处理流程
|
||
- 注释和空行过滤
|
||
- 真实使用场景验证
|
||
|
||
**真实场景示例**:
|
||
```python
|
||
# 输入
|
||
\\koha9-nas\koha9-nas\Music\Rock\track.flac
|
||
/music/cache/temp.flac
|
||
|
||
# 规则
|
||
1. \\koha9-nas\koha9-nas\Music → N:\Music
|
||
2. /music/cache/ → /data/music/
|
||
3. \ → /
|
||
|
||
# 输出
|
||
N:/Music/Rock/track.flac
|
||
/data/music/temp.flac
|
||
```
|
||
|
||
### 5. TestEdgeCases (9 个测试)
|
||
测试边界情况和异常场景
|
||
|
||
- ✅ `test_very_long_path` - 超长路径(1000+ 字符)
|
||
- ✅ `test_special_characters_in_path` - 特殊字符 `[]()&#`
|
||
- ✅ `test_dot_in_path` - 相对路径符号 `../` `./`
|
||
- ✅ `test_trailing_slash` - 尾部斜杠处理
|
||
- ✅ `test_duplicate_slashes` - 重复斜杠 `//` `///`
|
||
- ✅ `test_mixed_path_separators` - 混合路径分隔符 `\` `/`
|
||
- ✅ `test_regex_metacharacters_in_replacement` - 替换字符串中的元字符
|
||
- ✅ `test_empty_string_replacement` - 替换为空字符串
|
||
- ✅ `test_replacement_creates_invalid_path` - 可能产生无效路径
|
||
|
||
**关键测试点**:
|
||
- 极端输入处理
|
||
- 路径规范化场景
|
||
- 错误容忍性
|
||
|
||
### 6. TestPerformance (3 个测试)
|
||
测试性能相关场景
|
||
|
||
- ✅ `test_large_playlist` - 大型播放列表(10,000 首歌曲)
|
||
- ✅ `test_many_rules` - 大量规则(5+ 个规则链式执行)
|
||
- ✅ `test_complex_regex_pattern` - 复杂正则表达式
|
||
|
||
**性能示例**:
|
||
```python
|
||
# 复杂正则模式
|
||
Pattern: /music/(.+?) - (.+?) \((\d+)\) \[([^\]]+)\]/
|
||
Input: /music/Artist - Album (2024) [FLAC]/01. Track.flac
|
||
Output: /library/FLAC/2024/Artist/Album/01. Track.flac
|
||
```
|
||
|
||
**关键测试点**:
|
||
- 大数据量处理能力
|
||
- 复杂模式匹配性能
|
||
- 规则链执行效率
|
||
|
||
## 🔍 覆盖的功能点
|
||
|
||
### 核心功能
|
||
- ✅ 正则规则编译和验证
|
||
- ✅ 规则按顺序应用到路径
|
||
- ✅ 播放列表文本预处理
|
||
- ✅ 捕获组和反向引用
|
||
- ✅ 大小写敏感/不敏感匹配
|
||
|
||
### 路径类型
|
||
- ✅ Linux/Unix 绝对路径 `/path/to/file`
|
||
- ✅ Windows 绝对路径 `C:\path\to\file`
|
||
- ✅ UNC 网络路径 `\\server\share\file`
|
||
- ✅ 相对路径 `../path/./file`
|
||
- ✅ URL 编码路径 `/artist%20name/track.mp3`
|
||
- ✅ Unicode 路径 `/音乐/专辑/歌曲.mp3`
|
||
|
||
### 正则特性
|
||
- ✅ 简单字符串匹配
|
||
- ✅ 特殊字符转义 `()[].*+?`
|
||
- ✅ 捕获组 `(pattern)` 和引用 `\1`
|
||
- ✅ 不区分大小写 `(?i)`
|
||
- ✅ 量词 `*+?{n}`
|
||
- ✅ 字符类 `[^/]+` `\d+` `\w+`
|
||
|
||
### 边界情况
|
||
- ✅ 空输入(规则/路径)
|
||
- ✅ 无效正则表达式
|
||
- ✅ 不匹配的规则
|
||
- ✅ 超长路径
|
||
- ✅ 特殊字符
|
||
- ✅ 链式替换
|
||
|
||
### 容错性
|
||
- ✅ 跳过空模式
|
||
- ✅ 跳过无效正则
|
||
- ✅ 默认替换为空字符串
|
||
- ✅ 保留不匹配的路径
|
||
|
||
## 🎓 测试用例示例
|
||
|
||
### 基础替换
|
||
```python
|
||
paths = ["/old/path/file.mp3"]
|
||
rules = [{"pattern": r"/old/", "replacement": "/new/"}]
|
||
# 结果: ["/new/path/file.mp3"]
|
||
```
|
||
|
||
### 捕获组替换
|
||
```python
|
||
paths = ["/music/2024/album/track.mp3"]
|
||
rules = [{"pattern": r"/music/(\d+)/", "replacement": r"/archive/\1/"}]
|
||
# 结果: ["/archive/2024/album/track.mp3"]
|
||
```
|
||
|
||
### 链式替换(真实场景)
|
||
```python
|
||
paths = [r"\\nas\Music\Album\track.mp3"]
|
||
rules = [
|
||
{"pattern": r"\\\\nas\\Music", "replacement": "/mnt/music"},
|
||
{"pattern": r"\\", "replacement": "/"},
|
||
]
|
||
# 结果: ["/mnt/music/Album/track.mp3"]
|
||
```
|
||
|
||
### 复杂模式匹配
|
||
```python
|
||
paths = ["/music/Artist - Album (2024) [FLAC]/01. Track.flac"]
|
||
rules = [
|
||
{
|
||
"pattern": r"/music/(.+?) - (.+?) \((\d+)\) \[([^\]]+)\]/",
|
||
"replacement": r"/library/\4/\3/\1/\2/"
|
||
}
|
||
]
|
||
# 结果: ["/library/FLAC/2024/Artist/Album/01. Track.flac"]
|
||
```
|
||
|
||
## 🚀 运行测试
|
||
|
||
### 运行正则替换测试
|
||
```bash
|
||
pytest tests/test_regex_path_replacement.py -v
|
||
```
|
||
|
||
### 运行特定测试类
|
||
```bash
|
||
pytest tests/test_regex_path_replacement.py::TestApplyRegexRulesToPaths -v
|
||
```
|
||
|
||
### 运行特定测试
|
||
```bash
|
||
pytest tests/test_regex_path_replacement.py::TestPreprocessPlaylistText::test_preprocess_real_world_scenario -v
|
||
```
|
||
|
||
### 查看测试覆盖率
|
||
```bash
|
||
pytest tests/test_regex_path_replacement.py --cov=app.utils.playlist_merge --cov-report=term
|
||
```
|
||
|
||
## 💡 测试最佳实践
|
||
|
||
本测试套件遵循的最佳实践:
|
||
|
||
1. **分类清晰** - 按功能层级组织测试类
|
||
2. **命名规范** - 测试名称清楚描述测试内容
|
||
3. **独立性** - 每个测试独立运行,无依赖
|
||
4. **覆盖全面** - 正常流程、边界情况、错误处理全覆盖
|
||
5. **文档化** - 每个测试都有描述性文档字符串
|
||
6. **真实场景** - 包含实际使用场景的测试用例
|
||
7. **性能考虑** - 包含大数据量和复杂模式的性能测试
|
||
|
||
## 📈 测试价值
|
||
|
||
这套测试为正则路径替换功能提供了:
|
||
|
||
- **信心保证** - 46 个测试覆盖各种场景
|
||
- **回归防护** - 修改代码时快速验证功能完整性
|
||
- **文档作用** - 测试即使用示例和功能文档
|
||
- **重构支持** - 安全重构代码而不破坏功能
|
||
- **Bug 预防** - 边界情况测试防止潜在 Bug
|
||
|
||
## 🔧 维护建议
|
||
|
||
1. **添加新功能时**同步添加测试
|
||
2. **发现 Bug 时**先写失败测试,再修复
|
||
3. **定期运行**完整测试套件
|
||
4. **保持测试更新**与代码变更同步
|
||
5. **关注覆盖率**保持 80% 以上
|
||
|
||
## 相关文件
|
||
|
||
- 测试文件: `tests/test_regex_path_replacement.py`
|
||
- 被测代码: `app/utils/playlist_merge.py`
|
||
- 相关函数:
|
||
- `_compile_regex_rules()`
|
||
- `_apply_compiled_rules_to_paths()`
|
||
- `apply_regex_rules_to_paths()`
|
||
- `preprocess_playlist_text()`
|