Path: blob/main/py-polars/tests/unit/meta/test_plugins.py
8407 views
from __future__ import annotations12import sys3from pathlib import Path4from typing import Any56import pytest78import polars as pl9from polars.exceptions import ComputeError10from polars.plugins import (11_is_dynamic_lib,12_resolve_plugin_path,13_serialize_kwargs,14register_plugin_function,15)16from tests.conftest import PlMonkeyPatch171819@pytest.mark.write_disk20def test_register_plugin_function_invalid_plugin_path(tmp_path: Path) -> None:21tmp_path.mkdir(exist_ok=True)22plugin_path = tmp_path / "lib.so"23plugin_path.touch()2425expr = register_plugin_function(26plugin_path=plugin_path, function_name="hello", args=527)2829with pytest.raises(ComputeError, match="error loading dynamic library"):30pl.select(expr)313233@pytest.mark.parametrize(34("input", "expected"),35[36(None, b""),37({}, b""),38(39{"hi": 0},40b"\x80\x05\x95\x0b\x00\x00\x00\x00\x00\x00\x00}\x94\x8c\x02hi\x94K\x00s.",41),42],43)44def test_serialize_kwargs(input: dict[str, Any] | None, expected: bytes) -> None:45assert _serialize_kwargs(input) == expected464748@pytest.mark.write_disk49@pytest.mark.parametrize("use_abs_path", [True, False])50def test_resolve_plugin_path(51plmonkeypatch: PlMonkeyPatch,52tmp_path: Path,53use_abs_path: bool,54) -> None:55tmp_path.mkdir(exist_ok=True)5657mock_venv = tmp_path / ".venv"58mock_venv.mkdir(exist_ok=True)59mock_venv_lib = mock_venv / "lib"60mock_venv_lib.mkdir(exist_ok=True)61(mock_venv_lib / "lib1.so").touch()62(mock_venv_lib / "__init__.py").touch()6364with PlMonkeyPatch.context() as mp:65mp.setattr(sys, "prefix", str(mock_venv))66expected_full_path = mock_venv_lib / "lib1.so"67expected_relative_path = expected_full_path.relative_to(mock_venv)6869if use_abs_path:70result = _resolve_plugin_path(mock_venv_lib, use_abs_path=use_abs_path)71assert result == expected_full_path72else:73result = _resolve_plugin_path(mock_venv_lib, use_abs_path=use_abs_path)74assert result == expected_relative_path757677@pytest.mark.write_disk78def test_resolve_plugin_path_raises(tmp_path: Path) -> None:79tmp_path.mkdir(exist_ok=True)80(tmp_path / "__init__.py").touch()8182with pytest.raises(FileNotFoundError, match="no dynamic library found"):83_resolve_plugin_path(tmp_path)848586@pytest.mark.write_disk87@pytest.mark.parametrize(88("path", "expected"),89[90(Path("lib.so"), True),91(Path("lib.pyd"), True),92(Path("lib.dll"), True),93(Path("lib.py"), False),94],95)96def test_is_dynamic_lib(path: Path, expected: bool, tmp_path: Path) -> None:97tmp_path.mkdir(exist_ok=True)98full_path = tmp_path / path99full_path.touch()100assert _is_dynamic_lib(full_path) is expected101102103@pytest.mark.write_disk104def test_is_dynamic_lib_dir(tmp_path: Path) -> None:105path = Path("lib.so")106full_path = tmp_path / path107108full_path.mkdir(exist_ok=True)109(full_path / "hello.txt").touch()110111assert _is_dynamic_lib(full_path) is False112113114