File: test_preserve_values.py

package info (click to toggle)
python-inline-snapshot 0.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,116 kB
  • sloc: python: 6,888; makefile: 34; sh: 28
file content (248 lines) | stat: -rw-r--r-- 6,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import itertools
import sys

import pytest

from inline_snapshot import snapshot


def test_fix_list_fix(check_update):
    assert check_update(
        """assert [1,2]==snapshot([0+1,3])""", reported_flags="update,fix", flags="fix"
    ) == snapshot("assert [1,2]==snapshot([0+1,2])")


def test_fix_list_insert(check_update):
    assert check_update(
        """assert [1,2,3,4,5,6]==snapshot([0+1,3])""",
        reported_flags="update,fix",
        flags="fix",
    ) == snapshot("assert [1,2,3,4,5,6]==snapshot([0+1, 2, 3, 4, 5, 6])")


def test_fix_list_delete(check_update):
    assert check_update(
        """assert [1,5]==snapshot([0+1,2,3,4,5])""",
        reported_flags="update,fix",
        flags="fix",
    ) == snapshot("assert [1,5]==snapshot([0+1, 5])")


def test_fix_tuple_delete(check_update):
    assert check_update(
        """assert (1,5)==snapshot((0+1,2,3,4,5))""",
        reported_flags="update,fix",
        flags="fix",
    ) == snapshot("assert (1,5)==snapshot((0+1, 5))")


def test_fix_dict_change(check_update):
    assert check_update(
        """assert {1:1, 2:2}==snapshot({1:0+1, 2:3})""",
        reported_flags="update,fix",
        flags="fix",
    ) == snapshot("assert {1:1, 2:2}==snapshot({1:0+1, 2:2})")


def test_fix_dict_remove(check_update):
    assert check_update(
        """assert {1:1}==snapshot({0:0, 1:0+1, 2:2})""",
        reported_flags="update,fix",
        flags="fix",
    ) == snapshot("assert {1:1}==snapshot({1:0+1})")

    assert check_update(
        """assert {}==snapshot({0:0})""",
        reported_flags="fix",
        flags="fix",
    ) == snapshot("assert {}==snapshot({})")


def test_fix_dict_insert(check_update):
    assert check_update(
        """assert {0:"before",1:1,2:"after"}==snapshot({1:0+1})""",
        reported_flags="update,fix",
        flags="fix",
    ) == snapshot(
        'assert {0:"before",1:1,2:"after"}==snapshot({0: "before", 1:0+1, 2: "after"})'
    )


def test_fix_dict_with_non_literal_keys(check_update):
    assert check_update(
        """assert {1+2:"3"}==snapshot({1+2:"5"})""",
        reported_flags="fix",
        flags="fix",
    ) == snapshot('assert {1+2:"3"}==snapshot({1+2:"3"})')


@pytest.mark.skipif(
    sys.version_info < (3, 8), reason="dirty equals has dropped the 3.7 support"
)
def test_no_update_for_dirty_equals(check_update):
    assert (
        check_update(
            """\
from dirty_equals import IsInt
assert {5:5,2:2}==snapshot({5:IsInt(),2:1+1})
""",
            reported_flags="update",
            flags="update",
        )
        == snapshot(
            """\
from dirty_equals import IsInt
assert {5:5,2:2}==snapshot({5:IsInt(),2:2})
"""
        )
    )


# @pytest.mark.skipif(not hasattr(ast, "unparse"), reason="ast.unparse not available")
def test_preserve_case_from_original_mr(check_update):
    assert (
        check_update(
            """\
left = {
    "a": 1,
    "b": {
        "c": 2,
        "d": [
            3,
            4,
            5,
        ],
    },
    "e": (
        {
            "f": 6,
            "g": 7,
        },
    ),
}
assert left == snapshot(
    {
        "a": 10,
        "b": {
            "c": 2 * 1 + 0,
            "d": [
                int(3),
                40,
                5,
            ],
            "h": 8,
        },
        "e": (
            {
                "f": 3 + 3,
            },
            9,
        ),
    }
)
""",
            reported_flags="update,fix",
            flags="fix",
        )
        == snapshot(
            """\
left = {
    "a": 1,
    "b": {
        "c": 2,
        "d": [
            3,
            4,
            5,
        ],
    },
    "e": (
        {
            "f": 6,
            "g": 7,
        },
    ),
}
assert left == snapshot(
    {
        "a": 1,
        "b": {
            "c": 2 * 1 + 0,
            "d": [
                int(3),
                4,
                5,
            ]},
        "e": ({"f": 6, "g": 7},),
    }
)
"""
        )
    )


stuff = [
    (["5"], [], "delete", {"fix"}),
    ([], ["8"], "insert", {"fix"}),
    (["2+2"], ["4"], "update", {"update"}),
    (["3"], ["3"], "no_change", set()),
]


def test_generic(source, subtests):
    for braces in ("[]", "()", "{}"):
        for value_specs in itertools.product(stuff, repeat=3):
            flags = set().union(*[e[3] for e in value_specs])
            all_flags = {
                frozenset(x) - {""}
                for x in itertools.combinations_with_replacement(
                    flags | {""}, len(flags)
                )
            }

            def build(value_lists):
                value_lists = list(value_lists)

                if braces == "{}":
                    values = [
                        f"{i}: {value_list[0]}"
                        for i, value_list in enumerate(value_lists)
                        if value_list
                    ]
                else:
                    values = [x for value_list in value_lists for x in value_list]

                code = ", ".join(values)

                comma = ""
                if len(values) == 1 and braces == "()":
                    comma = ","

                return f"{braces[0]}{code}{comma}{braces[1]}"

            c1 = build(spec[0] for spec in value_specs)
            c2 = build(spec[1] for spec in value_specs)
            code = f"assert {c2}==snapshot({c1})"

            named_flags = ", ".join(flags)

            with subtests.test(f"{c1} -> {c2} <{named_flags}>"):
                s1 = source(code)
                print("source:", code)

                assert set(s1.flags) == flags

                assert ("fix" in flags) == s1.error

                for f in all_flags:
                    c3 = build(
                        [(spec[1] if spec[3] & f else spec[0]) for spec in value_specs]
                    )
                    new_code = f"assert {c2}==snapshot({c3})"

                    print(f"{set(f)}:")
                    print("  ", code)
                    print("  ", new_code)
                    s2 = s1.run(*f)
                    assert s2.source == new_code
                    # assert s2.flags== flags-f