|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | """Testable usage examples for Google BigQuery API wrapper |
16 | | -
|
17 | 16 | Each example function takes a ``client`` argument (which must be an instance |
18 | 17 | of :class:`google.cloud.bigquery.client.Client`) and uses it to perform a task |
19 | 18 | with the API. |
20 | | -
|
21 | 19 | To facilitate running the examples as system tests, each example is also passed |
22 | 20 | a ``to_delete`` list; the function adds to the list any objects created which |
23 | 21 | need to be deleted during teardown. |
@@ -303,47 +301,6 @@ def test_load_and_query_partitioned_table(client, to_delete): |
303 | 301 | assert len(rows) == 29 |
304 | 302 |
|
305 | 303 |
|
306 | | -# [START bigquery_table_exists] |
307 | | -def table_exists(client, table_reference): |
308 | | - """Return if a table exists. |
309 | | -
|
310 | | - Args: |
311 | | - client (google.cloud.bigquery.client.Client): |
312 | | - A client to connect to the BigQuery API. |
313 | | - table_reference (google.cloud.bigquery.table.TableReference): |
314 | | - A reference to the table to look for. |
315 | | -
|
316 | | - Returns: |
317 | | - bool: ``True`` if the table exists, ``False`` otherwise. |
318 | | - """ |
319 | | - from google.cloud.exceptions import NotFound |
320 | | - |
321 | | - try: |
322 | | - client.get_table(table_reference) |
323 | | - return True |
324 | | - except NotFound: |
325 | | - return False |
326 | | - |
327 | | - |
328 | | -# [END bigquery_table_exists] |
329 | | - |
330 | | - |
331 | | -def test_table_exists(client, to_delete): |
332 | | - """Determine if a table exists.""" |
333 | | - DATASET_ID = "get_table_dataset_{}".format(_millis()) |
334 | | - TABLE_ID = "get_table_table_{}".format(_millis()) |
335 | | - dataset = bigquery.Dataset(client.dataset(DATASET_ID)) |
336 | | - dataset = client.create_dataset(dataset) |
337 | | - to_delete.append(dataset) |
338 | | - |
339 | | - table_ref = dataset.table(TABLE_ID) |
340 | | - table = bigquery.Table(table_ref, schema=SCHEMA) |
341 | | - table = client.create_table(table) |
342 | | - |
343 | | - assert table_exists(client, table_ref) |
344 | | - assert not table_exists(client, dataset.table("i_dont_exist")) |
345 | | - |
346 | | - |
347 | 304 | @pytest.mark.skip( |
348 | 305 | reason=( |
349 | 306 | "update_table() is flaky " |
@@ -698,36 +655,6 @@ def test_manage_views(client, to_delete): |
698 | 655 | # [END bigquery_grant_view_access] |
699 | 656 |
|
700 | 657 |
|
701 | | -def test_table_insert_rows(client, to_delete): |
702 | | - """Insert / fetch table data.""" |
703 | | - dataset_id = "table_insert_rows_dataset_{}".format(_millis()) |
704 | | - table_id = "table_insert_rows_table_{}".format(_millis()) |
705 | | - dataset = bigquery.Dataset(client.dataset(dataset_id)) |
706 | | - dataset = client.create_dataset(dataset) |
707 | | - dataset.location = "US" |
708 | | - to_delete.append(dataset) |
709 | | - |
710 | | - table = bigquery.Table(dataset.table(table_id), schema=SCHEMA) |
711 | | - table = client.create_table(table) |
712 | | - |
713 | | - # [START bigquery_table_insert_rows] |
714 | | - # TODO(developer): Uncomment the lines below and replace with your values. |
715 | | - # from google.cloud import bigquery |
716 | | - # client = bigquery.Client() |
717 | | - # dataset_id = 'my_dataset' # replace with your dataset ID |
718 | | - # For this sample, the table must already exist and have a defined schema |
719 | | - # table_id = 'my_table' # replace with your table ID |
720 | | - # table_ref = client.dataset(dataset_id).table(table_id) |
721 | | - # table = client.get_table(table_ref) # API request |
722 | | - |
723 | | - rows_to_insert = [(u"Phred Phlyntstone", 32), (u"Wylma Phlyntstone", 29)] |
724 | | - |
725 | | - errors = client.insert_rows(table, rows_to_insert) # API request |
726 | | - |
727 | | - assert errors == [] |
728 | | - # [END bigquery_table_insert_rows] |
729 | | - |
730 | | - |
731 | 658 | def test_load_table_from_file(client, to_delete): |
732 | 659 | """Upload table data from a CSV file.""" |
733 | 660 | dataset_id = "load_table_from_file_dataset_{}".format(_millis()) |
@@ -993,12 +920,10 @@ def test_load_table_from_uri_orc(client, to_delete, capsys): |
993 | 920 |
|
994 | 921 | def test_load_table_from_uri_autodetect(client, to_delete, capsys): |
995 | 922 | """Load table from a GCS URI using various formats and auto-detected schema |
996 | | -
|
997 | 923 | Each file format has its own tested load from URI sample. Because most of |
998 | 924 | the code is common for autodetect, append, and truncate, this sample |
999 | 925 | includes snippets for all supported formats but only calls a single load |
1000 | 926 | job. |
1001 | | -
|
1002 | 927 | This code snippet is made up of shared code, then format-specific code, |
1003 | 928 | followed by more shared code. Note that only the last format in the |
1004 | 929 | format-specific code section will be tested in this test. |
@@ -1058,12 +983,10 @@ def test_load_table_from_uri_autodetect(client, to_delete, capsys): |
1058 | 983 |
|
1059 | 984 | def test_load_table_from_uri_truncate(client, to_delete, capsys): |
1060 | 985 | """Replaces table data with data from a GCS URI using various formats |
1061 | | -
|
1062 | 986 | Each file format has its own tested load from URI sample. Because most of |
1063 | 987 | the code is common for autodetect, append, and truncate, this sample |
1064 | 988 | includes snippets for all supported formats but only calls a single load |
1065 | 989 | job. |
1066 | | -
|
1067 | 990 | This code snippet is made up of shared code, then format-specific code, |
1068 | 991 | followed by more shared code. Note that only the last format in the |
1069 | 992 | format-specific code section will be tested in this test. |
@@ -1303,38 +1226,6 @@ def test_load_table_relax_column(client, to_delete): |
1303 | 1226 | assert table.num_rows > 0 |
1304 | 1227 |
|
1305 | 1228 |
|
1306 | | -def test_copy_table(client, to_delete): |
1307 | | - dataset_id = "copy_table_dataset_{}".format(_millis()) |
1308 | | - dest_dataset = bigquery.Dataset(client.dataset(dataset_id)) |
1309 | | - dest_dataset.location = "US" |
1310 | | - dest_dataset = client.create_dataset(dest_dataset) |
1311 | | - to_delete.append(dest_dataset) |
1312 | | - |
1313 | | - # [START bigquery_copy_table] |
1314 | | - # from google.cloud import bigquery |
1315 | | - # client = bigquery.Client() |
1316 | | - |
1317 | | - source_dataset = client.dataset("samples", project="bigquery-public-data") |
1318 | | - source_table_ref = source_dataset.table("shakespeare") |
1319 | | - |
1320 | | - # dataset_id = 'my_dataset' |
1321 | | - dest_table_ref = client.dataset(dataset_id).table("destination_table") |
1322 | | - |
1323 | | - job = client.copy_table( |
1324 | | - source_table_ref, |
1325 | | - dest_table_ref, |
1326 | | - # Location must match that of the source and destination tables. |
1327 | | - location="US", |
1328 | | - ) # API request |
1329 | | - |
1330 | | - job.result() # Waits for job to complete. |
1331 | | - |
1332 | | - assert job.state == "DONE" |
1333 | | - dest_table = client.get_table(dest_table_ref) # API request |
1334 | | - assert dest_table.num_rows > 0 |
1335 | | - # [END bigquery_copy_table] |
1336 | | - |
1337 | | - |
1338 | 1229 | def test_copy_table_multiple_source(client, to_delete): |
1339 | 1230 | dest_dataset_id = "dest_dataset_{}".format(_millis()) |
1340 | 1231 | dest_dataset = bigquery.Dataset(client.dataset(dest_dataset_id)) |
@@ -1601,31 +1492,6 @@ def test_undelete_table(client, to_delete): |
1601 | 1492 | # [END bigquery_undelete_table] |
1602 | 1493 |
|
1603 | 1494 |
|
1604 | | -def test_client_query(client): |
1605 | | - """Run a simple query.""" |
1606 | | - |
1607 | | - # [START bigquery_query] |
1608 | | - # from google.cloud import bigquery |
1609 | | - # client = bigquery.Client() |
1610 | | - |
1611 | | - query = ( |
1612 | | - "SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` " |
1613 | | - 'WHERE state = "TX" ' |
1614 | | - "LIMIT 100" |
1615 | | - ) |
1616 | | - query_job = client.query( |
1617 | | - query, |
1618 | | - # Location must match that of the dataset(s) referenced in the query. |
1619 | | - location="US", |
1620 | | - ) # API request - starts the query |
1621 | | - |
1622 | | - for row in query_job: # API request - fetches results |
1623 | | - # Row values can be accessed by field name or index |
1624 | | - assert row[0] == row.name == row["name"] |
1625 | | - print(row) |
1626 | | - # [END bigquery_query] |
1627 | | - |
1628 | | - |
1629 | 1495 | def test_client_query_legacy_sql(client): |
1630 | 1496 | """Run a query with Legacy SQL explicitly set""" |
1631 | 1497 | # [START bigquery_query_legacy] |
@@ -2360,42 +2226,6 @@ def test_ddl_create_view(client, to_delete, capsys): |
2360 | 2226 | assert len(df) == 0 |
2361 | 2227 |
|
2362 | 2228 |
|
2363 | | -def test_client_list_jobs(client): |
2364 | | - """List jobs for a project.""" |
2365 | | - |
2366 | | - # [START bigquery_list_jobs] |
2367 | | - # TODO(developer): Uncomment the lines below and replace with your values. |
2368 | | - # from google.cloud import bigquery |
2369 | | - # project = 'my_project' # replace with your project ID |
2370 | | - # client = bigquery.Client(project=project) |
2371 | | - import datetime |
2372 | | - |
2373 | | - # List the 10 most recent jobs in reverse chronological order. |
2374 | | - # Omit the max_results parameter to list jobs from the past 6 months. |
2375 | | - print("Last 10 jobs:") |
2376 | | - for job in client.list_jobs(max_results=10): # API request(s) |
2377 | | - print(job.job_id) |
2378 | | - |
2379 | | - # The following are examples of additional optional parameters: |
2380 | | - |
2381 | | - # Use min_creation_time and/or max_creation_time to specify a time window. |
2382 | | - print("Jobs from the last ten minutes:") |
2383 | | - ten_mins_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) |
2384 | | - for job in client.list_jobs(min_creation_time=ten_mins_ago): |
2385 | | - print(job.job_id) |
2386 | | - |
2387 | | - # Use all_users to include jobs run by all users in the project. |
2388 | | - print("Last 10 jobs run by all users:") |
2389 | | - for job in client.list_jobs(max_results=10, all_users=True): |
2390 | | - print("{} run by user: {}".format(job.job_id, job.user_email)) |
2391 | | - |
2392 | | - # Use state_filter to filter by job state. |
2393 | | - print("Jobs currently running:") |
2394 | | - for job in client.list_jobs(state_filter="RUNNING"): |
2395 | | - print(job.job_id) |
2396 | | - # [END bigquery_list_jobs] |
2397 | | - |
2398 | | - |
2399 | 2229 | @pytest.mark.skipif(pandas is None, reason="Requires `pandas`") |
2400 | 2230 | def test_query_results_as_dataframe(client): |
2401 | 2231 | # [START bigquery_query_results_dataframe] |
|
0 commit comments