Skip to content

Commit 2008717

Browse files
committed
fix:extract table from markdown and test
1 parent fabb37d commit 2008717

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

tests/test_table_extraction.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ def test_html_table_extraction(self):
8080
result = self.metric._extract_from_markdown(text)
8181

8282
# 验证HTML表格被提取
83-
self.assertIn('<table>', result['table'])
84-
self.assertIn('<tr>', result['table'])
85-
self.assertIn('标题1', result['table'])
83+
expected_table = """<table>
84+
<tr><th>标题1</th><th>标题2</th></tr>
85+
<tr><td>数据1</td><td>数据2</td></tr>
86+
</table>"""
87+
self.assertIn(expected_table, result['table'])
8688

8789
# 验证文本中HTML表格被移除
8890
self.assertNotIn('<table>', result['text'])
@@ -102,9 +104,12 @@ def test_complex_markdown_table(self):
102104
result = self.metric._extract_from_markdown(text)
103105

104106
# 验证复杂表格被完整提取
105-
self.assertIn('| 姓名 | 年龄 | 职业 | 薪资 |', result['table'])
106-
self.assertIn('|:-----|:----:|-----:|------|', result['table'])
107-
self.assertIn('| 张三 | 25 | 工程师 | 15k |', result['table'])
107+
expected_table = """| 姓名 | 年龄 | 职业 | 薪资 |
108+
|:-----|:----:|-----:|------|
109+
| 张三 | 25 | 工程师 | 15k |
110+
| 李四 | 30 | 设计师 | 18k |
111+
| 王五 | 28 | 产品经理 | 20k |"""
112+
self.assertIn(expected_table, result['table'])
108113

109114
# 验证文本中表格被移除
110115
self.assertNotIn('| 姓名 | 年龄 | 职业 | 薪资 |', result['text'])
@@ -121,8 +126,10 @@ def test_table_with_alignment(self):
121126
result = self.metric._extract_from_markdown(text)
122127

123128
# 验证对齐表格被提取
124-
self.assertIn('| 左对齐 | 居中 | 右对齐 |', result['table'])
125-
self.assertIn('|:-------|:----:|-------:|', result['table'])
129+
expected_table = """| 左对齐 | 居中 | 右对齐 |
130+
|:-------|:----:|-------:|
131+
| 内容1 | 内容2 | 内容3 |"""
132+
self.assertIn(expected_table, result['table'])
126133

127134
def test_invalid_table_ignored(self):
128135
"""测试无效表格被忽略"""
@@ -148,7 +155,10 @@ def test_table_with_escaped_pipes(self):
148155
result = self.metric._extract_from_markdown(text)
149156

150157
# 验证包含转义管道的表格被提取
151-
self.assertIn('| 列1 | 列2 \\| 列3 | 列4 |', result['table'])
158+
expected_table = """| 列1 | 列2 \\| 列3 | 列4 |
159+
|-----|-----|-----|
160+
| 数据1 | 数据2 | 数据3 |"""
161+
self.assertIn(expected_table, result['table'])
152162

153163
def test_table_at_document_end(self):
154164
"""测试文档末尾的表格"""
@@ -160,9 +170,10 @@ def test_table_at_document_end(self):
160170
result = self.metric._extract_from_markdown(text)
161171

162172
# 验证文档末尾的表格被提取
163-
self.assertIn('| 列1 | 列2 |', result['table'])
164-
self.assertIn('|-----|-----|', result['table'])
165-
self.assertIn('| 数据1 | 数据2 |', result['table'])
173+
expected_table = """| 列1 | 列2 |
174+
|-----|-----|
175+
| 数据1 | 数据2 |"""
176+
self.assertIn(expected_table, result['table'])
166177

167178

168179

@@ -189,10 +200,11 @@ def test_table_with_complex_content(self):
189200
result = self.metric._extract_from_markdown(text)
190201

191202
# 验证复杂内容表格被提取
192-
self.assertIn('| 列1 | 列2 | 列3 |', result['table'])
193-
self.assertIn('包含**粗体**', result['table'])
194-
self.assertIn('包含`代码`', result['table'])
195-
self.assertIn('包含$公式$', result['table'])
203+
expected_table = """| 列1 | 列2 | 列3 |
204+
|-----|-----|-----|
205+
| 包含**粗体** | 包含`代码` | 包含[链接](url) |
206+
| 包含*斜体* | 包含$公式$ | 包含>引用 |"""
207+
self.assertIn(expected_table, result['table'])
196208

197209
def test_nested_html_tables(self):
198210
"""测试嵌套HTML表格"""
@@ -207,11 +219,20 @@ def test_nested_html_tables(self):
207219
</table>"""
208220

209221
result = self.metric._extract_from_markdown(text)
210-
222+
print("result['table']",result['table'])
211223
# 验证嵌套表格被完整提取
212-
self.assertIn('<table>', result['table'])
213-
self.assertIn('外层表格', result['table'])
214-
self.assertIn('内层表格', result['table'])
224+
expected_table = """<table>
225+
<tr><td>外层表格</td></tr>
226+
<tr><td>
227+
<table>
228+
<tr><td>内层表格</td></tr>
229+
</table>
230+
</td></tr>
231+
</table>
232+
<table>
233+
<tr><td>内层表格</td></tr>
234+
</table>"""
235+
self.assertIn(expected_table, result['table'])
215236

216237

217238
if __name__ == '__main__':

0 commit comments

Comments
 (0)