|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING, Literal |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from tests.plan.utils import dataframe |
| 9 | +from tests.utils import is_windows |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from collections.abc import Mapping |
| 13 | + |
| 14 | + from typing_extensions import TypeAlias |
| 15 | + |
| 16 | + from narwhals.typing import FileSource |
| 17 | + from tests.conftest import Data |
| 18 | + |
| 19 | +pytest.importorskip("pyarrow") |
| 20 | + |
| 21 | +IOTargetKind: TypeAlias = Literal["str", "Path", "PathLike"] |
| 22 | +"""Duplicated from `tests.read_scan_test.py`. |
| 23 | +
|
| 24 | +Needs extending for `BytesIO`. |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +class MockPathLike: |
| 29 | + def __init__(self, path: Path) -> None: |
| 30 | + self._super_secret: Path = path |
| 31 | + |
| 32 | + def __fspath__(self) -> str: |
| 33 | + return self._super_secret.__fspath__() |
| 34 | + |
| 35 | + |
| 36 | +def _into_file_source(source: Path, which: IOTargetKind, /) -> FileSource: |
| 37 | + mapping: Mapping[IOTargetKind, FileSource] = { |
| 38 | + "str": str(source), |
| 39 | + "Path": source, |
| 40 | + "PathLike": MockPathLike(source), |
| 41 | + } |
| 42 | + return mapping[which] |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(params=["str", "Path", "PathLike"]) |
| 46 | +def csv_path( |
| 47 | + tmp_path_factory: pytest.TempPathFactory, request: pytest.FixtureRequest |
| 48 | +) -> FileSource: |
| 49 | + fp = tmp_path_factory.mktemp("data") / "file.csv" |
| 50 | + return _into_file_source(fp, request.param) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(params=["str", "Path", "PathLike"]) |
| 54 | +def parquet_path( |
| 55 | + tmp_path_factory: pytest.TempPathFactory, request: pytest.FixtureRequest |
| 56 | +) -> FileSource: |
| 57 | + fp = tmp_path_factory.mktemp("data") / "file.parquet" |
| 58 | + return _into_file_source(fp, request.param) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture(scope="module") |
| 62 | +def data() -> Data: |
| 63 | + return {"a": [1, 2, 3]} |
| 64 | + |
| 65 | + |
| 66 | +XFAIL_DATAFRAME_EXPORT = pytest.mark.xfail( |
| 67 | + reason="TODO: `DataFrame.write_{csv,parquet}`()", raises=NotImplementedError |
| 68 | +) |
| 69 | + |
| 70 | + |
| 71 | +@XFAIL_DATAFRAME_EXPORT |
| 72 | +def test_write_csv(data: Data, csv_path: FileSource) -> None: # pragma: no cover |
| 73 | + df = dataframe(data) |
| 74 | + result_none = df.write_csv(csv_path) |
| 75 | + assert Path(csv_path).exists() |
| 76 | + assert result_none is None |
| 77 | + result = dataframe(data).write_csv() |
| 78 | + if is_windows(): # pragma: no cover |
| 79 | + result = result.replace("\r\n", "\n") |
| 80 | + if df.implementation.is_pyarrow(): |
| 81 | + assert result == '"a"\n1\n2\n3\n' |
| 82 | + else: # pragma: no cover |
| 83 | + assert result == "a\n1\n2\n3\n" |
| 84 | + |
| 85 | + |
| 86 | +@XFAIL_DATAFRAME_EXPORT |
| 87 | +def test_write_parquet(data: Data, parquet_path: FileSource) -> None: # pragma: no cover |
| 88 | + dataframe(data).write_parquet(parquet_path) |
| 89 | + assert Path(parquet_path).exists() |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.xfail( |
| 93 | + reason="TODO: `DataFrame.lazy()`, `LazyFrame.sink_parquet()`", raises=AttributeError |
| 94 | +) |
| 95 | +def test_sink_parquet(data: Data, parquet_path: FileSource) -> None: # pragma: no cover |
| 96 | + df = dataframe(data) |
| 97 | + df.lazy().sink_parquet(parquet_path) # type: ignore[attr-defined] |
| 98 | + assert Path(parquet_path).exists() |
0 commit comments