|
23 | 23 | import six |
24 | 24 | from six.moves import http_client |
25 | 25 | import pytest |
| 26 | +try: |
| 27 | + import pandas |
| 28 | +except (ImportError, AttributeError): # pragma: NO COVER |
| 29 | + pandas = None |
| 30 | +try: |
| 31 | + import pyarrow |
| 32 | +except (ImportError, AttributeError): # pragma: NO COVER |
| 33 | + pyarrow = None |
26 | 34 |
|
27 | 35 | from google.cloud.bigquery.dataset import DatasetReference |
28 | 36 |
|
@@ -3484,6 +3492,68 @@ def test_load_table_from_file_bad_mode(self): |
3484 | 3492 | with pytest.raises(ValueError): |
3485 | 3493 | client.load_table_from_file(file_obj, self.TABLE_REF) |
3486 | 3494 |
|
| 3495 | + @unittest.skipIf(pandas is None, 'Requires `pandas`') |
| 3496 | + @unittest.skipIf(pyarrow is None, 'Requires `pyarrow`') |
| 3497 | + def test_load_table_from_dataframe(self): |
| 3498 | + from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES |
| 3499 | + from google.cloud.bigquery import job |
| 3500 | + |
| 3501 | + client = self._make_client() |
| 3502 | + records = [ |
| 3503 | + {'name': 'Monty', 'age': 100}, |
| 3504 | + {'name': 'Python', 'age': 60}, |
| 3505 | + ] |
| 3506 | + dataframe = pandas.DataFrame(records) |
| 3507 | + |
| 3508 | + load_patch = mock.patch( |
| 3509 | + 'google.cloud.bigquery.client.Client.load_table_from_file', |
| 3510 | + autospec=True) |
| 3511 | + with load_patch as load_table_from_file: |
| 3512 | + client.load_table_from_dataframe(dataframe, self.TABLE_REF) |
| 3513 | + |
| 3514 | + load_table_from_file.assert_called_once_with( |
| 3515 | + client, mock.ANY, self.TABLE_REF, num_retries=_DEFAULT_NUM_RETRIES, |
| 3516 | + rewind=True, job_id=None, job_id_prefix=None, location=None, |
| 3517 | + project=None, job_config=mock.ANY) |
| 3518 | + |
| 3519 | + sent_file = load_table_from_file.mock_calls[0][1][1] |
| 3520 | + sent_bytes = sent_file.getvalue() |
| 3521 | + assert isinstance(sent_bytes, bytes) |
| 3522 | + assert len(sent_bytes) > 0 |
| 3523 | + |
| 3524 | + sent_config = load_table_from_file.mock_calls[0][2]['job_config'] |
| 3525 | + assert sent_config.source_format == job.SourceFormat.PARQUET |
| 3526 | + |
| 3527 | + @unittest.skipIf(pandas is None, 'Requires `pandas`') |
| 3528 | + @unittest.skipIf(pyarrow is None, 'Requires `pyarrow`') |
| 3529 | + def test_load_table_from_dataframe_w_custom_job_config(self): |
| 3530 | + from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES |
| 3531 | + from google.cloud.bigquery import job |
| 3532 | + |
| 3533 | + client = self._make_client() |
| 3534 | + records = [ |
| 3535 | + {'name': 'Monty', 'age': 100}, |
| 3536 | + {'name': 'Python', 'age': 60}, |
| 3537 | + ] |
| 3538 | + dataframe = pandas.DataFrame(records) |
| 3539 | + job_config = job.LoadJobConfig() |
| 3540 | + |
| 3541 | + load_patch = mock.patch( |
| 3542 | + 'google.cloud.bigquery.client.Client.load_table_from_file', |
| 3543 | + autospec=True) |
| 3544 | + with load_patch as load_table_from_file: |
| 3545 | + client.load_table_from_dataframe( |
| 3546 | + dataframe, self.TABLE_REF, job_config=job_config) |
| 3547 | + |
| 3548 | + load_table_from_file.assert_called_once_with( |
| 3549 | + client, mock.ANY, self.TABLE_REF, num_retries=_DEFAULT_NUM_RETRIES, |
| 3550 | + rewind=True, job_id=None, job_id_prefix=None, location=None, |
| 3551 | + project=None, job_config=mock.ANY) |
| 3552 | + |
| 3553 | + sent_config = load_table_from_file.mock_calls[0][2]['job_config'] |
| 3554 | + assert sent_config is job_config |
| 3555 | + assert sent_config.source_format == job.SourceFormat.PARQUET |
| 3556 | + |
3487 | 3557 | # Low-level tests |
3488 | 3558 |
|
3489 | 3559 | @classmethod |
|
0 commit comments