-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathtest_app.py
More file actions
1184 lines (997 loc) · 39.9 KB
/
test_app.py
File metadata and controls
1184 lines (997 loc) · 39.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import io
import os
import tempfile
import uuid
from pathlib import Path
from unittest.mock import ANY, Mock
import pandas as pd
import pytest
import requests
from fastapi import HTTPException
from fastapi.testclient import TestClient
from pypdf import PdfReader, PdfWriter
from prepline_general.api import general
from prepline_general.api.app import app
MAIN_API_ROUTE = "general/v0/general"
def test_general_api_health_check():
client = TestClient(app)
response = client.get("/healthcheck")
assert response.status_code == 200
@pytest.mark.parametrize(
"example_filename, content_type",
[
# Note(yuming): Please sort filetypes alphabetically according to
# https://github.com/Unstructured-IO/unstructured/blob/main/unstructured/partition/auto.py#L14
("stanley-cups.csv", "application/csv"),
("fake.doc", "application/msword"),
("fake.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
("family-day.eml", "message/rfc822"),
("alert.eml", "message/rfc822"),
("announcement.eml", "message/rfc822"),
("fake-email-attachment.eml", "message/rfc822"),
("fake-email-image-embedded.eml", "message/rfc822"),
("fake-email.eml", "message/rfc822"),
# After https://github.com/Unstructured-IO/unstructured-api/pull/487 updated Starlette
# to resolve a vulnerability, these unit tests fail with:
# AttributeError: 'SpooledTemporaryFile' object has no attribute 'seekable'
# These files pass the smoke test that runs against Docker, so assume there's no regression.
# ("winter-sports.epub", "application/epub"),
# ("fake.odt", "application/vnd.oasis.opendocument.text"),
("fake-html.html", "text/html"),
("layout-parser-paper-fast.jpg", "image/jpeg"),
("spring-weather.html.json", "application/json"),
("README.md", "text/markdown"),
("fake-email.msg", "application/x-ole-storage"),
("layout-parser-paper.pdf", "application/pdf"),
("fake-power-point.ppt", "application/vnd.ms-powerpoint"),
(
"fake-power-point.pptx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
),
("README.rst", "text/x-rst"),
("fake-doc.rtf", "application/rtf"),
("fake-text.txt", "text/plain"),
("stanley-cups.tsv", "text/tsv"),
(
"stanley-cups.xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
),
("fake-xml.xml", "application/xml"),
],
)
def test_general_api(example_filename, content_type):
client = TestClient(app)
# Ensure files are properly closed
test_file_path = str(Path("sample-docs") / example_filename)
with open(test_file_path, "rb") as f:
response = client.post(MAIN_API_ROUTE, files=[("files", (test_file_path, f, content_type))])
assert response.status_code == 200
assert len(response.json()) > 0
for i in response.json():
assert i["metadata"]["filename"] == example_filename
assert len("".join(elem["text"] for elem in response.json())) > 20
# Just hit the second path (posting multiple files) to bump the coverage
# We'll come back and make smarter tests
with open(test_file_path, "rb") as f, open(test_file_path, "rb") as g:
response = client.post(
MAIN_API_ROUTE,
files=[
("files", (str(test_file_path), f, content_type)),
("files", (str(test_file_path), g, content_type)),
],
)
assert response.status_code == 200
assert all(x["metadata"]["filename"] == example_filename for i in response.json() for x in i)
assert len(response.json()) > 0
with open(test_file_path, "rb") as f, open(test_file_path, "rb") as g:
csv_response = client.post(
MAIN_API_ROUTE,
files=[
("files", (str(test_file_path), f, content_type)),
("files", (str(test_file_path), g, content_type)),
],
data={"output_format": "text/csv"},
)
assert csv_response.status_code == 200
dfs = pd.read_csv(io.StringIO(csv_response.text))
assert len(dfs) > 0
def test_metadata_fields_removed():
"""
Verify that responses do not include coordinates unless requested
Verify that certain other metadata fields are dropped
"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.jpg"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "hi_res"},
)
assert response.status_code == 200
response_without_coords = response.json()
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"coordinates": "true", "strategy": "hi_res"},
)
assert response.status_code == 200
response_with_coords = response.json()
# Each element should be the same except for the coordinates field
# Also, check for metadata fields we explicitly dropped
for i in range(len(response_with_coords)):
assert "coordinates" in response_with_coords[i]["metadata"]
del response_with_coords[i]["metadata"]["coordinates"]
assert response_with_coords[i] == response_without_coords[i]
assert "last_modified" not in response_without_coords[i]["metadata"]
assert "file_directory" not in response_without_coords[i]["metadata"]
assert "detection_class_prob" not in response_without_coords[i]["metadata"]
@pytest.mark.parametrize("ocr_languages", [["eng", "kor"], ["eng+kor"]])
def test_ocr_languages_param(ocr_languages): # will eventually be deprecated
"""
Verify that we get the corresponding languages from the response with ocr_languages
"""
client = TestClient(app)
test_file = Path("sample-docs") / "english-and-korean.png"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "ocr_only", "ocr_languages": ocr_languages},
)
assert response.status_code == 200
elements = response.json()
assert elements[3]["text"].startswith("안녕하세요, 저 희 는 YGEAS 그룹")
def test_languages_param():
"""
Verify that we get the corresponding languages from the response with `languages`
"""
client = TestClient(app)
test_file = Path("sample-docs") / "english-and-korean.png"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "ocr_only", "languages": ["eng", "kor"]},
)
assert response.status_code == 200
elements = response.json()
assert elements[3]["text"].startswith("안녕하세요, 저 희 는 YGEAS 그룹")
def test_skip_infer_table_types_param():
"""Verify that we extract table unless excluded by skip_infer_table_types"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-with-table.jpg"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
)
assert response.status_code == 200
# test we skip table extraction by default
elements = response.json()
table = [el["metadata"]["text_as_html"] for el in elements if "text_as_html" in el["metadata"]]
assert len(table) == 1
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"skip_infer_table_types": ["jpg"]},
)
assert response.status_code == 200
# test we specified to skip extraction for jpg
elements = response.json()
table = [el["metadata"]["text_as_html"] for el in elements if "text_as_html" in el["metadata"]]
assert len(table) == 0
# This text is not currently picked up
# assert "Layouts of history Japanese documents" in table[0]
def test_strategy_param_400():
"""Verify that we get a 400 if we pass in a bad strategy"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "text/plain"))],
data={"strategy": "not_a_strategy"},
)
assert response.status_code == 422
def test_valid_encoding_param():
"""
Verify that we get a 200 for passing an encoding param
"""
client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "text/plain"))],
data={"encoding": "ascii"},
)
assert response.status_code == 200
def test_invalid_encoding_param():
"""
Verify that we get a 500 if we pass an invalid encoding through to partition
"""
client = TestClient(app)
test_file = Path("sample-docs") / "fake-html.html"
with pytest.raises(LookupError) as excinfo:
client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "text/plain"))],
data={"encoding": "not_an_encoding"},
)
assert "unknown encoding" in str(excinfo.value)
def test_api_with_different_encodings():
"""
Verify that we get different text results for different encodings
"""
client = TestClient(app)
test_file = Path("sample-docs") / "fake-text-utf-32.txt"
# utf-16
response_16 = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "text/plain"))],
data={"encoding": "utf-16"},
)
assert response_16.status_code == 200
elements_16 = response_16.json()
assert elements_16[0]["text"].startswith("\x00T\x00h\x00i\x00s\x00 \x00i\x00s\x00")
# utf-32
response_32 = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "text/plain"))],
data={"encoding": "utf-32"},
)
assert response_32.status_code == 200
elements_32 = response_32.json()
assert elements_32[2]["text"].startswith("Important points:")
# utf-8
with pytest.raises(UnicodeDecodeError) as excinfo:
client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "text/plain"))],
data={"encoding": "utf8"},
)
assert "invalid start byte" in str(excinfo.value)
def test_xml_keep_tags_param():
"""
Verify that responses do not include xml tags unless requested
"""
client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "hi_res"},
)
assert response.status_code == 200
response_without_xml_tags = response.json()
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"xml_keep_tags": "true", "strategy": "hi_res"},
)
assert response.status_code == 200
# xml_keep_tags returns one element with the full xml
# Just assert the tags are still present
response_with_xml_tags = response.json()[0]
for element in response_without_xml_tags:
assert element["text"].replace("&", "&") in response_with_xml_tags["text"]
def test_element_ids_unique_and_deterministic_by_default():
client = TestClient(app)
# This xml file contains duplicate text elements
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={},
)
assert response.status_code == 200
elements = response.json()
ids = [element["element_id"] for element in elements]
# If there are duplicate ids in the ids list, the count of resulting
# set will be lower than the count of ids
assert len(ids) == len(set(ids)), "Elements do not have unique ids"
expected_hashes = [
"0e0e3ceecc272305ff14af4c2fbf8ef7",
"ceaa4718ddf40162aafdf8c3ed34814a",
"c0bae0d8252610f0feddeeca7651788b",
"d5040a3e459502598199a640aa5e59d2",
"74054df9eb33cdde45981d5c76e70c45",
"81d74d759dd7b7b05db708390e7eedb8",
"a2846f501cd00d941b61686dd983d643",
"f6a475f24979daba2b907814b6c1ede7",
"00a33894b23223160b3fb564fde7d7be",
"a9df034d5bfee8873453ccb027a27bd6",
"bb5322c12f0331a5bfb5ea1cda64fcbc",
"4fce9662ee90d3ab8083cb811f09ae28",
"1558d7a0135725499f96bf81abe271d9",
"d09458fa64f67d1849b81d9e4ed88a39",
"69a118fc97ebbd1d2545faaa91ee59db",
"eb5fde2ae8d3d84808c81852e64114c3",
"0966f9e480789093095d3c82492d9137",
"c5e820fb11c36d5a989ef118862f3077",
"fbf71c3fcc7e64987e1085fecf17abbb",
"cf664bf47d676872da9bea30384a2c5e",
]
assert ids == expected_hashes, "Element hashes are not deterministic"
def test_unique_element_ids_param():
"""
Verify that when requested, the element_ids are unique.
"""
client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={
"unique_element_ids": "True",
},
)
assert response.status_code == 200
elements = response.json()
ids = [element["element_id"] for element in elements]
# If all ids are unique, the count of resulting set
# will be same as the count of ids - which is expected here.
assert len(ids) == len(set(ids)), "Elements have non-unique ids"
try:
uuid.UUID(ids[0], version=4)
except ValueError:
raise AssertionError("Element ID is not in UUID format.")
def test_include_page_breaks_param():
"""
Verify that responses do not include page breaks unless requested
"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "fast"},
)
assert response.status_code == 200
response_without_page_breaks = response.json()
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"include_page_breaks": "true", "strategy": "fast"},
)
assert response.status_code == 200
response_with_page_breaks = response.json()
# The responses should have the same content except extra PageBreak objects
response_with_page_breaks_index, response_without_page_breaks_index = 0, 0
while response_with_page_breaks_index <= len(response_without_page_breaks):
curr_response_with_page_breaks_element = response_with_page_breaks[
response_with_page_breaks_index
]
curr_response_without_page_breaks_element = response_without_page_breaks[
response_without_page_breaks_index
]
if curr_response_with_page_breaks_element["type"] == "PageBreak":
assert curr_response_without_page_breaks_element["type"] != "PageBreak"
response_with_page_breaks_index += 1
else:
assert (
curr_response_without_page_breaks_element["text"]
== curr_response_with_page_breaks_element["text"]
)
response_with_page_breaks_index += 1
response_without_page_breaks_index += 1
last_response_with_page_breaks_element = response_with_page_breaks[
response_with_page_breaks_index
]
assert last_response_with_page_breaks_element["type"] == "PageBreak"
assert response_without_page_breaks[-1]["type"] != "PageBreak"
@pytest.mark.parametrize(
"extract_image_block_types",
[
'["Image", "Table"]',
["Image", "Table"],
],
)
def test_include_extract_image_block_types_param(extract_image_block_types):
"""
Verify that responses do not include base64 image in Table/Image metadata unless requested.
"""
client = TestClient(app)
test_file = Path("sample-docs") / "embedded-images-tables.pdf"
with open(test_file, "rb") as file:
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), file))],
data={"strategy": "hi_res"},
)
assert response.status_code == 200
response_without_image = response.json()
with open(test_file, "rb") as file:
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), file))],
data={"strategy": "hi_res", "extract_image_block_types": extract_image_block_types},
)
assert response.status_code == 200
response_with_image = response.json()
# Each element should be the same except for the image_base64 and image_mime_type fields
# in metadata
assert len(response_without_image) == len(response_with_image)
for element, element_with_image in zip(response_without_image, response_with_image):
if element["type"] in ["Image", "Table"]:
assert "image_base64" in element_with_image["metadata"]
assert "image_mime_type" in element_with_image["metadata"]
del element_with_image["metadata"]["image_base64"]
del element_with_image["metadata"]["image_mime_type"]
assert element == element_with_image
def test_general_api_returns_422_bad_pdf():
"""
Verify that we get a 422 for invalid PDF files
"""
tmp = tempfile.NamedTemporaryFile(suffix=".pdf")
tmp.write(b"This is not a valid PDF")
client = TestClient(app)
response = client.post(
MAIN_API_ROUTE, files=[("files", (str(tmp.name), open(tmp.name, "rb"), "application/pdf"))]
)
assert response.json() == {"detail": "File does not appear to be a valid PDF"}
assert response.status_code == 422
tmp.close()
# Don't blow up if this isn't actually a pdf
test_file = Path("sample-docs") / "fake-power-point.pptx"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
)
assert response.json() == {"detail": "File does not appear to be a valid PDF"}
assert response.status_code == 422
def test_general_api_returns_503(monkeypatch):
"""
When available memory is below the minimum. return a 503, unless our origin ip is 10.{4,5}.x.x
"""
monkeypatch.setenv("UNSTRUCTURED_MEMORY_FREE_MINIMUM_MB", "300000")
client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
)
assert response.status_code == 503
def test_general_api_returns_401(monkeypatch):
"""
When UNSTRUCTURED_API_KEY is set, return a 401 if the unstructured-api-key header does not match
"""
monkeypatch.setenv("UNSTRUCTURED_API_KEY", "foobar")
client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
headers={"unstructured-api-key": "foobar"},
)
assert response.status_code == 200
client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
headers={"unstructured-api-key": "helloworld"},
)
assert response.status_code == 401
class MockResponse:
def __init__(self, status_code):
self.status_code = status_code
self.body = {}
self.text = ""
def json(self):
return self.body
def call_api_using_test_client(
request_url: str,
api_key: str,
filename: str,
file,
content_type: str,
client: TestClient,
**partition_kwargs,
) -> str:
"""Exact copy of call_api from call_api.py, but with the test client parameter added."""
headers = {"unstructured-api-key": api_key}
response = client.post(
MAIN_API_ROUTE,
files={"files": (filename, file, content_type)},
data=partition_kwargs,
headers=headers,
)
if response.status_code != 200:
detail = response.json().get("detail") or response.text
raise HTTPException(status_code=response.status_code, detail=detail)
return response.text
def test_parallel_mode_preserves_uniqueness_of_hashes_when_assembling_pages_splits(monkeypatch):
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_URL", "unused")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_ENABLED", "true")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_SPLIT_SIZE", "1")
client = TestClient(app)
monkeypatch.setattr(
general,
"call_api",
lambda *args, **kwargs: call_api_using_test_client(*args, client=client, **kwargs),
)
# -- there are 3 pages identical pages in this pdf --
test_file = Path("sample-docs") / "DA-1p-with-duplicate-pages.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
data={"chunking_strategy": "by_title", "new_after_n_chars": 1000},
)
assert response.status_code == 200
elements = response.json()
texts = [element.get("text") for element in elements]
num_pages = 3
num_elements_per_page = len(elements) // num_pages
def get_texts_on_page(texts, page_num):
start = page_num * num_elements_per_page
end = start + num_elements_per_page
return texts[start:end]
pages = [get_texts_on_page(texts, idx) for idx in range(num_pages)]
assert all(page == pages[0] for page in pages), "Texts on all pages should be identical."
ids = [element.get("element_id") for element in elements]
assert len(set(ids)) == len(ids), "Element IDs across all pages should be unique."
def test_general_api_can_set_content_type():
"""Test that we can override the content type via header or form data param"""
client = TestClient(app)
example_filename = "family-day.eml"
test_file_path = str(Path("sample-docs") / example_filename)
# requests can override the content type in the header by using this tuple
with open(test_file_path, "rb") as f:
response = client.post(MAIN_API_ROUTE, files=[("files", (test_file_path, f, "text/plain"))])
assert response.status_code == 200
assert len(response.json()) > 0
for i in response.json():
assert i["metadata"]["filetype"] == "text/plain"
# We can also override the type via api param
with open(test_file_path, "rb") as f:
response = client.post(
MAIN_API_ROUTE,
files=[("files", f)],
data={"content_type": "text/plain"},
)
assert response.status_code == 200
assert len(response.json()) > 0
for i in response.json():
assert i["metadata"]["filetype"] == "text/plain"
def test_parallel_mode_passes_params(monkeypatch):
"""
Verify that parallel mode passes all params correctly into local partition.
If you add something to partition_kwargs, you need to explicitly test it here
with some non default value.
TODO - do the same test when params are sent back to the api
"""
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_ENABLED", "true")
# Make this really big so we just call partition
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_SPLIT_SIZE", "500")
mock_partition = Mock(return_value={})
monkeypatch.setattr(
general,
"partition",
mock_partition,
)
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper.pdf"
# For list params, send the formdata keys with brackets
# This is how Speakeasy sends them
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
data={
"encoding": "foo",
"hi_res_model_name": "yolox",
"include_page_breaks": "True",
"languages": "eng",
"pdf_infer_table_structure": "True",
"strategy": "hi_res",
"xml_keep_tags": "True",
"skip_infer_table_types[]": ["pdf"],
"extract_image_block_types[]": ["Image", "Table"],
"unique_element_ids": "True",
"starting_page_number": 1,
# -- chunking options --
"chunking_strategy": "by_title",
"combine_under_n_chars": "501",
"max_characters": "1502",
"multipage_sections": "False",
"new_after_n_chars": "1501",
"overlap": "25",
"overlap_all": "true",
"include_slide_notes": "true",
},
)
assert response.status_code == 200
mock_partition.assert_called_once_with(
file=ANY,
metadata_filename=str(test_file),
content_type="application/pdf",
hi_res_model_name="yolox",
encoding="foo",
include_page_breaks=True,
ocr_languages=None,
languages=["eng"],
# NOTE(robinson) - pdf_infer_table_structure is False because
# skip_infer_table_type=["pdf"] superceded pdf_infer_table_structure
pdf_infer_table_structure=False,
strategy="hi_res",
xml_keep_tags=True,
skip_infer_table_types=["pdf"],
extract_image_block_types=["Image", "Table"],
extract_image_block_to_payload=True, # Set to true because block_types is non empty
unique_element_ids=True,
starting_page_number=1,
# -- chunking options --
chunking_strategy="by_title",
combine_text_under_n_chars=501,
max_characters=1502,
multipage_sections=False,
new_after_n_chars=1501,
overlap=25,
overlap_all=True,
include_slide_notes=True,
)
def test_parallel_mode_returns_errors(monkeypatch):
"""
If we get an error sending a page to the api, bubble it up
"""
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_ENABLED", "true")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_URL", "unused")
monkeypatch.setattr(
requests,
"post",
lambda *args, **kwargs: MockResponse(status_code=500),
)
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
)
assert response.status_code == 500
monkeypatch.setattr(
requests,
"post",
lambda *args, **kwargs: MockResponse(status_code=400),
)
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
)
assert response.status_code == 400
def test_partition_file_via_api_will_retry(monkeypatch, mocker):
"""
Verify number of retries with parallel mode
"""
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_ENABLED", "true")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_URL", "unused")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_THREADS", "1")
num_calls = 0
# Validate the retry count by returning an error the first 2 times
def mock_response(*args, **kwargs):
nonlocal num_calls
num_calls += 1
if num_calls <= 2:
return MockResponse(status_code=500)
return MockResponse(status_code=200)
monkeypatch.setattr(
requests,
"post",
mock_response,
)
# This needs to be mocked when we return 200
mocker.patch("prepline_general.api.general.elements_from_json")
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
)
assert response.status_code == 200
def test_partition_file_via_api_not_retryable_error_code(monkeypatch, mocker):
"""
Verify we didn't retry if the error code is not retryable
"""
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_ENABLED", "true")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_URL", "unused")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_THREADS", "1")
monkeypatch.setenv("UNSTRUCTURED_PARALLEL_MODE_RETRY_ATTEMPTS", "3")
remote_partition = Mock(side_effect=HTTPException(status_code=401))
monkeypatch.setattr(
requests,
"post",
remote_partition,
)
client = TestClient(app)
test_file = Path("sample-docs") / "list-item-example.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb"), "application/pdf"))],
)
assert response.status_code == 401
# one call for each page
assert remote_partition.call_count == 1
def test_chunking_strategy_param():
"""
Verify that responses do not chunk elements unless requested
"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.pdf"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "hi_res"},
)
assert response.status_code == 200
response_without_chunking = response.json()
# chunking
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"chunking_strategy": "by_title"},
)
assert response.status_code == 200
response_with_chunking = response.json()
assert len(response_with_chunking) != len(response_without_chunking)
assert "CompositeElement" in [element.get("type") for element in response_with_chunking]
# Defaults:
# multipage = True, combine_text_under_n_chars = None, new_after_n_chars = None,
# max_characters = 500
@pytest.mark.parametrize(
("multipage_sections", "combine_under_n_chars", "new_after_n_chars", "max_characters"),
[
(False, None, None, 600), # test multipage_sections
(True, 1000, None, 5000), # test combine_under_n_chars
(True, None, 10, 500), # test new_after_n_chars
(True, None, None, 100), # test max__characters
],
)
def test_chunking_strategy_additional_params(
multipage_sections: bool,
combine_under_n_chars: int,
new_after_n_chars: int,
max_characters: int,
):
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.pdf"
arg_resp = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={
"chunking_strategy": "by_title",
"multipage_sections": multipage_sections,
"combine_under_n_chars": combine_under_n_chars,
"new_after_n_chars": new_after_n_chars,
"max_characters": max_characters,
},
)
arg_resp_json = arg_resp.json()
default_resp = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"chunking_strategy": "by_title"},
)
default_resp_json = default_resp.json()
assert arg_resp_json != default_resp_json
def test_encrypted_pdf():
"""
Test that we throw an error if a pdf is password protected.
A pdf can be encrypted but still readable - don't throw an error here.
"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.pdf"
original_pdf = PdfReader(test_file)
with tempfile.NamedTemporaryFile() as temp_file:
# This file is user encrypted and cannot be read
writer = PdfWriter()
writer.append_pages_from_reader(original_pdf)
writer.encrypt(user_password="password123")
writer.write(temp_file.name)
# Response should be 400
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(temp_file.name), open(temp_file.name, "rb"), "application/pdf"))],
)
assert response.json() == {"detail": "File is encrypted. Please decrypt it with password."}
assert response.status_code == 400
# This file is owner encrypted, i.e. readable with edit restrictions
writer = PdfWriter()
writer.append_pages_from_reader(original_pdf)
writer.encrypt(user_password="", owner_password="password123", permissions_flag=0b1100)
writer.write(temp_file.name)
# Response should be 200
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(temp_file.name), open(temp_file.name, "rb"), "application/pdf"))],
)
assert response.status_code == 200
@pytest.mark.skip(reason="the json became processable in the 0.17.2 unstructured library")
def test_general_api_returns_400_bad_json(tmpdir):
"""
Verify that we get a 400 for invalid json schemas
"""
client = TestClient(app)
data = '{"hi": "there"}'
filepath = os.path.join(tmpdir, "unprocessable.json")
with open(filepath, "w") as f:
f.write(data)
response = client.post(
MAIN_API_ROUTE,
files=[
(
"files",
(
str(filepath),
open(filepath, "rb"),
),
)
],
)
assert "Unstructured schema" in response.json().get("detail")
assert response.status_code == 400
def test_invalid_strategy_for_image_file():
"""
Verify that we get a 400 error if we use "strategy=fast" with an image file
"""
client = TestClient(app)
test_file = Path("sample-docs") / "layout-parser-paper-fast.jpg"
resp = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
data={"strategy": "fast"},