diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index bb584fa0494a..a44ea2a87fde 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -456,131 +456,6 @@ def test_update_table_cmek(client, to_delete): # [END bigquery_update_table_cmek] -@pytest.mark.skip( - reason=( - "update_table() is flaky " - "https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5589" - ) -) -def test_manage_views(client, to_delete): - project = client.project - source_dataset_id = "source_dataset_{}".format(_millis()) - source_dataset_ref = client.dataset(source_dataset_id) - source_dataset = bigquery.Dataset(source_dataset_ref) - source_dataset = client.create_dataset(source_dataset) - to_delete.append(source_dataset) - - job_config = bigquery.LoadJobConfig() - job_config.schema = [ - bigquery.SchemaField("name", "STRING"), - bigquery.SchemaField("post_abbr", "STRING"), - ] - job_config.skip_leading_rows = 1 - uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv" - source_table_id = "us_states" - load_job = client.load_table_from_uri( - uri, source_dataset.table(source_table_id), job_config=job_config - ) - load_job.result() - - shared_dataset_id = "shared_dataset_{}".format(_millis()) - shared_dataset_ref = client.dataset(shared_dataset_id) - shared_dataset = bigquery.Dataset(shared_dataset_ref) - shared_dataset = client.create_dataset(shared_dataset) - to_delete.append(shared_dataset) - - # [START bigquery_create_view] - # from google.cloud import bigquery - # client = bigquery.Client() - # project = 'my-project' - # source_dataset_id = 'my_source_dataset' - # source_table_id = 'us_states' - # shared_dataset_ref = client.dataset('my_shared_dataset') - - # This example shows how to create a shared view of a source table of - # US States. The source table contains all 50 states, while the view will - # contain only states with names starting with 'W'. - view_ref = shared_dataset_ref.table("my_shared_view") - view = bigquery.Table(view_ref) - sql_template = 'SELECT name, post_abbr FROM `{}.{}.{}` WHERE name LIKE "W%"' - view.view_query = sql_template.format(project, source_dataset_id, source_table_id) - view = client.create_table(view) # API request - - print("Successfully created view at {}".format(view.full_table_id)) - # [END bigquery_create_view] - - # [START bigquery_update_view_query] - # from google.cloud import bigquery - # client = bigquery.Client() - # project = 'my-project' - # source_dataset_id = 'my_source_dataset' - # source_table_id = 'us_states' - # shared_dataset_ref = client.dataset('my_shared_dataset') - - # This example shows how to update a shared view of a source table of - # US States. The view's query will be updated to contain only states with - # names starting with 'M'. - view_ref = shared_dataset_ref.table("my_shared_view") - view = bigquery.Table(view_ref) - sql_template = 'SELECT name, post_abbr FROM `{}.{}.{}` WHERE name LIKE "M%"' - view.view_query = sql_template.format(project, source_dataset_id, source_table_id) - view = client.update_table(view, ["view_query"]) # API request - # [END bigquery_update_view_query] - - # [START bigquery_get_view] - # from google.cloud import bigquery - # client = bigquery.Client() - # shared_dataset_id = 'my_shared_dataset' - - view_ref = client.dataset(shared_dataset_id).table("my_shared_view") - view = client.get_table(view_ref) # API Request - - # Display view properties - print("View at {}".format(view.full_table_id)) - print("View Query:\n{}".format(view.view_query)) - # [END bigquery_get_view] - assert view.view_query is not None - - analyst_group_email = "example-analyst-group@google.com" - # [START bigquery_grant_view_access] - # from google.cloud import bigquery - # client = bigquery.Client() - - # Assign access controls to the dataset containing the view - # shared_dataset_id = 'my_shared_dataset' - # analyst_group_email = 'data_analysts@example.com' - shared_dataset = client.get_dataset( - client.dataset(shared_dataset_id) - ) # API request - access_entries = shared_dataset.access_entries - access_entries.append( - bigquery.AccessEntry("READER", "groupByEmail", analyst_group_email) - ) - shared_dataset.access_entries = access_entries - shared_dataset = client.update_dataset( - shared_dataset, ["access_entries"] - ) # API request - - # Authorize the view to access the source dataset - # project = 'my-project' - # source_dataset_id = 'my_source_dataset' - source_dataset = client.get_dataset( - client.dataset(source_dataset_id) - ) # API request - view_reference = { - "projectId": project, - "datasetId": shared_dataset_id, - "tableId": "my_shared_view", - } - access_entries = source_dataset.access_entries - access_entries.append(bigquery.AccessEntry(None, "view", view_reference)) - source_dataset.access_entries = access_entries - source_dataset = client.update_dataset( - source_dataset, ["access_entries"] - ) # API request - # [END bigquery_grant_view_access] - - def test_load_table_from_file(client, to_delete): """Upload table data from a CSV file.""" dataset_id = "load_table_from_file_dataset_{}".format(_millis()) @@ -1359,100 +1234,5 @@ def test_query_external_gcs_permanent_table(client, to_delete): assert len(w_states) == 4 -def test_ddl_create_view(client, to_delete, capsys): - """Create a view via a DDL query.""" - project = client.project - dataset_id = "ddl_view_{}".format(_millis()) - table_id = "new_view" - dataset = bigquery.Dataset(client.dataset(dataset_id)) - client.create_dataset(dataset) - to_delete.append(dataset) - - # [START bigquery_ddl_create_view] - # from google.cloud import bigquery - # project = 'my-project' - # dataset_id = 'my_dataset' - # table_id = 'new_view' - # client = bigquery.Client(project=project) - - sql = """ - CREATE VIEW `{}.{}.{}` - OPTIONS( - expiration_timestamp=TIMESTAMP_ADD( - CURRENT_TIMESTAMP(), INTERVAL 48 HOUR), - friendly_name="new_view", - description="a view that expires in 2 days", - labels=[("org_unit", "development")] - ) - AS SELECT name, state, year, number - FROM `bigquery-public-data.usa_names.usa_1910_current` - WHERE state LIKE 'W%' - """.format( - project, dataset_id, table_id - ) - - job = client.query(sql) # API request. - job.result() # Waits for the query to finish. - - print( - 'Created new view "{}.{}.{}".'.format( - job.destination.project, - job.destination.dataset_id, - job.destination.table_id, - ) - ) - # [END bigquery_ddl_create_view] - - out, _ = capsys.readouterr() - assert 'Created new view "{}.{}.{}".'.format(project, dataset_id, table_id) in out - - # Test that listing query result rows succeeds so that generic query - # processing tools work with DDL statements. - rows = list(job) - assert len(rows) == 0 - - if pandas is not None: - df = job.to_dataframe() - assert len(df) == 0 - - -@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") -def test_query_results_as_dataframe(client): - # [START bigquery_query_results_dataframe] - # from google.cloud import bigquery - # client = bigquery.Client() - - sql = """ - SELECT name, SUM(number) as count - FROM `bigquery-public-data.usa_names.usa_1910_current` - GROUP BY name - ORDER BY count DESC - LIMIT 10 - """ - - df = client.query(sql).to_dataframe() - # [END bigquery_query_results_dataframe] - assert isinstance(df, pandas.DataFrame) - assert len(list(df)) == 2 # verify the number of columns - assert len(df) == 10 # verify the number of rows - - -@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") -def test_list_rows_as_dataframe(client): - # [START bigquery_list_rows_dataframe] - # from google.cloud import bigquery - # client = bigquery.Client() - - dataset_ref = client.dataset("samples", project="bigquery-public-data") - table_ref = dataset_ref.table("shakespeare") - table = client.get_table(table_ref) - - df = client.list_rows(table).to_dataframe() - # [END bigquery_list_rows_dataframe] - assert isinstance(df, pandas.DataFrame) - assert len(list(df)) == len(table.schema) # verify the number of columns - assert len(df) == table.num_rows # verify the number of rows - - if __name__ == "__main__": pytest.main() diff --git a/bigquery/docs/usage/pandas.rst b/bigquery/docs/usage/pandas.rst index 9db98dfbbccb..ae03a12b497e 100644 --- a/bigquery/docs/usage/pandas.rst +++ b/bigquery/docs/usage/pandas.rst @@ -23,7 +23,7 @@ Alternatively, you can install the BigQuery python client library with To retrieve query results as a :class:`pandas.DataFrame`: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/query_results_as_dataframe.py :language: python :dedent: 4 :start-after: [START bigquery_query_results_dataframe] @@ -31,7 +31,7 @@ To retrieve query results as a :class:`pandas.DataFrame`: To retrieve table rows as a :class:`pandas.DataFrame`: -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/list_rows_as_dataframe.py :language: python :dedent: 4 :start-after: [START bigquery_list_rows_dataframe] diff --git a/bigquery/docs/usage/tables.rst b/bigquery/docs/usage/tables.rst index b6f8dbdde646..dc134320cfbe 100644 --- a/bigquery/docs/usage/tables.rst +++ b/bigquery/docs/usage/tables.rst @@ -198,3 +198,46 @@ Restore a deleted table from a snapshot by using the :dedent: 4 :start-after: [START bigquery_undelete_table] :end-before: [END bigquery_undelete_table] + +Share a view of Table +^^^^^^^^^^^^^^^^^^^^^^ + +Create and share a view of source table: + +.. literalinclude:: ../samples/create_view.py + :language: python + :dedent: 4 + :start-after: [START bigquery_create_view] + :end-before: [END bigquery_create_view] + +Create a view via DDL query: + +.. literalinclude:: ../samples/create_view_dll.py + :language: python + :dedent: 4 + :start-after: [START bigquery_ddl_create_view] + :end-before: [END bigquery_ddl_create_view] + +Update a shared view of a source table: + +.. literalinclude:: ../samples/update_view.py + :language: python + :dedent: 4 + :start-after: [START bigquery_update_view_query] + :end-before: [END bigquery_update_view_query] + +Authorize the view to access the source dataset: + +.. literalinclude:: ../samples/grant_view_access.py + :language: python + :dedent: 4 + :start-after: [START bigquery_grant_view_access] + :end-before: [END bigquery_grant_view_access] + +Get a view of table: + +.. literalinclude:: ../samples/get_view.py + :language: python + :dedent: 4 + :start-after: [START bigquery_get_view] + :end-before: [END bigquery_get_view] \ No newline at end of file diff --git a/bigquery/noxfile.py b/bigquery/noxfile.py index 17a2dee417c0..ce9971cf11d8 100644 --- a/bigquery/noxfile.py +++ b/bigquery/noxfile.py @@ -175,7 +175,7 @@ def lint_setup_py(session): session.run("python", "setup.py", "check", "--restructuredtext", "--strict") -@nox.session(python="3.6") +@nox.session(python="3.7") def blacken(session): """Run black. Format code to uniform standard. diff --git a/bigquery/samples/create_view.py b/bigquery/samples/create_view.py new file mode 100644 index 000000000000..582e6209d339 --- /dev/null +++ b/bigquery/samples/create_view.py @@ -0,0 +1,54 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def create_view(load_table_id, table_id): + + # [START bigquery_create_view] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to load the data. + # load_table_id = "your-project.your_dataset.your_table_name" + + # TODO(developer): Set table_id to the ID of the table to create. + # table_id = "your-project.your_dataset.your_table_name" + + job_config = bigquery.LoadJobConfig( + schema=[ + bigquery.SchemaField("name", "STRING"), + bigquery.SchemaField("post_abbr", "STRING"), + ], + skip_leading_rows=1, + ) + uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv" + + load_job = client.load_table_from_uri(uri, load_table_id, job_config=job_config) + load_job.result() + table = client.get_table(load_table_id) + view = bigquery.Table(table_id) + sql_template = 'SELECT name, post_abbr FROM `{}.{}.{}` WHERE name LIKE "W%"' + view.view_query = sql_template.format( + view.project, table.dataset_id, table.table_id + ) + view = client.create_table(view) # Make an API request. + + print( + "Successfully created view at {}.{}.{}".format( + view.project, view.dataset_id, view.table_id + ) + ) + # [END bigquery_create_view] diff --git a/bigquery/samples/create_view_dll.py b/bigquery/samples/create_view_dll.py new file mode 100644 index 000000000000..f5e142e511bb --- /dev/null +++ b/bigquery/samples/create_view_dll.py @@ -0,0 +1,54 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def create_view_dll(table_id): + + # [START bigquery_ddl_create_view] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to load the data. + # table_id = "your-project.your_dataset.your_table_name" + + sql = """ + CREATE VIEW `{}` + OPTIONS( + expiration_timestamp=TIMESTAMP_ADD( + CURRENT_TIMESTAMP(), INTERVAL 48 HOUR), + friendly_name="new_view", + description="a view that expires in 2 days", + labels=[("org_unit", "development")] + ) + AS SELECT name, state, year, number + FROM `bigquery-public-data.usa_names.usa_1910_current` + WHERE state LIKE 'W%' + """.format( + table_id + ) + + job = client.query(sql) # API request. + job.result() # Waits for the query to finish. + + print( + "Created new view {}.{}.{}.".format( + job.destination.project, + job.destination.dataset_id, + job.destination.table_id, + ) + ) + # [END bigquery_ddl_create_view] + return job diff --git a/bigquery/samples/get_view.py b/bigquery/samples/get_view.py new file mode 100644 index 000000000000..ed5f116eac2e --- /dev/null +++ b/bigquery/samples/get_view.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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def get_view(table_id): + + # [START bigquery_get_view] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to load the data. + # load_table_id = "your-project.your_dataset.your_table_name" + + # TODO(developer): Set table_id to the ID of the table to create. + # table_id = "your-project.your_dataset.your_table_name" + + view = client.get_table(table_id) # Make an API request. + + # Display view properties + print("View at {}".format(view.full_table_id)) + print("View Query:\n{}".format(view.view_query)) + # [END bigquery_get_view] diff --git a/bigquery/samples/grant_view_access.py b/bigquery/samples/grant_view_access.py new file mode 100644 index 000000000000..a229d6dfc9b3 --- /dev/null +++ b/bigquery/samples/grant_view_access.py @@ -0,0 +1,64 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def grant_view_access(table_id, dataset_id): + + # [START bigquery_grant_view_access] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table. + # load_table_id = "your-project.your_dataset.your_table_name" + + # TODO(developer): Set dataset_id to the ID of the dataset to create. + # dataset_id = "{}.your_dataset".format(client.project) + + table = client.get_table(table_id) # Make an API request. + analyst_group_email = "example-analyst-group@google.com" + + # Assign access controls to the dataset + shared_dataset = client.get_dataset(client.dataset(table.dataset_id)) # API request + + access_entries = shared_dataset.access_entries + access_entries.append( + bigquery.AccessEntry("READER", "groupByEmail", analyst_group_email) + ) + shared_dataset.access_entries = access_entries + shared_dataset = client.update_dataset( + shared_dataset, ["access_entries"] + ) # Make an API request. + if shared_dataset.access_entries: + print("Assign access controls to the dataset successfully.") + + # Authorize the view to access the source dataset + source_dataset = client.create_dataset(dataset_id) # Make an API request. + + view_reference = { + "projectId": client.project, + "datasetId": table.dataset_id, + "tableId": table.table_id, + } + access_entries = source_dataset.access_entries + access_entries.append(bigquery.AccessEntry(None, "view", view_reference)) + source_dataset.access_entries = access_entries + source_dataset = client.update_dataset( + source_dataset, ["access_entries"] + ) # Make an API request. + for access_entry in source_dataset.access_entries: + if access_entry.entity_type == "view": + print("Grant view access successfully.") + # [END bigquery_grant_view_access] diff --git a/bigquery/samples/list_rows_as_dataframe.py b/bigquery/samples/list_rows_as_dataframe.py new file mode 100644 index 000000000000..dd55ecd2e97b --- /dev/null +++ b/bigquery/samples/list_rows_as_dataframe.py @@ -0,0 +1,37 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def list_rows_as_dataframe(table_id): + + # [START bigquery_list_rows_dataframe] + import pandas + + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the model to fetch. + # table_id = 'your-project.your_dataset.your_table' + + table = client.get_table(table_id) + + df = client.list_rows(table).to_dataframe() + + assert isinstance(df, pandas.DataFrame) + print( + "There are {} rows and {} columns in dataframe.".format(len(list(df)), len(df)) + ) + # [END bigquery_list_rows_dataframe] diff --git a/bigquery/samples/query_results_as_dataframe.py b/bigquery/samples/query_results_as_dataframe.py new file mode 100644 index 000000000000..5a6fe8cb9e4f --- /dev/null +++ b/bigquery/samples/query_results_as_dataframe.py @@ -0,0 +1,41 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def query_results_as_dataframe(): + + # [START bigquery_query_results_dataframe] + from google.cloud import bigquery + + import pandas + + # Construct a BigQuery client object. + client = bigquery.Client() + + # Run a SQL script. + sql = """ + SELECT name, SUM(number) as count + FROM `bigquery-public-data.usa_names.usa_1910_current` + GROUP BY name + ORDER BY count DESC + LIMIT 10 + """ + + df = client.query(sql).to_dataframe() + + assert isinstance(df, pandas.DataFrame) + print( + "There are {} rows and {} columns in dataframe.".format(len(list(df)), len(df)) + ) + # [END bigquery_query_results_dataframe] diff --git a/bigquery/samples/tests/test_create_view_dll.py b/bigquery/samples/tests/test_create_view_dll.py new file mode 100644 index 000000000000..12a9b00894f6 --- /dev/null +++ b/bigquery/samples/tests/test_create_view_dll.py @@ -0,0 +1,26 @@ +# 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 +# +# https://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 .. import create_view_dll + + +def test_create_view_dll(capsys, random_table_id): + job = create_view_dll.create_view_dll(random_table_id) + out, _ = capsys.readouterr() + assert "Created new view {}.".format(random_table_id) in out + + # Test that listing query result rows succeeds so that generic query + # processing tools work with DDL statements. + rows = list(job) + assert len(rows) == 0 diff --git a/bigquery/samples/tests/test_list_rows_as_dataframe.py b/bigquery/samples/tests/test_list_rows_as_dataframe.py new file mode 100644 index 000000000000..7183886ebcc5 --- /dev/null +++ b/bigquery/samples/tests/test_list_rows_as_dataframe.py @@ -0,0 +1,26 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +from .. import list_rows_as_dataframe + + +pandas = pytest.importorskip("pandas") + + +def test_list_rows_as_dataframe(capsys, table_with_data_id): + list_rows_as_dataframe.list_rows_as_dataframe(table_with_data_id) + out, _ = capsys.readouterr() + assert "There are 4 rows and 164656 columns in dataframe." in out diff --git a/bigquery/samples/tests/test_manage_views.py b/bigquery/samples/tests/test_manage_views.py new file mode 100644 index 000000000000..5b925d5f4827 --- /dev/null +++ b/bigquery/samples/tests/test_manage_views.py @@ -0,0 +1,38 @@ +# 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 +# +# https://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 .. import create_view +from .. import update_view +from .. import get_view +from .. import grant_view_access + + +def test_manage_views(capsys, table_id, random_table_id, random_dataset_id): + create_view.create_view(table_id, random_table_id) + out, err = capsys.readouterr() + assert "Successfully created view at {}".format(random_table_id) in out + + update_view.update_view(random_table_id) + out, err = capsys.readouterr() + assert "The View query has been updated." in out + + get_view.get_view(random_table_id) + out, err = capsys.readouterr() + assert "View at" in out + assert "View Query" in out + + grant_view_access.grant_view_access(table_id, random_dataset_id) + out, err = capsys.readouterr() + assert "Assign access controls to the dataset successfully." in out + assert "Grant view access successfully" in out diff --git a/bigquery/samples/tests/test_query_results_as_dataframe.py b/bigquery/samples/tests/test_query_results_as_dataframe.py new file mode 100644 index 000000000000..20df94ccfcdb --- /dev/null +++ b/bigquery/samples/tests/test_query_results_as_dataframe.py @@ -0,0 +1,26 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +from .. import query_results_as_dataframe + + +pandas = pytest.importorskip("pandas") + + +def test_query_results_as_dataframe(capsys): + query_results_as_dataframe.query_results_as_dataframe() + out, _ = capsys.readouterr() + assert "There are 2 rows and 10 columns in dataframe." in out diff --git a/bigquery/samples/update_view.py b/bigquery/samples/update_view.py new file mode 100644 index 000000000000..4e9e2a552c64 --- /dev/null +++ b/bigquery/samples/update_view.py @@ -0,0 +1,40 @@ +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def update_view(table_id): + + # [START bigquery_update_view_query] + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to load the data. + # load_table_id = "your-project.your_dataset.your_table_name" + + # TODO(developer): Set table_id to the ID of the table to create. + # table_id = "your-project.your_dataset.your_table_name" + + view = client.get_table(table_id) # Make an API request. + old_view_query = view.view_query + + sql_template = 'SELECT name, post_abbr FROM `{}.{}.{}` WHERE name LIKE "M%"' + view.view_query = sql_template.format(view.project, view.dataset_id, view.table_id) + view = client.update_table(view, ["view_query"]) # API request + new_view_query = view.view_query + + if old_view_query != new_view_query: + print("The View query has been updated.") + # [END bigquery_update_view_query]