-
Notifications
You must be signed in to change notification settings - Fork 6
/
decodable_test.py
57 lines (43 loc) · 1.32 KB
/
decodable_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
from typing import List, Optional
from chili import Decoder, Mapper, decodable
def test_decodable_with_default_values() -> None:
# given
@decodable
class Book:
name: str
author: str
isbn: Optional[str] = "1234567890"
tags: Optional[List[str]] = []
book_data = {"name": "The Hobbit", "author": "J.R.R. Tolkien"}
decoder = Decoder[Book]()
# when
result_1 = decoder.decode(book_data)
result_1.tags.append("Fantasy")
# then
assert isinstance(result_1, Book)
assert result_1.isbn == "1234567890"
assert result_1.tags == ["Fantasy"]
def test_can_decode_with_mapper() -> None:
# given
mapper = Mapper({"_name": "name", "_author": "author"}, preserve_keys=True)
@decodable(mapper=mapper)
class Book:
_name: str
_author: str
isbn: str
tags: List[str] = []
book_data = {
"name": "The Hobbit",
"author": "J.R.R. Tolkien",
"isbn": "1234567890",
"tags": ["Fantasy", "Adventure"],
}
decoder = Decoder[Book]()
# when
a_book = decoder.decode(book_data)
# then
assert isinstance(a_book, Book)
assert a_book._name == "The Hobbit"
assert a_book._author == "J.R.R. Tolkien"
assert a_book.isbn == "1234567890"
assert a_book.tags == ["Fantasy", "Adventure"]