-
Notifications
You must be signed in to change notification settings - Fork 0
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 #15 from gMatas/update
fix(serialization): Fix enable/disable defaults
- Loading branch information
Showing
6 changed files
with
215 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "ezserialization" | ||
version = "0.2.10" | ||
version = "0.3.0" | ||
description = "Simple, easy to use & transparent python objects serialization & deserialization." | ||
authors = ["Matas Gumbinas <[email protected]>"] | ||
repository = "https://github.com/gMatas/ezserialization" | ||
|
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
66 changes: 66 additions & 0 deletions
66
tests/ezserialization_tests/test_serializable_decorator.py
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,66 @@ | ||
import json | ||
from typing import Mapping, cast | ||
|
||
from ezserialization import ( | ||
Serializable, | ||
deserialize, | ||
serializable, | ||
) | ||
|
||
|
||
@serializable # <- valid for serialization | ||
@serializable(name="A") | ||
@serializable(name="XXX") | ||
class _CaseAUsingAutoName(Serializable): | ||
def __init__(self, value: str): | ||
self.value = value | ||
|
||
def to_raw_dict(self) -> dict: | ||
return {"value": self.value} | ||
|
||
def to_dict(self) -> Mapping: | ||
return self.to_raw_dict() | ||
|
||
@classmethod | ||
def from_dict(cls, src: Mapping): | ||
return cls(value=src["value"]) | ||
|
||
@classmethod | ||
def abs_qualname(cls) -> str: | ||
return f"{cls.__module__}.{cls.__qualname__}" | ||
|
||
|
||
@serializable(name="B") # <- valid for serialization | ||
@serializable(name="YYY") | ||
@serializable | ||
@serializable(name="ZZZ") | ||
class _CaseBUsingNameAlias(Serializable): | ||
def __init__(self, value: str): | ||
self.value = value | ||
|
||
def to_dict(self) -> Mapping: | ||
return {"value": self.value} | ||
|
||
@classmethod | ||
def from_dict(cls, src: Mapping): | ||
return cls(value=src["value"]) | ||
|
||
|
||
def test_serialization_typenames_order(): | ||
""" | ||
Expected behaviour: Only the top typename is used to serialize instances. | ||
On the other hand, for deserialization all typenames are valid. | ||
""" | ||
|
||
a = _CaseAUsingAutoName("a") | ||
data = a.to_dict() | ||
|
||
a.from_dict(data) | ||
|
||
assert data["_type_"] == _CaseAUsingAutoName.abs_qualname() | ||
assert a.value == cast(_CaseAUsingAutoName, deserialize(json.loads(json.dumps(data)))).value | ||
|
||
b = _CaseBUsingNameAlias("b") | ||
data = b.to_dict() | ||
assert data["_type_"] == "B" | ||
assert b.value == cast(_CaseBUsingNameAlias, deserialize(json.loads(json.dumps(data)))).value |
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
Oops, something went wrong.