|
16 | 16 | import datetime |
17 | 17 | import decimal |
18 | 18 | import email |
| 19 | +import gzip |
19 | 20 | import io |
20 | 21 | import json |
21 | 22 | import unittest |
@@ -3712,6 +3713,12 @@ def _make_do_upload_patch(cls, client, method, |
3712 | 3713 | def _make_file_obj(): |
3713 | 3714 | return io.BytesIO(b'hello, is it me you\'re looking for?') |
3714 | 3715 |
|
| 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 | + |
3715 | 3722 | @staticmethod |
3716 | 3723 | def _make_config(): |
3717 | 3724 | from google.cloud.bigquery.job import LoadJobConfig |
@@ -3892,6 +3899,33 @@ def test_load_table_from_file_with_rewind(self): |
3892 | 3899 |
|
3893 | 3900 | assert file_obj.tell() == 0 |
3894 | 3901 |
|
| 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 | + |
3895 | 3929 | def test_load_table_from_file_failure(self): |
3896 | 3930 | from google.resumable_media import InvalidResponse |
3897 | 3931 | from google.cloud import exceptions |
|
0 commit comments