python/shim-korp-map/tests/test_shim.py
"""The korp-map shim: warns, and is korp_map_client under the old name."""
from __future__ import annotations
import importlib
import subprocess
import sys
import pytest
import korp_map_client
MSG = "korp-map is renamed korp-map-client; pip install korp-map-client, import korp_map_client"
def _fresh_import():
sys.modules.pop("korp_map", None)
return importlib.import_module("korp_map")
def test_import_warns():
with pytest.warns(DeprecationWarning, match="korp-map is renamed korp-map-client"):
_fresh_import()
def test_reexports_everything():
with pytest.warns(DeprecationWarning):
km = _fresh_import()
assert km.__all__ == korp_map_client.__all__
for name in korp_map_client.__all__:
assert getattr(km, name) is getattr(korp_map_client, name), name
assert km.Client is korp_map_client.Client
assert km.KorpMapError is korp_map_client.KorpMapError
assert km.__version__ == korp_map_client.__version__
def test_warning_shown_by_default_in_main():
# Default filters show a DeprecationWarning attributed to __main__ code.
r = subprocess.run([sys.executable, "-c", "import korp_map"], capture_output=True, text=True, check=True)
assert MSG in r.stderr
assert "<string>" in r.stderr # attributed to the importer, not to the shim
def test_error_mode_refuses():
r = subprocess.run([sys.executable, "-W", "error::DeprecationWarning", "-c", "import korp_map"], capture_output=True, text=True)
assert r.returncode != 0 and "DeprecationWarning" in r.stderr
def test_new_client_features_pass_through():
with pytest.warns(DeprecationWarning):
km = _fresh_import()
# whatever korp-map-client version is installed (0.1 up to < 1), the shim is that client
for meth in ("map", "geocode", "revgeocode"):
assert hasattr(km.Client, meth)
if tuple(int(p) for p in korp_map_client.__version__.split(".")[:2]) >= (0, 2):
for meth in ("files", "file_url", "download", "read_parquet", "read_gdf"):
assert getattr(km.Client, meth) is getattr(korp_map_client.Client, meth)