Skip to content

Commit b04113a

Browse files
authored
test: add retries to flaky datalab-migration test (GoogleCloudPlatform#8435)
* test: add retries to flaky datalab-migration test * test: change name of function * test: remove invalid type hint * test: fix import order lint issue * linting
1 parent 138b974 commit b04113a

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

bigquery/datalab-migration/samples_test.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
import time
1616

17+
from google.api_core.retry import Retry
1718
import google.auth
1819
import google.datalab
1920
import IPython
2021
from IPython.terminal import interactiveshell
2122
from IPython.testing import tools
2223
import pytest
2324

24-
2525
# Get default project
2626
_, PROJECT_ID = google.auth.default()
2727
# Set Datalab project ID
@@ -270,46 +270,50 @@ def test_client_library_load_table_from_gcs_csv(to_delete):
270270

271271

272272
def test_datalab_load_table_from_dataframe(to_delete):
273-
# [START bigquery_migration_datalab_load_table_from_dataframe]
274-
import google.datalab.bigquery as bq
275-
import pandas
276-
277-
# Create the dataset
278-
dataset_id = 'import_sample'
279-
# [END bigquery_migration_datalab_load_table_from_dataframe]
280-
# Use unique dataset ID to avoid collisions when running tests
281-
dataset_id = 'test_dataset_{}'.format(int(time.time() * 1000))
282-
to_delete.append(dataset_id)
283-
# [START bigquery_migration_datalab_load_table_from_dataframe]
284-
bq.Dataset(dataset_id).create()
285-
286-
# Create the table and load the data
287-
dataframe = pandas.DataFrame([
288-
{'title': 'The Meaning of Life', 'release_year': 1983},
289-
{'title': 'Monty Python and the Holy Grail', 'release_year': 1975},
290-
{'title': 'Life of Brian', 'release_year': 1979},
291-
{
292-
'title': 'And Now for Something Completely Different',
293-
'release_year': 1971
294-
},
295-
])
296-
schema = bq.Schema.from_data(dataframe)
297-
table = bq.Table(
298-
'{}.monty_python'.format(dataset_id)).create(schema=schema)
299-
table.insert(dataframe) # Starts steaming insert of data
300-
# [END bigquery_migration_datalab_load_table_from_dataframe]
301-
# The Datalab library uses tabledata().insertAll() to load data from
302-
# pandas DataFrames to tables. Because it can take a long time for the rows
303-
# to be available in the table, this test does not assert on the number of
304-
# rows in the destination table after the job is run. If errors are
305-
# encountered during the insertion, this test will fail.
306-
# See https://cloud.google.com/bigquery/streaming-data-into-bigquery
273+
""" Wrap test with retries to handle transient errors """
274+
@Retry()
275+
def datalab_load_table_from_dataframe(to_delete):
276+
# [START bigquery_migration_datalab_load_table_from_dataframe]
277+
import google.datalab.bigquery as bq
278+
import pandas
279+
280+
# Create the dataset
281+
dataset_id = 'import_sample'
282+
# [END bigquery_migration_datalab_load_table_from_dataframe]
283+
# Use unique dataset ID to avoid collisions when running tests
284+
dataset_id = 'test_dataset_{}'.format(int(time.time() * 1000))
285+
to_delete.append(dataset_id)
286+
# [START bigquery_migration_datalab_load_table_from_dataframe]
287+
bq.Dataset(dataset_id).create()
288+
289+
# Create the table and load the data
290+
dataframe = pandas.DataFrame([
291+
{'title': 'The Meaning of Life', 'release_year': 1983},
292+
{'title': 'Monty Python and the Holy Grail', 'release_year': 1975},
293+
{'title': 'Life of Brian', 'release_year': 1979},
294+
{
295+
'title': 'And Now for Something Completely Different',
296+
'release_year': 1971
297+
},
298+
])
299+
schema = bq.Schema.from_data(dataframe)
300+
table = bq.Table(
301+
'{}.monty_python'.format(dataset_id)).create(schema=schema)
302+
table.insert(dataframe) # Starts steaming insert of data
303+
# [END bigquery_migration_datalab_load_table_from_dataframe]
304+
# The Datalab library uses tabledata().insertAll() to load data from
305+
# pandas DataFrames to tables. Because it can take a long time for the rows
306+
# to be available in the table, this test does not assert on the number of
307+
# rows in the destination table after the job is run. If errors are
308+
# encountered during the insertion, this test will fail.
309+
# See https://cloud.google.com/bigquery/streaming-data-into-bigquery
310+
datalab_load_table_from_dataframe(to_delete)
307311

308312

309313
def test_client_library_load_table_from_dataframe(to_delete):
310314
# [START bigquery_migration_client_library_load_table_from_dataframe]
311-
from google.cloud import bigquery
312315
import pandas
316+
from google.cloud import bigquery
313317

314318
client = bigquery.Client(location='US')
315319

0 commit comments

Comments
 (0)