From 78d66ecc8b2266f3c52bc33f2149e13605f228c9 Mon Sep 17 00:00:00 2001 From: Noritaka Sekiyama Date: Sun, 29 Nov 2026 22:39:37 +0900 Subject: [PATCH] Test manifest cache size without reloading pyiceberg.manifest The cache size tests reloaded pyiceberg.manifest to pick up a new configuration. A reload replaces the module's classes, so test modules that imported them earlier, such as tests/table/test_validate.py, fail when they run afterwards. Build a _ManifestCache directly and patch it in with monkeypatch instead; the size is read when the cache is created. Generated-by: Claude Code --- tests/utils/test_manifest.py | 95 ++++++++++++++---------------------- 1 file changed, 37 insertions(+), 58 deletions(-) diff --git a/tests/utils/test_manifest.py b/tests/utils/test_manifest.py index c084e0ccf7..2f5270b86d 100644 --- a/tests/utils/test_manifest.py +++ b/tests/utils/test_manifest.py @@ -15,7 +15,6 @@ # specific language governing permissions and limitations # under the License. # pylint: disable=redefined-outer-name,arguments-renamed,fixme -import importlib from dataclasses import replace from pathlib import Path from tempfile import TemporaryDirectory @@ -1560,53 +1559,45 @@ def test_clear_manifest_cache() -> None: def test_manifest_cache_can_be_disabled_with_size_zero(monkeypatch: pytest.MonkeyPatch) -> None: """Test that manifest-cache-size=0 disables caching.""" monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "0") - importlib.reload(manifest_module) + monkeypatch.setattr(manifest_module, "_manifest_cache", manifest_module._ManifestCache()) - try: - assert manifest_module._manifest_cache.maxsize == 0 - assert len(manifest_module._manifest_cache) == 0 + assert manifest_module._manifest_cache.maxsize == 0 + assert len(manifest_module._manifest_cache) == 0 - io = PyArrowFileIO() + io = PyArrowFileIO() - with TemporaryDirectory() as tmp_dir: - list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="disabled", snapshot_id=1) + with TemporaryDirectory() as tmp_dir: + list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="disabled", snapshot_id=1) - manifests_first_call = manifest_module._manifests(io, list_path) - manifests_second_call = manifest_module._manifests(io, list_path) + manifests_first_call = manifest_module._manifests(io, list_path) + manifests_second_call = manifest_module._manifests(io, list_path) - assert len(manifest_module._manifest_cache) == 0 - assert manifests_first_call[0] is not manifests_second_call[0] - finally: - monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False) - importlib.reload(manifest_module) + assert len(manifest_module._manifest_cache) == 0 + assert manifests_first_call[0] is not manifests_second_call[0] def test_manifest_cache_respects_positive_env_size(monkeypatch: pytest.MonkeyPatch) -> None: """Test that a positive manifest-cache-size enables a bounded cache.""" monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "1") - importlib.reload(manifest_module) + monkeypatch.setattr(manifest_module, "_manifest_cache", manifest_module._ManifestCache()) - try: - assert manifest_module._manifest_cache.maxsize == 1 + assert manifest_module._manifest_cache.maxsize == 1 - io = PyArrowFileIO() + io = PyArrowFileIO() - with TemporaryDirectory() as tmp_dir: - first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1) - second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2) + with TemporaryDirectory() as tmp_dir: + first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1) + second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2) - manifests_first_call = manifest_module._manifests(io, first_list_path) - manifests_second_call = manifest_module._manifests(io, first_list_path) + manifests_first_call = manifest_module._manifests(io, first_list_path) + manifests_second_call = manifest_module._manifests(io, first_list_path) - assert manifests_first_call[0] is manifests_second_call[0] - assert len(manifest_module._manifest_cache) == 1 + assert manifests_first_call[0] is manifests_second_call[0] + assert len(manifest_module._manifest_cache) == 1 - manifest_module._manifests(io, second_list_path) + manifest_module._manifests(io, second_list_path) - assert len(manifest_module._manifest_cache) == 1 - finally: - monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False) - importlib.reload(manifest_module) + assert len(manifest_module._manifest_cache) == 1 def test_manifest_cache_reads_size_from_configuration_file(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: @@ -1617,47 +1608,35 @@ def test_manifest_cache_reads_size_from_configuration_file(monkeypatch: pytest.M monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False) monkeypatch.setenv("PYICEBERG_HOME", str(config_dir)) - importlib.reload(manifest_module) + monkeypatch.setattr(manifest_module, "_manifest_cache", manifest_module._ManifestCache()) - try: - assert manifest_module._manifest_cache.maxsize == 2 + assert manifest_module._manifest_cache.maxsize == 2 - io = PyArrowFileIO() + io = PyArrowFileIO() - with TemporaryDirectory() as tmp_dir: - first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1) - second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2) - third_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="third", snapshot_id=3) + with TemporaryDirectory() as tmp_dir: + first_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="first", snapshot_id=1) + second_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="second", snapshot_id=2) + third_list_path = _create_test_manifest_list(manifest_module, io, tmp_dir, name="third", snapshot_id=3) - manifest_module._manifests(io, first_list_path) - manifest_module._manifests(io, second_list_path) - manifest_module._manifests(io, third_list_path) + manifest_module._manifests(io, first_list_path) + manifest_module._manifests(io, second_list_path) + manifest_module._manifests(io, third_list_path) - assert len(manifest_module._manifest_cache) == 2 - finally: - monkeypatch.delenv("PYICEBERG_HOME", raising=False) - importlib.reload(manifest_module) + assert len(manifest_module._manifest_cache) == 2 def test_invalid_manifest_cache_size_raises_value_error(monkeypatch: pytest.MonkeyPatch) -> None: """Test that invalid manifest-cache-size values raise a helpful error.""" monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "not-an-int") - try: - with pytest.raises(ValueError, match="manifest-cache-size should be an integer or left unset"): - importlib.reload(manifest_module) - finally: - monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False) - importlib.reload(manifest_module) + with pytest.raises(ValueError, match="manifest-cache-size should be an integer or left unset"): + manifest_module._ManifestCache() def test_negative_manifest_cache_size_raises_value_error(monkeypatch: pytest.MonkeyPatch) -> None: """Test that negative manifest-cache-size values raise a helpful error.""" monkeypatch.setenv("PYICEBERG_MANIFEST_CACHE_SIZE", "-1") - try: - with pytest.raises(ValueError, match="manifest-cache-size should be a non-negative integer or left unset"): - importlib.reload(manifest_module) - finally: - monkeypatch.delenv("PYICEBERG_MANIFEST_CACHE_SIZE", raising=False) - importlib.reload(manifest_module) + with pytest.raises(ValueError, match="manifest-cache-size should be a non-negative integer or left unset"): + manifest_module._ManifestCache()