File: test_literal.py

package info (click to toggle)
python-mashumaro 3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,400 kB
  • sloc: python: 19,890; sh: 16; makefile: 5
file content (129 lines) | stat: -rw-r--r-- 3,932 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
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
from dataclasses import dataclass

import pytest
from typing_extensions import Literal

from mashumaro import DataClassDictMixin
from mashumaro.config import ADD_DIALECT_SUPPORT, BaseConfig
from mashumaro.dialect import Dialect
from mashumaro.exceptions import InvalidFieldValue
from mashumaro.helper import pass_through
from tests.entities import MyEnum


def test_literal_with_str():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal["1", "2", "3"]

    assert DataClass.from_dict({"x": "1"}) == DataClass("1")
    assert DataClass.from_dict({"x": "2"}) == DataClass("2")

    assert DataClass("1").to_dict() == {"x": "1"}
    assert DataClass("2").to_dict() == {"x": "2"}

    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": 1})
    with pytest.raises(InvalidFieldValue):
        DataClass(1).to_dict()


def test_literal_with_int():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[1, 2]

    assert DataClass.from_dict({"x": 1}) == DataClass(1)
    assert DataClass.from_dict({"x": 2}) == DataClass(2)

    assert DataClass(1).to_dict() == {"x": 1}
    assert DataClass(2).to_dict() == {"x": 2}

    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "1"})
    with pytest.raises(InvalidFieldValue):
        DataClass("1").to_dict()


def test_literal_with_bool():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[True, False]

    assert DataClass.from_dict({"x": True}) == DataClass(True)
    assert DataClass.from_dict({"x": False}) == DataClass(False)

    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "a"})
    with pytest.raises(InvalidFieldValue):
        DataClass("a").to_dict()


def test_literal_with_none():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[None]

    assert DataClass.from_dict({"x": None}) == DataClass(None)
    assert DataClass(None).to_dict() == {"x": None}
    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "1"})
    with pytest.raises(InvalidFieldValue):
        DataClass("1").to_dict()


def test_literal_with_bytes():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[b"\x00"]

    assert DataClass.from_dict({"x": "AA==\n"}) == DataClass(b"\x00")
    assert DataClass(b"\x00").to_dict() == {"x": "AA==\n"}
    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "\x00"})
    with pytest.raises(InvalidFieldValue):
        DataClass("AA==\n").to_dict()


def test_literal_with_bytes_overridden():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[b"\x00"]

        class Config(BaseConfig):
            serialization_strategy = {bytes: pass_through}

    assert DataClass.from_dict({"x": b"\x00"}) == DataClass(b"\x00")
    assert DataClass(b"\x00").to_dict() == {"x": b"\x00"}
    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "AA==\n"})


def test_literal_with_enum():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[MyEnum.a]

    assert DataClass.from_dict({"x": "letter a"}) == DataClass(MyEnum.a)
    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "letter b"})
    with pytest.raises(InvalidFieldValue):
        DataClass(MyEnum.b).to_dict()


def test_literal_with_dialect():
    @dataclass
    class DataClass(DataClassDictMixin):
        x: Literal[b"\x00"]

        class Config(BaseConfig):
            code_generation_options = [ADD_DIALECT_SUPPORT]

    class MyDialect(Dialect):
        serialization_strategy = {bytes: pass_through}

    instance = DataClass(b"\x00")
    assert DataClass.from_dict({"x": b"\x00"}, dialect=MyDialect) == instance
    assert instance.to_dict(dialect=MyDialect) == {"x": b"\x00"}
    with pytest.raises(InvalidFieldValue):
        DataClass.from_dict({"x": "AA==\n"}, dialect=MyDialect)