File: test_single_file.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 (71 lines) | stat: -rw-r--r-- 1,992 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
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from syrupy.data import (
    Snapshot,
    SnapshotCollection,
)
from syrupy.extensions.single_file import (
    SingleFileSnapshotExtension,
    WriteMode,
)

if TYPE_CHECKING:
    from syrupy.assertion import SnapshotAssertion


@pytest.fixture
def snapshot_single(snapshot):
    return snapshot.use_extension(SingleFileSnapshotExtension)


class SingleFileUTF8SnapshotExtension(SingleFileSnapshotExtension):
    _write_mode = WriteMode.TEXT


@pytest.fixture
def snapshot_utf8(snapshot):
    return snapshot.use_extension(SingleFileUTF8SnapshotExtension)


def test_does_not_write_non_binary(testdir, snapshot_single: "SnapshotAssertion"):
    snapshot_collection = SnapshotCollection(
        location=str(Path(testdir.tmpdir).joinpath("snapshot_collection.raw")),
    )
    snapshot_collection.add(Snapshot(name="snapshot_name", data="non binary data"))
    with pytest.raises(TypeError, match="Expected 'bytes', got 'str'"):
        snapshot_single.extension._write_snapshot_collection(
            snapshot_collection=snapshot_collection
        )
    assert not Path(snapshot_collection.location).exists()


class TestClass:
    def test_class_method_name(self, snapshot_single):
        assert snapshot_single == b"this is in a test class"

    @pytest.mark.parametrize("content", [b"x", b"y", b"z"])
    def test_class_method_parametrized(self, snapshot_single, content):
        assert snapshot_single == content


def test_underscore(snapshot_single):
    assert snapshot_single == b"apple"


def test_____underscore(snapshot_single):
    assert snapshot_single == b"orange"


@pytest.mark.parametrize(
    "content", [b"", b"_", b"a?", b"space space", b".123~!@#$%^&*()/[]{}|"]
)
def test_special_characters(snapshot_single, content):
    assert snapshot_single == content


@pytest.mark.parametrize("content", ["greek ῴ"])
def test_unicode(snapshot_utf8, content):
    assert snapshot_utf8 == content