From 6bccf934dbf34345c2102ed69c93961947f74e87 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 12 Dec 2019 16:30:09 -0700 Subject: [PATCH 01/13] automl: add base samples --- automl/cloud-client/batch_predict.py | 50 +++++++++++++++++++ automl/cloud-client/get_operation_status.py | 36 +++++++++++++ .../cloud-client/get_operation_status_test.py | 28 +++++++++++ automl/cloud-client/list_operation_status.py | 38 ++++++++++++++ .../list_operation_status_test.py | 30 +++++++++++ automl/cloud-client/requirements.txt | 2 + 6 files changed, 184 insertions(+) create mode 100644 automl/cloud-client/batch_predict.py create mode 100644 automl/cloud-client/get_operation_status.py create mode 100644 automl/cloud-client/get_operation_status_test.py create mode 100644 automl/cloud-client/list_operation_status.py create mode 100644 automl/cloud-client/list_operation_status_test.py create mode 100644 automl/cloud-client/requirements.txt diff --git a/automl/cloud-client/batch_predict.py b/automl/cloud-client/batch_predict.py new file mode 100644 index 00000000000..dce26f1ad63 --- /dev/null +++ b/automl/cloud-client/batch_predict.py @@ -0,0 +1,50 @@ +# Copyright 2019 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. + + +def batch_predict(project_id, model_id, input_uri, output_uri): + """Batch predict""" + # [START automl_batch_predict] + from google.cloud import automl + + # TODO(developer): Uncomment and set the following variables + # project_id = 'YOUR_PROJECT_ID' + # model_id = 'YOUR_MODEL_ID' + # input_uri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl' + # output_uri = 'gs://YOUR_BUCKET_ID/path_to_save_results/' + + prediction_client = automl.PredictionServiceClient() + + # Get the full path of the model. + model_full_id = prediction_client.model_path( + project_id, 'us-central1', model_id) + + gcs_source = automl.types.GcsSource( + input_uris=[input_uri]) + + input_config = automl.types.BatchPredictInputConfig(gcs_source=gcs_source) + gcs_destination = automl.types.GcsDestination( + output_uri_prefix=output_uri) + output_config = automl.types.BatchPredictOutputConfig( + gcs_destination=gcs_destination) + # [0.0-1.0] Only produce results higher than this value + params = {'score_threshold': '0.8'} + + response = prediction_client.batch_predict( + model_full_id, input_config, output_config, params) + + print('Waiting for operation to complete...') + print(u'Batch Prediction results saved to Cloud Storage bucket. {}'.format( + response.result())) + # [END automl_batch_predict] diff --git a/automl/cloud-client/get_operation_status.py b/automl/cloud-client/get_operation_status.py new file mode 100644 index 00000000000..774fd8db826 --- /dev/null +++ b/automl/cloud-client/get_operation_status.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +# Copyright 2019 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. + + +def get_operation_status(operation_full_id): + """Get operation status.""" + # [START automl_get_operation_status] + from google.cloud import automl + + # TODO(developer): Uncomment and set the following variables + # operation_full_id = \ + # 'projects/[projectId]/locations/us-central1/operations/[operationId]' + + client = automl.AutoMlClient() + # Get the latest state of a long-running operation. + response = client.transport._operations_client.get_operation( + operation_full_id + ) + + print(u'Name: {}'.format(response.name)) + print('Operation details:') + print(response) + # [END automl_get_operation_status] diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py new file mode 100644 index 00000000000..eb19cd59f8e --- /dev/null +++ b/automl/cloud-client/get_operation_status_test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +# Copyright 2018 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 + +import get_operation_status + +PROJECT_ID = os.environ['GCLOUD_PROJECT'] +OPERATION_ID = "" + + +def test_get_operation_status(capsys): + get_operation_status.get_operation_status(OPERATION_ID) + out, _ = capsys.readouterr() + assert 'Operation details' in out diff --git a/automl/cloud-client/list_operation_status.py b/automl/cloud-client/list_operation_status.py new file mode 100644 index 00000000000..80798fa2aa3 --- /dev/null +++ b/automl/cloud-client/list_operation_status.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +# Copyright 2019 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. + + +def list_operation_status(project_id): + """List operation status.""" + # [START automl_list_operation_status] + from google.cloud import automl + + # TODO(developer): Uncomment and set the following variables + # project_id = 'YOUR_PROJECT_ID' + + client = automl.AutoMlClient() + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, 'us-central1') + # List all the operations names available in the region. + response = client.transport._operations_client.list_operations( + project_location, '') + + print('List of operations:') + for operation in response: + print(u'Name: {}'.format(operation.name)) + print('Operation details:') + print(operation) + # [END automl_list_operation_status] diff --git a/automl/cloud-client/list_operation_status_test.py b/automl/cloud-client/list_operation_status_test.py new file mode 100644 index 00000000000..81e97344812 --- /dev/null +++ b/automl/cloud-client/list_operation_status_test.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +# Copyright 2018 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 + +import pytest + +import list_operation_status + +PROJECT_ID = os.environ['GCLOUD_PROJECT'] + + +@pytest.mark.slow +def test_list_operation_status(capsys): + list_operation_status.list_operation_status(PROJECT_ID) + out, _ = capsys.readouterr() + assert 'Operation details' in out \ No newline at end of file diff --git a/automl/cloud-client/requirements.txt b/automl/cloud-client/requirements.txt new file mode 100644 index 00000000000..d64d7c33cbc --- /dev/null +++ b/automl/cloud-client/requirements.txt @@ -0,0 +1,2 @@ +google-cloud-automl==0.8.0 +google-cloud-storage==1.20.0 \ No newline at end of file From 93566840a139763137c353d6be0d77c656b9568e Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 13 Dec 2019 11:07:38 -0700 Subject: [PATCH 02/13] automl: add base set of samples --- automl/cloud-client/batch_predict.py | 28 +++++---- automl/cloud-client/batch_predict_test.py | 59 +++++++++++++++++++ automl/cloud-client/get_operation_status.py | 8 +-- .../cloud-client/get_operation_status_test.py | 4 +- automl/cloud-client/list_operation_status.py | 11 ++-- .../list_operation_status_test.py | 4 +- 6 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 automl/cloud-client/batch_predict_test.py diff --git a/automl/cloud-client/batch_predict.py b/automl/cloud-client/batch_predict.py index dce26f1ad63..ea5a4699655 100644 --- a/automl/cloud-client/batch_predict.py +++ b/automl/cloud-client/batch_predict.py @@ -27,24 +27,26 @@ def batch_predict(project_id, model_id, input_uri, output_uri): prediction_client = automl.PredictionServiceClient() # Get the full path of the model. - model_full_id = prediction_client.model_path( - project_id, 'us-central1', model_id) + model_full_id = prediction_client.model_path(project_id, "us-central1", model_id) - gcs_source = automl.types.GcsSource( - input_uris=[input_uri]) + gcs_source = automl.types.GcsSource(input_uris=[input_uri]) input_config = automl.types.BatchPredictInputConfig(gcs_source=gcs_source) - gcs_destination = automl.types.GcsDestination( - output_uri_prefix=output_uri) + gcs_destination = automl.types.GcsDestination(output_uri_prefix=output_uri) output_config = automl.types.BatchPredictOutputConfig( - gcs_destination=gcs_destination) + gcs_destination=gcs_destination + ) # [0.0-1.0] Only produce results higher than this value - params = {'score_threshold': '0.8'} + params = {"score_threshold": "0.8"} response = prediction_client.batch_predict( - model_full_id, input_config, output_config, params) - - print('Waiting for operation to complete...') - print(u'Batch Prediction results saved to Cloud Storage bucket. {}'.format( - response.result())) + model_full_id, input_config, output_config, params + ) + + print("Waiting for operation to complete...") + print( + u"Batch Prediction results saved to Cloud Storage bucket. {}".format( + response.result() + ) + ) # [END automl_batch_predict] diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py new file mode 100644 index 00000000000..6ed80d8e5e7 --- /dev/null +++ b/automl/cloud-client/batch_predict_test.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2018 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 datetime +import os + +import pytest + +import batch_predict + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +BUCKET_ID = "{}-lcm".format(PROJECT_ID) +MODEL_ID = "TEN5112482778553778176" +PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + + +@pytest.fixture(scope="function") +def verify_model_state(): + from google.cloud import automl + + client = automl.AutoMlClient() + model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) + model = client.get_model(model_full_id) + if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: + # Deploy model if it is not deployed + response = client.deploy_model(model_full_id) + response.result() + + +@pytest.mark.slow +def test_batch_predict(capsys, verify_model_state): + verify_model_state + input_uri = "gs://{}/entity_extraction/input.jsonl".format(BUCKET_ID) + output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX) + batch_predict.batch_predict(PROJECT_ID, MODEL_ID, input_uri, output_uri) + out, _ = capsys.readouterr() + assert "Batch Prediction results saved to Cloud Storage bucket" in out + + # Delete created files + from google.cloud import storage + + storage_client = storage.Client() + bucket = storage_client.get_bucket(BUCKET_ID) + if len(list(bucket.list_blobs(prefix=PREFIX))) > 0: + for blob in bucket.list_blobs(prefix=PREFIX): + blob.delete() diff --git a/automl/cloud-client/get_operation_status.py b/automl/cloud-client/get_operation_status.py index 774fd8db826..8cd71e2cf72 100644 --- a/automl/cloud-client/get_operation_status.py +++ b/automl/cloud-client/get_operation_status.py @@ -26,11 +26,9 @@ def get_operation_status(operation_full_id): client = automl.AutoMlClient() # Get the latest state of a long-running operation. - response = client.transport._operations_client.get_operation( - operation_full_id - ) + response = client.transport._operations_client.get_operation(operation_full_id) - print(u'Name: {}'.format(response.name)) - print('Operation details:') + print(u"Name: {}".format(response.name)) + print("Operation details:") print(response) # [END automl_get_operation_status] diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py index eb19cd59f8e..56d1b68fe5d 100644 --- a/automl/cloud-client/get_operation_status_test.py +++ b/automl/cloud-client/get_operation_status_test.py @@ -18,11 +18,11 @@ import get_operation_status -PROJECT_ID = os.environ['GCLOUD_PROJECT'] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] OPERATION_ID = "" def test_get_operation_status(capsys): get_operation_status.get_operation_status(OPERATION_ID) out, _ = capsys.readouterr() - assert 'Operation details' in out + assert "Operation details" in out diff --git a/automl/cloud-client/list_operation_status.py b/automl/cloud-client/list_operation_status.py index 80798fa2aa3..0695d9fc858 100644 --- a/automl/cloud-client/list_operation_status.py +++ b/automl/cloud-client/list_operation_status.py @@ -25,14 +25,13 @@ def list_operation_status(project_id): client = automl.AutoMlClient() # A resource that represents Google Cloud Platform location. - project_location = client.location_path(project_id, 'us-central1') + project_location = client.location_path(project_id, "us-central1") # List all the operations names available in the region. - response = client.transport._operations_client.list_operations( - project_location, '') + response = client.transport._operations_client.list_operations(project_location, "") - print('List of operations:') + print("List of operations:") for operation in response: - print(u'Name: {}'.format(operation.name)) - print('Operation details:') + print(u"Name: {}".format(operation.name)) + print("Operation details:") print(operation) # [END automl_list_operation_status] diff --git a/automl/cloud-client/list_operation_status_test.py b/automl/cloud-client/list_operation_status_test.py index 81e97344812..c0d44310b9f 100644 --- a/automl/cloud-client/list_operation_status_test.py +++ b/automl/cloud-client/list_operation_status_test.py @@ -20,11 +20,11 @@ import list_operation_status -PROJECT_ID = os.environ['GCLOUD_PROJECT'] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] @pytest.mark.slow def test_list_operation_status(capsys): list_operation_status.list_operation_status(PROJECT_ID) out, _ = capsys.readouterr() - assert 'Operation details' in out \ No newline at end of file + assert "Operation details" in out From f527a46a2c2a62fd2c6c90473e6a812bffdcd9d4 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 20 Dec 2019 09:22:05 -0700 Subject: [PATCH 03/13] Clean up tests --- automl/cloud-client/batch_predict.py | 16 ++++++------ automl/cloud-client/batch_predict_test.py | 16 ++++++------ automl/cloud-client/get_operation_status.py | 8 +++--- .../cloud-client/get_operation_status_test.py | 25 +++++++++++++++---- automl/cloud-client/list_operation_status.py | 8 +++--- .../list_operation_status_test.py | 2 -- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/automl/cloud-client/batch_predict.py b/automl/cloud-client/batch_predict.py index ea5a4699655..4844e7fecde 100644 --- a/automl/cloud-client/batch_predict.py +++ b/automl/cloud-client/batch_predict.py @@ -19,15 +19,17 @@ def batch_predict(project_id, model_id, input_uri, output_uri): from google.cloud import automl # TODO(developer): Uncomment and set the following variables - # project_id = 'YOUR_PROJECT_ID' - # model_id = 'YOUR_MODEL_ID' - # input_uri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl' - # output_uri = 'gs://YOUR_BUCKET_ID/path_to_save_results/' + # project_id = "YOUR_PROJECT_ID" + # model_id = "YOUR_MODEL_ID" + # input_uri = "gs://YOUR_BUCKET_ID/path/to/your/input/csv_or_jsonl" + # output_uri = "gs://YOUR_BUCKET_ID/path/to/save/results/" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. - model_full_id = prediction_client.model_path(project_id, "us-central1", model_id) + model_full_id = prediction_client.model_path( + project_id, "us-central1", model_id + ) gcs_source = automl.types.GcsSource(input_uris=[input_uri]) @@ -36,11 +38,9 @@ def batch_predict(project_id, model_id, input_uri, output_uri): output_config = automl.types.BatchPredictOutputConfig( gcs_destination=gcs_destination ) - # [0.0-1.0] Only produce results higher than this value - params = {"score_threshold": "0.8"} response = prediction_client.batch_predict( - model_full_id, input_config, output_config, params + model_full_id, input_config, output_config ) print("Waiting for operation to complete...") diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py index 6ed80d8e5e7..04aca91a525 100644 --- a/automl/cloud-client/batch_predict_test.py +++ b/automl/cloud-client/batch_predict_test.py @@ -1,6 +1,4 @@ -#!/usr/bin/env python - -# Copyright 2018 Google LLC +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,12 +9,14 @@ # 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 +# See the License for the specific ladnguage governing permissions and # limitations under the License. import datetime import os +from google.cloud import automl +from google.cloud import storage import pytest import batch_predict @@ -24,13 +24,13 @@ PROJECT_ID = os.environ["GCLOUD_PROJECT"] BUCKET_ID = "{}-lcm".format(PROJECT_ID) MODEL_ID = "TEN5112482778553778176" -PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime( + "%Y%m%d%H%M%S" +) @pytest.fixture(scope="function") def verify_model_state(): - from google.cloud import automl - client = automl.AutoMlClient() model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) model = client.get_model(model_full_id) @@ -50,8 +50,6 @@ def test_batch_predict(capsys, verify_model_state): assert "Batch Prediction results saved to Cloud Storage bucket" in out # Delete created files - from google.cloud import storage - storage_client = storage.Client() bucket = storage_client.get_bucket(BUCKET_ID) if len(list(bucket.list_blobs(prefix=PREFIX))) > 0: diff --git a/automl/cloud-client/get_operation_status.py b/automl/cloud-client/get_operation_status.py index 8cd71e2cf72..b54484eda1e 100644 --- a/automl/cloud-client/get_operation_status.py +++ b/automl/cloud-client/get_operation_status.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,11 +20,13 @@ def get_operation_status(operation_full_id): # TODO(developer): Uncomment and set the following variables # operation_full_id = \ - # 'projects/[projectId]/locations/us-central1/operations/[operationId]' + # "projects/[projectId]/locations/us-central1/operations/[operationId]" client = automl.AutoMlClient() # Get the latest state of a long-running operation. - response = client.transport._operations_client.get_operation(operation_full_id) + response = client.transport._operations_client.get_operation( + operation_full_id + ) print(u"Name: {}".format(response.name)) print("Operation details:") diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py index 56d1b68fe5d..4854800bc6b 100644 --- a/automl/cloud-client/get_operation_status_test.py +++ b/automl/cloud-client/get_operation_status_test.py @@ -1,6 +1,4 @@ -#!/usr/bin/env python - -# Copyright 2018 Google LLC +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,13 +14,30 @@ import os +from google.cloud import automl +import pytest + import get_operation_status PROJECT_ID = os.environ["GCLOUD_PROJECT"] OPERATION_ID = "" -def test_get_operation_status(capsys): - get_operation_status.get_operation_status(OPERATION_ID) +@pytest.fixture(scope="function") +def get_operation_id(): + client = automl.AutoMlClient() + project_location = client.location_path(PROJECT_ID, "us-central1") + response = client.transport._operations_client.list_operations( + project_location, "" + ) + operation_id = "" + for operation in response: + operation_id = operation.name + break + yield operation_id + + +def test_get_operation_status(capsys, get_operation_id): + get_operation_status.get_operation_status(get_operation_id) out, _ = capsys.readouterr() assert "Operation details" in out diff --git a/automl/cloud-client/list_operation_status.py b/automl/cloud-client/list_operation_status.py index 0695d9fc858..c7c42e4d382 100644 --- a/automl/cloud-client/list_operation_status.py +++ b/automl/cloud-client/list_operation_status.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,13 +19,15 @@ def list_operation_status(project_id): from google.cloud import automl # TODO(developer): Uncomment and set the following variables - # project_id = 'YOUR_PROJECT_ID' + # project_id = "YOUR_PROJECT_ID" client = automl.AutoMlClient() # A resource that represents Google Cloud Platform location. project_location = client.location_path(project_id, "us-central1") # List all the operations names available in the region. - response = client.transport._operations_client.list_operations(project_location, "") + response = client.transport._operations_client.list_operations( + project_location, "" + ) print("List of operations:") for operation in response: diff --git a/automl/cloud-client/list_operation_status_test.py b/automl/cloud-client/list_operation_status_test.py index c0d44310b9f..0be693c0a1c 100644 --- a/automl/cloud-client/list_operation_status_test.py +++ b/automl/cloud-client/list_operation_status_test.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Copyright 2018 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); From c2232a1afcb764dbe6bcf680ce382e41cb220f33 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 2 Jan 2020 11:01:33 -0700 Subject: [PATCH 04/13] License year 2020, drop python2 print statement unicode --- automl/cloud-client/batch_predict.py | 4 ++-- automl/cloud-client/batch_predict_test.py | 2 +- automl/cloud-client/get_operation_status.py | 4 ++-- automl/cloud-client/get_operation_status_test.py | 2 +- automl/cloud-client/list_operation_status.py | 4 ++-- automl/cloud-client/list_operation_status_test.py | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/automl/cloud-client/batch_predict.py b/automl/cloud-client/batch_predict.py index 4844e7fecde..efe484f4350 100644 --- a/automl/cloud-client/batch_predict.py +++ b/automl/cloud-client/batch_predict.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ def batch_predict(project_id, model_id, input_uri, output_uri): print("Waiting for operation to complete...") print( - u"Batch Prediction results saved to Cloud Storage bucket. {}".format( + "Batch Prediction results saved to Cloud Storage bucket. {}".format( response.result() ) ) diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py index 04aca91a525..72b1c850394 100644 --- a/automl/cloud-client/batch_predict_test.py +++ b/automl/cloud-client/batch_predict_test.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/automl/cloud-client/get_operation_status.py b/automl/cloud-client/get_operation_status.py index b54484eda1e..4e5c90f86e1 100644 --- a/automl/cloud-client/get_operation_status.py +++ b/automl/cloud-client/get_operation_status.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ def get_operation_status(operation_full_id): operation_full_id ) - print(u"Name: {}".format(response.name)) + print("Name: {}".format(response.name)) print("Operation details:") print(response) # [END automl_get_operation_status] diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py index 4854800bc6b..8bc541545fc 100644 --- a/automl/cloud-client/get_operation_status_test.py +++ b/automl/cloud-client/get_operation_status_test.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/automl/cloud-client/list_operation_status.py b/automl/cloud-client/list_operation_status.py index c7c42e4d382..45534fda73a 100644 --- a/automl/cloud-client/list_operation_status.py +++ b/automl/cloud-client/list_operation_status.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -31,7 +31,7 @@ def list_operation_status(project_id): print("List of operations:") for operation in response: - print(u"Name: {}".format(operation.name)) + print("Name: {}".format(operation.name)) print("Operation details:") print(operation) # [END automl_list_operation_status] diff --git a/automl/cloud-client/list_operation_status_test.py b/automl/cloud-client/list_operation_status_test.py index 0be693c0a1c..85100ef57d8 100644 --- a/automl/cloud-client/list_operation_status_test.py +++ b/automl/cloud-client/list_operation_status_test.py @@ -1,4 +1,4 @@ -# Copyright 2018 Google LLC +# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From 7cfad55a98b076d00001953c2a5a428f99a5313e Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 7 Jan 2020 13:32:59 -0700 Subject: [PATCH 05/13] use centralized automl testing project --- automl/cloud-client/batch_predict_test.py | 4 ++-- automl/cloud-client/get_operation_status_test.py | 3 +-- automl/cloud-client/list_operation_status_test.py | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py index 72b1c850394..9bb54fe440f 100644 --- a/automl/cloud-client/batch_predict_test.py +++ b/automl/cloud-client/batch_predict_test.py @@ -21,9 +21,9 @@ import batch_predict -PROJECT_ID = os.environ["GCLOUD_PROJECT"] +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] BUCKET_ID = "{}-lcm".format(PROJECT_ID) -MODEL_ID = "TEN5112482778553778176" +MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime( "%Y%m%d%H%M%S" ) diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py index 8bc541545fc..233dc89a5d9 100644 --- a/automl/cloud-client/get_operation_status_test.py +++ b/automl/cloud-client/get_operation_status_test.py @@ -19,8 +19,7 @@ import get_operation_status -PROJECT_ID = os.environ["GCLOUD_PROJECT"] -OPERATION_ID = "" +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] @pytest.fixture(scope="function") diff --git a/automl/cloud-client/list_operation_status_test.py b/automl/cloud-client/list_operation_status_test.py index 85100ef57d8..ff6a09730a8 100644 --- a/automl/cloud-client/list_operation_status_test.py +++ b/automl/cloud-client/list_operation_status_test.py @@ -18,7 +18,7 @@ import list_operation_status -PROJECT_ID = os.environ["GCLOUD_PROJECT"] +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] @pytest.mark.slow From ce95040fbeed9839ce5f2635c44881498b04f460 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 7 Jan 2020 13:40:41 -0700 Subject: [PATCH 06/13] Fix GCS path typo --- automl/cloud-client/batch_predict_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py index 9bb54fe440f..f1bf035820b 100644 --- a/automl/cloud-client/batch_predict_test.py +++ b/automl/cloud-client/batch_predict_test.py @@ -43,7 +43,7 @@ def verify_model_state(): @pytest.mark.slow def test_batch_predict(capsys, verify_model_state): verify_model_state - input_uri = "gs://{}/entity_extraction/input.jsonl".format(BUCKET_ID) + input_uri = "gs://{}/entity-extraction/input.jsonl".format(BUCKET_ID) output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX) batch_predict.batch_predict(PROJECT_ID, MODEL_ID, input_uri, output_uri) out, _ = capsys.readouterr() From 2cd710ab7d146ca9a4b571a1b74cc94c66987c4a Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 7 Jan 2020 16:18:09 -0700 Subject: [PATCH 07/13] Use fake dataset for batch predict --- automl/cloud-client/batch_predict_test.py | 52 +++++++++-------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py index f1bf035820b..494fbb8d610 100644 --- a/automl/cloud-client/batch_predict_test.py +++ b/automl/cloud-client/batch_predict_test.py @@ -15,43 +15,33 @@ import datetime import os -from google.cloud import automl -from google.cloud import storage -import pytest - import batch_predict PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] BUCKET_ID = "{}-lcm".format(PROJECT_ID) -MODEL_ID = os.environ["ENTITY_EXTRACTION_MODEL_ID"] +MODEL_ID = "TEN0000000000000000000" PREFIX = "TEST_EXPORT_OUTPUT_" + datetime.datetime.now().strftime( "%Y%m%d%H%M%S" ) -@pytest.fixture(scope="function") -def verify_model_state(): - client = automl.AutoMlClient() - model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) - model = client.get_model(model_full_id) - if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: - # Deploy model if it is not deployed - response = client.deploy_model(model_full_id) - response.result() - - -@pytest.mark.slow -def test_batch_predict(capsys, verify_model_state): - verify_model_state - input_uri = "gs://{}/entity-extraction/input.jsonl".format(BUCKET_ID) - output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX) - batch_predict.batch_predict(PROJECT_ID, MODEL_ID, input_uri, output_uri) - out, _ = capsys.readouterr() - assert "Batch Prediction results saved to Cloud Storage bucket" in out - - # Delete created files - storage_client = storage.Client() - bucket = storage_client.get_bucket(BUCKET_ID) - if len(list(bucket.list_blobs(prefix=PREFIX))) > 0: - for blob in bucket.list_blobs(prefix=PREFIX): - blob.delete() +def test_batch_predict(capsys): + # As batch prediction can take a long time. Try to batch predict on a model + # and confirm that the model was not found, but other elements of the + # request were valid. + try: + input_uri = "gs://{}/entity-extraction/input.jsonl".format(BUCKET_ID) + output_uri = "gs://{}/{}/".format(BUCKET_ID, PREFIX) + batch_predict.batch_predict( + PROJECT_ID, MODEL_ID, input_uri, output_uri + ) + out, _ = capsys.readouterr() + assert ( + "The model is either not found or not supported for prediction yet." + in out + ) + except Exception as e: + assert ( + "The model is either not found or not supported for prediction yet." + in e.message + ) From 9594c94b31a5351db8282e6f33e92d1e6fbddbb8 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 7 Jan 2020 16:24:36 -0700 Subject: [PATCH 08/13] lint: line length --- automl/cloud-client/batch_predict_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/automl/cloud-client/batch_predict_test.py b/automl/cloud-client/batch_predict_test.py index 494fbb8d610..0f9417f6410 100644 --- a/automl/cloud-client/batch_predict_test.py +++ b/automl/cloud-client/batch_predict_test.py @@ -37,11 +37,11 @@ def test_batch_predict(capsys): ) out, _ = capsys.readouterr() assert ( - "The model is either not found or not supported for prediction yet." + "The model is either not found or not supported for prediction yet" in out ) except Exception as e: assert ( - "The model is either not found or not supported for prediction yet." + "The model is either not found or not supported for prediction yet" in e.message ) From 58a8128200475c72fa6c08069fc93a41d76c2061 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 9 Jan 2020 12:01:18 -0700 Subject: [PATCH 09/13] fix fixture naming and use --- automl/cloud-client/delete_dataset_test.py | 6 +++--- automl/cloud-client/get_model_evaluation_test.py | 6 +++--- automl/cloud-client/get_operation_status_test.py | 6 +++--- automl/cloud-client/import_dataset_test.py | 10 +++++----- .../language_sentiment_analysis_predict_test.py | 8 ++++---- automl/cloud-client/translate_predict_test.py | 8 ++++---- .../cloud-client/vision_classification_predict_test.py | 8 ++++---- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/automl/cloud-client/delete_dataset_test.py b/automl/cloud-client/delete_dataset_test.py index 8a1057a6f95..6d204dde8c0 100644 --- a/automl/cloud-client/delete_dataset_test.py +++ b/automl/cloud-client/delete_dataset_test.py @@ -25,7 +25,7 @@ @pytest.fixture(scope="function") -def create_dataset(): +def dataset_id(): client = automl.AutoMlClient() project_location = client.location_path(PROJECT_ID, "us-central1") display_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") @@ -39,8 +39,8 @@ def create_dataset(): yield dataset_id -def test_delete_dataset(capsys, create_dataset): +def test_delete_dataset(capsys, dataset_id): # delete dataset - delete_dataset.delete_dataset(PROJECT_ID, create_dataset) + delete_dataset.delete_dataset(PROJECT_ID, dataset_id) out, _ = capsys.readouterr() assert "Dataset deleted." in out diff --git a/automl/cloud-client/get_model_evaluation_test.py b/automl/cloud-client/get_model_evaluation_test.py index 40a88a82e85..f3fe1b2bc18 100644 --- a/automl/cloud-client/get_model_evaluation_test.py +++ b/automl/cloud-client/get_model_evaluation_test.py @@ -24,7 +24,7 @@ @pytest.fixture(scope="function") -def get_evaluation_id(): +def model_evaluation_id(): client = automl.AutoMlClient() model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) evaluation = None @@ -37,9 +37,9 @@ def get_evaluation_id(): yield model_evaluation_id -def test_get_model_evaluation(capsys, get_evaluation_id): +def test_get_model_evaluation(capsys, model_evaluation_id): get_model_evaluation.get_model_evaluation( - PROJECT_ID, MODEL_ID, get_evaluation_id + PROJECT_ID, MODEL_ID, model_evaluation_id ) out, _ = capsys.readouterr() assert "Model evaluation name: " in out diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py index 233dc89a5d9..0158b7600e1 100644 --- a/automl/cloud-client/get_operation_status_test.py +++ b/automl/cloud-client/get_operation_status_test.py @@ -23,7 +23,7 @@ @pytest.fixture(scope="function") -def get_operation_id(): +def operation_id(): client = automl.AutoMlClient() project_location = client.location_path(PROJECT_ID, "us-central1") response = client.transport._operations_client.list_operations( @@ -36,7 +36,7 @@ def get_operation_id(): yield operation_id -def test_get_operation_status(capsys, get_operation_id): - get_operation_status.get_operation_status(get_operation_id) +def test_get_operation_status(capsys, operation_id): + get_operation_status.get_operation_status(operation_id) out, _ = capsys.readouterr() assert "Operation details" in out diff --git a/automl/cloud-client/import_dataset_test.py b/automl/cloud-client/import_dataset_test.py index 2064abbcb48..580c42d3a68 100644 --- a/automl/cloud-client/import_dataset_test.py +++ b/automl/cloud-client/import_dataset_test.py @@ -25,7 +25,7 @@ @pytest.fixture(scope="function") -def create_dataset(): +def dataset_id(): client = automl.AutoMlClient() project_location = client.location_path(PROJECT_ID, "us-central1") display_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") @@ -42,19 +42,19 @@ def create_dataset(): @pytest.mark.slow -def test_import_dataset(capsys, create_dataset): +def test_import_dataset(capsys, dataset_id): data = ( "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) ) - dataset_id = create_dataset - import_dataset.import_dataset(PROJECT_ID, dataset_id, data) + created_dataset_id = dataset_id + import_dataset.import_dataset(PROJECT_ID, created_dataset_id, data) out, _ = capsys.readouterr() assert "Data imported." in out # delete created dataset client = automl.AutoMlClient() dataset_full_id = client.dataset_path( - PROJECT_ID, "us-central1", dataset_id + PROJECT_ID, "us-central1", created_dataset_id ) response = client.delete_dataset(dataset_full_id) response.result() diff --git a/automl/cloud-client/language_sentiment_analysis_predict_test.py b/automl/cloud-client/language_sentiment_analysis_predict_test.py index d4fffc7f086..6f4834b3b0c 100644 --- a/automl/cloud-client/language_sentiment_analysis_predict_test.py +++ b/automl/cloud-client/language_sentiment_analysis_predict_test.py @@ -23,8 +23,9 @@ MODEL_ID = os.environ["SENTIMENT_ANALYSIS_MODEL_ID"] -@pytest.fixture(scope="function") -def verify_model_state(): +@pytest.fixture(scope="function", autouse=True) +def setup(): + # Verify the model is deployed before trying to predict client = automl.AutoMlClient() model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) @@ -35,8 +36,7 @@ def verify_model_state(): response.result() -def test_predict(capsys, verify_model_state): - verify_model_state +def test_predict(capsys): text = "Hopefully this Claritin kicks in soon" language_sentiment_analysis_predict.predict(PROJECT_ID, MODEL_ID, text) out, _ = capsys.readouterr() diff --git a/automl/cloud-client/translate_predict_test.py b/automl/cloud-client/translate_predict_test.py index aabfd05b61a..8cf99a5bc0f 100644 --- a/automl/cloud-client/translate_predict_test.py +++ b/automl/cloud-client/translate_predict_test.py @@ -23,8 +23,9 @@ MODEL_ID = os.environ["TRANSLATION_MODEL_ID"] -@pytest.fixture(scope="function") -def verify_model_state(): +@pytest.fixture(scope="function", autouse=True) +def setup(): + # Verify the model is deployed before trying to predict client = automl.AutoMlClient() model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) @@ -35,8 +36,7 @@ def verify_model_state(): response.result() -def test_predict(capsys, verify_model_state): - verify_model_state +def test_predict(capsys): translate_predict.predict(PROJECT_ID, MODEL_ID, "resources/input.txt") out, _ = capsys.readouterr() assert "Translated content: " in out diff --git a/automl/cloud-client/vision_classification_predict_test.py b/automl/cloud-client/vision_classification_predict_test.py index 9df9c91161c..bc91796a980 100644 --- a/automl/cloud-client/vision_classification_predict_test.py +++ b/automl/cloud-client/vision_classification_predict_test.py @@ -23,8 +23,9 @@ MODEL_ID = os.environ["VISION_CLASSIFICATION_MODEL_ID"] -@pytest.fixture(scope="function") -def verify_model_state(): +@pytest.fixture(scope="function", autouse=True) +def setup(): + # Verify the model is deployed before tyring to predict client = automl.AutoMlClient() model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) @@ -35,8 +36,7 @@ def verify_model_state(): response.result() -def test_vision_classification_predict(capsys, verify_model_state): - verify_model_state +def test_vision_classification_predict(capsys): file_path = "resources/test.png" vision_classification_predict.predict(PROJECT_ID, MODEL_ID, file_path) out, _ = capsys.readouterr() From e5ffd29a828f90679d57bd914339a7acf5538be9 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 21 Jan 2020 15:23:21 -0700 Subject: [PATCH 10/13] Fix fixture changes --- .../cloud-client/language_sentiment_analysis_predict_test.py | 3 +-- automl/cloud-client/translate_predict_test.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/automl/cloud-client/language_sentiment_analysis_predict_test.py b/automl/cloud-client/language_sentiment_analysis_predict_test.py index 26c2546e51a..bfd35649cb1 100644 --- a/automl/cloud-client/language_sentiment_analysis_predict_test.py +++ b/automl/cloud-client/language_sentiment_analysis_predict_test.py @@ -36,8 +36,7 @@ def setup(): response.result() -def test_sentiment_analysis_predict(capsys, verify_model_state): - verify_model_state +def test_sentiment_analysis_predict(capsys): text = "Hopefully this Claritin kicks in soon" language_sentiment_analysis_predict.predict(PROJECT_ID, MODEL_ID, text) out, _ = capsys.readouterr() diff --git a/automl/cloud-client/translate_predict_test.py b/automl/cloud-client/translate_predict_test.py index cb677f378ef..cd31d98b1c4 100644 --- a/automl/cloud-client/translate_predict_test.py +++ b/automl/cloud-client/translate_predict_test.py @@ -36,8 +36,7 @@ def setup(): response.result() -def test_translate_predict(capsys, verify_model_state): - verify_model_state +def test_translate_predict(capsys): translate_predict.predict(PROJECT_ID, MODEL_ID, "resources/input.txt") out, _ = capsys.readouterr() assert "Translated content: " in out From 83d1a4b8863e941fa23209fc3634a68c7fbd758c Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 21 Jan 2020 16:16:02 -0700 Subject: [PATCH 11/13] Catch resource exhausted error --- automl/cloud-client/import_dataset_test.py | 26 ++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/automl/cloud-client/import_dataset_test.py b/automl/cloud-client/import_dataset_test.py index 580c42d3a68..44fed3fc93a 100644 --- a/automl/cloud-client/import_dataset_test.py +++ b/automl/cloud-client/import_dataset_test.py @@ -43,13 +43,23 @@ def dataset_id(): @pytest.mark.slow def test_import_dataset(capsys, dataset_id): - data = ( - "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) - ) - created_dataset_id = dataset_id - import_dataset.import_dataset(PROJECT_ID, created_dataset_id, data) - out, _ = capsys.readouterr() - assert "Data imported." in out + # Importing a dataset can take a long time and only four operations can be + # run on a project at once. Try to import the dataset and if a resource + # exhausted error is thrown, catch it. Otherwise, proceed as usual. + try: + data = ( + "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) + ) + created_dataset_id = dataset_id + import_dataset.import_dataset(PROJECT_ID, created_dataset_id, data) + out, _ = capsys.readouterr() + assert "Data imported." in out + except Exception as e: + assert ( + "ResourceExhausted: 429 There are too many import data " + "operations are running in parallel" + in e.message + ) # delete created dataset client = automl.AutoMlClient() @@ -58,3 +68,5 @@ def test_import_dataset(capsys, dataset_id): ) response = client.delete_dataset(dataset_full_id) response.result() + + From 5d40849ac2690041a10f004ecc90edbe0af8275d Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 22 Jan 2020 14:35:20 -0700 Subject: [PATCH 12/13] use fake data for import test --- automl/cloud-client/import_dataset_test.py | 59 +++++----------------- 1 file changed, 14 insertions(+), 45 deletions(-) diff --git a/automl/cloud-client/import_dataset_test.py b/automl/cloud-client/import_dataset_test.py index 44fed3fc93a..35d23edc7e8 100644 --- a/automl/cloud-client/import_dataset_test.py +++ b/automl/cloud-client/import_dataset_test.py @@ -12,61 +12,30 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime import os -from google.cloud import automl -import pytest - import import_dataset PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] BUCKET_ID = "{}-lcm".format(PROJECT_ID) +DATASET_ID = "TEN0000000000000000000" -@pytest.fixture(scope="function") -def dataset_id(): - client = automl.AutoMlClient() - project_location = client.location_path(PROJECT_ID, "us-central1") - display_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - metadata = automl.types.TextSentimentDatasetMetadata( - sentiment_max=4 - ) - dataset = automl.types.Dataset( - display_name=display_name, text_sentiment_dataset_metadata=metadata - ) - response = client.create_dataset(project_location, dataset) - dataset_id = response.result().name.split("/")[-1] - - yield dataset_id - - -@pytest.mark.slow -def test_import_dataset(capsys, dataset_id): - # Importing a dataset can take a long time and only four operations can be - # run on a project at once. Try to import the dataset and if a resource - # exhausted error is thrown, catch it. Otherwise, proceed as usual. +def test_import_dataset(capsys): + # As importing a dataset can take a long time and only four operations can + # be run on a dataset at once. Try to import into a nonexistent dataset and + # confirm that the dataset was not found, but other elements of the request + # were valid. try: - data = ( - "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) - ) - created_dataset_id = dataset_id - import_dataset.import_dataset(PROJECT_ID, created_dataset_id, data) + data = "gs://{}/sentiment-analysis/dataset.csv".format(BUCKET_ID) + import_dataset.import_dataset(PROJECT_ID, DATASET_ID, data) out, _ = capsys.readouterr() - assert "Data imported." in out + assert ( + "The Dataset doesn't exist or is inaccessible for use with AutoMl." + in out + ) except Exception as e: assert ( - "ResourceExhausted: 429 There are too many import data " - "operations are running in parallel" - in e.message + "The Dataset doesn't exist or is inaccessible for use with AutoMl." + in e.message ) - - # delete created dataset - client = automl.AutoMlClient() - dataset_full_id = client.dataset_path( - PROJECT_ID, "us-central1", created_dataset_id - ) - response = client.delete_dataset(dataset_full_id) - response.result() - - From ccb5792c73fbc6bccd1dd4025b8b55ccadfbac42 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 28 Jan 2020 12:15:10 -0700 Subject: [PATCH 13/13] update how to access an operation id --- automl/cloud-client/get_operation_status_test.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/automl/cloud-client/get_operation_status_test.py b/automl/cloud-client/get_operation_status_test.py index 0158b7600e1..c08095fc2bf 100644 --- a/automl/cloud-client/get_operation_status_test.py +++ b/automl/cloud-client/get_operation_status_test.py @@ -26,14 +26,12 @@ def operation_id(): client = automl.AutoMlClient() project_location = client.location_path(PROJECT_ID, "us-central1") - response = client.transport._operations_client.list_operations( - project_location, "" - ) - operation_id = "" - for operation in response: - operation_id = operation.name - break - yield operation_id + generator = client.transport._operations_client.list_operations( + project_location, filter_="" + ).pages + page = next(generator) + operation = page.next() + yield operation.name def test_get_operation_status(capsys, operation_id):