From a449a1c310ac8b6bef1967095a95c52e4837a938 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 11 May 2020 17:26:35 +0000 Subject: [PATCH 1/5] Re-enable AutoML model create tests --- translate/automl/model_test.py | 1 - vision/automl/model_test.py | 1 - 2 files changed, 2 deletions(-) diff --git a/translate/automl/model_test.py b/translate/automl/model_test.py index 0d37a85c674..6e1a344550a 100644 --- a/translate/automl/model_test.py +++ b/translate/automl/model_test.py @@ -26,7 +26,6 @@ compute_region = "us-central1" -@pytest.mark.skip(reason="creates too many models") def test_model_create_status_delete(capsys): # create model client = automl.AutoMlClient() diff --git a/vision/automl/model_test.py b/vision/automl/model_test.py index a272ce92334..20c3552d8ee 100644 --- a/vision/automl/model_test.py +++ b/vision/automl/model_test.py @@ -24,7 +24,6 @@ compute_region = "us-central1" -@pytest.mark.skip(reason="creates too many models") def test_model_create_status_delete(capsys): # create model client = automl.AutoMlClient() From 6f1d2eb3cc19aec07f612618d31b7c0f1ba833e8 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 11 May 2020 22:58:41 +0000 Subject: [PATCH 2/5] test: use fixtures and uuid --- translate/automl/model_test.py | 22 +++-- translate/automl/noxfile.py | 153 +++++++++++++++++++++++++++++++++ vision/automl/model_test.py | 17 ++-- 3 files changed, 177 insertions(+), 15 deletions(-) create mode 100644 translate/automl/noxfile.py diff --git a/translate/automl/model_test.py b/translate/automl/model_test.py index 6e1a344550a..4a897097a48 100644 --- a/translate/automl/model_test.py +++ b/translate/automl/model_test.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime import os +import uuid from google.cloud import automl_v1beta1 as automl import pytest @@ -26,18 +26,25 @@ compute_region = "us-central1" -def test_model_create_status_delete(capsys): - # create model +@pytest.fixture +def model(): client = automl.AutoMlClient() - model_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + model_name = "test_" + str(uuid.uuid4()) project_location = client.location_path(project_id, compute_region) my_model = { "display_name": model_name, "dataset_id": "3876092572857648864", "translation_model_metadata": {"base_model": ""}, } - response = client.create_model(project_location, my_model) - operation_name = response.operation.name + operation = client.create_model(project_location, my_model) + + yield operation + + operation.cancel() + + +def test_model_create_status_delete(capsys, model): + operation_name = model.operation.name assert operation_name # get operation status @@ -45,9 +52,6 @@ def test_model_create_status_delete(capsys): out, _ = capsys.readouterr() assert "Operation status: " in out - # cancel operation - response.cancel() - def test_model_list_get_evaluate(capsys): # list models diff --git a/translate/automl/noxfile.py b/translate/automl/noxfile.py new file mode 100644 index 00000000000..9f2242194ce --- /dev/null +++ b/translate/automl/noxfile.py @@ -0,0 +1,153 @@ +# 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. + +from __future__ import print_function + +import os +from pathlib import Path + +import nox + + +# DO NOT EDIT - automatically generated. +# All versions used to tested samples. +ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = ["2.7"] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +# +# Style Checks +# + + +# Ignore I202 "Additional newline in a section of imports." to accommodate +# region tags in import blocks. Since we specify an explicit ignore, we also +# have to explicitly ignore the list of default ignores: +# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`. +def _determine_local_import_names(start_dir): + """Determines all import names that should be considered "local". + + This is used when running the linter to insure that import order is + properly checked. + """ + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] + return [ + basename + for basename, extension in file_ext_pairs + if extension == ".py" + or os.path.isdir(os.path.join(start_dir, basename)) + and basename not in ("__pycache__") + ] + + +FLAKE8_COMMON_ARGS = [ + "--show-source", + "--builtin=gettext", + "--max-complexity=20", + "--import-order-style=google", + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", + "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I100,I201,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session): + session.install("flake8", "flake8-import-order") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + ".", + ] + session.run("flake8", *args) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests(session, post_install=None): + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + session.install("-r", "requirements-test.txt") + + if post_install: + post_install(session) + + session.run( + "pytest", + *(PYTEST_COMMON_ARGS + session.posargs), + # Pytest will return 5 when no tests are collected. This can happen + # on travis where slow and flaky tests are excluded. + # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html + success_codes=[0, 5] + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session): + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + print("SKIPPED: {} tests are disabled for this sample.".format(session.python)) + + +# +# Readmegen +# + + +def _get_repo_root(): + """ Returns the root folder of the project. """ + # Get root of this repository. Assume we don't have directories nested deeper than 10 items. + p = Path(os.getcwd()) + for i in range(10): + if p is None: + break + if Path(p / ".git").exists(): + return str(p) + p = p.parent + raise Exception("Unable to detect repository root.") + + +GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) + + +@nox.session +@nox.parametrize("path", GENERATED_READMES) +def readmegen(session, path): + """(Re-)generates the readme for a sample.""" + session.install("jinja2", "pyyaml") + dir_ = os.path.dirname(path) + + if os.path.exists(os.path.join(dir_, "requirements.txt")): + session.install("-r", os.path.join(dir_, "requirements.txt")) + + in_file = os.path.join(dir_, "README.rst.in") + session.run( + "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file + ) diff --git a/vision/automl/model_test.py b/vision/automl/model_test.py index 20c3552d8ee..a04747f408e 100644 --- a/vision/automl/model_test.py +++ b/vision/automl/model_test.py @@ -16,6 +16,7 @@ import datetime import os +import uuid from google.cloud import automl_v1beta1 as automl import pytest @@ -24,10 +25,10 @@ compute_region = "us-central1" -def test_model_create_status_delete(capsys): - # create model +@pytest.fixture +def model(): client = automl.AutoMlClient() - model_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + model_name = "test_" + str(uuid.uuid4()) project_location = client.location_path(project_id, compute_region) my_model = { "display_name": model_name, @@ -35,8 +36,12 @@ def test_model_create_status_delete(capsys): "image_classification_model_metadata": {"train_budget": 24}, } response = client.create_model(project_location, my_model) - operation_name = response.operation.name - assert operation_name - # cancel operation + yield response + response.cancel() + + +def test_model_create_status_delete(capsys, model): + operation_name = model.operation.name + assert operation_name \ No newline at end of file From 6b56cfe0a5ddf7f8a404218646ede02acf58977c Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 11 May 2020 23:04:17 +0000 Subject: [PATCH 3/5] remove unused import --- vision/automl/model_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/vision/automl/model_test.py b/vision/automl/model_test.py index a04747f408e..438a8a08926 100644 --- a/vision/automl/model_test.py +++ b/vision/automl/model_test.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime import os import uuid From 202d1cbd81a09a7990fb594d6f81ec3b95dc5c96 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 11 May 2020 23:33:12 +0000 Subject: [PATCH 4/5] use 32 character name --- translate/automl/model_test.py | 2 +- vision/automl/model_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/translate/automl/model_test.py b/translate/automl/model_test.py index 4a897097a48..eca489dbd9a 100644 --- a/translate/automl/model_test.py +++ b/translate/automl/model_test.py @@ -29,7 +29,7 @@ @pytest.fixture def model(): client = automl.AutoMlClient() - model_name = "test_" + str(uuid.uuid4()) + model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] project_location = client.location_path(project_id, compute_region) my_model = { "display_name": model_name, diff --git a/vision/automl/model_test.py b/vision/automl/model_test.py index 438a8a08926..bae516664c0 100644 --- a/vision/automl/model_test.py +++ b/vision/automl/model_test.py @@ -27,7 +27,7 @@ @pytest.fixture def model(): client = automl.AutoMlClient() - model_name = "test_" + str(uuid.uuid4()) + model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] project_location = client.location_path(project_id, compute_region) my_model = { "display_name": model_name, @@ -43,4 +43,4 @@ def model(): def test_model_create_status_delete(capsys, model): operation_name = model.operation.name - assert operation_name \ No newline at end of file + assert operation_name From b53a47b6f9e246e242c30c34519e757fead82c58 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 11 May 2020 23:35:33 +0000 Subject: [PATCH 5/5] remove noxfile --- translate/automl/noxfile.py | 153 ------------------------------------ 1 file changed, 153 deletions(-) delete mode 100644 translate/automl/noxfile.py diff --git a/translate/automl/noxfile.py b/translate/automl/noxfile.py deleted file mode 100644 index 9f2242194ce..00000000000 --- a/translate/automl/noxfile.py +++ /dev/null @@ -1,153 +0,0 @@ -# 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. - -from __future__ import print_function - -import os -from pathlib import Path - -import nox - - -# DO NOT EDIT - automatically generated. -# All versions used to tested samples. -ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = ["2.7"] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -# -# Style Checks -# - - -# Ignore I202 "Additional newline in a section of imports." to accommodate -# region tags in import blocks. Since we specify an explicit ignore, we also -# have to explicitly ignore the list of default ignores: -# `E121,E123,E126,E226,E24,E704,W503,W504` as shown by `flake8 --help`. -def _determine_local_import_names(start_dir): - """Determines all import names that should be considered "local". - - This is used when running the linter to insure that import order is - properly checked. - """ - file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] - return [ - basename - for basename, extension in file_ext_pairs - if extension == ".py" - or os.path.isdir(os.path.join(start_dir, basename)) - and basename not in ("__pycache__") - ] - - -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--import-order-style=google", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I100,I201,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session): - session.install("flake8", "flake8-import-order") - - local_names = _determine_local_import_names(".") - args = FLAKE8_COMMON_ARGS + [ - "--application-import-names", - ",".join(local_names), - ".", - ] - session.run("flake8", *args) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests(session, post_install=None): - """Runs py.test for a particular project.""" - if os.path.exists("requirements.txt"): - session.install("-r", "requirements.txt") - - if os.path.exists("requirements-test.txt"): - session.install("-r", "requirements-test.txt") - - if post_install: - post_install(session) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5] - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session): - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - print("SKIPPED: {} tests are disabled for this sample.".format(session.python)) - - -# -# Readmegen -# - - -def _get_repo_root(): - """ Returns the root folder of the project. """ - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session, path): - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - )