"""测试 XLSX Reader 的解析功能。""" import pytest import os from readers.xlsx import XlsxReader class TestPandasXlsxReaderParse: """测试 XLSX Reader 的 parse 方法。""" def test_normal_file(self, temp_xlsx): """测试正常 XLSX 文件解析。""" # 创建包含数据的测试文件 file_path = temp_xlsx(data=[ ["列1", "列2", "列3"], ["数据1", "数据2", "数据3"], ["测试A", "测试B", "测试C"], ]) reader = XlsxReader() content, failures = reader.parse(file_path) # 验证解析成功 assert content is not None, f"解析失败: {failures}" assert len(failures) == 0 or all("成功" in f or not f for f in failures) # 验证关键内容存在 assert "列1" in content or "列2" in content assert "数据1" in content or "数据2" in content assert "测试A" in content or "测试B" in content def test_file_not_exists(self, tmp_path): """测试文件不存在的情况。""" non_existent_file = str(tmp_path / "non_existent.xlsx") reader = XlsxReader() content, failures = reader.parse(non_existent_file) # 验证返回 None 和错误信息 assert content is None assert len(failures) > 0 assert any("不存在" in f or "找不到" in f for f in failures) def test_empty_file(self, temp_xlsx): """测试空 XLSX 文件。""" # 创建没有任何内容的文件 file_path = temp_xlsx() reader = XlsxReader() content, failures = reader.parse(file_path) # 空文件可能返回 None、空字符串或只包含表格结构 assert content is None or len(content.strip()) < 50 # 允许有基本的表格结构 def test_corrupted_file(self, temp_xlsx, tmp_path): """测试损坏的 XLSX 文件。""" # 先创建正常文件 file_path = temp_xlsx(data=[["测试", "内容"]]) # 破坏文件内容 - 完全覆盖文件 with open(file_path, "wb") as f: f.write(b"corrupted content that is not a valid xlsx file") reader = XlsxReader() content, failures = reader.parse(file_path) # 验证返回 None 和错误信息 assert content is None assert len(failures) > 0 def test_special_chars(self, temp_xlsx): """测试特殊字符处理。""" special_data = [ ["中文", "Emoji😀", "特殊符号©"], ["测试内容", "🎉🚀", "®™°±"], ["Hello你好", "World世界", "混合内容"], ] file_path = temp_xlsx(data=special_data) reader = XlsxReader() content, failures = reader.parse(file_path) assert content is not None, f"解析失败: {failures}" # 验证各种特殊字符都被正确处理 assert "中文" in content assert "😀" in content or "🎉" in content # 至少包含一个 emoji assert "©" in content or "®" in content # 至少包含一个特殊符号 assert "Hello" in content or "World" in content class TestPandasXlsxReaderSupports: """测试 XLSX Reader 的 supports 方法。""" def test_supports_xlsx_extension(self): """测试识别 .xlsx 扩展名。""" reader = XlsxReader() assert reader.supports("test.xlsx") is True def test_supports_uppercase_extension(self): """测试识别大写扩展名。""" reader = XlsxReader() assert reader.supports("TEST.XLSX") is True def test_rejects_unsupported_format(self): """测试拒绝不支持的格式。""" reader = XlsxReader() assert reader.supports("test.pdf") is False assert reader.supports("test.txt") is False def test_supports_path_with_spaces(self): """测试包含空格的路径。""" reader = XlsxReader() assert reader.supports("path with spaces/test.xlsx") is True def test_supports_absolute_path(self): """测试绝对路径。""" reader = XlsxReader() assert reader.supports("/absolute/path/test.xlsx") is True assert reader.supports("C:\\Windows\\path\\test.xlsx") is True