-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mapping availability in decorators
- Loading branch information
1 parent
4782b62
commit 22637df
Showing
11 changed files
with
228 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import List | ||
|
||
from chili import Mapper, Serializer, serializable | ||
|
||
|
||
def test_serializable_with_mappers() -> None: | ||
# given | ||
encode_mapper = Mapper({"_name": "name", "_author": "author"}, preserve_keys=True) | ||
decode_mapper = Mapper({"name": "_name", "author": "_author"}, preserve_keys=True) | ||
|
||
@serializable(in_mapper=decode_mapper, out_mapper=encode_mapper) | ||
class Book: | ||
name: str | ||
author: str | ||
isbn: str | ||
tags: List[str] = [] | ||
|
||
def __init__(self, name: str, author: str, isbn: str, tags: List[str]): | ||
self.name = name | ||
self.author = author | ||
self.isbn = isbn | ||
self.tags = tags | ||
|
||
a_book = Book("The Hobbit", "J.R.R. Tolkien", "1234567890", ["Fantasy", "Adventure"]) | ||
a_book_snapshot = { | ||
"_name": "The Hobbit", | ||
"_author": "J.R.R. Tolkien", | ||
"isbn": "1234567890", | ||
"tags": ["Fantasy", "Adventure"], | ||
} | ||
serializer = Serializer[Book]() | ||
|
||
# when | ||
encoded_book = serializer.encode(a_book) | ||
decoded_book = serializer.decode(a_book_snapshot) | ||
|
||
# then | ||
assert encoded_book == a_book_snapshot | ||
assert decoded_book.name == "The Hobbit" | ||
assert decoded_book.author == "J.R.R. Tolkien" | ||
assert decoded_book.isbn == "1234567890" | ||
assert decoded_book.tags == ["Fantasy", "Adventure"] |