forked from googleapis/python-documentai-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quickstart_sample.py
More file actions
157 lines (124 loc) · 5.52 KB
/
test_quickstart_sample.py
File metadata and controls
157 lines (124 loc) · 5.52 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
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
from google.cloud import documentai
from google.longrunning.operations_pb2 import \
ListOperationsRequest # type: ignore
import pytest
from samples.snippets import quickstart_sample
location = "us"
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
def test_quickstart_sample_gcs_bucket_prefix(capsys: pytest.CaptureFixture) -> None:
gcs_bucket_name = "documentai_toolbox_samples"
gcs_prefix = "output/123456789/0"
quickstart_sample.quickstart_sample(
gcs_bucket_name=gcs_bucket_name, gcs_prefix=gcs_prefix
)
out, _ = capsys.readouterr()
assert "Document structure in Cloud Storage" in out
assert "Number of Pages: 1" in out
assert "Number of Entities: 35" in out
assert "Confidence:" in out
assert "Detected Languages:" in out or "No language detected" in out
def test_quickstart_sample_gcs_uri(capsys: pytest.CaptureFixture) -> None:
gcs_uri = (
"gs://documentai_toolbox_samples/output/123456789/0/toolbox_invoice_test-0.json"
)
quickstart_sample.quickstart_sample(gcs_uri=gcs_uri)
out, _ = capsys.readouterr()
assert "Number of Pages: 1" in out
assert "Number of Entities: 35" in out
assert "Confidence:" in out
assert "Detected Languages:" in out or "No language detected" in out
def test_quickstart_sample_document_path(capsys: pytest.CaptureFixture) -> None:
document_path = "resources/form_with_tables.json"
quickstart_sample.quickstart_sample(document_path=document_path)
out, _ = capsys.readouterr()
assert "Number of Pages: 1" in out
assert "Number of Entities: 0" in out
assert "Form Date" in out
assert "Confidence:" in out
assert "Detected Languages:" in out or "No language detected" in out
def test_quickstart_sample_documentai_document(capsys: pytest.CaptureFixture) -> None:
with open("resources/form_with_tables.json", encoding="utf-8") as f:
documentai_document = documentai.Document.from_json(
f.read(), ignore_unknown_fields=True
)
quickstart_sample.quickstart_sample(documentai_document=documentai_document)
out, _ = capsys.readouterr()
assert "Number of Pages: 1" in out
assert "Number of Entities: 0" in out
assert "Form Date" in out
assert "Confidence:" in out
assert "Detected Languages:" in out or "No language detected" in out
def test_quickstart_sample_batch_process_metadata(
capsys: pytest.CaptureFixture,
) -> None:
client = documentai.DocumentProcessorServiceClient()
name = f"{client.common_location_path(project=project_id, location=location)}/operations"
response = client.list_operations(
request=ListOperationsRequest(
name=name,
filter="TYPE=BATCH_PROCESS_DOCUMENTS AND STATE=DONE",
page_size=1,
)
)
batch_process_metadata = documentai.BatchProcessMetadata.deserialize(
response.operations[0].metadata.value
)
quickstart_sample.quickstart_sample(batch_process_metadata=batch_process_metadata)
out, _ = capsys.readouterr()
assert "Document Successfully Loaded!" in out
def test_quickstart_sample_batch_process_metadata_matching_prefixes(
capsys: pytest.CaptureFixture,
) -> None:
batch_process_metadata = documentai.BatchProcessMetadata(
state=documentai.BatchProcessMetadata.State.SUCCEEDED,
individual_process_statuses=[
documentai.BatchProcessMetadata.IndividualProcessStatus(
input_gcs_source="gs://test-directory/documentai/input.pdf",
output_gcs_destination="gs://documentai_toolbox_samples/output/matching-prefixes/1",
),
documentai.BatchProcessMetadata.IndividualProcessStatus(
input_gcs_source="gs://test-directory/documentai/input.pdf",
output_gcs_destination="gs://documentai_toolbox_samples/output/matching-prefixes/11",
),
],
)
wrapped_document = quickstart_sample.quickstart_sample(
batch_process_metadata=batch_process_metadata
)
assert wrapped_document.gcs_prefix == "output/matching-prefixes/1/"
out, _ = capsys.readouterr()
assert "Document Successfully Loaded!" in out
def test_quickstart_sample_batch_process_operation(
capsys: pytest.CaptureFixture,
) -> None:
client = documentai.DocumentProcessorServiceClient()
name = f"{client.common_location_path(project=project_id, location=location)}/operations"
response = client.list_operations(
request=ListOperationsRequest(
name=name,
filter="TYPE=BATCH_PROCESS_DOCUMENTS AND STATE=DONE",
page_size=1,
)
)
batch_process_operation = response.operations[0].name
quickstart_sample.quickstart_sample(batch_process_operation=batch_process_operation)
out, _ = capsys.readouterr()
assert "Document Successfully Loaded!" in out
def test_quickstart_sample_no_input() -> None:
with pytest.raises(ValueError, match="No document source provided."):
quickstart_sample.quickstart_sample()