Skip to content

Commit

Permalink
Using binary streams for uploads in BigQuery system tests. (#3374)
Browse files Browse the repository at this point in the history
Fixes #3371.
  • Loading branch information
dhermes authored May 5, 2017
1 parent 98c9cb7 commit 9870057
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ def test_insert_data_then_dump_table(self):

def test_load_table_from_local_file_then_dump_table(self):
import csv
import tempfile
from google.cloud._testing import _NamedTemporaryFile

ROWS = [
('Phred Phlyntstone', 32),
('Bharney Rhubble', 33),
Expand All @@ -360,13 +361,13 @@ def test_load_table_from_local_file_then_dump_table(self):
table.create()
self.to_delete.insert(0, table)

with tempfile.NamedTemporaryFile(mode='w+') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(('Full Name', 'Age'))
writer.writerows(ROWS)
csv_file.flush()
with _NamedTemporaryFile() as temp:
with open(temp.name, 'w') as csv_write:
writer = csv.writer(csv_write)
writer.writerow(('Full Name', 'Age'))
writer.writerows(ROWS)

with open(csv_file.name, 'rb') as csv_read:
with open(temp.name, 'rb') as csv_read:
job = table.upload_from_file(
csv_read,
source_format='CSV',
Expand All @@ -391,8 +392,9 @@ def _job_done(instance):

def test_load_table_from_storage_then_dump_table(self):
import csv
import tempfile
from google.cloud._testing import _NamedTemporaryFile
from google.cloud.storage import Client as StorageClient

local_id = unique_resource_id()
BUCKET_NAME = 'bq_load_test' + local_id
BLOB_NAME = 'person_ages.csv'
Expand All @@ -414,12 +416,14 @@ def test_load_table_from_storage_then_dump_table(self):

blob = bucket.blob(BLOB_NAME)

with tempfile.TemporaryFile(mode='w+') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(('Full Name', 'Age'))
writer.writerows(ROWS)
blob.upload_from_file(
csv_file, rewind=True, content_type='text/csv')
with _NamedTemporaryFile() as temp:
with open(temp.name, 'w') as csv_write:
writer = csv.writer(csv_write)
writer.writerow(('Full Name', 'Age'))
writer.writerows(ROWS)

with open(temp.name, 'rb') as csv_read:
blob.upload_from_file(csv_read, content_type='text/csv')

self.to_delete.insert(0, blob)

Expand Down

0 comments on commit 9870057

Please sign in to comment.