From cfb38058f54abf9ab201f2bdcdde040ed156dd00 Mon Sep 17 00:00:00 2001 From: Lo Ferris Date: Thu, 29 Jul 2021 14:56:55 -0700 Subject: [PATCH] updated test --- samples/snippets/README.rst | 29 ++++--------------- .../snippets/authenticate_service_account.py | 8 +++-- samples/snippets/noxfile.py | 2 +- samples/snippets/revoke_dataset_access.py | 11 +++---- .../snippets/revoke_dataset_access_test.py | 24 +++++++++++++-- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/samples/snippets/README.rst b/samples/snippets/README.rst index 7c3e19e68..9446125cd 100644 --- a/samples/snippets/README.rst +++ b/samples/snippets/README.rst @@ -1,4 +1,3 @@ - .. This file is automatically generated. Do not edit this file directly. Google BigQuery Python Samples @@ -16,11 +15,14 @@ This directory contains samples for Google BigQuery. `Google BigQuery`_ is Googl .. _Google BigQuery: https://cloud.google.com/bigquery/docs +To run the sample, you need to have `BigQuery Admin` role. + + + Setup ------------------------------------------------------------------------------- - Authentication ++++++++++++++ @@ -31,9 +33,6 @@ credentials for applications. .. _Authentication Getting Started Guide: https://cloud.google.com/docs/authentication/getting-started - - - Install Dependencies ++++++++++++++++++++ @@ -48,7 +47,7 @@ Install Dependencies .. _Python Development Environment Setup Guide: https://cloud.google.com/python/setup -#. Create a virtualenv. Samples are compatible with Python 3.6+. +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. .. code-block:: bash @@ -64,15 +63,9 @@ Install Dependencies .. _pip: https://pip.pypa.io/ .. _virtualenv: https://virtualenv.pypa.io/ - - - - - Samples ------------------------------------------------------------------------------- - Quickstart +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -89,8 +82,6 @@ To run this sample: $ python quickstart.py - - Simple Application +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -107,8 +98,6 @@ To run this sample: $ python simple_app.py - - User Credentials +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -124,7 +113,6 @@ To run this sample: $ python user_credentials.py - usage: user_credentials.py [-h] [--launch-browser] project Command-line application to run a query using user credentials. @@ -143,10 +131,6 @@ To run this sample: - - - - The client library ------------------------------------------------------------------------------- @@ -162,5 +146,4 @@ to `browse the source`_ and `report issues`_. https://github.com/GoogleCloudPlatform/google-cloud-python/issues - -.. _Google Cloud SDK: https://cloud.google.com/sdk/ +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/samples/snippets/authenticate_service_account.py b/samples/snippets/authenticate_service_account.py index c07848bee..fa3c53cda 100644 --- a/samples/snippets/authenticate_service_account.py +++ b/samples/snippets/authenticate_service_account.py @@ -27,7 +27,8 @@ def main(): # key_path = "path/to/service_account.json" credentials = service_account.Credentials.from_service_account_file( - key_path, scopes=["https://www.googleapis.com/auth/cloud-platform"], + key_path, + scopes=["https://www.googleapis.com/auth/cloud-platform"], ) # Alternatively, use service_account.Credentials.from_service_account_info() @@ -35,7 +36,10 @@ def main(): # TODO(developer): Set key_json to the content of the service account key file. # credentials = service_account.Credentials.from_service_account_info(key_json) - client = bigquery.Client(credentials=credentials, project=credentials.project_id,) + client = bigquery.Client( + credentials=credentials, + project=credentials.project_id, + ) # [END bigquery_client_json_credentials] return client diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index 160fe7286..b3c8658a3 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -226,7 +226,7 @@ def py(session: nox.sessions.Session) -> None: def _get_repo_root() -> Optional[str]: - """ Returns the root folder of the project. """ + """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): diff --git a/samples/snippets/revoke_dataset_access.py b/samples/snippets/revoke_dataset_access.py index 60727e3dc..84b537c4c 100644 --- a/samples/snippets/revoke_dataset_access.py +++ b/samples/snippets/revoke_dataset_access.py @@ -36,12 +36,9 @@ def revoke_dataset_access(dataset_id, entity_id): dataset = client.get_dataset(dataset_id) # Make an API request. entries = list(dataset.access_entries) - for entry in entries: - if entry.entity_id == entity_id: - entry.role = None - break - - dataset.access_entries = entries + dataset.access_entries = entries.filter( + lambda entry: entry.entity_id != entity_id, entries + ) dataset = client.update_dataset( dataset, @@ -50,5 +47,5 @@ def revoke_dataset_access(dataset_id, entity_id): ) # Make an API request. full_dataset_id = f"{dataset.project}.{dataset.dataset_id}" - print(f"Updated dataset '{full_dataset_id}' with modified user permissions.") + print(f"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'") # [END bigquery_revoke_dataset_access] diff --git a/samples/snippets/revoke_dataset_access_test.py b/samples/snippets/revoke_dataset_access_test.py index 0181f9e69..34cbff933 100644 --- a/samples/snippets/revoke_dataset_access_test.py +++ b/samples/snippets/revoke_dataset_access_test.py @@ -14,12 +14,30 @@ from . import update_dataset_access from . import revoke_dataset_access +from google.cloud import bigquery +from functools import reduce -def test_revoke_dataset_access(capsys, dataset_id, entity_id): - +def test_revoke_dataset_access( + capsys, dataset_id, entity_id, bigquery_client: bigquery.Client +): update_dataset_access.update_dataset_access(dataset_id) + updated_dataset = bigquery_client.get_dataset(dataset_id) + updated_dataset_entries = list(updated_dataset.access_entries) revoke_dataset_access.revoke_dataset_access(dataset_id, entity_id) + revoked_dataset = bigquery_client.get_dataset(dataset_id) + revoked_dataset_entries = list(revoked_dataset.access_entries) + full_dataset_id = f"{updated_dataset.project}.{updated_dataset.dataset_id}" out, err = capsys.readouterr() - assert f"Updated dataset '{dataset_id}' with modified user permissions." in out + assert ( + f"Revoked dataset access for '{entity_id}' to ' dataset '{full_dataset_id}.'" + in out + ) + assert len(revoked_dataset_entries) == len(updated_dataset_entries) - 1 + assert ( + reduce( + lambda entry: bool(entry.entity_id == entity_id), revoked_dataset_entries + ) + == False + )