File: test_get_snapshot_value.py

package info (click to toggle)
python-inline-snapshot 0.29.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,400 kB
  • sloc: python: 8,877; makefile: 37; sh: 30
file content (89 lines) | stat: -rw-r--r-- 2,068 bytes parent folder | download | duplicates (2)
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
import pytest

from inline_snapshot import Is
from inline_snapshot import external
from inline_snapshot import get_snapshot_value
from inline_snapshot import outsource
from inline_snapshot import snapshot
from inline_snapshot._global_state import state
from inline_snapshot.testing._example import snapshot_env


def inspect(value):
    if isinstance(value, (int, str, bytes)):
        return repr(value)

    if isinstance(value, list):
        return "[" + ", ".join(map(inspect, value)) + "]"

    args = ", ".join(f"{k}={v}" for k, v in value.__dict__.items())
    return f"{type(value).__name__}({args})"


@pytest.fixture(params=[True, False])
def try_snapshot_disable(request):
    if request.param != state().active:
        storages = state().all_storages
        with snapshot_env() as env:
            env.active = request.param
            env.all_storages = storages
            yield
    else:
        yield


def test_snapshot(try_snapshot_disable):
    s = snapshot(5)

    assert s == 5

    assert inspect(get_snapshot_value(s)) == snapshot("5")


def test_snapshot_external(try_snapshot_disable):
    s = snapshot([0, external("hash:ef2d127de37b*.json")])

    assert s == [0, 5]

    assert inspect(get_snapshot_value(s)) == snapshot("[0, 5]")


def test_external(try_snapshot_disable):
    s = external("hash:ef2d127de37b*.json")

    assert s == 5

    assert inspect(get_snapshot_value(s)) == snapshot("5")


def test_snapshot_is(try_snapshot_disable):
    s = snapshot([0, Is(5)])

    assert s == [0, 5]

    assert inspect(get_snapshot_value(s)) == snapshot("[0, 5]")


def test_snapshot_snapshot(try_snapshot_disable):
    s = snapshot([0, snapshot(5)])

    assert s == [0, 5]

    assert inspect(get_snapshot_value(s)) == snapshot("[0, 5]")


from dataclasses import dataclass


@dataclass
class A:
    a: int
    b: int


def test_dataclass(try_snapshot_disable):
    s = snapshot(A(a=external("hash:ef2d127de37b*.json"), b=2))

    assert s == A(a=outsource(5), b=2)

    assert inspect(get_snapshot_value(s)) == snapshot("A(a=5, b=2)")