File: test_custom_object_repr.py

package info (click to toggle)
python-syrupy 4.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,368 kB
  • sloc: python: 5,978; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 1,075 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
from syrupy.extensions.amber.serializer import AmberDataSerializer
from syrupy.filters import props
from syrupy.matchers import path_type


class MyCustomClass:
    prop1 = 1
    prop2 = "a"
    prop3 = {1, 2, 3}


def test_snapshot_custom_class(snapshot):
    assert MyCustomClass() == snapshot


class MyCustomReprClass(MyCustomClass):
    def __repr__(self):
        state = "\n".join(
            f"  {a}={repr(getattr(self, a))},"
            for a in sorted(dir(self))
            if not a.startswith("_")
        )
        return f"{self.__class__.__name__}(\n{state}\n)"


def test_snapshot_custom_repr_class(snapshot):
    assert MyCustomReprClass() == snapshot


def test_snapshot_object_as_named_tuple_class(snapshot):
    """
    Show helper `object_as_named_tuple` to revert representation to amber standard
    """
    assert MyCustomReprClass() == snapshot(
        exclude=props("prop1"),
        matcher=path_type(
            types=(MyCustomReprClass,),
            replacer=lambda data, _: AmberDataSerializer.object_as_named_tuple(data),
        ),
    )