|
20 | 20 | import pytest |
21 | 21 | import requests_mock |
22 | 22 |
|
23 | | -from pdftables_api import APIException, Client |
| 23 | +from pdftables_api import ( |
| 24 | + Client, |
| 25 | + APIException, |
| 26 | + EXTRACTOR_STANDARD, |
| 27 | + EXTRACTOR_AI_1, |
| 28 | + EXTRACTOR_AI_2, |
| 29 | + EXTRACT_TABLES, |
| 30 | + EXTRACT_TABLES_PARAGRAPHS, |
| 31 | +) |
24 | 32 |
|
25 | 33 |
|
26 | 34 | class TestEnsureExtFormat(TestCase): |
@@ -181,6 +189,129 @@ def test_response_unknown_file_format(self): |
181 | 189 | c.dump(png_fo) |
182 | 190 |
|
183 | 191 |
|
| 192 | +class TestExtractorParameters(TestCase): |
| 193 | + def test_default_extractor(self): |
| 194 | + """Test that default extractor is 'standard' with no extract parameter.""" |
| 195 | + with requests_mock.mock() as m: |
| 196 | + m.post( |
| 197 | + "https://pdftables.com/api?key=fake_key&format=xlsx-multiple&extractor=standard", |
| 198 | + text="xlsx output", |
| 199 | + ) |
| 200 | + |
| 201 | + c = Client("fake_key") |
| 202 | + with NamedTemporaryFile(suffix="test.pdf") as tf: |
| 203 | + tf.write(b"Hello world") |
| 204 | + tf.file.close() |
| 205 | + c.convert(tf.name) |
| 206 | + |
| 207 | + def test_ai_extractor_with_tables(self): |
| 208 | + """Test AI extractor with 'tables' extract parameter.""" |
| 209 | + with requests_mock.mock() as m: |
| 210 | + m.post( |
| 211 | + "https://pdftables.com/api?key=fake_key&format=xlsx-multiple&extractor=ai-1&extract=tables", |
| 212 | + text="xlsx output", |
| 213 | + ) |
| 214 | + |
| 215 | + c = Client("fake_key", extractor=EXTRACTOR_AI_1, extract=EXTRACT_TABLES) |
| 216 | + with NamedTemporaryFile(suffix="test.pdf") as tf: |
| 217 | + tf.write(b"Hello world") |
| 218 | + tf.file.close() |
| 219 | + c.convert(tf.name) |
| 220 | + |
| 221 | + def test_ai_extractor_with_tables_paragraphs(self): |
| 222 | + """Test AI extractor with 'tables-paragraphs' extract parameter.""" |
| 223 | + with requests_mock.mock() as m: |
| 224 | + m.post( |
| 225 | + "https://pdftables.com/api?key=fake_key&format=csv&extractor=ai-1&extract=tables-paragraphs", |
| 226 | + text="csv output", |
| 227 | + ) |
| 228 | + |
| 229 | + c = Client( |
| 230 | + "fake_key", extractor=EXTRACTOR_AI_1, extract=EXTRACT_TABLES_PARAGRAPHS |
| 231 | + ) |
| 232 | + with NamedTemporaryFile(suffix="test.pdf") as tf: |
| 233 | + tf.write(b"Hello world") |
| 234 | + tf.file.close() |
| 235 | + c.convert(tf.name, out_format="csv") |
| 236 | + |
| 237 | + def test_ai2_extractor_with_tables(self): |
| 238 | + """Test AI-2 extractor with 'tables' extract parameter.""" |
| 239 | + with requests_mock.mock() as m: |
| 240 | + m.post( |
| 241 | + "https://pdftables.com/api?key=fake_key&format=xlsx-multiple&extractor=ai-2&extract=tables", |
| 242 | + text="xlsx output", |
| 243 | + ) |
| 244 | + |
| 245 | + c = Client("fake_key", extractor=EXTRACTOR_AI_2, extract=EXTRACT_TABLES) |
| 246 | + with NamedTemporaryFile(suffix="test.pdf") as tf: |
| 247 | + tf.write(b"Hello world") |
| 248 | + tf.file.close() |
| 249 | + c.convert(tf.name) |
| 250 | + |
| 251 | + def test_ai2_extractor_with_tables_paragraphs(self): |
| 252 | + """Test AI-2 extractor with 'tables-paragraphs' extract parameter.""" |
| 253 | + with requests_mock.mock() as m: |
| 254 | + m.post( |
| 255 | + "https://pdftables.com/api?key=fake_key&format=csv&extractor=ai-2&extract=tables-paragraphs", |
| 256 | + text="csv output", |
| 257 | + ) |
| 258 | + |
| 259 | + c = Client( |
| 260 | + "fake_key", extractor=EXTRACTOR_AI_2, extract=EXTRACT_TABLES_PARAGRAPHS |
| 261 | + ) |
| 262 | + with NamedTemporaryFile(suffix="test.pdf") as tf: |
| 263 | + tf.write(b"Hello world") |
| 264 | + tf.file.close() |
| 265 | + c.convert(tf.name, out_format="csv") |
| 266 | + |
| 267 | + def test_standard_extractor_no_extract_param_in_url(self): |
| 268 | + """Test that standard extractor doesn't include extract parameter in URL.""" |
| 269 | + with requests_mock.mock() as m: |
| 270 | + # Note: no 'extract' parameter in the URL for standard extractor |
| 271 | + m.post( |
| 272 | + "https://pdftables.com/api?key=fake_key&format=csv&extractor=standard", |
| 273 | + text="csv output", |
| 274 | + ) |
| 275 | + |
| 276 | + c = Client("fake_key", extractor=EXTRACTOR_STANDARD, extract=None) |
| 277 | + with NamedTemporaryFile(suffix="test.pdf") as tf: |
| 278 | + tf.write(b"Hello world") |
| 279 | + tf.file.close() |
| 280 | + c.convert(tf.name, out_format="csv") |
| 281 | + |
| 282 | + def test_invalid_extractor_raises_error(self): |
| 283 | + """Test that invalid extractor raises ValueError.""" |
| 284 | + with pytest.raises( |
| 285 | + ValueError, |
| 286 | + match='^Invalid extractor "invalid". Valid options are: standard, ai-1, ai-2$', |
| 287 | + ): |
| 288 | + Client("fake_key", extractor="invalid") |
| 289 | + |
| 290 | + def test_invalid_extract_for_standard_raises_error(self): |
| 291 | + """Test that providing extract parameter for standard extractor raises ValueError.""" |
| 292 | + with pytest.raises( |
| 293 | + ValueError, |
| 294 | + match='^Extractor "standard" does not support extract parameter$', |
| 295 | + ): |
| 296 | + Client("fake_key", extractor=EXTRACTOR_STANDARD, extract=EXTRACT_TABLES) |
| 297 | + |
| 298 | + def test_invalid_extract_for_ai_raises_error(self): |
| 299 | + """Test that invalid extract value for AI extractor raises ValueError.""" |
| 300 | + with pytest.raises( |
| 301 | + ValueError, |
| 302 | + match='^Invalid extract value "invalid" for extractor "ai-1". Valid values are: tables, tables-paragraphs$', |
| 303 | + ): |
| 304 | + Client("fake_key", extractor=EXTRACTOR_AI_1, extract="invalid") |
| 305 | + |
| 306 | + def test_invalid_extract_for_ai2_raises_error(self): |
| 307 | + """Test that invalid extract value for AI-2 extractor raises ValueError.""" |
| 308 | + with pytest.raises( |
| 309 | + ValueError, |
| 310 | + match='^Invalid extract value "invalid" for extractor "ai-2". Valid values are: tables, tables-paragraphs$', |
| 311 | + ): |
| 312 | + Client("fake_key", extractor=EXTRACTOR_AI_2, extract="invalid") |
| 313 | + |
| 314 | + |
184 | 315 | def consume(s): |
185 | 316 | r = b"" |
186 | 317 | for chunk in s: |
|
0 commit comments