Skip to content

Commit ca0830f

Browse files
munkhuushmgldandhlee
authored andcommitted
samples: added test that covers the wrong file type case (#69)
* samples: added test that covers the wrong file type case
1 parent 228327b commit ca0830f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
from uuid import uuid4
18+
19+
from samples.snippets import batch_process_documents_sample_v1beta3
20+
21+
location = "us"
22+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
23+
processor_id = "90484cfdedb024f6"
24+
gcs_input_uri = "gs://cloud-samples-data/documentai/invoice.pdf"
25+
# following bucket contains .csv file which will cause the sample to fail.
26+
gcs_output_full_uri_with_wrong_type = "gs://documentai-beta-samples"
27+
BUCKET_NAME = f"document-ai-python-{uuid4()}"
28+
29+
30+
def test_batch_process_documents_with_bad_input(capsys):
31+
try:
32+
batch_process_documents_sample_v1beta3.batch_process_documents(
33+
project_id=project_id,
34+
location=location,
35+
processor_id=processor_id,
36+
gcs_input_uri=gcs_input_uri,
37+
gcs_output_uri=gcs_output_full_uri_with_wrong_type,
38+
gcs_output_uri_prefix="test",
39+
timeout=450,
40+
)
41+
out, _ = capsys.readouterr()
42+
assert "Failed to process" in out
43+
except Exception as e:
44+
assert "Failed to process" in e.message

documentai/snippets/batch_process_documents_sample_v1beta3.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def batch_process_documents(
3535
gcs_input_uri,
3636
gcs_output_uri,
3737
gcs_output_uri_prefix,
38+
timeout: int = 300,
3839
):
3940

4041
client = documentai.DocumentProcessorServiceClient()
@@ -63,7 +64,7 @@ def batch_process_documents(
6364
operation = client.batch_process_documents(request)
6465

6566
# Wait for the operation to finish
66-
operation.result()
67+
operation.result(timeout=timeout)
6768

6869
# Results are written to GCS. Use a regex to find
6970
# output files
@@ -79,6 +80,7 @@ def batch_process_documents(
7980
for i, blob in enumerate(blob_list):
8081
# Download the contents of this blob as a bytes object.
8182
if ".json" not in blob.name:
83+
print(f"skipping non-supported file type {blob.name}")
8284
return
8385
# Only parses JSON files
8486
blob_as_bytes = blob.download_as_bytes()

0 commit comments

Comments
 (0)