|
15 | 15 |
|
16 | 16 | # pylint: disable=protected-access,bad-continuation |
17 | 17 |
|
| 18 | +import contextlib |
18 | 19 | import importlib |
19 | 20 | import pytest |
| 21 | +import sys |
20 | 22 | import warnings |
21 | 23 | from unittest import mock |
22 | 24 |
|
|
31 | 33 | _TEST_PROJECT = "test-project" |
32 | 34 | _TEST_LOCATION = "us-central1" |
33 | 35 |
|
| 36 | +_AGENTPLATFORM_GCS_UTILS = "agentplatform._genai._gcs_utils" |
| 37 | +_AGENTPLATFORM_RAG = "agentplatform._genai.rag" |
| 38 | +_VERTEXAI_GCS_UTILS = "vertexai._genai._gcs_utils" |
| 39 | + |
34 | 40 |
|
35 | 41 | pytestmark = pytest.mark.usefixtures("google_auth_mock") |
36 | 42 |
|
@@ -122,3 +128,63 @@ def test_vertexai_client_deprecation_warning(self): |
122 | 128 | warnings.simplefilter("error", FutureWarning) |
123 | 129 | _ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION) |
124 | 130 | _ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 131 | + |
| 132 | + |
| 133 | +@contextlib.contextmanager |
| 134 | +def _pandas_unavailable(reimported_modules=()): |
| 135 | + """Makes `import pandas` fail, as it does when the extra is not installed. |
| 136 | +
|
| 137 | + Modules named in `reimported_modules` are evicted from the module cache and |
| 138 | + from their parent package, so that their top-level code runs again while |
| 139 | + pandas is unavailable. Evicting the parent package attribute matters because |
| 140 | + `from . import <submodule>` reads it in preference to re-importing. |
| 141 | + """ |
| 142 | + evicted = [] |
| 143 | + for name in reimported_modules: |
| 144 | + parent_name, _, attribute = name.rpartition(".") |
| 145 | + importlib.import_module(name) |
| 146 | + parent = sys.modules[parent_name] |
| 147 | + evicted.append((parent, attribute, getattr(parent, attribute))) |
| 148 | + try: |
| 149 | + with mock.patch.dict(sys.modules): |
| 150 | + for name in list(sys.modules): |
| 151 | + if name == "pandas" or name.startswith("pandas."): |
| 152 | + del sys.modules[name] |
| 153 | + # A None entry makes the import machinery raise ModuleNotFoundError. |
| 154 | + sys.modules["pandas"] = None |
| 155 | + for name in reimported_modules: |
| 156 | + del sys.modules[name] |
| 157 | + for parent, attribute, _ in evicted: |
| 158 | + delattr(parent, attribute) |
| 159 | + yield |
| 160 | + finally: |
| 161 | + for parent, attribute, original in evicted: |
| 162 | + setattr(parent, attribute, original) |
| 163 | + |
| 164 | + |
| 165 | +class TestPandasIsOptional: |
| 166 | + """pandas is only a dependency of the [evaluation] extra.""" |
| 167 | + |
| 168 | + @pytest.mark.parametrize( |
| 169 | + "module_name", [_AGENTPLATFORM_GCS_UTILS, _VERTEXAI_GCS_UTILS] |
| 170 | + ) |
| 171 | + def test_gcs_utils_imports_without_pandas(self, module_name): |
| 172 | + with _pandas_unavailable([module_name]): |
| 173 | + importlib.import_module(module_name) |
| 174 | + |
| 175 | + @pytest.mark.usefixtures("google_auth_mock") |
| 176 | + def test_rag_does_not_require_pandas(self): |
| 177 | + test_client = agentplatform.Client( |
| 178 | + project=_TEST_PROJECT, location=_TEST_LOCATION |
| 179 | + ) |
| 180 | + with _pandas_unavailable([_AGENTPLATFORM_RAG, _AGENTPLATFORM_GCS_UTILS]): |
| 181 | + assert test_client.rag is not None |
| 182 | + |
| 183 | + def test_read_gcs_file_to_dataframe_without_pandas_names_the_extra(self): |
| 184 | + gcs_utils_module = importlib.import_module(_AGENTPLATFORM_GCS_UTILS) |
| 185 | + with mock.patch.object(gcs_utils_module.storage, "Client", autospec=True): |
| 186 | + gcs_utils = gcs_utils_module.GcsUtils(mock.MagicMock()) |
| 187 | + |
| 188 | + with _pandas_unavailable(): |
| 189 | + with pytest.raises(ImportError, match=r"\[evaluation\]"): |
| 190 | + gcs_utils.read_gcs_file_to_dataframe("gs://bucket/data.csv", "csv") |
0 commit comments