File: test_custom_defaults.py

package info (click to toggle)
python-syrupy 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,396 kB
  • sloc: python: 5,982; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,533 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
"""Example: Custom snapshot defaults

Here we modify snapshot defaults globally, instead of per assert.
This gives the ability to modify the snapshot functionality to your hearts content
and then simply re-use them, without having to pass those defaults to every assert.
Especially useful if there's a lot of tests that need to modify the default behaviour.
"""

import pytest

from syrupy.extensions.json import JSONSnapshotExtension
from syrupy.filters import paths
from syrupy.matchers import path_type


@pytest.fixture
def snapshot_matcher(snapshot):
    return snapshot.with_defaults(matcher=path_type(mapping={"my-field": (str, int)}))


@pytest.fixture
def snapshot_exclude(snapshot_matcher):
    return snapshot_matcher.with_defaults(exclude=paths("excluded"))


@pytest.fixture
def snapshot_json(snapshot_exclude):
    return snapshot_exclude.with_defaults(extension_class=JSONSnapshotExtension)


def test_asserting_multiple_times(snapshot_matcher):
    assert {"my-field": "my-string", "excluded": "value"} == snapshot_matcher
    assert {"my-field": 12345, "excluded": "value"} == snapshot_matcher


def test_asserting_multiple_times_chained(snapshot_exclude):
    assert {"my-field": "my-string", "excluded": "value"} == snapshot_exclude
    assert {"my-field": 12345, "excluded": "value"} == snapshot_exclude


def test_asserting_multiple_times_chained_json(snapshot_json):
    assert {"my-field": "my-string", "excluded": "value"} == snapshot_json
    assert {"my-field": 12345, "excluded": "value"} == snapshot_json