Skip to content

Commit fa88745

Browse files
tseavertswast
authored andcommitted
Add support/tests for loading tables from 'gzip.GzipFile'. (googleapis#5711)
Closes googleapis#5276.
1 parent 3b34b63 commit fa88745

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

bigquery/google/cloud/bigquery/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import collections
2020
import functools
21+
import gzip
2122
import os
2223
import uuid
2324

@@ -1593,10 +1594,16 @@ def _check_mode(stream):
15931594
"""
15941595
mode = getattr(stream, 'mode', None)
15951596

1596-
if mode is not None and mode not in ('rb', 'r+b', 'rb+'):
1597-
raise ValueError(
1598-
"Cannot upload files opened in text mode: use "
1599-
"open(filename, mode='rb') or open(filename, mode='r+b')")
1597+
if isinstance(stream, gzip.GzipFile):
1598+
if mode != gzip.READ:
1599+
raise ValueError(
1600+
"Cannot upload gzip files opened in write mode: use "
1601+
"gzip.GzipFile(filename, mode='rb')")
1602+
else:
1603+
if mode is not None and mode not in ('rb', 'r+b', 'rb+'):
1604+
raise ValueError(
1605+
"Cannot upload files opened in text mode: use "
1606+
"open(filename, mode='rb') or open(filename, mode='r+b')")
16001607

16011608

16021609
def _get_upload_headers(user_agent):

bigquery/tests/unit/test_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import datetime
1717
import decimal
1818
import email
19+
import gzip
1920
import io
2021
import json
2122
import unittest
@@ -3712,6 +3713,12 @@ def _make_do_upload_patch(cls, client, method,
37123713
def _make_file_obj():
37133714
return io.BytesIO(b'hello, is it me you\'re looking for?')
37143715

3716+
def _make_gzip_file_obj(self, writable):
3717+
if writable:
3718+
return gzip.GzipFile(mode='w', fileobj=io.BytesIO())
3719+
else:
3720+
return gzip.GzipFile(mode='r', fileobj=self._make_file_obj())
3721+
37153722
@staticmethod
37163723
def _make_config():
37173724
from google.cloud.bigquery.job import LoadJobConfig
@@ -3892,6 +3899,33 @@ def test_load_table_from_file_with_rewind(self):
38923899

38933900
assert file_obj.tell() == 0
38943901

3902+
def test_load_table_from_file_with_readable_gzip(self):
3903+
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
3904+
3905+
client = self._make_client()
3906+
gzip_file = self._make_gzip_file_obj(writable=False)
3907+
3908+
do_upload_patch = self._make_do_upload_patch(
3909+
client, '_do_resumable_upload', self.EXPECTED_CONFIGURATION)
3910+
with do_upload_patch as do_upload:
3911+
client.load_table_from_file(
3912+
gzip_file, self.TABLE_REF, job_id='job_id',
3913+
job_config=self._make_config())
3914+
3915+
do_upload.assert_called_once_with(
3916+
gzip_file,
3917+
self.EXPECTED_CONFIGURATION,
3918+
_DEFAULT_NUM_RETRIES)
3919+
3920+
def test_load_table_from_file_with_writable_gzip(self):
3921+
client = self._make_client()
3922+
gzip_file = self._make_gzip_file_obj(writable=True)
3923+
3924+
with pytest.raises(ValueError):
3925+
client.load_table_from_file(
3926+
gzip_file, self.TABLE_REF, job_id='job_id',
3927+
job_config=self._make_config())
3928+
38953929
def test_load_table_from_file_failure(self):
38963930
from google.resumable_media import InvalidResponse
38973931
from google.cloud import exceptions

0 commit comments

Comments
 (0)