Skip to content

Commit 0f5b0f8

Browse files
chalmerlowenidhiii-27
authored andcommitted
test: remove pandas version restrictions and fix precision issue (#17005)
This PR removes the pandas version restrictions from tests that were skipped for Pandas >= 2.0 without a stated reason and enables testing by addressing a precision issue in the tests. It also adds a catch for ImportError when tqdm tries to import matplotlib when matplotlib is not installed. It also adds two small ignore pragmas to resolve mypy's struggles with handling optional imports. This PR partially fixes #16042 (for bigquery). For completeness, we did an assessment of any remaining test skips and they all appear to be legitimate skips that are applied when a dependency is not installed. 1. PyArrow 2. tqdm (Progress Bar) 3. Pandas 4. OpenTelemetry 5. GeoPandas 6. Shapely
1 parent 1117443 commit 0f5b0f8

7 files changed

Lines changed: 45 additions & 17 deletions

File tree

packages/google-cloud-bigquery/google/cloud/bigquery/_pyarrow_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
try:
2727
import pyarrow # type: ignore
2828
except ImportError:
29-
pyarrow = None
29+
pyarrow = None # type: ignore[assignment]
3030

3131
try:
3232
import db_dtypes # type: ignore

packages/google-cloud-bigquery/google/cloud/bigquery/_tqdm_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def get_progress_bar(progress_bar_type, description, total, unit):
7070
)
7171
elif progress_bar_type == "tqdm_gui":
7272
return tqdm.tqdm_gui(desc=description, total=total, unit=unit)
73-
except (KeyError, TypeError): # pragma: NO COVER
73+
except (KeyError, TypeError, ImportError): # pragma: NO COVER
7474
# Protect ourselves from any tqdm errors. In case of
7575
# unexpected tqdm behavior, just fall back to showing
7676
# no progress bar.

packages/google-cloud-bigquery/google/cloud/bigquery/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
try:
3434
import pyarrow # type: ignore
3535
except ImportError:
36-
pyarrow = None
36+
pyarrow = None # type: ignore[assignment]
3737

3838
try:
3939
import db_dtypes # type: ignore

packages/google-cloud-bigquery/tests/system/test_pandas.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,6 @@ def test_list_rows_max_results_w_bqstorage(bigquery_client):
10531053
assert len(dataframe.index) == 100
10541054

10551055

1056-
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
10571056
@pytest.mark.parametrize(
10581057
("max_results",),
10591058
(

packages/google-cloud-bigquery/tests/unit/test__pandas_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,15 @@ def test_bq_to_arrow_array_w_nullable_scalars(module_under_test, bq_type, rows):
551551
],
552552
)
553553
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
554-
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
555554
@pytest.mark.skipif(isinstance(pyarrow, mock.Mock), reason="Requires `pyarrow`")
556555
def test_bq_to_arrow_array_w_pandas_timestamp(module_under_test, bq_type, rows):
557556
rows = [pandas.Timestamp(row) for row in rows]
558557
series = pandas.Series(rows)
559558
bq_field = schema.SchemaField("field_name", bq_type)
560559
arrow_array = module_under_test.bq_to_arrow_array(series, bq_field)
561560
roundtrip = arrow_array.to_pandas()
562-
assert series.equals(roundtrip)
561+
series = series.astype(roundtrip.dtype)
562+
pandas.testing.assert_series_equal(series, roundtrip)
563563

564564

565565
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")

packages/google-cloud-bigquery/tests/unit/test_magics.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545
pandas = pytest.importorskip("pandas")
4646

4747

48+
@pytest.fixture()
49+
def use_local_magics_context(monkeypatch):
50+
if magics is not None:
51+
local_context = magics.Context()
52+
local_context._project = "unit-test-project"
53+
mock_credentials = mock.create_autospec(
54+
google.auth.credentials.Credentials, instance=True
55+
)
56+
local_context._credentials = mock_credentials
57+
monkeypatch.setattr(magics, "context", local_context)
58+
59+
4860
@pytest.fixture(scope="session")
4961
def ipython():
5062
config = tools.default_config()
@@ -523,7 +535,7 @@ def test_bigquery_magic_default_connection_user_agent(monkeypatch):
523535

524536

525537
@pytest.mark.usefixtures("ipython_interactive")
526-
def test_bigquery_magic_with_legacy_sql(monkeypatch):
538+
def test_bigquery_magic_with_legacy_sql(monkeypatch, use_local_magics_context):
527539
ip = IPython.get_ipython()
528540
monkeypatch.setattr(bigquery, "bigquery_magics", None)
529541
bigquery.load_ipython_extension(ip)
@@ -543,7 +555,9 @@ def test_bigquery_magic_with_legacy_sql(monkeypatch):
543555

544556
@pytest.mark.usefixtures("ipython_interactive")
545557
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
546-
def test_bigquery_magic_with_result_saved_to_variable(ipython_ns_cleanup, monkeypatch):
558+
def test_bigquery_magic_with_result_saved_to_variable(
559+
ipython_ns_cleanup, monkeypatch, use_local_magics_context
560+
):
547561
ip = IPython.get_ipython()
548562
monkeypatch.setattr(bigquery, "bigquery_magics", None)
549563
bigquery.load_ipython_extension(ip)
@@ -577,7 +591,9 @@ def test_bigquery_magic_with_result_saved_to_variable(ipython_ns_cleanup, monkey
577591

578592

579593
@pytest.mark.usefixtures("ipython_interactive")
580-
def test_bigquery_magic_does_not_clear_display_in_verbose_mode(monkeypatch):
594+
def test_bigquery_magic_does_not_clear_display_in_verbose_mode(
595+
monkeypatch, use_local_magics_context
596+
):
581597
ip = IPython.get_ipython()
582598
monkeypatch.setattr(bigquery, "bigquery_magics", None)
583599
bigquery.load_ipython_extension(ip)
@@ -599,7 +615,9 @@ def test_bigquery_magic_does_not_clear_display_in_verbose_mode(monkeypatch):
599615

600616

601617
@pytest.mark.usefixtures("ipython_interactive")
602-
def test_bigquery_magic_clears_display_in_non_verbose_mode(monkeypatch):
618+
def test_bigquery_magic_clears_display_in_non_verbose_mode(
619+
monkeypatch, use_local_magics_context
620+
):
603621
ip = IPython.get_ipython()
604622
monkeypatch.setattr(bigquery, "bigquery_magics", None)
605623
bigquery.load_ipython_extension(ip)
@@ -624,7 +642,9 @@ def test_bigquery_magic_clears_display_in_non_verbose_mode(monkeypatch):
624642
@pytest.mark.skipif(
625643
bigquery_storage is None, reason="Requires `google-cloud-bigquery-storage`"
626644
)
627-
def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch):
645+
def test_bigquery_magic_with_bqstorage_from_argument(
646+
monkeypatch, use_local_magics_context
647+
):
628648
ip = IPython.get_ipython()
629649
monkeypatch.setattr(bigquery, "bigquery_magics", None)
630650
bigquery.load_ipython_extension(ip)
@@ -691,7 +711,9 @@ def warning_match(warning):
691711
@pytest.mark.skipif(
692712
bigquery_storage is None, reason="Requires `google-cloud-bigquery-storage`"
693713
)
694-
def test_bigquery_magic_with_rest_client_requested(monkeypatch):
714+
def test_bigquery_magic_with_rest_client_requested(
715+
monkeypatch, use_local_magics_context
716+
):
695717
pandas = pytest.importorskip("pandas")
696718

697719
ip = IPython.get_ipython()
@@ -1437,7 +1459,9 @@ def test_bigquery_magic_with_project(monkeypatch):
14371459

14381460

14391461
@pytest.mark.usefixtures("ipython_interactive")
1440-
def test_bigquery_magic_with_bigquery_api_endpoint(ipython_ns_cleanup, monkeypatch):
1462+
def test_bigquery_magic_with_bigquery_api_endpoint(
1463+
ipython_ns_cleanup, monkeypatch, use_local_magics_context
1464+
):
14411465
ip = IPython.get_ipython()
14421466
monkeypatch.setattr(bigquery, "bigquery_magics", None)
14431467
bigquery.load_ipython_extension(ip)
@@ -1460,7 +1484,9 @@ def test_bigquery_magic_with_bigquery_api_endpoint(ipython_ns_cleanup, monkeypat
14601484

14611485

14621486
@pytest.mark.usefixtures("ipython_interactive")
1463-
def test_bigquery_magic_with_bigquery_api_endpoint_context_dict(monkeypatch):
1487+
def test_bigquery_magic_with_bigquery_api_endpoint_context_dict(
1488+
monkeypatch, use_local_magics_context
1489+
):
14641490
ip = IPython.get_ipython()
14651491
monkeypatch.setattr(bigquery, "bigquery_magics", None)
14661492
bigquery.load_ipython_extension(ip)
@@ -1484,7 +1510,9 @@ def test_bigquery_magic_with_bigquery_api_endpoint_context_dict(monkeypatch):
14841510

14851511

14861512
@pytest.mark.usefixtures("ipython_interactive")
1487-
def test_bigquery_magic_with_bqstorage_api_endpoint(ipython_ns_cleanup, monkeypatch):
1513+
def test_bigquery_magic_with_bqstorage_api_endpoint(
1514+
ipython_ns_cleanup, monkeypatch, use_local_magics_context
1515+
):
14881516
ip = IPython.get_ipython()
14891517
monkeypatch.setattr(bigquery, "bigquery_magics", None)
14901518
bigquery.load_ipython_extension(ip)
@@ -1507,7 +1535,9 @@ def test_bigquery_magic_with_bqstorage_api_endpoint(ipython_ns_cleanup, monkeypa
15071535

15081536

15091537
@pytest.mark.usefixtures("ipython_interactive")
1510-
def test_bigquery_magic_with_bqstorage_api_endpoint_context_dict(monkeypatch):
1538+
def test_bigquery_magic_with_bqstorage_api_endpoint_context_dict(
1539+
monkeypatch, use_local_magics_context
1540+
):
15111541
ip = IPython.get_ipython()
15121542
monkeypatch.setattr(bigquery, "bigquery_magics", None)
15131543
bigquery.load_ipython_extension(ip)

packages/google-cloud-bigquery/tests/unit/test_table.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4092,7 +4092,6 @@ def test_to_dataframe_no_tqdm(self):
40924092

40934093
def test_to_dataframe_tqdm_error(self):
40944094
pytest.importorskip("pandas")
4095-
pytest.importorskip("matplotlib")
40964095
tqdm = pytest.importorskip("tqdm")
40974096
mock.patch("tqdm.tqdm_gui", new=None)
40984097
mock.patch("tqdm.notebook.tqdm", new=None)

0 commit comments

Comments
 (0)