-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataclasses_test.py
177 lines (133 loc) · 3.82 KB
/
dataclasses_test.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from dataclasses import dataclass, field
from typing import Generic, List, Optional, TypeVar
import pytest
from chili import Decoder, Encoder
from chili.decoder import ClassDecoder, decode
from chili.error import DecoderError
def test_can_decode_dataclass() -> None:
# given
@dataclass
class Pet:
name: str
age: int
favourite_food: List[str]
decoder = Decoder[Pet]()
data = {"name": "Fido", "age": 3, "favourite_food": [7, "bones"]}
# when
result = decoder.decode(data)
# then
assert isinstance(result, Pet)
assert result.name == "Fido"
assert result.age == 3
assert result.favourite_food == ["7", "bones"]
def test_can_encode_dataclass() -> None:
# given
@dataclass
class Pet:
name: str
age: int
favourite_food: List[str]
decoder = Encoder[Pet]()
pet = Pet("Fido", 3, ["7", "bones"])
# when
result = decoder.encode(pet)
# then
assert isinstance(result, dict)
assert result == {"name": "Fido", "age": 3, "favourite_food": ["7", "bones"]}
def test_can_encode_nested_dataclasses() -> None:
# given
@dataclass
class Tag:
name: str
@dataclass
class Author:
first_name: str
last_name: str
@dataclass
class Book:
name: str
author: Author
tags: List[Tag]
book = Book("The Hobbit", Author("J.R.R.", "Tolkien"), [Tag("Fantasy"), Tag("Adventure")])
encoder = Encoder[Book]()
# when
result = encoder.encode(book)
# then
assert isinstance(result, dict)
assert result == {
"name": "The Hobbit",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
}
def test_can_decode_nested_dataclasses() -> None:
# given
@dataclass
class Tag:
name: str
@dataclass
class Author:
first_name: str
last_name: str
@dataclass
class Book:
name: str
author: Author
tags: List[Tag]
book_data = {
"name": "The Hobbit",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
}
decoder = Decoder[Book]()
# when
result = decoder.decode(book_data)
# then
assert isinstance(result, Book)
assert isinstance(result.author, Author)
assert all(isinstance(tag, Tag) for tag in result.tags)
assert result.tags[0].name == "Fantasy"
assert result.tags[1].name == "Adventure"
def test_can_decode_with_default_values() -> None:
# given
@dataclass
class Book:
name: str
author: str
tags: Optional[List[str]] = field(default_factory=list)
book_data = {"name": "The Hobbit", "author": "J.R.R. Tolkien"}
decoder = Decoder[Book]()
# when
result = decoder.decode(book_data)
# then
assert isinstance(result.tags, list)
def test_can_decode_with_default_values_explicit_dataclass_type() -> None:
# given
@dataclass
class Book:
name: str
author: str
tags: List[str] = field(default_factory=list)
book_data = {"name": "The Hobbit", "author": "J.R.R. Tolkien"}
decoder = ClassDecoder(Book)
# when
result: Book = decoder.decode(book_data)
# then
assert isinstance(result.tags, list)
def test_fail_decode_non_parametrized_generic_class() -> None:
# given
T = TypeVar("T")
@dataclass
class Book(Generic[T]):
name: str
author: str
tags: List[T] = field(default_factory=list)
class TaggedBook(Book[str]):
pass
tagged_book_data = {
"name": "The Hobbit",
"author": "J.R.R. Tolkien",
"tags": ["Fantasy"],
}
# when
with pytest.raises(DecoderError):
decode(tagged_book_data, TaggedBook)