# 🎯 正则路径替换测试 - 快速参考 ## ✅ 测试状态 ``` ✅ 59/59 测试通过 ⚡ 执行时间: 0.56s 📦 包含: 13 个合并测试 + 46 个正则测试 ``` ## 🚀 快速运行 ```bash # 运行所有测试 pytest tests/ # 只运行正则测试 pytest tests/test_regex_path_replacement.py -v # 运行特定测试类 pytest tests/test_regex_path_replacement.py::TestApplyRegexRulesToPaths -v # 查看覆盖率 pytest tests/test_regex_path_replacement.py --cov=app.utils.playlist_merge ``` ## 📋 测试分类 | 测试类 | 数量 | 说明 | |--------|------|------| | TestCompileRegexRules | 7 | 正则编译和验证 | | TestApplyCompiledRulesToPaths | 5 | 应用已编译规则 | | TestApplyRegexRulesToPaths | 17 | 完整替换流程 | | TestPreprocessPlaylistText | 7 | 播放列表预处理 | | TestEdgeCases | 9 | 边界情况处理 | | TestPerformance | 3 | 性能测试 | ## 💡 常用示例 ### 简单替换 ```python rules = [{"pattern": r"/old/", "replacement": "/new/"}] "/old/music/track.mp3" → "/new/music/track.mp3" ``` ### Windows 路径 ```python rules = [{"pattern": r"C:\\Music", "replacement": r"D:\\Audio"}] r"C:\Music\track.mp3" → r"D:\Audio\track.mp3" ``` ### 捕获组 ```python rules = [{"pattern": r"/(\d+)/", "replacement": r"/year-\1/"}] "/music/2024/track.mp3" → "/music/year-2024/track.mp3" ``` ### NAS 路径转换(真实场景) ```python rules = [ {"pattern": r"\\\\nas\\Music", "replacement": r"N:\\Music"}, {"pattern": r"\\", "replacement": "/"}, ] r"\\nas\Music\Album\track.mp3" → "N:/Music/Album/track.mp3" ``` ## 🎨 测试覆盖的场景 ### ✅ 路径类型 - Linux 路径 `/path/to/file` - Windows 路径 `C:\path\to\file` - UNC 路径 `\\server\share\file` - 相对路径 `../path/./file` - URL 编码 `/artist%20name/track.mp3` - Unicode `/音乐/歌曲.mp3` ### ✅ 正则特性 - 简单匹配 `foo` - 特殊字符 `\(\d+\)` - 捕获组 `(pattern)` → `\1` - 不区分大小写 `(?i)pattern` - 字符类 `[A-Z]+` `\d+` `\w+` ### ✅ 边界情况 - 空输入(规则/路径) - 无效正则表达式 - 超长路径 (1000+ 字符) - 特殊字符 `[]()&#` - 链式替换 ### ✅ 性能测试 - 10,000 首歌曲的播放列表 - 5+ 条规则链式执行 - 复杂正则模式匹配 ## 📖 相关文档 - 详细总结: `tests/REGEX_TESTS_SUMMARY.md` - 测试文件: `tests/test_regex_path_replacement.py` - 被测代码: `app/utils/playlist_merge.py` ## 🔍 调试技巧 ```bash # 显示详细输出 pytest tests/test_regex_path_replacement.py -v -s # 遇到第一个失败就停止 pytest tests/test_regex_path_replacement.py -x # 进入调试器 pytest tests/test_regex_path_replacement.py --pdb # 只运行失败的测试 pytest tests/test_regex_path_replacement.py --lf ``` ## 🎓 测试即文档 每个测试都是一个使用示例,查看测试代码了解如何使用正则替换功能! ```python # 示例: 查看如何使用捕获组 def test_capture_group_replacement(): paths = ["/music/2024/album/track.mp3"] rules = [{"pattern": r"/music/(\d+)/", "replacement": r"/archive/\1/"}] result = apply_regex_rules_to_paths(paths, rules) assert result == ["/archive/2024/album/track.mp3"] ```