Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using binary streams for uploads in BigQuery system tests. #3374

Merged
merged 1 commit into from
May 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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