Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lo Ferris committed Jul 29, 2021
1 parent 5197d0f commit cfb3805
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
29 changes: 6 additions & 23 deletions samples/snippets/README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.. This file is automatically generated. Do not edit this file directly.
Google BigQuery Python Samples
Expand All @@ -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
++++++++++++++

Expand All @@ -31,9 +33,6 @@ credentials for applications.
.. _Authentication Getting Started Guide:
https://cloud.google.com/docs/authentication/getting-started




Install Dependencies
++++++++++++++++++++

Expand All @@ -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
Expand All @@ -64,15 +63,9 @@ Install Dependencies
.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/






Samples
-------------------------------------------------------------------------------


Quickstart
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -89,8 +82,6 @@ To run this sample:
$ python quickstart.py
Simple Application
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -107,8 +98,6 @@ To run this sample:
$ python simple_app.py
User Credentials
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -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.
Expand All @@ -143,10 +131,6 @@ To run this sample:
The client library
-------------------------------------------------------------------------------

Expand All @@ -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/
8 changes: 6 additions & 2 deletions samples/snippets/authenticate_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ 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()
# to set credentials directly via a json object rather than set a filepath
# 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

Expand Down
2 changes: 1 addition & 1 deletion samples/snippets/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 4 additions & 7 deletions samples/snippets/revoke_dataset_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
24 changes: 21 additions & 3 deletions samples/snippets/revoke_dataset_access_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit cfb3805

Please sign in to comment.