|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from localstack_snapshot.snapshots import SnapshotSession |
| 7 | + |
| 8 | +pytest_plugins = [ |
| 9 | + "localstack_snapshot.pytest.snapshot", |
| 10 | +] |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def snapshot(): |
| 15 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 16 | + session = SnapshotSession( |
| 17 | + scope_key="test", |
| 18 | + verify=True, |
| 19 | + base_file_path=os.path.join(temp_dir, "test"), |
| 20 | + update=False, |
| 21 | + ) |
| 22 | + yield session |
| 23 | + |
| 24 | + |
| 25 | +class TestSnapshotIntegration: |
| 26 | + @pytest.mark.skip_snapshot_verify(paths=["$..id"]) |
| 27 | + def test_skip_id_field_passes(self, snapshot): |
| 28 | + snapshot.recorded_state = {"user": {"name": "John", "id": "old"}} |
| 29 | + snapshot.match("user", {"name": "John", "id": "new"}) |
| 30 | + |
| 31 | + # HACK(gregfurman): xfail(strict=True) means we expect the test to fail -- where the underlying test failing |
| 32 | + # results in an expected XFAIL, skipping the test. Otherwise, a PASS should trigger a true FAIL. |
| 33 | + @pytest.mark.xfail(strict=True, reason="Should fail because name differs, only ID is skipped") |
| 34 | + @pytest.mark.skip_snapshot_verify(paths=["$..id"]) |
| 35 | + def test_skip_id_but_name_differs_fails(self, snapshot): |
| 36 | + snapshot.recorded_state = {"user": {"name": "John", "id": "old"}} |
| 37 | + snapshot.match("user", {"name": "Jane", "id": "new"}) |
| 38 | + |
| 39 | + @pytest.mark.xfail(strict=True, reason="Should fail because name differs, only ID is skipped") |
| 40 | + @pytest.mark.skip_snapshot_verify(["$..id"]) |
| 41 | + def test_skip_id_field_passes_args(self, snapshot): |
| 42 | + snapshot.recorded_state = {"user": {"name": "John", "id": "old"}} |
| 43 | + snapshot.match("user", {"name": "Jane", "id": "new"}) |
| 44 | + |
| 45 | + @pytest.mark.xfail(strict=True, reason="Should fail because no fields are skipped") |
| 46 | + def test_no_skip_marker_fails(self, snapshot): |
| 47 | + snapshot.recorded_state = {"user": {"name": "John", "id": "old"}} |
| 48 | + snapshot.match("user", {"name": "John", "id": "new"}) |
| 49 | + |
| 50 | + @pytest.mark.skip_snapshot_verify(paths=["$..id", "$..timestamp"]) |
| 51 | + def test_skip_multiple_fields_passes(self, snapshot): |
| 52 | + snapshot.recorded_state = {"event": {"type": "login", "id": "123", "timestamp": "old"}} |
| 53 | + snapshot.match("event", {"type": "login", "id": "456", "timestamp": "new"}) |
| 54 | + |
| 55 | + @pytest.mark.skip_snapshot_verify(condition=lambda: True) |
| 56 | + def test_condition_true_skips_all_verification(self, snapshot): |
| 57 | + snapshot.recorded_state = {"data": "old"} |
| 58 | + snapshot.match("data", "completely_different") |
| 59 | + |
| 60 | + @pytest.mark.skip_snapshot_verify(condition=lambda: False, paths=["$..id"]) |
| 61 | + def test_condition_false_ignores_paths(self, snapshot): |
| 62 | + snapshot.recorded_state = {"user": {"name": "John", "id": "123"}} |
| 63 | + snapshot.match("user", {"name": "John", "id": "123"}) |
| 64 | + |
| 65 | + @pytest.mark.skip_snapshot_verify(["$..id"], lambda: True) |
| 66 | + def test_condition_with_args_skips_all(self, snapshot): |
| 67 | + snapshot.recorded_state = {"data": {"id": "old"}} |
| 68 | + snapshot.match("data", {"id": "new"}) |
| 69 | + |
| 70 | + @pytest.mark.xfail(strict=True, reason="Should fail because condition is False") |
| 71 | + @pytest.mark.skip_snapshot_verify(["$..id"], lambda: False) |
| 72 | + def test_condition_false_with_args_fails(self, snapshot): |
| 73 | + snapshot.recorded_state = {"user": {"name": "John", "id": "old"}} |
| 74 | + snapshot.match("user", {"name": "John", "id": "new"}) |
0 commit comments