From 9219e811d5c657bff7880bf9e782b2c2ec9ae4ab Mon Sep 17 00:00:00 2001 From: t-karasova Date: Mon, 24 Jan 2022 17:57:30 +0100 Subject: [PATCH 01/16] feat: Retail Interactive Tutorials. Search service code samples --- .kokoro/presubmit/common.cfg | 4 + samples/interactive-tutorials/LICENSE | 202 +++++++++++++ samples/interactive-tutorials/README.md | 129 +++++++++ samples/interactive-tutorials/noxfile.py | 270 ++++++++++++++++++ .../interactive-tutorials/noxfile_config.py | 35 +++ .../interactive-tutorials/search/noxfile.py | 270 ++++++++++++++++++ .../search/noxfile_config.py | 35 +++ .../search/requirements-test.txt | 1 + .../search/requirements.txt | 4 + .../search/search_simple_query.py | 62 ++++ .../search/search_simple_query_test.py | 38 +++ .../search/search_with_boost_spec.py | 71 +++++ .../search/search_with_boost_spec_test.py | 39 +++ .../search/search_with_facet_spec.py | 69 +++++ .../search/search_with_facet_test.py | 41 +++ .../search/search_with_filtering.py | 63 ++++ .../search/search_with_filtering_test.py | 41 +++ .../search/search_with_ordering.py | 64 +++++ .../search/search_with_ordering_test.py | 38 +++ .../search/search_with_pagination.py | 76 +++++ .../search/search_with_pagination_test.py | 37 +++ .../search_with_query_expansion_spec.py | 68 +++++ .../search_with_query_expansion_test.py | 39 +++ 23 files changed, 1696 insertions(+) create mode 100644 samples/interactive-tutorials/LICENSE create mode 100644 samples/interactive-tutorials/README.md create mode 100644 samples/interactive-tutorials/noxfile.py create mode 100644 samples/interactive-tutorials/noxfile_config.py create mode 100644 samples/interactive-tutorials/search/noxfile.py create mode 100644 samples/interactive-tutorials/search/noxfile_config.py create mode 100644 samples/interactive-tutorials/search/requirements-test.txt create mode 100644 samples/interactive-tutorials/search/requirements.txt create mode 100644 samples/interactive-tutorials/search/search_simple_query.py create mode 100644 samples/interactive-tutorials/search/search_simple_query_test.py create mode 100644 samples/interactive-tutorials/search/search_with_boost_spec.py create mode 100644 samples/interactive-tutorials/search/search_with_boost_spec_test.py create mode 100644 samples/interactive-tutorials/search/search_with_facet_spec.py create mode 100644 samples/interactive-tutorials/search/search_with_facet_test.py create mode 100644 samples/interactive-tutorials/search/search_with_filtering.py create mode 100644 samples/interactive-tutorials/search/search_with_filtering_test.py create mode 100644 samples/interactive-tutorials/search/search_with_ordering.py create mode 100644 samples/interactive-tutorials/search/search_with_ordering_test.py create mode 100644 samples/interactive-tutorials/search/search_with_pagination.py create mode 100644 samples/interactive-tutorials/search/search_with_pagination_test.py create mode 100644 samples/interactive-tutorials/search/search_with_query_expansion_spec.py create mode 100644 samples/interactive-tutorials/search/search_with_query_expansion_test.py diff --git a/.kokoro/presubmit/common.cfg b/.kokoro/presubmit/common.cfg index a0f9b6a5..db5417dc 100644 --- a/.kokoro/presubmit/common.cfg +++ b/.kokoro/presubmit/common.cfg @@ -25,3 +25,7 @@ env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-retail/.kokoro/build.sh" } +env_vars: { + key: "SECRET_MANAGER_KEYS" + value: "retail-samples-secrets" +} diff --git a/samples/interactive-tutorials/LICENSE b/samples/interactive-tutorials/LICENSE new file mode 100644 index 00000000..7a4a3ea2 --- /dev/null +++ b/samples/interactive-tutorials/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. \ No newline at end of file diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md new file mode 100644 index 00000000..65fff84f --- /dev/null +++ b/samples/interactive-tutorials/README.md @@ -0,0 +1,129 @@ +# Google Retail Search: Python code samples +## Get started +### Select your project and enable the Retail API + +Google Cloud organizes resources into projects. This lets you +collect all the related resources for a single application in one place. + +If you don't have a Google Cloud project yet or you're not the owner of an existing one, you can +[create a new project](https://console.cloud.google.com/projectcreate). + +After the project is created, set your PROJECT_ID to a ```project``` variable. +1. Run the following command in Terminal: + ```bash + gcloud config set project + ``` + +1. Check that the Retail API is enabled for your Project in the [Admin Console](https://console.cloud.google.com/ai/retail/). + +### Set up authentication + +To run a code sample from the Cloud Shell, you need to authenticate. To do this, use the Application Default Credentials. + +1. Set your user credentials to authenticate your requests to the Retail API + + ```bash + gcloud auth application-default login + ``` + +1. Type `Y` and press **Enter**. Click the link in Terminal. A browser window should appear asking you to log in using your Gmail account. + +1. Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal. + +1. Run the code sample and check the Retail API in action. + +**Note**: Click the copy button on the side of the code box to paste the command in the Cloud Shell terminal and run it. + +### Set the PROJECT_NUMBER environment variable + +Because you are going to run the code samples in your own Google Cloud project, you should specify the **project_number** as an environment variable. It will be used in every request to the Retail API. + +1. You can find the ```project_number``` in the Project Info card displayed on **Home/Dashboard**. + +1. Set the environment variable with the following command: + ```bash + export PROJECT_NUMBER= + ``` + +### Install Google Cloud Retail libraries + +To run Python code samples for the Retail API tutorial, you need to set up your virtual environment. + +1. Run the following commands in a Terminal to create an isolated Python environment: + ```bash + pip install virtualenv + virtualenv myenv + source myenv/bin/activate + ``` +1. Next, install Google packages: + ```bash + pip install google + pip install google-cloud-retail + pip install google.cloud.storage + pip install google.cloud.bigquery + + ``` + +## Clone the Retail code samples + +This step is required if this is the first Retail API Tutorial you run. +Otherwise, you can skip it. + +Clone the Git repository with all the code samples to learn the Retail features and check them in action. + + +1. Run the following command in the Terminal: + ```bash + git clone https://github.com/t-karasova/grs-samples-python.git + ``` + + The code samples for each of the Retail services are stored in different directories. + +1. Go to the ```grs-samples-python``` directory. It's our starting point to run more commands. + ```bash + cd grs-samples-python + ``` + +## Import catalog data + +This step is required if this is the first Retail API Tutorial you run. +Otherwise, you can skip it. + +### Upload catalog data to Cloud Storage + +There is a JSON file with valid products prepared in the `product` directory: +`product/resources/products.json`. + +Another file, `product/resources/products_some_invalid.json`, contains both valid and invalid products, and you will use it to check the error handling. + +In your own project, create a Cloud Storage bucket and put the JSON file there. +The bucket name must be unique. For convenience, you can name it `_`. + +1. To create the bucket and upload the JSON file, run the following command in the Terminal: + + ```bash + python product/setup/create_gcs_bucket.py + ``` + + Now you can see the bucket is created in the [Cloud Storage](https://console.cloud.google.com/storage/browser), and the files are uploaded. + +1. The name of the created Retail Search bucket is printed in the Terminal. Copy the name and set it as the environment variable `BUCKET_NAME`: + + ```bash + export BUCKET_NAME= + ``` + +### Import products to the Retail Catalog + +To import the prepared products to a catalog, run the following command in the Terminal: + +```bash +python product/import_products_gcs.py +``` + +## Run your code sample + +Run the sample in a terminal with the following command: +```bash +python search/search_with_boost_spec.py +``` diff --git a/samples/interactive-tutorials/noxfile.py b/samples/interactive-tutorials/noxfile.py new file mode 100644 index 00000000..f63fefdd --- /dev/null +++ b/samples/interactive-tutorials/noxfile.py @@ -0,0 +1,270 @@ +# 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 sys +from typing import Callable, Dict, List, Optional + +import nox + + +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +BLACK_VERSION = "black==19.10b0" + +# Copy `noxfile_config.py` to your directory and modify it instead. + +# `TEST_CONFIG` dict is a configuration hook that allows users to +# modify the test configurations. The values here should be in sync +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. + +TEST_CONFIG = { + # You can opt out from the test for specific Python versions. + "ignored_versions": [], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "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': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} + + +try: + # Ensure we can import noxfile_config in the project's directory. + sys.path.append(".") + from noxfile_config import TEST_CONFIG_OVERRIDE +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) + TEST_CONFIG_OVERRIDE = {} + +# Update the TEST_CONFIG with the user supplied values. +TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) + + +def get_pytest_env_vars() -> Dict[str, str]: + """Returns a dict for pytest invocation.""" + ret = {} + + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG["gcloud_project_env"] + # This should error out if not set. + ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] + + # Apply user supplied envs. + ret.update(TEST_CONFIG["envs"]) + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to test samples. +ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( + "True", + "true", +) + +# Error if a python version is missing +nox.options.error_on_missing_interpreters = True + +# +# Style Checks +# + + +def _determine_local_import_names(start_dir: str) -> List[str]: + """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__") + ] + + +# Linting with flake8. +# +# We ignore the following rules: +# E203: whitespace before ‘:’ +# E266: too many leading ‘#’ for block comment +# E501: line too long +# I202: Additional newline in a section of imports +# +# We also need to specify the rules which are ignored by default: +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] +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,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session: nox.sessions.Session) -> None: + if not TEST_CONFIG["enforce_type_hints"]: + session.install("flake8", "flake8-import-order") + else: + session.install("flake8", "flake8-import-order", "flake8-annotations") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + ".", + ] + session.run("flake8", *args) + + +# +# Black +# + + +@nox.session +def blacken(session: nox.sessions.Session) -> None: + session.install(BLACK_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + session.run("black", *python_files) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests( + session: nox.sessions.Session, post_install: Callable = None +) -> None: + if TEST_CONFIG["pip_version_override"]: + pip_version = TEST_CONFIG["pip_version_override"] + session.install(f"pip=={pip_version}") + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + if os.path.exists("constraints.txt"): + session.install("-r", "requirements.txt", "-c", "constraints.txt") + else: + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + if os.path.exists("constraints-test.txt"): + session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") + else: + session.install("-r", "requirements-test.txt") + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + 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], + env=get_pytest_env_vars(), + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session: nox.sessions.Session) -> None: + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + session.skip( + "SKIPPED: {} tests are disabled for this sample.".format(session.python) + ) + + +# +# Readmegen +# + + +def _get_repo_root() -> Optional[str]: + """ 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) + # .git is not available in repos cloned via Cloud Build + # setup.py is always in the library's root, so use that instead + # https://github.com/googleapis/synthtool/issues/792 + if Path(p / "setup.py").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: nox.sessions.Session, path: str) -> None: + """(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/samples/interactive-tutorials/noxfile_config.py b/samples/interactive-tutorials/noxfile_config.py new file mode 100644 index 00000000..72486fb4 --- /dev/null +++ b/samples/interactive-tutorials/noxfile_config.py @@ -0,0 +1,35 @@ +# 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. +# 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.6", "3.8", "3.9"], + # An envvar key for determining the project id to use. Change it + # to 'PROJECT_NUMBER' 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": "PROJECT_NUMBER", + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": { + "DATA_LABELING_API_ENDPOINT": "us-central1-autopush-aiplatform.sandbox.googleapis.com", + "PYTEST_ADDOPTS": "-n=auto" # Run tests parallel using all available CPUs + }, +} diff --git a/samples/interactive-tutorials/search/noxfile.py b/samples/interactive-tutorials/search/noxfile.py new file mode 100644 index 00000000..f63fefdd --- /dev/null +++ b/samples/interactive-tutorials/search/noxfile.py @@ -0,0 +1,270 @@ +# 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 sys +from typing import Callable, Dict, List, Optional + +import nox + + +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING +# DO NOT EDIT THIS FILE EVER! +# WARNING - WARNING - WARNING - WARNING - WARNING +# WARNING - WARNING - WARNING - WARNING - WARNING + +BLACK_VERSION = "black==19.10b0" + +# Copy `noxfile_config.py` to your directory and modify it instead. + +# `TEST_CONFIG` dict is a configuration hook that allows users to +# modify the test configurations. The values here should be in sync +# with `noxfile_config.py`. Users will copy `noxfile_config.py` into +# their directory and modify it. + +TEST_CONFIG = { + # You can opt out from the test for specific Python versions. + "ignored_versions": [], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "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': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} + + +try: + # Ensure we can import noxfile_config in the project's directory. + sys.path.append(".") + from noxfile_config import TEST_CONFIG_OVERRIDE +except ImportError as e: + print("No user noxfile_config found: detail: {}".format(e)) + TEST_CONFIG_OVERRIDE = {} + +# Update the TEST_CONFIG with the user supplied values. +TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) + + +def get_pytest_env_vars() -> Dict[str, str]: + """Returns a dict for pytest invocation.""" + ret = {} + + # Override the GCLOUD_PROJECT and the alias. + env_key = TEST_CONFIG["gcloud_project_env"] + # This should error out if not set. + ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] + + # Apply user supplied envs. + ret.update(TEST_CONFIG["envs"]) + return ret + + +# DO NOT EDIT - automatically generated. +# All versions used to test samples. +ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] + +# Any default versions that should be ignored. +IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] + +TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) + +INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( + "True", + "true", +) + +# Error if a python version is missing +nox.options.error_on_missing_interpreters = True + +# +# Style Checks +# + + +def _determine_local_import_names(start_dir: str) -> List[str]: + """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__") + ] + + +# Linting with flake8. +# +# We ignore the following rules: +# E203: whitespace before ‘:’ +# E266: too many leading ‘#’ for block comment +# E501: line too long +# I202: Additional newline in a section of imports +# +# We also need to specify the rules which are ignored by default: +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] +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,I202", + "--max-line-length=88", +] + + +@nox.session +def lint(session: nox.sessions.Session) -> None: + if not TEST_CONFIG["enforce_type_hints"]: + session.install("flake8", "flake8-import-order") + else: + session.install("flake8", "flake8-import-order", "flake8-annotations") + + local_names = _determine_local_import_names(".") + args = FLAKE8_COMMON_ARGS + [ + "--application-import-names", + ",".join(local_names), + ".", + ] + session.run("flake8", *args) + + +# +# Black +# + + +@nox.session +def blacken(session: nox.sessions.Session) -> None: + session.install(BLACK_VERSION) + python_files = [path for path in os.listdir(".") if path.endswith(".py")] + + session.run("black", *python_files) + + +# +# Sample Tests +# + + +PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] + + +def _session_tests( + session: nox.sessions.Session, post_install: Callable = None +) -> None: + if TEST_CONFIG["pip_version_override"]: + pip_version = TEST_CONFIG["pip_version_override"] + session.install(f"pip=={pip_version}") + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + if os.path.exists("constraints.txt"): + session.install("-r", "requirements.txt", "-c", "constraints.txt") + else: + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + if os.path.exists("constraints-test.txt"): + session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") + else: + session.install("-r", "requirements-test.txt") + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + 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], + env=get_pytest_env_vars(), + ) + + +@nox.session(python=ALL_VERSIONS) +def py(session: nox.sessions.Session) -> None: + """Runs py.test for a sample using the specified version of Python.""" + if session.python in TESTED_VERSIONS: + _session_tests(session) + else: + session.skip( + "SKIPPED: {} tests are disabled for this sample.".format(session.python) + ) + + +# +# Readmegen +# + + +def _get_repo_root() -> Optional[str]: + """ 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) + # .git is not available in repos cloned via Cloud Build + # setup.py is always in the library's root, so use that instead + # https://github.com/googleapis/synthtool/issues/792 + if Path(p / "setup.py").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: nox.sessions.Session, path: str) -> None: + """(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/samples/interactive-tutorials/search/noxfile_config.py b/samples/interactive-tutorials/search/noxfile_config.py new file mode 100644 index 00000000..72486fb4 --- /dev/null +++ b/samples/interactive-tutorials/search/noxfile_config.py @@ -0,0 +1,35 @@ +# 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. +# 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.6", "3.8", "3.9"], + # An envvar key for determining the project id to use. Change it + # to 'PROJECT_NUMBER' 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": "PROJECT_NUMBER", + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": { + "DATA_LABELING_API_ENDPOINT": "us-central1-autopush-aiplatform.sandbox.googleapis.com", + "PYTEST_ADDOPTS": "-n=auto" # Run tests parallel using all available CPUs + }, +} diff --git a/samples/interactive-tutorials/search/requirements-test.txt b/samples/interactive-tutorials/search/requirements-test.txt new file mode 100644 index 00000000..9299a7a8 --- /dev/null +++ b/samples/interactive-tutorials/search/requirements-test.txt @@ -0,0 +1 @@ +pytest==6.2.5 \ No newline at end of file diff --git a/samples/interactive-tutorials/search/requirements.txt b/samples/interactive-tutorials/search/requirements.txt new file mode 100644 index 00000000..0ba6ea71 --- /dev/null +++ b/samples/interactive-tutorials/search/requirements.txt @@ -0,0 +1,4 @@ +google==3.0.0 +google-cloud-retail==1.1.0 +google-cloud-storage==1.43.0 +google-cloud-bigquery==2.30.1 \ No newline at end of file diff --git a/samples/interactive-tutorials/search/search_simple_query.py b/samples/interactive-tutorials/search/search_simple_query.py new file mode 100644 index 00000000..fefc0d3e --- /dev/null +++ b/samples/interactive-tutorials/search/search_simple_query.py @@ -0,0 +1,62 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_for_products_with_query_parameter] +# Call Retail API to search for a products in a catalog using only search query. +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str): + default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + + search_request = SearchRequest() + search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. + search_request.query = query + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.page_size = 10 + + print("---search request:---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT QUERY PHRASES HERE: + query_phrase = "Hoodie" + + search_request = get_search_request(query_phrase) + search_response = get_search_service_client().search(search_request) + + print("---search response---") + print(search_response) + return search_response + + +search() +# [END retail_search_for_products_with_query_parameter] diff --git a/samples/interactive-tutorials/search/search_simple_query_test.py b/samples/interactive-tutorials/search/search_simple_query_test.py new file mode 100644 index 00000000..70bf4389 --- /dev/null +++ b/samples/interactive-tutorials/search/search_simple_query_test.py @@ -0,0 +1,38 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_simple_query import search + + +def test_search_simple_query_pass(): + output = str(subprocess.check_output('python search/search_simple_query.py', + shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + + +def test_search_simple_query_response(): + response = search() + + assert len(response.results) == 10 + product_title = response.results[0].product.title + assert re.match('.*Hoodie.*', product_title) diff --git a/samples/interactive-tutorials/search/search_with_boost_spec.py b/samples/interactive-tutorials/search/search_with_boost_spec.py new file mode 100644 index 00000000..b5622106 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_boost_spec.py @@ -0,0 +1,71 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_product_with_boost_spec] +# Call Retail API to search for a products in a catalog, rerank the +# results boosting or burying the products that match defined condition. +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str, condition: str, boost_strength: float): + default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + + condition_boost_spec = SearchRequest.BoostSpec.ConditionBoostSpec() + condition_boost_spec.condition = condition + condition_boost_spec.boost = boost_strength + + boost_spec = SearchRequest.BoostSpec() + boost_spec.condition_boost_specs = [condition_boost_spec] + + search_request = SearchRequest() + search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. + search_request.query = query + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.boost_spec = boost_spec + search_request.page_size = 10 + + print("---search request---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT CONDITIONS HERE: + condition = '(colorFamilies: ANY("Blue"))' + boost = 0.0 + + search_request = get_search_request("Tee", condition, boost) + search_response = get_search_service_client().search(search_request) + print("---search response---") + print(search_response) + return search_response + + +search() +# [END retail_search_product_with_boost_spec] diff --git a/samples/interactive-tutorials/search/search_with_boost_spec_test.py b/samples/interactive-tutorials/search/search_with_boost_spec_test.py new file mode 100644 index 00000000..1306436b --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_boost_spec_test.py @@ -0,0 +1,39 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_with_boost_spec import search + + +def test_search_with_boost_spec_pass(): + output = str( + subprocess.check_output('python search/search_with_boost_spec.py', + shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + + +def test_search_with_boost_spec(): + response = search() + + assert len(response.results) == 10 + product_title = response.results[0].product.title + assert re.match('.*Tee.*', product_title) diff --git a/samples/interactive-tutorials/search/search_with_facet_spec.py b/samples/interactive-tutorials/search/search_with_facet_spec.py new file mode 100644 index 00000000..f76b68b4 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_facet_spec.py @@ -0,0 +1,69 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_product_with_facet_spec] +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient, Interval + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str, facet_key_param: str): + default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + # PUT THE INTERVALS HERE: + facet_key = SearchRequest.FacetSpec().FacetKey() + facet_key.key = facet_key_param + + facet_spec = SearchRequest.FacetSpec() + facet_spec.facet_key = facet_key + + search_request = SearchRequest() + search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. + search_request.query = query + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.facet_specs = [facet_spec] + search_request.page_size = 10 + + print("---search request---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT FACETS HERE: + facet_key = "colorFamilies" + + search_request = get_search_request("Tee", facet_key) + search_response = get_search_service_client().search(search_request) + print("---search response---") + print(search_response) + print("---facets:---") + print(search_response.facets) + return search_response + + +search() +# [END retail_search_product_with_facet_spec] diff --git a/samples/interactive-tutorials/search/search_with_facet_test.py b/samples/interactive-tutorials/search/search_with_facet_test.py new file mode 100644 index 00000000..6d06a534 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_facet_test.py @@ -0,0 +1,41 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_with_facet_spec import search + + +def test_search_with_filtering_pass(): + output = str( + subprocess.check_output('python search/search_with_facet_spec.py', + shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + assert re.match('.*facets.*?colorFamilies.*', output) + + +def test_search_with_filtering(): + response = search() + + assert len(response.results) == 10 + product_title = response.results[0].product.title + assert re.match('.*Tee.*', product_title) + assert response.facets[0].key == "colorFamilies" diff --git a/samples/interactive-tutorials/search/search_with_filtering.py b/samples/interactive-tutorials/search/search_with_filtering.py new file mode 100644 index 00000000..091f282b --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_filtering.py @@ -0,0 +1,63 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_for_products_with_filter] +# Call Retail API to search for a products in a catalog, filter the results by different product fields. +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str, _filter: str): + default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + + search_request = SearchRequest() + search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. + search_request.query = query + search_request.filter = _filter + search_request.page_size = 10 + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.page_size = 10 + + print("---search request:---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT FILTER EXPRESSIONS HERE: + filter_ = '(colorFamilies: ANY("Black"))' + + search_request = get_search_request("Tee", filter_) + search_response = get_search_service_client().search(search_request) + print("---search response---") + print(search_response) + return search_response + + +search() +# [END retail_search_for_products_with_filter] diff --git a/samples/interactive-tutorials/search/search_with_filtering_test.py b/samples/interactive-tutorials/search/search_with_filtering_test.py new file mode 100644 index 00000000..0f30c254 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_filtering_test.py @@ -0,0 +1,41 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_with_filtering import search + + +def test_search_with_filtering_pass(): + output = str( + subprocess.check_output('python search/search_with_filtering.py', + shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + + +def test_search_with_filtering(): + response = search() + + assert len(response.results) == 10 + product_title = response.results[0].product.title + assert re.match('.*Tee.*Black', product_title) + assert "Black" in response.results[0].product.color_info.color_families + assert response.total_size == 16 diff --git a/samples/interactive-tutorials/search/search_with_ordering.py b/samples/interactive-tutorials/search/search_with_ordering.py new file mode 100644 index 00000000..7e0f3a01 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_ordering.py @@ -0,0 +1,64 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_for_products_with_ordering] +# Call Retail API to search for a products in a catalog, order the results by different product fields. +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str, order: str): + default_search_placement = "projects/" + project_number + \ + "/locations/global/catalogs/default_catalog/placements/default_search" + + search_request = SearchRequest() + search_request.placement = default_search_placement + search_request.query = query + search_request.order_by = order + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.page_size = 10 + + print("---search request---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT ORDERING EXPRESSIONS HERE: + order = 'price desc' + + search_request = get_search_request("Hoodie", order) + search_response = get_search_service_client().search(search_request) + + print("---search response---") + print(search_response) + return search_response + + +search() +# [END retail_search_for_products_with_ordering] diff --git a/samples/interactive-tutorials/search/search_with_ordering_test.py b/samples/interactive-tutorials/search/search_with_ordering_test.py new file mode 100644 index 00000000..9f3b9be3 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_ordering_test.py @@ -0,0 +1,38 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_with_ordering import search + + +def test_search_with_ordering_pass(): + output = str( + subprocess.check_output('python search/search_with_ordering.py', + shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + + +def test_search_with_ordering(): + response = search() + + assert len(response.results) == 10 + assert response.results[0].product.price_info.price == 39 diff --git a/samples/interactive-tutorials/search/search_with_pagination.py b/samples/interactive-tutorials/search/search_with_pagination.py new file mode 100644 index 00000000..f2411f8f --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_pagination.py @@ -0,0 +1,76 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_for_products_with_page_size] +# Call Retail API to search for a products in a catalog, +# limit the number of the products per page and go to the next page using "next_page_token" +# or jump to chosen page using "offset". +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str, page_size: int, offset: int, + next_page_token: str): + default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + + search_request = SearchRequest() + search_request.placement = default_search_placement + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.query = query + search_request.page_size = page_size + search_request.offset = offset + search_request.page_token = next_page_token + + print("---search request:---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT PAGINATION PARAMETERS HERE: + page_size = 6 + offset = 0 + page_token = "" + + search_request_first_page = get_search_request("Hoodie", page_size, offset, + page_token) + search_response_first_page = get_search_service_client().search( + search_request_first_page) + + print("---search response---") + print(search_response_first_page) + + # PASTE CALL WITH NEXT PAGE TOKEN HERE: + + # PASTE CALL WITH OFFSET HERE: + + return search_response_first_page + + +search() +# [END retail_search_for_products_with_page_size] diff --git a/samples/interactive-tutorials/search/search_with_pagination_test.py b/samples/interactive-tutorials/search/search_with_pagination_test.py new file mode 100644 index 00000000..abfb44f9 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_pagination_test.py @@ -0,0 +1,37 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_with_pagination import search + + +def test_search_with_pagination_pass(): + output = str(subprocess.check_output('python search/search_with_pagination.py', shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + + +def test_search_with_pagination(): + response = search() + + product_title = response.results[0].product.title + assert re.match('.*Hoodie', product_title) + assert len(response.results) == 6 diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py new file mode 100644 index 00000000..a96fd74d --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py @@ -0,0 +1,68 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# [START retail_search_for_products_with_query_expansion_specification] +# Call Retail API to search for a products in a catalog, +# enabling the query expansion feature to let the Google Retail Search build an automatic query expansion. +# +import os + +from google.api_core.client_options import ClientOptions +from google.cloud.retail import SearchRequest, SearchServiceClient + +project_number = os.getenv('PROJECT_NUMBER') + + +# get search service client +def get_search_service_client(): + client_options = ClientOptions("retail.googleapis.com") + return SearchServiceClient(client_options=client_options) + + +# get search service request: +def get_search_request(query: str, + condition: SearchRequest.QueryExpansionSpec.Condition): + default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + + query_expansion_spec = SearchRequest().QueryExpansionSpec() + query_expansion_spec.condition = condition + + search_request = SearchRequest() + search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. + search_request.query = query + search_request.visitor_id = "123456" # A unique identifier to track visitors + search_request.query_expansion_spec = query_expansion_spec + search_request.page_size = 10 + + print("---search request:---") + print(search_request) + + return search_request + + +# call the Retail Search: +def search(): + # TRY DIFFERENT QUERY EXPANSION CONDITION HERE: + condition = SearchRequest.QueryExpansionSpec.Condition.AUTO + + search_request = get_search_request("Google Youth Hero Tee Grey", condition) + search_response = get_search_service_client().search(search_request) + + print("---search response---") + print(search_response) + return search_response + + +search() +# [END retail_search_for_products_with_query_expansion_specification] diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_test.py b/samples/interactive-tutorials/search/search_with_query_expansion_test.py new file mode 100644 index 00000000..01198e91 --- /dev/null +++ b/samples/interactive-tutorials/search/search_with_query_expansion_test.py @@ -0,0 +1,39 @@ +# Copyright 2021 Google Inc. All Rights Reserved. +# +# 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. + +# Call Retail API to search for a products in a catalog using only search query. +# +import re +import subprocess + +from search_with_query_expansion_spec import search + + +def test_search_with_query_expansion_spec_pass(): + output = str(subprocess.check_output( + 'python search/search_with_query_expansion_spec.py', shell=True)) + + assert re.match('.*search request.*', output) + assert re.match('.*search response.*', output) + # check the response contains some products + assert re.match('.*results.*id.*', output) + + +def test_search_with_query_expansion_spec(): + response = search() + + assert len(response.results) == 10 + assert response.results[0].product.title == 'Google Youth Hero Tee Grey' + assert response.results[2].product.title != 'Google Youth Hero Tee Grey' + assert response.query_expansion_info.expanded_query is True From 0f6ee7d7d50fec14e1620322b62abe42858165ee Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 27 Jan 2022 12:45:19 +0000 Subject: [PATCH 02/16] use default project for testing purposes --- samples/interactive-tutorials/noxfile_config.py | 4 ++-- samples/interactive-tutorials/search/noxfile_config.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/interactive-tutorials/noxfile_config.py b/samples/interactive-tutorials/noxfile_config.py index 72486fb4..cab99328 100644 --- a/samples/interactive-tutorials/noxfile_config.py +++ b/samples/interactive-tutorials/noxfile_config.py @@ -24,8 +24,8 @@ # to 'PROJECT_NUMBER' 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": "PROJECT_NUMBER", + '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": { diff --git a/samples/interactive-tutorials/search/noxfile_config.py b/samples/interactive-tutorials/search/noxfile_config.py index 72486fb4..cab99328 100644 --- a/samples/interactive-tutorials/search/noxfile_config.py +++ b/samples/interactive-tutorials/search/noxfile_config.py @@ -24,8 +24,8 @@ # to 'PROJECT_NUMBER' 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": "PROJECT_NUMBER", + '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": { From 9f2cc024be0be9304c785aa650f99b82a6be66c8 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 27 Jan 2022 14:56:00 +0000 Subject: [PATCH 03/16] add pytest-xdist as a requirement for testing --- samples/interactive-tutorials/search/requirements-test.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/interactive-tutorials/search/requirements-test.txt b/samples/interactive-tutorials/search/requirements-test.txt index 9299a7a8..bbf73145 100644 --- a/samples/interactive-tutorials/search/requirements-test.txt +++ b/samples/interactive-tutorials/search/requirements-test.txt @@ -1 +1,2 @@ -pytest==6.2.5 \ No newline at end of file +pytest==6.2.5 +pytest-xdist==2.5.0 From 3b5d5fb9d0b1ae8a50662f9a682bf68c02c21666 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 27 Jan 2022 15:22:44 +0000 Subject: [PATCH 04/16] fail on missing environment variable --- samples/interactive-tutorials/noxfile_config.py | 2 +- samples/interactive-tutorials/search/noxfile_config.py | 2 +- samples/interactive-tutorials/search/search_simple_query.py | 2 +- samples/interactive-tutorials/search/search_with_boost_spec.py | 2 +- samples/interactive-tutorials/search/search_with_facet_spec.py | 2 +- samples/interactive-tutorials/search/search_with_filtering.py | 2 +- samples/interactive-tutorials/search/search_with_ordering.py | 2 +- samples/interactive-tutorials/search/search_with_pagination.py | 2 +- .../search/search_with_query_expansion_spec.py | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/samples/interactive-tutorials/noxfile_config.py b/samples/interactive-tutorials/noxfile_config.py index cab99328..669040fe 100644 --- a/samples/interactive-tutorials/noxfile_config.py +++ b/samples/interactive-tutorials/noxfile_config.py @@ -21,7 +21,7 @@ # You can opt out from the test for specific Python versions. "ignored_versions": ["2.7", "3.6", "3.8", "3.9"], # An envvar key for determining the project id to use. Change it - # to 'PROJECT_NUMBER' if you want to opt in using a + # 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', diff --git a/samples/interactive-tutorials/search/noxfile_config.py b/samples/interactive-tutorials/search/noxfile_config.py index cab99328..669040fe 100644 --- a/samples/interactive-tutorials/search/noxfile_config.py +++ b/samples/interactive-tutorials/search/noxfile_config.py @@ -21,7 +21,7 @@ # You can opt out from the test for specific Python versions. "ignored_versions": ["2.7", "3.6", "3.8", "3.9"], # An envvar key for determining the project id to use. Change it - # to 'PROJECT_NUMBER' if you want to opt in using a + # 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', diff --git a/samples/interactive-tutorials/search/search_simple_query.py b/samples/interactive-tutorials/search/search_simple_query.py index fefc0d3e..e46e7f09 100644 --- a/samples/interactive-tutorials/search/search_simple_query.py +++ b/samples/interactive-tutorials/search/search_simple_query.py @@ -20,7 +20,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client diff --git a/samples/interactive-tutorials/search/search_with_boost_spec.py b/samples/interactive-tutorials/search/search_with_boost_spec.py index b5622106..e24acf05 100644 --- a/samples/interactive-tutorials/search/search_with_boost_spec.py +++ b/samples/interactive-tutorials/search/search_with_boost_spec.py @@ -21,7 +21,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client diff --git a/samples/interactive-tutorials/search/search_with_facet_spec.py b/samples/interactive-tutorials/search/search_with_facet_spec.py index f76b68b4..576656d7 100644 --- a/samples/interactive-tutorials/search/search_with_facet_spec.py +++ b/samples/interactive-tutorials/search/search_with_facet_spec.py @@ -19,7 +19,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient, Interval -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client diff --git a/samples/interactive-tutorials/search/search_with_filtering.py b/samples/interactive-tutorials/search/search_with_filtering.py index 091f282b..ab108d1b 100644 --- a/samples/interactive-tutorials/search/search_with_filtering.py +++ b/samples/interactive-tutorials/search/search_with_filtering.py @@ -20,7 +20,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client diff --git a/samples/interactive-tutorials/search/search_with_ordering.py b/samples/interactive-tutorials/search/search_with_ordering.py index 7e0f3a01..58d3c7d7 100644 --- a/samples/interactive-tutorials/search/search_with_ordering.py +++ b/samples/interactive-tutorials/search/search_with_ordering.py @@ -20,7 +20,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client diff --git a/samples/interactive-tutorials/search/search_with_pagination.py b/samples/interactive-tutorials/search/search_with_pagination.py index f2411f8f..9da66ee7 100644 --- a/samples/interactive-tutorials/search/search_with_pagination.py +++ b/samples/interactive-tutorials/search/search_with_pagination.py @@ -22,7 +22,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py index a96fd74d..c2b39278 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py @@ -21,7 +21,7 @@ from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.environ['PROJECT_NUMBER'] # get search service client From c8d8239198ae788a7e9df13ab6a94b10ad2c0323 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 27 Jan 2022 16:06:44 +0000 Subject: [PATCH 05/16] for testing purposes --- .kokoro/samples/python3.7/common.cfg | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.kokoro/samples/python3.7/common.cfg b/.kokoro/samples/python3.7/common.cfg index 4ae22aa9..6750331e 100644 --- a/.kokoro/samples/python3.7/common.cfg +++ b/.kokoro/samples/python3.7/common.cfg @@ -37,4 +37,9 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-retail/.kokoro/trampoline_v2.sh" \ No newline at end of file +build_file: "python-retail/.kokoro/trampoline_v2.sh" + +env_vars: { + key: "SECRET_MANAGER_KEYS" + value: "retail-samples-secrets" +} \ No newline at end of file From 854c1885ef7f95f534569604b65713b9414f422c Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 27 Jan 2022 16:58:14 +0000 Subject: [PATCH 06/16] for testing purposes --- .trampolinerc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.trampolinerc b/.trampolinerc index 0eee72ab..28ce747e 100644 --- a/.trampolinerc +++ b/.trampolinerc @@ -61,3 +61,6 @@ fi if [[ -z "${TRAMPOLINE_BUILD_FILE:-}" ]]; then TRAMPOLINE_BUILD_FILE="" fi + +# Secret Manager secrets. +source ${PROJECT_ROOT}/.kokoro/populate-secrets.sh From 8c59d0a00ecc6f1d62f63f8fc560518b25d9c5ac Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Thu, 27 Jan 2022 17:20:27 +0000 Subject: [PATCH 07/16] for testing purposes --- .kokoro/test-samples-impl.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.kokoro/test-samples-impl.sh b/.kokoro/test-samples-impl.sh index 8a324c9c..87b4a2b8 100755 --- a/.kokoro/test-samples-impl.sh +++ b/.kokoro/test-samples-impl.sh @@ -48,6 +48,11 @@ fi # - testing/client-secrets.json ./scripts/decrypt-secrets.sh +for FILE in ${KOKORO_GFILE_DIR}/secret_manager/*-samples-secrets; do + [[ -f "$FILE" ]] || continue + source "$FILE" +done + source ./testing/test-env.sh export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json From bb74497ca55c2bfeecf3f5c04e25d258f21910ad Mon Sep 17 00:00:00 2001 From: t-karasova Date: Fri, 28 Jan 2022 12:37:10 +0100 Subject: [PATCH 08/16] updated according to the review comments --- .../interactive-tutorials/search/noxfile_config.py | 6 +----- .../search/search_simple_query.py | 9 +-------- .../search/search_with_boost_spec.py | 9 +-------- .../search/search_with_facet_spec.py | 11 ++--------- .../search/search_with_filtering.py | 9 +-------- .../search/search_with_ordering.py | 9 +-------- .../search/search_with_pagination.py | 9 +-------- .../search/search_with_query_expansion_spec.py | 9 +-------- 8 files changed, 9 insertions(+), 62 deletions(-) diff --git a/samples/interactive-tutorials/search/noxfile_config.py b/samples/interactive-tutorials/search/noxfile_config.py index 669040fe..04697bd0 100644 --- a/samples/interactive-tutorials/search/noxfile_config.py +++ b/samples/interactive-tutorials/search/noxfile_config.py @@ -19,7 +19,7 @@ TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. - "ignored_versions": ["2.7", "3.6", "3.8", "3.9"], + "ignored_versions": ["2.7", "3.6"], # 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 @@ -28,8 +28,4 @@ # '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": { - "DATA_LABELING_API_ENDPOINT": "us-central1-autopush-aiplatform.sandbox.googleapis.com", - "PYTEST_ADDOPTS": "-n=auto" # Run tests parallel using all available CPUs - }, } diff --git a/samples/interactive-tutorials/search/search_simple_query.py b/samples/interactive-tutorials/search/search_simple_query.py index e46e7f09..3c7061ad 100644 --- a/samples/interactive-tutorials/search/search_simple_query.py +++ b/samples/interactive-tutorials/search/search_simple_query.py @@ -17,18 +17,11 @@ # import os -from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str): default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" @@ -51,7 +44,7 @@ def search(): query_phrase = "Hoodie" search_request = get_search_request(query_phrase) - search_response = get_search_service_client().search(search_request) + search_response = SearchServiceClient().search(search_request) print("---search response---") print(search_response) diff --git a/samples/interactive-tutorials/search/search_with_boost_spec.py b/samples/interactive-tutorials/search/search_with_boost_spec.py index e24acf05..79937dc0 100644 --- a/samples/interactive-tutorials/search/search_with_boost_spec.py +++ b/samples/interactive-tutorials/search/search_with_boost_spec.py @@ -18,18 +18,11 @@ # import os -from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str, condition: str, boost_strength: float): default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" @@ -61,7 +54,7 @@ def search(): boost = 0.0 search_request = get_search_request("Tee", condition, boost) - search_response = get_search_service_client().search(search_request) + search_response = SearchServiceClient().search(search_request) print("---search response---") print(search_response) return search_response diff --git a/samples/interactive-tutorials/search/search_with_facet_spec.py b/samples/interactive-tutorials/search/search_with_facet_spec.py index 576656d7..ac7fa62d 100644 --- a/samples/interactive-tutorials/search/search_with_facet_spec.py +++ b/samples/interactive-tutorials/search/search_with_facet_spec.py @@ -16,18 +16,11 @@ # import os -from google.api_core.client_options import ClientOptions -from google.cloud.retail import SearchRequest, SearchServiceClient, Interval +from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str, facet_key_param: str): default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" @@ -57,7 +50,7 @@ def search(): facet_key = "colorFamilies" search_request = get_search_request("Tee", facet_key) - search_response = get_search_service_client().search(search_request) + search_response = SearchServiceClient().search(search_request) print("---search response---") print(search_response) print("---facets:---") diff --git a/samples/interactive-tutorials/search/search_with_filtering.py b/samples/interactive-tutorials/search/search_with_filtering.py index ab108d1b..bcb07d3a 100644 --- a/samples/interactive-tutorials/search/search_with_filtering.py +++ b/samples/interactive-tutorials/search/search_with_filtering.py @@ -17,18 +17,11 @@ # import os -from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str, _filter: str): default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" @@ -53,7 +46,7 @@ def search(): filter_ = '(colorFamilies: ANY("Black"))' search_request = get_search_request("Tee", filter_) - search_response = get_search_service_client().search(search_request) + search_response = SearchServiceClient().search(search_request) print("---search response---") print(search_response) return search_response diff --git a/samples/interactive-tutorials/search/search_with_ordering.py b/samples/interactive-tutorials/search/search_with_ordering.py index 58d3c7d7..50388ff6 100644 --- a/samples/interactive-tutorials/search/search_with_ordering.py +++ b/samples/interactive-tutorials/search/search_with_ordering.py @@ -17,18 +17,11 @@ # import os -from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str, order: str): default_search_placement = "projects/" + project_number + \ @@ -53,7 +46,7 @@ def search(): order = 'price desc' search_request = get_search_request("Hoodie", order) - search_response = get_search_service_client().search(search_request) + search_response = SearchServiceClient().search(search_request) print("---search response---") print(search_response) diff --git a/samples/interactive-tutorials/search/search_with_pagination.py b/samples/interactive-tutorials/search/search_with_pagination.py index 9da66ee7..b2bcbe90 100644 --- a/samples/interactive-tutorials/search/search_with_pagination.py +++ b/samples/interactive-tutorials/search/search_with_pagination.py @@ -19,18 +19,11 @@ # import os -from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str, page_size: int, offset: int, next_page_token: str): @@ -59,7 +52,7 @@ def search(): search_request_first_page = get_search_request("Hoodie", page_size, offset, page_token) - search_response_first_page = get_search_service_client().search( + search_response_first_page = SearchServiceClient().search( search_request_first_page) print("---search response---") diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py index c2b39278..8d68a487 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py @@ -18,18 +18,11 @@ # import os -from google.api_core.client_options import ClientOptions from google.cloud.retail import SearchRequest, SearchServiceClient project_number = os.environ['PROJECT_NUMBER'] -# get search service client -def get_search_service_client(): - client_options = ClientOptions("retail.googleapis.com") - return SearchServiceClient(client_options=client_options) - - # get search service request: def get_search_request(query: str, condition: SearchRequest.QueryExpansionSpec.Condition): @@ -57,7 +50,7 @@ def search(): condition = SearchRequest.QueryExpansionSpec.Condition.AUTO search_request = get_search_request("Google Youth Hero Tee Grey", condition) - search_response = get_search_service_client().search(search_request) + search_response = SearchServiceClient().search(search_request) print("---search response---") print(search_response) From 35aec41b5d51f1d0147dc6363e8a9a3c6a58635b Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 28 Jan 2022 12:31:24 +0000 Subject: [PATCH 09/16] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- .kokoro/presubmit/common.cfg | 4 - .kokoro/samples/python3.7/common.cfg | 7 +- .kokoro/test-samples-impl.sh | 5 -- .trampolinerc | 3 - samples/interactive-tutorials/noxfile.py | 1 - .../interactive-tutorials/noxfile_config.py | 4 +- .../interactive-tutorials/search/noxfile.py | 73 +++++++++++-------- .../search/noxfile_config.py | 2 +- .../search/search_simple_query.py | 8 +- .../search/search_simple_query_test.py | 13 ++-- .../search/search_with_boost_spec.py | 8 +- .../search/search_with_boost_spec_test.py | 12 +-- .../search/search_with_facet_spec.py | 8 +- .../search/search_with_facet_test.py | 14 ++-- .../search/search_with_filtering.py | 8 +- .../search/search_with_filtering_test.py | 12 +-- .../search/search_with_ordering.py | 11 ++- .../search/search_with_ordering_test.py | 10 +-- .../search/search_with_pagination.py | 19 +++-- .../search/search_with_pagination_test.py | 12 +-- .../search_with_query_expansion_spec.py | 13 +++- .../search_with_query_expansion_test.py | 19 +++-- 22 files changed, 145 insertions(+), 121 deletions(-) diff --git a/.kokoro/presubmit/common.cfg b/.kokoro/presubmit/common.cfg index db5417dc..a0f9b6a5 100644 --- a/.kokoro/presubmit/common.cfg +++ b/.kokoro/presubmit/common.cfg @@ -25,7 +25,3 @@ env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-retail/.kokoro/build.sh" } -env_vars: { - key: "SECRET_MANAGER_KEYS" - value: "retail-samples-secrets" -} diff --git a/.kokoro/samples/python3.7/common.cfg b/.kokoro/samples/python3.7/common.cfg index 6750331e..4ae22aa9 100644 --- a/.kokoro/samples/python3.7/common.cfg +++ b/.kokoro/samples/python3.7/common.cfg @@ -37,9 +37,4 @@ gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples" gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" # Use the trampoline script to run in docker. -build_file: "python-retail/.kokoro/trampoline_v2.sh" - -env_vars: { - key: "SECRET_MANAGER_KEYS" - value: "retail-samples-secrets" -} \ No newline at end of file +build_file: "python-retail/.kokoro/trampoline_v2.sh" \ No newline at end of file diff --git a/.kokoro/test-samples-impl.sh b/.kokoro/test-samples-impl.sh index 87b4a2b8..8a324c9c 100755 --- a/.kokoro/test-samples-impl.sh +++ b/.kokoro/test-samples-impl.sh @@ -48,11 +48,6 @@ fi # - testing/client-secrets.json ./scripts/decrypt-secrets.sh -for FILE in ${KOKORO_GFILE_DIR}/secret_manager/*-samples-secrets; do - [[ -f "$FILE" ]] || continue - source "$FILE" -done - source ./testing/test-env.sh export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json diff --git a/.trampolinerc b/.trampolinerc index 28ce747e..0eee72ab 100644 --- a/.trampolinerc +++ b/.trampolinerc @@ -61,6 +61,3 @@ fi if [[ -z "${TRAMPOLINE_BUILD_FILE:-}" ]]; then TRAMPOLINE_BUILD_FILE="" fi - -# Secret Manager secrets. -source ${PROJECT_ROOT}/.kokoro/populate-secrets.sh diff --git a/samples/interactive-tutorials/noxfile.py b/samples/interactive-tutorials/noxfile.py index f63fefdd..1e30748a 100644 --- a/samples/interactive-tutorials/noxfile.py +++ b/samples/interactive-tutorials/noxfile.py @@ -267,4 +267,3 @@ def readmegen(session: nox.sessions.Session, path: str) -> None: session.run( "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file ) - diff --git a/samples/interactive-tutorials/noxfile_config.py b/samples/interactive-tutorials/noxfile_config.py index 669040fe..9b9b1756 100644 --- a/samples/interactive-tutorials/noxfile_config.py +++ b/samples/interactive-tutorials/noxfile_config.py @@ -24,12 +24,12 @@ # 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": { "DATA_LABELING_API_ENDPOINT": "us-central1-autopush-aiplatform.sandbox.googleapis.com", - "PYTEST_ADDOPTS": "-n=auto" # Run tests parallel using all available CPUs + "PYTEST_ADDOPTS": "-n=auto", # Run tests parallel using all available CPUs }, } diff --git a/samples/interactive-tutorials/search/noxfile.py b/samples/interactive-tutorials/search/noxfile.py index f63fefdd..20cdfc62 100644 --- a/samples/interactive-tutorials/search/noxfile.py +++ b/samples/interactive-tutorials/search/noxfile.py @@ -14,6 +14,7 @@ from __future__ import print_function +import glob import os from pathlib import Path import sys @@ -109,6 +110,7 @@ def get_pytest_env_vars() -> Dict[str, str]: def _determine_local_import_names(start_dir: str) -> List[str]: """Determines all import names that should be considered "local". + This is used when running the linter to insure that import order is properly checked. """ @@ -183,37 +185,45 @@ def blacken(session: nox.sessions.Session) -> None: def _session_tests( session: nox.sessions.Session, post_install: Callable = None ) -> None: - if TEST_CONFIG["pip_version_override"]: - pip_version = TEST_CONFIG["pip_version_override"] - session.install(f"pip=={pip_version}") - """Runs py.test for a particular project.""" - if os.path.exists("requirements.txt"): - if os.path.exists("constraints.txt"): - session.install("-r", "requirements.txt", "-c", "constraints.txt") - else: - session.install("-r", "requirements.txt") - - if os.path.exists("requirements-test.txt"): - if os.path.exists("constraints-test.txt"): - session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") - else: - session.install("-r", "requirements-test.txt") - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - 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], - env=get_pytest_env_vars(), - ) + # check for presence of tests + test_list = glob.glob("*_test.py") + glob.glob("test_*.py") + test_list.extend(glob.glob("tests")) + if len(test_list) == 0: + print("No tests found, skipping directory.") + else: + if TEST_CONFIG["pip_version_override"]: + pip_version = TEST_CONFIG["pip_version_override"] + session.install(f"pip=={pip_version}") + """Runs py.test for a particular project.""" + if os.path.exists("requirements.txt"): + if os.path.exists("constraints.txt"): + session.install("-r", "requirements.txt", "-c", "constraints.txt") + else: + session.install("-r", "requirements.txt") + + if os.path.exists("requirements-test.txt"): + if os.path.exists("constraints-test.txt"): + session.install( + "-r", "requirements-test.txt", "-c", "constraints-test.txt" + ) + else: + session.install("-r", "requirements-test.txt") + + if INSTALL_LIBRARY_FROM_SOURCE: + session.install("-e", _get_repo_root()) + + 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], + env=get_pytest_env_vars(), + ) @nox.session(python=ALL_VERSIONS) @@ -267,4 +277,3 @@ def readmegen(session: nox.sessions.Session, path: str) -> None: session.run( "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file ) - diff --git a/samples/interactive-tutorials/search/noxfile_config.py b/samples/interactive-tutorials/search/noxfile_config.py index 04697bd0..dfb6a937 100644 --- a/samples/interactive-tutorials/search/noxfile_config.py +++ b/samples/interactive-tutorials/search/noxfile_config.py @@ -24,7 +24,7 @@ # 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. diff --git a/samples/interactive-tutorials/search/search_simple_query.py b/samples/interactive-tutorials/search/search_simple_query.py index 3c7061ad..b71c3d8d 100644 --- a/samples/interactive-tutorials/search/search_simple_query.py +++ b/samples/interactive-tutorials/search/search_simple_query.py @@ -19,12 +19,16 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: def get_search_request(query: str): - default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) search_request = SearchRequest() search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. diff --git a/samples/interactive-tutorials/search/search_simple_query_test.py b/samples/interactive-tutorials/search/search_simple_query_test.py index 70bf4389..5f4a34e0 100644 --- a/samples/interactive-tutorials/search/search_simple_query_test.py +++ b/samples/interactive-tutorials/search/search_simple_query_test.py @@ -21,13 +21,14 @@ def test_search_simple_query_pass(): - output = str(subprocess.check_output('python search/search_simple_query.py', - shell=True)) + output = str( + subprocess.check_output("python search/search_simple_query.py", shell=True) + ) - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) + assert re.match(".*results.*id.*", output) def test_search_simple_query_response(): @@ -35,4 +36,4 @@ def test_search_simple_query_response(): assert len(response.results) == 10 product_title = response.results[0].product.title - assert re.match('.*Hoodie.*', product_title) + assert re.match(".*Hoodie.*", product_title) diff --git a/samples/interactive-tutorials/search/search_with_boost_spec.py b/samples/interactive-tutorials/search/search_with_boost_spec.py index 79937dc0..6e42658b 100644 --- a/samples/interactive-tutorials/search/search_with_boost_spec.py +++ b/samples/interactive-tutorials/search/search_with_boost_spec.py @@ -20,12 +20,16 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: def get_search_request(query: str, condition: str, boost_strength: float): - default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) condition_boost_spec = SearchRequest.BoostSpec.ConditionBoostSpec() condition_boost_spec.condition = condition diff --git a/samples/interactive-tutorials/search/search_with_boost_spec_test.py b/samples/interactive-tutorials/search/search_with_boost_spec_test.py index 1306436b..c695888d 100644 --- a/samples/interactive-tutorials/search/search_with_boost_spec_test.py +++ b/samples/interactive-tutorials/search/search_with_boost_spec_test.py @@ -22,13 +22,13 @@ def test_search_with_boost_spec_pass(): output = str( - subprocess.check_output('python search/search_with_boost_spec.py', - shell=True)) + subprocess.check_output("python search/search_with_boost_spec.py", shell=True) + ) - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) + assert re.match(".*results.*id.*", output) def test_search_with_boost_spec(): @@ -36,4 +36,4 @@ def test_search_with_boost_spec(): assert len(response.results) == 10 product_title = response.results[0].product.title - assert re.match('.*Tee.*', product_title) + assert re.match(".*Tee.*", product_title) diff --git a/samples/interactive-tutorials/search/search_with_facet_spec.py b/samples/interactive-tutorials/search/search_with_facet_spec.py index ac7fa62d..9763b492 100644 --- a/samples/interactive-tutorials/search/search_with_facet_spec.py +++ b/samples/interactive-tutorials/search/search_with_facet_spec.py @@ -18,12 +18,16 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: def get_search_request(query: str, facet_key_param: str): - default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) # PUT THE INTERVALS HERE: facet_key = SearchRequest.FacetSpec().FacetKey() facet_key.key = facet_key_param diff --git a/samples/interactive-tutorials/search/search_with_facet_test.py b/samples/interactive-tutorials/search/search_with_facet_test.py index 6d06a534..a67718dc 100644 --- a/samples/interactive-tutorials/search/search_with_facet_test.py +++ b/samples/interactive-tutorials/search/search_with_facet_test.py @@ -22,14 +22,14 @@ def test_search_with_filtering_pass(): output = str( - subprocess.check_output('python search/search_with_facet_spec.py', - shell=True)) + subprocess.check_output("python search/search_with_facet_spec.py", shell=True) + ) - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) - assert re.match('.*facets.*?colorFamilies.*', output) + assert re.match(".*results.*id.*", output) + assert re.match(".*facets.*?colorFamilies.*", output) def test_search_with_filtering(): @@ -37,5 +37,5 @@ def test_search_with_filtering(): assert len(response.results) == 10 product_title = response.results[0].product.title - assert re.match('.*Tee.*', product_title) + assert re.match(".*Tee.*", product_title) assert response.facets[0].key == "colorFamilies" diff --git a/samples/interactive-tutorials/search/search_with_filtering.py b/samples/interactive-tutorials/search/search_with_filtering.py index bcb07d3a..abf1b1d3 100644 --- a/samples/interactive-tutorials/search/search_with_filtering.py +++ b/samples/interactive-tutorials/search/search_with_filtering.py @@ -19,12 +19,16 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: def get_search_request(query: str, _filter: str): - default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) search_request = SearchRequest() search_request.placement = default_search_placement # Placement is used to identify the Serving Config name. diff --git a/samples/interactive-tutorials/search/search_with_filtering_test.py b/samples/interactive-tutorials/search/search_with_filtering_test.py index 0f30c254..f3111043 100644 --- a/samples/interactive-tutorials/search/search_with_filtering_test.py +++ b/samples/interactive-tutorials/search/search_with_filtering_test.py @@ -22,13 +22,13 @@ def test_search_with_filtering_pass(): output = str( - subprocess.check_output('python search/search_with_filtering.py', - shell=True)) + subprocess.check_output("python search/search_with_filtering.py", shell=True) + ) - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) + assert re.match(".*results.*id.*", output) def test_search_with_filtering(): @@ -36,6 +36,6 @@ def test_search_with_filtering(): assert len(response.results) == 10 product_title = response.results[0].product.title - assert re.match('.*Tee.*Black', product_title) + assert re.match(".*Tee.*Black", product_title) assert "Black" in response.results[0].product.color_info.color_families assert response.total_size == 16 diff --git a/samples/interactive-tutorials/search/search_with_ordering.py b/samples/interactive-tutorials/search/search_with_ordering.py index 50388ff6..a5cb3fd6 100644 --- a/samples/interactive-tutorials/search/search_with_ordering.py +++ b/samples/interactive-tutorials/search/search_with_ordering.py @@ -19,13 +19,16 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: def get_search_request(query: str, order: str): - default_search_placement = "projects/" + project_number + \ - "/locations/global/catalogs/default_catalog/placements/default_search" + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) search_request = SearchRequest() search_request.placement = default_search_placement @@ -43,7 +46,7 @@ def get_search_request(query: str, order: str): # call the Retail Search: def search(): # TRY DIFFERENT ORDERING EXPRESSIONS HERE: - order = 'price desc' + order = "price desc" search_request = get_search_request("Hoodie", order) search_response = SearchServiceClient().search(search_request) diff --git a/samples/interactive-tutorials/search/search_with_ordering_test.py b/samples/interactive-tutorials/search/search_with_ordering_test.py index 9f3b9be3..8d360056 100644 --- a/samples/interactive-tutorials/search/search_with_ordering_test.py +++ b/samples/interactive-tutorials/search/search_with_ordering_test.py @@ -22,13 +22,13 @@ def test_search_with_ordering_pass(): output = str( - subprocess.check_output('python search/search_with_ordering.py', - shell=True)) + subprocess.check_output("python search/search_with_ordering.py", shell=True) + ) - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) + assert re.match(".*results.*id.*", output) def test_search_with_ordering(): diff --git a/samples/interactive-tutorials/search/search_with_pagination.py b/samples/interactive-tutorials/search/search_with_pagination.py index b2bcbe90..fdbb8807 100644 --- a/samples/interactive-tutorials/search/search_with_pagination.py +++ b/samples/interactive-tutorials/search/search_with_pagination.py @@ -21,13 +21,16 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: -def get_search_request(query: str, page_size: int, offset: int, - next_page_token: str): - default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" +def get_search_request(query: str, page_size: int, offset: int, next_page_token: str): + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) search_request = SearchRequest() search_request.placement = default_search_placement @@ -50,10 +53,10 @@ def search(): offset = 0 page_token = "" - search_request_first_page = get_search_request("Hoodie", page_size, offset, - page_token) - search_response_first_page = SearchServiceClient().search( - search_request_first_page) + search_request_first_page = get_search_request( + "Hoodie", page_size, offset, page_token + ) + search_response_first_page = SearchServiceClient().search(search_request_first_page) print("---search response---") print(search_response_first_page) diff --git a/samples/interactive-tutorials/search/search_with_pagination_test.py b/samples/interactive-tutorials/search/search_with_pagination_test.py index abfb44f9..200abf16 100644 --- a/samples/interactive-tutorials/search/search_with_pagination_test.py +++ b/samples/interactive-tutorials/search/search_with_pagination_test.py @@ -21,17 +21,19 @@ def test_search_with_pagination_pass(): - output = str(subprocess.check_output('python search/search_with_pagination.py', shell=True)) + output = str( + subprocess.check_output("python search/search_with_pagination.py", shell=True) + ) - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) + assert re.match(".*results.*id.*", output) def test_search_with_pagination(): response = search() product_title = response.results[0].product.title - assert re.match('.*Hoodie', product_title) + assert re.match(".*Hoodie", product_title) assert len(response.results) == 6 diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py index 8d68a487..d9429fae 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py @@ -20,13 +20,18 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ['PROJECT_NUMBER'] +project_number = os.environ["PROJECT_NUMBER"] # get search service request: -def get_search_request(query: str, - condition: SearchRequest.QueryExpansionSpec.Condition): - default_search_placement = "projects/" + project_number + "/locations/global/catalogs/default_catalog/placements/default_search" +def get_search_request( + query: str, condition: SearchRequest.QueryExpansionSpec.Condition +): + default_search_placement = ( + "projects/" + + project_number + + "/locations/global/catalogs/default_catalog/placements/default_search" + ) query_expansion_spec = SearchRequest().QueryExpansionSpec() query_expansion_spec.condition = condition diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_test.py b/samples/interactive-tutorials/search/search_with_query_expansion_test.py index 01198e91..cad6ea6b 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_test.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_test.py @@ -21,19 +21,22 @@ def test_search_with_query_expansion_spec_pass(): - output = str(subprocess.check_output( - 'python search/search_with_query_expansion_spec.py', shell=True)) - - assert re.match('.*search request.*', output) - assert re.match('.*search response.*', output) + output = str( + subprocess.check_output( + "python search/search_with_query_expansion_spec.py", shell=True + ) + ) + + assert re.match(".*search request.*", output) + assert re.match(".*search response.*", output) # check the response contains some products - assert re.match('.*results.*id.*', output) + assert re.match(".*results.*id.*", output) def test_search_with_query_expansion_spec(): response = search() assert len(response.results) == 10 - assert response.results[0].product.title == 'Google Youth Hero Tee Grey' - assert response.results[2].product.title != 'Google Youth Hero Tee Grey' + assert response.results[0].product.title == "Google Youth Hero Tee Grey" + assert response.results[2].product.title != "Google Youth Hero Tee Grey" assert response.query_expansion_info.expanded_query is True From 203fd1373fc24621b369e136c4ce49719aa7bf37 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 28 Jan 2022 12:43:27 +0000 Subject: [PATCH 10/16] try out GOOGLE_CLOUD_PROJECT_NUMBER instead of PROJECT_NUMBER --- samples/interactive-tutorials/README.md | 4 ++-- samples/interactive-tutorials/search/search_simple_query.py | 2 +- .../interactive-tutorials/search/search_with_boost_spec.py | 2 +- .../interactive-tutorials/search/search_with_facet_spec.py | 2 +- samples/interactive-tutorials/search/search_with_filtering.py | 2 +- samples/interactive-tutorials/search/search_with_ordering.py | 2 +- .../interactive-tutorials/search/search_with_pagination.py | 2 +- .../search/search_with_query_expansion_spec.py | 2 +- samples/snippets/create_test_resources.py | 2 +- samples/snippets/remove_test_resources.py | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index 65fff84f..edf80366 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -34,7 +34,7 @@ To run a code sample from the Cloud Shell, you need to authenticate. To do this, **Note**: Click the copy button on the side of the code box to paste the command in the Cloud Shell terminal and run it. -### Set the PROJECT_NUMBER environment variable +### Set the GOOGLE_CLOUD_PROJECT_NUMBER environment variable Because you are going to run the code samples in your own Google Cloud project, you should specify the **project_number** as an environment variable. It will be used in every request to the Retail API. @@ -42,7 +42,7 @@ Because you are going to run the code samples in your own Google Cloud project, 1. Set the environment variable with the following command: ```bash - export PROJECT_NUMBER= + export GOOGLE_CLOUD_PROJECT_NUMBER= ``` ### Install Google Cloud Retail libraries diff --git a/samples/interactive-tutorials/search/search_simple_query.py b/samples/interactive-tutorials/search/search_simple_query.py index b71c3d8d..78889209 100644 --- a/samples/interactive-tutorials/search/search_simple_query.py +++ b/samples/interactive-tutorials/search/search_simple_query.py @@ -19,7 +19,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/interactive-tutorials/search/search_with_boost_spec.py b/samples/interactive-tutorials/search/search_with_boost_spec.py index 6e42658b..7b2995d8 100644 --- a/samples/interactive-tutorials/search/search_with_boost_spec.py +++ b/samples/interactive-tutorials/search/search_with_boost_spec.py @@ -20,7 +20,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/interactive-tutorials/search/search_with_facet_spec.py b/samples/interactive-tutorials/search/search_with_facet_spec.py index 9763b492..50a8fb7f 100644 --- a/samples/interactive-tutorials/search/search_with_facet_spec.py +++ b/samples/interactive-tutorials/search/search_with_facet_spec.py @@ -18,7 +18,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/interactive-tutorials/search/search_with_filtering.py b/samples/interactive-tutorials/search/search_with_filtering.py index abf1b1d3..e7a8a31d 100644 --- a/samples/interactive-tutorials/search/search_with_filtering.py +++ b/samples/interactive-tutorials/search/search_with_filtering.py @@ -19,7 +19,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/interactive-tutorials/search/search_with_ordering.py b/samples/interactive-tutorials/search/search_with_ordering.py index a5cb3fd6..118d4202 100644 --- a/samples/interactive-tutorials/search/search_with_ordering.py +++ b/samples/interactive-tutorials/search/search_with_ordering.py @@ -19,7 +19,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/interactive-tutorials/search/search_with_pagination.py b/samples/interactive-tutorials/search/search_with_pagination.py index fdbb8807..727227c2 100644 --- a/samples/interactive-tutorials/search/search_with_pagination.py +++ b/samples/interactive-tutorials/search/search_with_pagination.py @@ -21,7 +21,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py index d9429fae..9455e900 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_spec.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec.py @@ -20,7 +20,7 @@ from google.cloud.retail import SearchRequest, SearchServiceClient -project_number = os.environ["PROJECT_NUMBER"] +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] # get search service request: diff --git a/samples/snippets/create_test_resources.py b/samples/snippets/create_test_resources.py index 22005b40..e56dd575 100644 --- a/samples/snippets/create_test_resources.py +++ b/samples/snippets/create_test_resources.py @@ -23,7 +23,7 @@ ImportProductsRequest, ProductInputConfig from google.cloud.retail_v2 import ProductServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.getenv('GOOGLE_CLOUD_PROJECT_NUMBER') bucket_name = os.getenv('BUCKET_NAME') storage_client = storage.Client() resource_file = "resources/products.json" diff --git a/samples/snippets/remove_test_resources.py b/samples/snippets/remove_test_resources.py index df1c6ded..26891705 100644 --- a/samples/snippets/remove_test_resources.py +++ b/samples/snippets/remove_test_resources.py @@ -21,7 +21,7 @@ from google.cloud.retail import DeleteProductRequest, ListProductsRequest, \ ProductServiceClient -project_number = os.getenv('PROJECT_NUMBER') +project_number = os.getenv('GOOGLE_CLOUD_PROJECT_NUMBER') bucket_name = os.getenv('BUCKET_NAME') default_catalog = "projects/{0}/locations/global/catalogs/default_catalog/branches/default_branch".format( From f6259868aeed49ecdc7bd4faccccbc9da0858713 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 28 Jan 2022 13:03:23 +0000 Subject: [PATCH 11/16] add BUCKET_NAME as an environment variable --- samples/interactive-tutorials/search/noxfile_config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/samples/interactive-tutorials/search/noxfile_config.py b/samples/interactive-tutorials/search/noxfile_config.py index dfb6a937..8eb8d16f 100644 --- a/samples/interactive-tutorials/search/noxfile_config.py +++ b/samples/interactive-tutorials/search/noxfile_config.py @@ -28,4 +28,7 @@ # '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": { + "BUCKET_NAME": "retail-interactive-tutorials", + }, } From 3717616f48e441ebecabcc123f55d6fa9d7d762d Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 28 Jan 2022 14:55:24 +0000 Subject: [PATCH 12/16] adjust filepath so tests can run --- .../interactive-tutorials/search/search_simple_query_test.py | 2 +- .../interactive-tutorials/search/search_with_boost_spec_test.py | 2 +- ...search_with_facet_test.py => search_with_facet_spec_test.py} | 2 +- .../interactive-tutorials/search/search_with_filtering_test.py | 2 +- .../interactive-tutorials/search/search_with_ordering_test.py | 2 +- .../interactive-tutorials/search/search_with_pagination_test.py | 2 +- ...pansion_test.py => search_with_query_expansion_spec_test.py} | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) rename samples/interactive-tutorials/search/{search_with_facet_test.py => search_with_facet_spec_test.py} (93%) rename samples/interactive-tutorials/search/{search_with_query_expansion_test.py => search_with_query_expansion_spec_test.py} (94%) diff --git a/samples/interactive-tutorials/search/search_simple_query_test.py b/samples/interactive-tutorials/search/search_simple_query_test.py index 5f4a34e0..32749e2a 100644 --- a/samples/interactive-tutorials/search/search_simple_query_test.py +++ b/samples/interactive-tutorials/search/search_simple_query_test.py @@ -22,7 +22,7 @@ def test_search_simple_query_pass(): output = str( - subprocess.check_output("python search/search_simple_query.py", shell=True) + subprocess.check_output("python search_simple_query.py", shell=True) ) assert re.match(".*search request.*", output) diff --git a/samples/interactive-tutorials/search/search_with_boost_spec_test.py b/samples/interactive-tutorials/search/search_with_boost_spec_test.py index c695888d..cf391b2e 100644 --- a/samples/interactive-tutorials/search/search_with_boost_spec_test.py +++ b/samples/interactive-tutorials/search/search_with_boost_spec_test.py @@ -22,7 +22,7 @@ def test_search_with_boost_spec_pass(): output = str( - subprocess.check_output("python search/search_with_boost_spec.py", shell=True) + subprocess.check_output("python search_with_boost_spec.py", shell=True) ) assert re.match(".*search request.*", output) diff --git a/samples/interactive-tutorials/search/search_with_facet_test.py b/samples/interactive-tutorials/search/search_with_facet_spec_test.py similarity index 93% rename from samples/interactive-tutorials/search/search_with_facet_test.py rename to samples/interactive-tutorials/search/search_with_facet_spec_test.py index a67718dc..df082ac2 100644 --- a/samples/interactive-tutorials/search/search_with_facet_test.py +++ b/samples/interactive-tutorials/search/search_with_facet_spec_test.py @@ -22,7 +22,7 @@ def test_search_with_filtering_pass(): output = str( - subprocess.check_output("python search/search_with_facet_spec.py", shell=True) + subprocess.check_output("python search_with_facet_spec.py", shell=True) ) assert re.match(".*search request.*", output) diff --git a/samples/interactive-tutorials/search/search_with_filtering_test.py b/samples/interactive-tutorials/search/search_with_filtering_test.py index f3111043..c01cee3e 100644 --- a/samples/interactive-tutorials/search/search_with_filtering_test.py +++ b/samples/interactive-tutorials/search/search_with_filtering_test.py @@ -22,7 +22,7 @@ def test_search_with_filtering_pass(): output = str( - subprocess.check_output("python search/search_with_filtering.py", shell=True) + subprocess.check_output("python search_with_filtering.py", shell=True) ) assert re.match(".*search request.*", output) diff --git a/samples/interactive-tutorials/search/search_with_ordering_test.py b/samples/interactive-tutorials/search/search_with_ordering_test.py index 8d360056..0de2c979 100644 --- a/samples/interactive-tutorials/search/search_with_ordering_test.py +++ b/samples/interactive-tutorials/search/search_with_ordering_test.py @@ -22,7 +22,7 @@ def test_search_with_ordering_pass(): output = str( - subprocess.check_output("python search/search_with_ordering.py", shell=True) + subprocess.check_output("python search_with_ordering.py", shell=True) ) assert re.match(".*search request.*", output) diff --git a/samples/interactive-tutorials/search/search_with_pagination_test.py b/samples/interactive-tutorials/search/search_with_pagination_test.py index 200abf16..49bcd63a 100644 --- a/samples/interactive-tutorials/search/search_with_pagination_test.py +++ b/samples/interactive-tutorials/search/search_with_pagination_test.py @@ -22,7 +22,7 @@ def test_search_with_pagination_pass(): output = str( - subprocess.check_output("python search/search_with_pagination.py", shell=True) + subprocess.check_output("python search_with_pagination.py", shell=True) ) assert re.match(".*search request.*", output) diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_test.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py similarity index 94% rename from samples/interactive-tutorials/search/search_with_query_expansion_test.py rename to samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py index cad6ea6b..6f32dafd 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_test.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py @@ -23,7 +23,7 @@ def test_search_with_query_expansion_spec_pass(): output = str( subprocess.check_output( - "python search/search_with_query_expansion_spec.py", shell=True + "python search_with_query_expansion_spec.py", shell=True ) ) From 5e263982015d385a5d15d0162f8a919f0fa03f47 Mon Sep 17 00:00:00 2001 From: t-karasova Date: Fri, 28 Jan 2022 19:21:42 +0100 Subject: [PATCH 13/16] tests are fixed --- .../interactive-tutorials/search/search_with_filtering_test.py | 3 ++- .../search/search_with_query_expansion_spec_test.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/interactive-tutorials/search/search_with_filtering_test.py b/samples/interactive-tutorials/search/search_with_filtering_test.py index c01cee3e..270ff193 100644 --- a/samples/interactive-tutorials/search/search_with_filtering_test.py +++ b/samples/interactive-tutorials/search/search_with_filtering_test.py @@ -36,6 +36,7 @@ def test_search_with_filtering(): assert len(response.results) == 10 product_title = response.results[0].product.title - assert re.match(".*Tee.*Black", product_title) + assert re.match(".*Tee.*", product_title) + assert re.match(".*Black.*", product_title) assert "Black" in response.results[0].product.color_info.color_families assert response.total_size == 16 diff --git a/samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py b/samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py index 6f32dafd..b9c04e3f 100644 --- a/samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py +++ b/samples/interactive-tutorials/search/search_with_query_expansion_spec_test.py @@ -36,7 +36,6 @@ def test_search_with_query_expansion_spec_pass(): def test_search_with_query_expansion_spec(): response = search() - assert len(response.results) == 10 assert response.results[0].product.title == "Google Youth Hero Tee Grey" assert response.results[2].product.title != "Google Youth Hero Tee Grey" assert response.query_expansion_info.expanded_query is True From a3bc76c9ca67ef9e010837ebaff914479c65b108 Mon Sep 17 00:00:00 2001 From: t-karasova <91195610+t-karasova@users.noreply.github.com> Date: Fri, 28 Jan 2022 20:17:51 +0100 Subject: [PATCH 14/16] Update samples/interactive-tutorials/README.md Co-authored-by: Anthonios Partheniou --- samples/interactive-tutorials/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index edf80366..b2bb4482 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -26,7 +26,7 @@ To run a code sample from the Cloud Shell, you need to authenticate. To do this, gcloud auth application-default login ``` -1. Type `Y` and press **Enter**. Click the link in Terminal. A browser window should appear asking you to log in using your Gmail account. +1. Type `Y` and press **Enter**. Click the link in Terminal. A browser window should appear asking you to log in using your Google account. 1. Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal. From 4e342c6f97848e84957472f2e52806eea6696041 Mon Sep 17 00:00:00 2001 From: t-karasova Date: Fri, 28 Jan 2022 20:30:01 +0100 Subject: [PATCH 15/16] README file is updated --- samples/interactive-tutorials/LICENSE | 202 ------------------------ samples/interactive-tutorials/README.md | 96 ++++++----- 2 files changed, 59 insertions(+), 239 deletions(-) delete mode 100644 samples/interactive-tutorials/LICENSE diff --git a/samples/interactive-tutorials/LICENSE b/samples/interactive-tutorials/LICENSE deleted file mode 100644 index 7a4a3ea2..00000000 --- a/samples/interactive-tutorials/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - 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. \ No newline at end of file diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index b2bb4482..4863c531 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -1,5 +1,22 @@ -# Google Retail Search: Python code samples -## Get started +# Google Retail API: Python Code Samples + +## Get started with the Google Cloud Retail API + +The Retail API provides you with the following possibilities to: + - Create and maintaining the catalog data. + - Fine-tune the search configuration. + - Import and maintain the user events data. + +You can find the information about the Retail services in the [documentation](https://cloud.google.com/retail/docs) + + +If you would like to have a closer look at the Retail API features and try them yourself, +the best option is to use the [Interactive Tutorials](https://cloud.google.com/retail/docs/overview). The tutorials will be launched in the CloudShell environment, and you will be able to request the Retail services and check the response with minimum time and effort. + +The code samples in the directory **python-retail/samples/interactive-tutorials** are explicitly created for use with the Retail Interactive Tutorials. + +If, for some reason, you have decided to proceed with these code samples without the tutorial, please go through the following steps and set up the required preconditions. + ### Select your project and enable the Retail API Google Cloud organizes resources into projects. This lets you @@ -14,35 +31,60 @@ After the project is created, set your PROJECT_ID to a ```project``` variable. gcloud config set project ``` -1. Check that the Retail API is enabled for your Project in the [Admin Console](https://console.cloud.google.com/ai/retail/). +1. To check that the Retail API is enabled for your Project, go to the [Admin Console](https://console.cloud.google.com/ai/retail/). -### Set up authentication +### Create service account + +To access the Retail API, you must create a service account. -To run a code sample from the Cloud Shell, you need to authenticate. To do this, use the Application Default Credentials. +1. To create a service account, follow this [instruction](https://cloud.google.com/retail/docs/setting-up#service-account) -1. Set your user credentials to authenticate your requests to the Retail API +1. Find your service account on the [IAM page](https://console.cloud.google.com/iam-admin/iam), + click `Edit` icon, add the 'Storage Admin' and 'BigQuery Admin' roles. It may take some time for changes to apply. +1. Copy the service account email in the Principal field. + +### Set up authentication + +To run a code sample from the Cloud Shell, you need to be authenticated using the service account credentials. + +1. Login with your user credentials. ```bash - gcloud auth application-default login + gcloud auth login ``` -1. Type `Y` and press **Enter**. Click the link in Terminal. A browser window should appear asking you to log in using your Google account. +1. Type `Y` and press **Enter**. Click the link in a Terminal. A browser window should appear asking you to log in using your Gmail account. 1. Provide the Google Auth Library with access to your credentials and paste the code from the browser to the Terminal. -1. Run the code sample and check the Retail API in action. +1. Upload your service account key JSON file and use it to activate the service account: + + ```bash + gcloud iam service-accounts keys create ~/key.json --iam-account + ``` + + ```bash + gcloud auth activate-service-account --key-file ~/key.json + ``` -**Note**: Click the copy button on the side of the code box to paste the command in the Cloud Shell terminal and run it. +1. To request the Retail API, set your service account key JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable : + ```bash + export GOOGLE_APPLICATION_CREDENTIALS=~/key.json + ``` -### Set the GOOGLE_CLOUD_PROJECT_NUMBER environment variable +### Set the PROJECT_NUMBER and PROJECT_ID environment variables -Because you are going to run the code samples in your own Google Cloud project, you should specify the **project_number** as an environment variable. It will be used in every request to the Retail API. +You will run the code samples in your own Google Cloud project. To use the **project_number** and **project_id** in every request to the Retail API, you should first specify them as environment variables. -1. You can find the ```project_number``` in the Project Info card displayed on **Home/Dashboard**. +1. Find the project number and project ID in the Project Info card displayed on **Home/Dashboard**. -1. Set the environment variable with the following command: +1. Set the **project_number** with the following command: + ```bash + export PROJECT_NUMBER= + ``` +1. Set the **project_id** with the following command: ```bash - export GOOGLE_CLOUD_PROJECT_NUMBER= + export PROJECT_ID= ``` ### Install Google Cloud Retail libraries @@ -64,27 +106,7 @@ To run Python code samples for the Retail API tutorial, you need to set up your ``` -## Clone the Retail code samples - -This step is required if this is the first Retail API Tutorial you run. -Otherwise, you can skip it. - -Clone the Git repository with all the code samples to learn the Retail features and check them in action. - - -1. Run the following command in the Terminal: - ```bash - git clone https://github.com/t-karasova/grs-samples-python.git - ``` - - The code samples for each of the Retail services are stored in different directories. - -1. Go to the ```grs-samples-python``` directory. It's our starting point to run more commands. - ```bash - cd grs-samples-python - ``` - -## Import catalog data +## Import Catalog Data This step is required if this is the first Retail API Tutorial you run. Otherwise, you can skip it. @@ -126,4 +148,4 @@ python product/import_products_gcs.py Run the sample in a terminal with the following command: ```bash python search/search_with_boost_spec.py -``` +``` \ No newline at end of file From 298fa5f6dbd8902bcd2691c48a0ce1963037c109 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 28 Jan 2022 16:01:36 -0500 Subject: [PATCH 16/16] fix typo in pip install command --- samples/interactive-tutorials/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index 4863c531..17593f8a 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -101,8 +101,8 @@ To run Python code samples for the Retail API tutorial, you need to set up your ```bash pip install google pip install google-cloud-retail - pip install google.cloud.storage - pip install google.cloud.bigquery + pip install google-cloud-storage + pip install google-cloud-bigquery ```