-
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.
Merge pull request #27 from kodemore/fix-serializer
Allow Serializer to support non decorated types
- Loading branch information
Showing
3 changed files
with
64 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from chili import Serializer, serializable | ||
|
||
|
||
def test_can_instantiate() -> None: | ||
# given | ||
@serializable | ||
class Example: | ||
... | ||
|
||
# when | ||
instance = Serializer[Example]() | ||
|
||
# then | ||
assert isinstance(instance, Serializer) | ||
assert instance.__generic__ == Example | ||
|
||
|
||
def test_can_encode_non_serializable_type() -> None: | ||
# given | ||
class Example: | ||
name: str | ||
age: int | ||
|
||
def __init__(self, name: str, age: int): | ||
self.name = name | ||
self.age = age | ||
|
||
# when | ||
serializer = Serializer[Example]() | ||
value = serializer.encode(Example("bob", 33)) | ||
|
||
# then | ||
assert value == { | ||
"name": "bob", | ||
"age": 33, | ||
} | ||
|
||
|
||
def test_can_decode_non_serializable_type() -> None: | ||
# given | ||
class Example: | ||
name: str | ||
age: int | ||
|
||
def __init__(self, name: str, age: int): | ||
self.name = name | ||
self.age = age | ||
|
||
# when | ||
serializer = Serializer[Example]() | ||
value = serializer.decode( | ||
{ | ||
"name": "bob", | ||
"age": 33, | ||
} | ||
) | ||
|
||
# then | ||
assert isinstance(value, Example) | ||
assert value.name == "bob" | ||
assert value.age == 33 |