Skip to content

Commit 696b307

Browse files
authored
Print job progress in load job examples. (googleapis#5566)
This aligns the load job BigQuery samples with the Cloud DPE sample rubric guidelines and makes it easier to align samples across languages.
1 parent 69b6bdd commit 696b307

1 file changed

Lines changed: 55 additions & 35 deletions

File tree

docs/bigquery/snippets.py

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ def test_load_table_from_file(client, to_delete):
12711271
assert row2 in rows
12721272

12731273

1274-
def test_load_table_from_uri_csv(client, to_delete):
1274+
def test_load_table_from_uri_csv(client, to_delete, capsys):
12751275
dataset_id = 'load_table_dataset_{}'.format(_millis())
12761276
dataset = bigquery.Dataset(client.dataset(dataset_id))
12771277
client.create_dataset(dataset)
@@ -1297,17 +1297,20 @@ def test_load_table_from_uri_csv(client, to_delete):
12971297
uri,
12981298
dataset_ref.table('us_states'),
12991299
job_config=job_config) # API request
1300-
1301-
assert load_job.job_type == 'load'
1300+
print('Starting job {}'.format(load_job.job_id))
13021301

13031302
load_job.result() # Waits for table load to complete.
1303+
print('Job finished.')
13041304

1305-
assert load_job.state == 'DONE'
1306-
assert client.get_table(dataset_ref.table('us_states')).num_rows == 50
1305+
destination_table = client.get_table(dataset_ref.table('us_states'))
1306+
print('Loaded {} rows.'.format(destination_table.num_rows))
13071307
# [END bigquery_load_table_gcs_csv]
13081308

1309+
out, _ = capsys.readouterr()
1310+
assert 'Loaded 50 rows.' in out
1311+
13091312

1310-
def test_load_table_from_uri_json(client, to_delete):
1313+
def test_load_table_from_uri_json(client, to_delete, capsys):
13111314
dataset_id = 'load_table_dataset_{}'.format(_millis())
13121315
dataset = bigquery.Dataset(client.dataset(dataset_id))
13131316
dataset.location = 'US'
@@ -1333,15 +1336,18 @@ def test_load_table_from_uri_json(client, to_delete):
13331336
dataset_ref.table('us_states'),
13341337
location='US', # Location must match that of the destination dataset.
13351338
job_config=job_config) # API request
1336-
1337-
assert load_job.job_type == 'load'
1339+
print('Starting job {}'.format(load_job.job_id))
13381340

13391341
load_job.result() # Waits for table load to complete.
1342+
print('Job finished.')
13401343

1341-
assert load_job.state == 'DONE'
1342-
assert client.get_table(dataset_ref.table('us_states')).num_rows == 50
1344+
destination_table = client.get_table(dataset_ref.table('us_states'))
1345+
print('Loaded {} rows.'.format(destination_table.num_rows))
13431346
# [END bigquery_load_table_gcs_json]
13441347

1348+
out, _ = capsys.readouterr()
1349+
assert 'Loaded 50 rows.' in out
1350+
13451351

13461352
def test_load_table_from_uri_cmek(client, to_delete):
13471353
dataset_id = 'load_table_from_uri_cmek_{}'.format(_millis())
@@ -1385,7 +1391,7 @@ def test_load_table_from_uri_cmek(client, to_delete):
13851391
# [END bigquery_load_table_gcs_json_cmek]
13861392

13871393

1388-
def test_load_table_from_uri_parquet(client, to_delete):
1394+
def test_load_table_from_uri_parquet(client, to_delete, capsys):
13891395
dataset_id = 'load_table_dataset_{}'.format(_millis())
13901396
dataset = bigquery.Dataset(client.dataset(dataset_id))
13911397
client.create_dataset(dataset)
@@ -1405,17 +1411,20 @@ def test_load_table_from_uri_parquet(client, to_delete):
14051411
uri,
14061412
dataset_ref.table('us_states'),
14071413
job_config=job_config) # API request
1408-
1409-
assert load_job.job_type == 'load'
1414+
print('Starting job {}'.format(load_job.job_id))
14101415

14111416
load_job.result() # Waits for table load to complete.
1417+
print('Job finished.')
14121418

1413-
assert load_job.state == 'DONE'
1414-
assert client.get_table(dataset_ref.table('us_states')).num_rows == 50
1419+
destination_table = client.get_table(dataset_ref.table('us_states'))
1420+
print('Loaded {} rows.'.format(destination_table.num_rows))
14151421
# [END bigquery_load_table_gcs_parquet]
14161422

1423+
out, _ = capsys.readouterr()
1424+
assert 'Loaded 50 rows.' in out
1425+
14171426

1418-
def test_load_table_from_uri_orc(client, to_delete):
1427+
def test_load_table_from_uri_orc(client, to_delete, capsys):
14191428
dataset_id = 'load_table_dataset_{}'.format(_millis())
14201429
dataset = bigquery.Dataset(client.dataset(dataset_id))
14211430
client.create_dataset(dataset)
@@ -1435,17 +1444,20 @@ def test_load_table_from_uri_orc(client, to_delete):
14351444
uri,
14361445
dataset_ref.table('us_states'),
14371446
job_config=job_config) # API request
1438-
1439-
assert load_job.job_type == 'load'
1447+
print('Starting job {}'.format(load_job.job_id))
14401448

14411449
load_job.result() # Waits for table load to complete.
1450+
print('Job finished.')
14421451

1443-
assert load_job.state == 'DONE'
1444-
assert client.get_table(dataset_ref.table('us_states')).num_rows == 50
1452+
destination_table = client.get_table(dataset_ref.table('us_states'))
1453+
print('Loaded {} rows.'.format(destination_table.num_rows))
14451454
# [END bigquery_load_table_gcs_orc]
14461455

1456+
out, _ = capsys.readouterr()
1457+
assert 'Loaded 50 rows.' in out
1458+
14471459

1448-
def test_load_table_from_uri_autodetect(client, to_delete):
1460+
def test_load_table_from_uri_autodetect(client, to_delete, capsys):
14491461
"""Load table from a GCS URI using various formats and auto-detected schema
14501462
14511463
Each file format has its own tested load from URI sample. Because most of
@@ -1497,18 +1509,21 @@ def test_load_table_from_uri_autodetect(client, to_delete):
14971509
uri,
14981510
dataset_ref.table('us_states'),
14991511
job_config=job_config) # API request
1500-
1501-
assert load_job.job_type == 'load'
1512+
print('Starting job {}'.format(load_job.job_id))
15021513

15031514
load_job.result() # Waits for table load to complete.
1515+
print('Job finished.')
15041516

1505-
assert load_job.state == 'DONE'
1506-
assert client.get_table(dataset_ref.table('us_states')).num_rows == 50
1517+
destination_table = client.get_table(dataset_ref.table('us_states'))
1518+
print('Loaded {} rows.'.format(destination_table.num_rows))
15071519
# [END bigquery_load_table_gcs_csv_autodetect]
15081520
# [END bigquery_load_table_gcs_json_autodetect]
15091521

1522+
out, _ = capsys.readouterr()
1523+
assert 'Loaded 50 rows.' in out
1524+
15101525

1511-
def test_load_table_from_uri_append(client, to_delete):
1526+
def test_load_table_from_uri_append(client, to_delete, capsys):
15121527
"""Appends data to a table from a GCS URI using various formats
15131528
15141529
Each file format has its own tested load from URI sample. Because most of
@@ -1590,22 +1605,24 @@ def test_load_table_from_uri_append(client, to_delete):
15901605
uri,
15911606
table_ref,
15921607
job_config=job_config) # API request
1593-
1594-
assert load_job.job_type == 'load'
1608+
print('Starting job {}'.format(load_job.job_id))
15951609

15961610
load_job.result() # Waits for table load to complete.
1611+
print('Job finished.')
15971612

1598-
assert load_job.state == 'DONE'
1599-
assert client.get_table(table_ref).num_rows == previous_rows + 50
1613+
destination_table = client.get_table(table_ref)
1614+
print('Loaded {} rows.'.format(destination_table.num_rows - previous_rows))
16001615
# [END bigquery_load_table_gcs_csv_append]
16011616
# [END bigquery_load_table_gcs_json_append]
16021617
# [END bigquery_load_table_gcs_parquet_append]
16031618
# [END bigquery_load_table_gcs_orc_append]
16041619

1620+
out, _ = capsys.readouterr()
16051621
assert previous_rows == 1
1622+
assert 'Loaded 50 rows.' in out
16061623

16071624

1608-
def test_load_table_from_uri_truncate(client, to_delete):
1625+
def test_load_table_from_uri_truncate(client, to_delete, capsys):
16091626
"""Replaces table data with data from a GCS URI using various formats
16101627
16111628
Each file format has its own tested load from URI sample. Because most of
@@ -1685,18 +1702,21 @@ def test_load_table_from_uri_truncate(client, to_delete):
16851702
uri,
16861703
table_ref,
16871704
job_config=job_config) # API request
1688-
1689-
assert load_job.job_type == 'load'
1705+
print('Starting job {}'.format(load_job.job_id))
16901706

16911707
load_job.result() # Waits for table load to complete.
1708+
print('Job finished.')
16921709

1693-
assert load_job.state == 'DONE'
1694-
assert client.get_table(table_ref).num_rows == 50
1710+
destination_table = client.get_table(table_ref)
1711+
print('Loaded {} rows.'.format(destination_table.num_rows))
16951712
# [END bigquery_load_table_gcs_csv_truncate]
16961713
# [END bigquery_load_table_gcs_json_truncate]
16971714
# [END bigquery_load_table_gcs_parquet_truncate]
16981715
# [END bigquery_load_table_gcs_orc_truncate]
16991716

1717+
out, _ = capsys.readouterr()
1718+
assert 'Loaded 50 rows.' in out
1719+
17001720

17011721
def test_load_table_add_column(client, to_delete):
17021722
dataset_id = 'load_table_add_column_{}'.format(_millis())

0 commit comments

Comments
 (0)