|
1 | 1 | import io |
| 2 | +import logging |
2 | 3 | import os |
3 | 4 | import tempfile |
4 | 5 | import time |
|
12 | 13 | from types import SimpleNamespace |
13 | 14 | from unittest import mock |
14 | 15 |
|
| 16 | +import fsspec |
15 | 17 | import pytest |
16 | 18 | from fsspec import Callback |
| 19 | +from fsspec.registry import _registry, known_implementations |
17 | 20 |
|
18 | 21 | from pyathena.filesystem import register_s3_filesystem |
19 | 22 | from pyathena.filesystem.s3 import S3File, S3FileSystem |
@@ -1653,3 +1656,43 @@ def test_upload_chunk_multipart(self, autocommit): |
1653 | 1656 | file.commit() |
1654 | 1657 | file.fs._finish_multipart_upload.assert_called_once() |
1655 | 1658 | file.fs._put_object.assert_not_called() |
| 1659 | + |
| 1660 | + |
| 1661 | +class _DummyFileSystem: |
| 1662 | + pass |
| 1663 | + |
| 1664 | + |
| 1665 | +@pytest.fixture |
| 1666 | +def registry_state(): |
| 1667 | + registry = dict(_registry) |
| 1668 | + known = {k: dict(v) for k, v in known_implementations.items()} |
| 1669 | + yield |
| 1670 | + _registry.clear() |
| 1671 | + _registry.update(registry) |
| 1672 | + known_implementations.clear() |
| 1673 | + known_implementations.update(known) |
| 1674 | + |
| 1675 | + |
| 1676 | +def test_register_s3_filesystem(registry_state): |
| 1677 | + _registry.pop("s3", None) |
| 1678 | + _registry.pop("s3a", None) |
| 1679 | + |
| 1680 | + register_s3_filesystem() |
| 1681 | + assert fsspec.registry["s3"] is S3FileSystem |
| 1682 | + assert fsspec.registry["s3a"] is S3FileSystem |
| 1683 | + |
| 1684 | + |
| 1685 | +def test_register_s3_filesystem_overwrites_existing_registration(registry_state, caplog): |
| 1686 | + fsspec.register_implementation("s3", _DummyFileSystem, clobber=True) |
| 1687 | + |
| 1688 | + with caplog.at_level(logging.WARNING, logger="pyathena.filesystem"): |
| 1689 | + register_s3_filesystem() |
| 1690 | + # An explicitly registered filesystem class is overwritten with a warning. |
| 1691 | + assert fsspec.registry["s3"] is S3FileSystem |
| 1692 | + assert "will be overwritten" in caplog.text |
| 1693 | + |
| 1694 | + # Re-registering PyAthena's own filesystem does not warn. |
| 1695 | + caplog.clear() |
| 1696 | + with caplog.at_level(logging.WARNING, logger="pyathena.filesystem"): |
| 1697 | + register_s3_filesystem() |
| 1698 | + assert not caplog.text |
0 commit comments