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+ class TestSnapshotIntegration :
25+ @pytest .mark .skip_snapshot_verify (paths = ["$..id" ])
26+ def test_skip_id_field_passes (self , snapshot ):
27+ snapshot .recorded_state = {"user" : {"name" : "John" , "id" : "old" }}
28+ snapshot .match ("user" , {"name" : "John" , "id" : "new" })
29+
30+ # HACK(gregfurman): xfail(strict=True) means we expect the test to fail -- where the underlying test failing
31+ # results in an expected XFAIL, skipping the test. Otherwise, a PASS should trigger a true FAIL.
32+ @pytest .mark .xfail (strict = True , reason = "Should fail because name differs, only ID is skipped" )
33+ @pytest .mark .skip_snapshot_verify (paths = ["$..id" ])
34+ def test_skip_id_but_name_differs_fails (self , snapshot ):
35+ snapshot .recorded_state = {"user" : {"name" : "John" , "id" : "old" }}
36+ snapshot .match ("user" , {"name" : "Jane" , "id" : "new" })
37+
38+ @pytest .mark .xfail (strict = True , reason = "Should fail because name differs, only ID is skipped" )
39+ @pytest .mark .skip_snapshot_verify (["$..id" ])
40+ def test_skip_id_field_passes_args (self , snapshot ):
41+ snapshot .recorded_state = {"user" : {"name" : "John" , "id" : "old" }}
42+ snapshot .match ("user" , {"name" : "Jane" , "id" : "new" })
43+
44+ @pytest .mark .xfail (strict = True , reason = "Should fail because no fields are skipped" )
45+ def test_no_skip_marker_fails (self , snapshot ):
46+ snapshot .recorded_state = {"user" : {"name" : "John" , "id" : "old" }}
47+ snapshot .match ("user" , {"name" : "John" , "id" : "new" })
48+
49+ @pytest .mark .skip_snapshot_verify (paths = ["$..id" , "$..timestamp" ])
50+ def test_skip_multiple_fields_passes (self , snapshot ):
51+ snapshot .recorded_state = {"event" : {"type" : "login" , "id" : "123" , "timestamp" : "old" }}
52+ snapshot .match ("event" , {"type" : "login" , "id" : "456" , "timestamp" : "new" })
53+
54+ @pytest .mark .skip_snapshot_verify (condition = lambda : True )
55+ def test_condition_true_skips_all_verification (self , snapshot ):
56+ snapshot .recorded_state = {"data" : "old" }
57+ snapshot .match ("data" , "completely_different" )
58+
59+ @pytest .mark .skip_snapshot_verify (condition = lambda : False , paths = ["$..id" ])
60+ def test_condition_false_ignores_paths (self , snapshot ):
61+ snapshot .recorded_state = {"user" : {"name" : "John" , "id" : "123" }}
62+ snapshot .match ("user" , {"name" : "John" , "id" : "123" })
63+
64+ @pytest .mark .skip_snapshot_verify (["$..id" ], lambda : True )
65+ def test_condition_with_args_skips_all (self , snapshot ):
66+ snapshot .recorded_state = {"data" : {"id" : "old" }}
67+ snapshot .match ("data" , {"id" : "new" })
68+
69+ @pytest .mark .xfail (strict = True , reason = "Should fail because condition is False" )
70+ @pytest .mark .skip_snapshot_verify (["$..id" ], lambda : False )
71+ def test_condition_false_with_args_fails (self , snapshot ):
72+ snapshot .recorded_state = {"user" : {"name" : "John" , "id" : "old" }}
73+ snapshot .match ("user" , {"name" : "John" , "id" : "new" })
0 commit comments