Taking snapshots of polars data frames
Write a snapshot test for a data frame matrix
# test_dataframe.py
import polars as pl
import numpy as np
def test_dataframe(snapshot):
df = pl.DataFrame(np.eye(3), schema=["a", "b", "c"], orient="col")
snapshot.check(df, atol=1e-2, rtol=1e-2)
Run the test
The first execution of this test fails:
$ pytest -v test_dataframe.py
============================= test session starts ==============================
platform linux -- Python 3.12.12, pytest-9.0.3, pluggy-1.6.0 -- /home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/.venv/bin/python
cachedir: .pytest_cache
rootdir: /tmp/tmpa7e0icoa
plugins: regtest-2.5.0, cov-7.1.0
collecting ... collected 1 item
test_dataframe.py::test_dataframe FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_dataframe ________________________________
snapshot error(s) for test_dataframe.py::test_dataframe:
snapshot not recorded yet:
> test_dataframe.py +6
> snapshot.check(df, atol=1e-2, rtol=1e-2)
shape: (3, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 │
╞═════╪═════╪═════╡
│ 1.0 ┆ 0.0 ┆ 0.0 │
│ 0.0 ┆ 1.0 ┆ 0.0 │
│ 0.0 ┆ 0.0 ┆ 1.0 │
└─────┴─────┴─────┘
---------------------------- pytest-regtest report -----------------------------
total number of failed regression tests: 0
total number of failed snapshot tests : 1
=========================== short test summary info ============================
FAILED test_dataframe.py::test_dataframe
============================== 1 failed in 0.12s ===============================
Reset the snapshot
Let us record the snapshot:
$ pytest -v --regtest-reset test_dataframe.py
============================= test session starts ==============================
platform linux -- Python 3.12.12, pytest-9.0.3, pluggy-1.6.0 -- /home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/.venv/bin/python
cachedir: .pytest_cache
rootdir: /tmp/tmpa7e0icoa
plugins: regtest-2.5.0, cov-7.1.0
collecting ... collected 1 item
test_dataframe.py::test_dataframe RESET [100%]
---------------------------- pytest-regtest report -----------------------------
total number of failed regression tests: 0
total number of failed snapshot tests : 0
the following output files have been reset:
_regtest_outputs/test_dataframe.test_dataframe__0
============================== 1 passed in 0.01s ===============================
Now test test passes:
$ pytest -v test_dataframe.py
============================= test session starts ==============================
platform linux -- Python 3.12.12, pytest-9.0.3, pluggy-1.6.0 -- /home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/.venv/bin/python
cachedir: .pytest_cache
rootdir: /tmp/tmpa7e0icoa
plugins: regtest-2.5.0, cov-7.1.0
collecting ... collected 1 item
test_dataframe.py::test_dataframe PASSED [100%]
=============================== warnings summary ===============================
test_dataframe.py::test_dataframe
/home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/src/pytest_regtest/polars_handler.py:42: DeprecationWarning: the argument `rtol` for `assert_frame_equal` is deprecated. It was renamed to `rel_tol` in version 1.32.3.
assert_frame_equal(
test_dataframe.py::test_dataframe
/home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/src/pytest_regtest/polars_handler.py:42: DeprecationWarning: the argument `atol` for `assert_frame_equal` is deprecated. It was renamed to `abs_tol` in version 1.32.3.
assert_frame_equal(
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
---------------------------- pytest-regtest report -----------------------------
total number of failed regression tests: 0
total number of failed snapshot tests : 0
======================== 1 passed, 2 warnings in 0.01s =========================
Break the test
# test_dataframe.py
import polars as pl
import numpy as np
def test_dataframe(snapshot):
df = pl.DataFrame(np.eye(3), schema=["a", "b", "d"], orient="col")
snapshot.check(df, atol=1e-2, rtol=1e-2)
$ pytest -v test_dataframe.py
============================= test session starts ==============================
platform linux -- Python 3.12.12, pytest-9.0.3, pluggy-1.6.0 -- /home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/.venv/bin/python
cachedir: .pytest_cache
rootdir: /tmp/tmpa7e0icoa
plugins: regtest-2.5.0, cov-7.1.0
collecting ... collected 1 item
test_dataframe.py::test_dataframe FAILED [100%]
=================================== FAILURES ===================================
________________________________ test_dataframe ________________________________
snapshot error(s) for test_dataframe.py::test_dataframe:
snapshot mismatch:
> test_dataframe.py +6:
> snapshot.check(df, atol=1e-2, rtol=1e-2)
--- current
+++ expected
@@ -3,4 +3,4 @@
--- ------ -------------- -----
0 a 3 non-null Float64
1 b 3 non-null Float64
- 2 d 3 non-null Float64
+ 2 c 3 non-null Float64
--- current
+++ expected
@@ -1,6 +1,6 @@
shape: (3, 3)
┌─────┬─────┬─────┐
-│ a ┆ b ┆ d │
+│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 │
╞═════╪═════╪═════╡
=============================== warnings summary ===============================
test_dataframe.py::test_dataframe
/home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/src/pytest_regtest/polars_handler.py:42: DeprecationWarning: the argument `rtol` for `assert_frame_equal` is deprecated. It was renamed to `rel_tol` in version 1.32.3.
assert_frame_equal(
test_dataframe.py::test_dataframe
/home/docs/checkouts/readthedocs.org/user_builds/pytest-regtest/checkouts/latest/src/pytest_regtest/polars_handler.py:42: DeprecationWarning: the argument `atol` for `assert_frame_equal` is deprecated. It was renamed to `abs_tol` in version 1.32.3.
assert_frame_equal(
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
---------------------------- pytest-regtest report -----------------------------
total number of failed regression tests: 0
total number of failed snapshot tests : 1
=========================== short test summary info ============================
FAILED test_dataframe.py::test_dataframe
======================== 1 failed, 2 warnings in 0.12s =========================