|
18 | 18 | import decimal |
19 | 19 | import functools |
20 | 20 | import gc |
21 | | -import importlib.metadata as metadata |
22 | 21 | import operator |
23 | 22 | import queue |
24 | 23 | import time |
25 | 24 | from typing import Union |
26 | 25 | from unittest import mock |
27 | 26 | import warnings |
28 | 27 |
|
| 28 | +import importlib.metadata as metadata |
| 29 | + |
29 | 30 | try: |
30 | 31 | import pandas |
31 | 32 | import pandas.api.types |
|
46 | 47 | import pytest |
47 | 48 |
|
48 | 49 | from google import api_core |
49 | | -from google.cloud.bigquery import ( |
50 | | - _pyarrow_helpers, |
51 | | - _versions_helpers, |
52 | | - exceptions, |
53 | | - schema, |
54 | | -) |
| 50 | + |
| 51 | +from google.cloud.bigquery import exceptions |
| 52 | +from google.cloud.bigquery import _pyarrow_helpers |
| 53 | +from google.cloud.bigquery import _versions_helpers |
| 54 | +from google.cloud.bigquery import schema |
55 | 55 | from google.cloud.bigquery._pandas_helpers import determine_requested_streams |
56 | 56 |
|
57 | 57 | pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import() |
@@ -1831,7 +1831,8 @@ def test__download_table_bqstorage( |
1831 | 1831 | expected_call_count, |
1832 | 1832 | expected_maxsize, |
1833 | 1833 | ): |
1834 | | - from google.cloud.bigquery import dataset, table |
| 1834 | + from google.cloud.bigquery import dataset |
| 1835 | + from google.cloud.bigquery import table |
1835 | 1836 |
|
1836 | 1837 | queue_used = None # A reference to the queue used by code under test. |
1837 | 1838 |
|
@@ -1884,11 +1885,11 @@ def test__download_table_bqstorage_shuts_down_workers( |
1884 | 1885 | the child threads are also stopped. |
1885 | 1886 | """ |
1886 | 1887 | pytest.importorskip("google.cloud.bigquery_storage_v1") |
| 1888 | + from google.cloud.bigquery import dataset |
| 1889 | + from google.cloud.bigquery import table |
1887 | 1890 | import google.cloud.bigquery_storage_v1.reader |
1888 | 1891 | import google.cloud.bigquery_storage_v1.types |
1889 | 1892 |
|
1890 | | - from google.cloud.bigquery import dataset, table |
1891 | | - |
1892 | 1893 | monkeypatch.setattr( |
1893 | 1894 | _versions_helpers.BQ_STORAGE_VERSIONS, "_installed_version", None |
1894 | 1895 | ) |
@@ -2210,10 +2211,10 @@ def test_determine_requested_streams_invalid_max_stream_count(): |
2210 | 2211 | bigquery_storage is None, reason="Requires google-cloud-bigquery-storage" |
2211 | 2212 | ) |
2212 | 2213 | def test__download_table_bqstorage_w_timeout_error(module_under_test): |
| 2214 | + from google.cloud.bigquery import dataset |
| 2215 | + from google.cloud.bigquery import table |
2213 | 2216 | from unittest import mock |
2214 | 2217 |
|
2215 | | - from google.cloud.bigquery import dataset, table |
2216 | | - |
2217 | 2218 | mock_bqstorage_client = mock.create_autospec( |
2218 | 2219 | bigquery_storage.BigQueryReadClient, instance=True |
2219 | 2220 | ) |
@@ -2247,10 +2248,10 @@ def slow_download_stream( |
2247 | 2248 | bigquery_storage is None, reason="Requires google-cloud-bigquery-storage" |
2248 | 2249 | ) |
2249 | 2250 | def test__download_table_bqstorage_w_timeout_success(module_under_test): |
| 2251 | + from google.cloud.bigquery import dataset |
| 2252 | + from google.cloud.bigquery import table |
2250 | 2253 | from unittest import mock |
2251 | 2254 |
|
2252 | | - from google.cloud.bigquery import dataset, table |
2253 | | - |
2254 | 2255 | mock_bqstorage_client = mock.create_autospec( |
2255 | 2256 | bigquery_storage.BigQueryReadClient, instance=True |
2256 | 2257 | ) |
@@ -2408,38 +2409,3 @@ def test_download_arrow_bqstorage_passes_timeout_to_create_read_session( |
2408 | 2409 | assert retry_policy is not None |
2409 | 2410 | # Check if deadline is set correctly in the retry policy |
2410 | 2411 | assert retry_policy._deadline == timeout |
2411 | | - |
2412 | | - |
2413 | | -@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") |
2414 | | -def test_dataframe_to_bq_schema_w_unused_schema_field(module_under_test): |
2415 | | - with mock.patch.object(module_under_test, "pandas_gbq", None): |
2416 | | - with pytest.raises( |
2417 | | - ValueError, match="bq_schema contains fields not present in dataframe" |
2418 | | - ): |
2419 | | - module_under_test.dataframe_to_bq_schema( |
2420 | | - pandas.DataFrame(), (schema.SchemaField("not_in_df", "STRING"),) |
2421 | | - ) |
2422 | | - |
2423 | | - |
2424 | | -@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") |
2425 | | -@pytest.mark.skipif(isinstance(pyarrow, mock.Mock), reason="Requires `pyarrow`") |
2426 | | -def test_get_schema_by_pyarrow_bignumeric(module_under_test): |
2427 | | - series = pandas.Series([decimal.Decimal("1.12345678901")]) |
2428 | | - result = module_under_test._get_schema_by_pyarrow("col", series) |
2429 | | - assert result is not None |
2430 | | - assert result.field_type == "BIGNUMERIC" |
2431 | | - |
2432 | | - |
2433 | | -@pytest.mark.skipif(pandas is None, reason="Requires `pandas`") |
2434 | | -@pytest.mark.skipif(isinstance(pyarrow, mock.Mock), reason="Requires `pyarrow`") |
2435 | | -def test_get_types_mapper_range_timestamp_mismatch(module_under_test): |
2436 | | - if not hasattr(pandas, "ArrowDtype"): |
2437 | | - return |
2438 | | - range_ts = pandas.ArrowDtype( |
2439 | | - pyarrow.struct( |
2440 | | - [("start", pyarrow.timestamp("us")), ("end", pyarrow.timestamp("us"))] |
2441 | | - ) |
2442 | | - ) |
2443 | | - mapper = module_under_test.default_types_mapper(range_timestamp_dtype=range_ts) |
2444 | | - unmatched_struct = pyarrow.struct([("other", pyarrow.int64())]) |
2445 | | - assert mapper(unmatched_struct) is None |
0 commit comments