-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(python): Refactor serde tests, add hypothesis tests
- Loading branch information
Showing
4 changed files
with
148 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import io | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
from polars.exceptions import ComputeError | ||
|
||
|
||
def test_expr_serialization_roundtrip() -> None: | ||
expr = pl.col("foo").sum().over("bar") | ||
json = expr.meta.serialize() | ||
round_tripped = pl.Expr.deserialize(io.StringIO(json)) | ||
assert round_tripped.meta == expr | ||
|
||
|
||
def test_expr_deserialize_file_not_found() -> None: | ||
with pytest.raises(FileNotFoundError): | ||
pl.Expr.deserialize("abcdef") | ||
|
||
|
||
def test_expr_deserialize_invalid_json() -> None: | ||
with pytest.raises( | ||
ComputeError, match="could not deserialize input into an expression" | ||
): | ||
pl.Expr.deserialize(io.StringIO("abcdef")) | ||
|
||
|
||
def test_expr_write_json_from_json_deprecated() -> None: | ||
expr = pl.col("foo").sum().over("bar") | ||
|
||
with pytest.deprecated_call(): | ||
json = expr.meta.write_json() | ||
|
||
with pytest.deprecated_call(): | ||
round_tripped = pl.Expr.from_json(json) | ||
|
||
assert round_tripped.meta == expr | ||
|
||
|
||
def test_expression_json_13991() -> None: | ||
expr = pl.col("foo").cast(pl.Decimal) | ||
json = expr.meta.serialize() | ||
|
||
round_tripped = pl.Expr.deserialize(io.StringIO(json)) | ||
assert round_tripped.meta == expr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
|
||
import io | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
from hypothesis import given | ||
|
||
import polars as pl | ||
from polars.testing import assert_frame_equal | ||
from polars.testing.parametric import dataframes | ||
|
||
if TYPE_CHECKING: | ||
from pathlib import Path | ||
|
||
|
||
@given( | ||
lf=dataframes( | ||
lazy=True, | ||
excluded_dtypes=[ | ||
pl.Null, # Not implemented yet | ||
pl.Float32, # Bug, see: https://github.com/pola-rs/polars/issues/17211 | ||
pl.Float64, # Bug, see: https://github.com/pola-rs/polars/issues/17211 | ||
], | ||
) | ||
) | ||
def test_lf_serde_roundtrip(lf: pl.LazyFrame) -> None: | ||
serialized = lf.serialize() | ||
result = pl.LazyFrame.deserialize(io.StringIO(serialized)) | ||
assert_frame_equal(result, lf, categorical_as_str=True) | ||
|
||
|
||
@pytest.fixture() | ||
def lf() -> pl.LazyFrame: | ||
"""Sample LazyFrame for testing serialization/deserialization.""" | ||
return pl.LazyFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}).select("a").sum() | ||
|
||
|
||
def test_lf_serde(lf: pl.LazyFrame) -> None: | ||
serialized = lf.serialize() | ||
assert isinstance(serialized, str) | ||
result = pl.LazyFrame.deserialize(io.StringIO(serialized)) | ||
|
||
assert_frame_equal(result, lf) | ||
|
||
|
||
@pytest.mark.parametrize("buf", [io.BytesIO(), io.StringIO()]) | ||
def test_lf_serde_to_from_buffer(lf: pl.LazyFrame, buf: io.IOBase) -> None: | ||
lf.serialize(buf) | ||
buf.seek(0) | ||
result = pl.LazyFrame.deserialize(buf) | ||
assert_frame_equal(lf, result) | ||
|
||
|
||
@pytest.mark.write_disk() | ||
def test_lf_serde_to_from_file(lf: pl.LazyFrame, tmp_path: Path) -> None: | ||
tmp_path.mkdir(exist_ok=True) | ||
|
||
file_path = tmp_path / "small.json" | ||
lf.serialize(file_path) | ||
result = pl.LazyFrame.deserialize(file_path) | ||
|
||
assert_frame_equal(lf, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters