|
| 1 | +# pandas/tests/frame/test_versioning.py |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +def test_snapshot_and_restore_returns_dataframe(): |
| 7 | + df = pd.DataFrame({"x": [1, 2, 3]}) |
| 8 | + sid = df.snapshot("t1") |
| 9 | + assert sid in df.list_snapshots() |
| 10 | + df.loc[0, "x"] = 99 |
| 11 | + restored = df.restore(sid) |
| 12 | + assert list(restored["x"]) == [1, 2, 3] |
| 13 | + |
| 14 | + |
| 15 | +def test_restore_inplace_mutates_dataframe(): |
| 16 | + df = pd.DataFrame({"x": [1, 2, 3]}) |
| 17 | + sid = df.snapshot("t2") |
| 18 | + df.loc[1, "x"] = 999 |
| 19 | + df.restore(sid, inplace=True) |
| 20 | + assert list(df["x"]) == [1, 2, 3] |
| 21 | + |
| 22 | + |
| 23 | +def test_drop_and_clear_behaviour(): |
| 24 | + df = pd.DataFrame({"a": [1, 2]}) |
| 25 | + sid1 = df.snapshot("s1") |
| 26 | + sid2 = df.snapshot("s2") |
| 27 | + assert set(df.list_snapshots()) == {sid1, sid2} |
| 28 | + df.drop_snapshot(sid1) |
| 29 | + assert sid1 not in df.list_snapshots() |
| 30 | + df.clear_snapshots() |
| 31 | + assert df.list_snapshots() == [] |
| 32 | + |
| 33 | + |
| 34 | +def test_snapshot_on_empty_dataframe(): |
| 35 | + df = pd.DataFrame() |
| 36 | + sid = df.snapshot() |
| 37 | + df.loc[0, "a"] = 1 |
| 38 | + restored = df.restore(sid) |
| 39 | + assert restored.empty |
| 40 | + |
| 41 | + |
| 42 | +def test_copy_does_not_inherit_snapshots(): |
| 43 | + df = pd.DataFrame({"a": [1, 2, 3]}) |
| 44 | + sid = df.snapshot("orig") |
| 45 | + df2 = df.copy() |
| 46 | + # design decision: copies do not copy snapshots |
| 47 | + assert df2.list_snapshots() == [] |
| 48 | + assert sid in df.list_snapshots() |
| 49 | + |
| 50 | + |
| 51 | +def test_missing_snapshot_raises(): |
| 52 | + df = pd.DataFrame({"x": [1]}) |
| 53 | + with pytest.raises(KeyError): |
| 54 | + df.restore("no-such-snapshot") |
0 commit comments