Skip to content

Commit

Permalink
samples: more updates for v1 (#121)
Browse files Browse the repository at this point in the history
* samples: more updates for v1

* fix: lint

* fix: tests

* fix: tests

* fix: tests
  • Loading branch information
telpirion authored and dandhlee committed Jan 5, 2023
1 parent 1e7d15a commit 62ff4ec
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion documentai/snippets/batch_parse_form_v1beta2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def batch_parse_form(
project_id="YOUR_PROJECT_ID",
input_uri="gs://cloud-samples-data/documentai/form.pdf",
destination_uri="gs://your-bucket-id/path/to/save/results/",
timeout=90
timeout=90,
):
"""Parse a form"""

Expand Down
4 changes: 3 additions & 1 deletion documentai/snippets/batch_parse_form_v1beta2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def setup_teardown():


def test_batch_parse_form(capsys):
batch_parse_form_v1beta2.batch_parse_form(PROJECT_ID, INPUT_URI, BATCH_OUTPUT_URI, 120)
batch_parse_form_v1beta2.batch_parse_form(
PROJECT_ID, INPUT_URI, BATCH_OUTPUT_URI, 120
)
out, _ = capsys.readouterr()
assert "Output files" in out
2 changes: 1 addition & 1 deletion documentai/snippets/batch_parse_table_v1beta2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def batch_parse_table(
project_id="YOUR_PROJECT_ID",
input_uri="gs://cloud-samples-data/documentai/form.pdf",
destination_uri="gs://your-bucket-id/path/to/save/results/",
timeout=90
timeout=90,
):
"""Parse a form"""

Expand Down
4 changes: 3 additions & 1 deletion documentai/snippets/batch_parse_table_v1beta2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def setup_teardown():


def test_batch_parse_table(capsys):
batch_parse_table_v1beta2.batch_parse_table(PROJECT_ID, INPUT_URI, BATCH_OUTPUT_URI, 120)
batch_parse_table_v1beta2.batch_parse_table(
PROJECT_ID, INPUT_URI, BATCH_OUTPUT_URI, 120
)
out, _ = capsys.readouterr()
assert "Output files:" in out
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os
from uuid import uuid4

from samples.snippets import batch_process_documents_sample_v1beta3
from samples.snippets import batch_process_documents_sample

location = "us"
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
Expand All @@ -29,7 +29,7 @@

def test_batch_process_documents_with_bad_input(capsys):
try:
batch_process_documents_sample_v1beta3.batch_process_documents(
batch_process_documents_sample.batch_process_documents(
project_id=project_id,
location=location,
processor_id=processor_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import pytest

from samples.snippets import batch_process_documents_sample_v1beta3
from samples.snippets import batch_process_documents_sample

location = "us"
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
Expand All @@ -47,7 +47,7 @@ def test_bucket():


def test_batch_process_documents(capsys, test_bucket):
batch_process_documents_sample_v1beta3.batch_process_documents(
batch_process_documents_sample.batch_process_documents(
project_id=project_id,
location=location,
processor_id=processor_id,
Expand Down
38 changes: 20 additions & 18 deletions documentai/snippets/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,25 @@

TEST_CONFIG = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

"ignored_versions": ["2.7"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
'enforce_type_hints': False,

"enforce_type_hints": False,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
"envs": {},
}


try:
# Ensure we can import noxfile_config in the project's directory.
sys.path.append('.')
sys.path.append(".")
from noxfile_config import TEST_CONFIG_OVERRIDE
except ImportError as e:
print("No user noxfile_config found: detail: {}".format(e))
Expand All @@ -74,12 +71,12 @@ def get_pytest_env_vars() -> Dict[str, str]:
ret = {}

# Override the GCLOUD_PROJECT and the alias.
env_key = TEST_CONFIG['gcloud_project_env']
env_key = TEST_CONFIG["gcloud_project_env"]
# This should error out if not set.
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]

# Apply user supplied envs.
ret.update(TEST_CONFIG['envs'])
ret.update(TEST_CONFIG["envs"])
return ret


Expand All @@ -88,7 +85,7 @@ def get_pytest_env_vars() -> Dict[str, str]:
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8", "3.9"]

# Any default versions that should be ignored.
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]

TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])

Expand Down Expand Up @@ -137,7 +134,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:

@nox.session
def lint(session: nox.sessions.Session) -> None:
if not TEST_CONFIG['enforce_type_hints']:
if not TEST_CONFIG["enforce_type_hints"]:
session.install("flake8", "flake8-import-order")
else:
session.install("flake8", "flake8-import-order", "flake8-annotations")
Expand All @@ -146,9 +143,11 @@ def lint(session: nox.sessions.Session) -> None:
args = FLAKE8_COMMON_ARGS + [
"--application-import-names",
",".join(local_names),
"."
".",
]
session.run("flake8", *args)


#
# Black
#
Expand All @@ -161,6 +160,7 @@ def blacken(session: nox.sessions.Session) -> None:

session.run("black", *python_files)


#
# Sample Tests
#
Expand All @@ -169,7 +169,9 @@ def blacken(session: nox.sessions.Session) -> None:
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]


def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
def _session_tests(
session: nox.sessions.Session, post_install: Callable = None
) -> None:
"""Runs py.test for a particular project."""
if os.path.exists("requirements.txt"):
session.install("-r", "requirements.txt")
Expand Down Expand Up @@ -200,9 +202,9 @@ def py(session: nox.sessions.Session) -> None:
if session.python in TESTED_VERSIONS:
_session_tests(session)
else:
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
session.python
))
session.skip(
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
)


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
def process_document_sample(
project_id: str, location: str, processor_id: str, file_path: str
):
from google.cloud import documentai_v1beta3 as documentai
from google.cloud import documentai_v1 as documentai

# You must set the api_endpoint if you use a location other than 'us', e.g.:
opts = {}
Expand All @@ -46,7 +46,7 @@ def process_document_sample(
document = {"content": image_content, "mime_type": "application/pdf"}

# Configure the process request
request = {"name": name, "document": document}
request = {"name": name, "raw_document": document}

# Recognizes text entities in the PDF document
result = client.process_document(request=request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import os

from samples.snippets import process_document_sample_v1beta3
from samples.snippets import process_document_sample


location = "us"
Expand All @@ -25,7 +25,7 @@


def test_process_documents(capsys):
process_document_sample_v1beta3.process_document_sample(
process_document_sample.process_document_sample(
project_id=project_id,
location=location,
processor_id=processor_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

from google.cloud import documentai_v1beta3 as documentai
from google.cloud import documentai_v1 as documentai

# [START documentai_quickstart]

Expand Down Expand Up @@ -45,7 +45,7 @@ def quickstart(project_id: str, location: str, processor_id: str, file_path: str
document = {"content": image_content, "mime_type": "application/pdf"}

# Configure the process request
request = {"name": name, "document": document}
request = {"name": name, "raw_document": document}

result = client.process_document(request=request)
document = result.document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import os

from samples.snippets import quickstart_sample_v1beta3
from samples.snippets import quickstart_sample

location = "us"
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
Expand All @@ -24,7 +24,7 @@


def test_quickstart(capsys):
quickstart_sample_v1beta3.quickstart(
quickstart_sample.quickstart(
project_id=project_id,
location=location,
processor_id=processor_id,
Expand Down

0 comments on commit 62ff4ec

Please sign in to comment.