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
|
from serializable import Serializable
from nose.tools import eq_
class TestClassWithKeywordAliases(Serializable):
_SERIALIZABLE_KEYWORD_ALIASES = {"old_x": "x", "old_gone": None}
def __init__(self, x):
self.x = x
class DerivedClassWithMoreKeywordAliases(TestClassWithKeywordAliases):
_SERIALIZABLE_KEYWORD_ALIASES = {"old_y": "y"}
def __init__(self, x, y):
self.x = x
self.y = y
class DerivedClassWithoutMoreKeywordAliases(TestClassWithKeywordAliases):
def __init__(self, x, y):
self.x = x
self.y = y
def test_normal_keywords():
# testing that nothing got screwed in the normal logic of object
# serialization/deserialization by the addition of keyword aliases
obj1 = TestClassWithKeywordAliases.from_json(
TestClassWithKeywordAliases(x=1).to_json())
eq_(obj1.x, 1)
obj2 = DerivedClassWithMoreKeywordAliases.from_json(
DerivedClassWithMoreKeywordAliases(x=10, y=20).to_json())
eq_(obj2.x, 10)
eq_(obj2.y, 20)
obj3 = DerivedClassWithoutMoreKeywordAliases.from_json(
DerivedClassWithoutMoreKeywordAliases(x=100, y=200).to_json())
eq_(obj3.x, 100)
eq_(obj3.y, 200)
def test_removed_keyword():
d = TestClassWithKeywordAliases(x=1).to_dict()
d["old_gone"] = "WILL BE ERASED"
obj = TestClassWithKeywordAliases.from_dict(d)
eq_(obj.x, 1)
assert not hasattr(obj, "old_gone")
def test_removed_keyword_inheritance():
d = DerivedClassWithoutMoreKeywordAliases(x=1, y=2).to_dict()
d["old_gone"] = "WILL BE REMOVED"
obj = DerivedClassWithoutMoreKeywordAliases.from_dict(d)
eq_(obj.x, 1)
eq_(obj.y, 2)
assert not hasattr(obj, "old_gone")
def test_renamed_keyword():
d = TestClassWithKeywordAliases(x=1).to_dict()
x = d.pop("x")
d["old_x"] = x
obj = TestClassWithKeywordAliases.from_dict(d)
eq_(obj.x, 1)
assert not hasattr(obj, "old_x")
def test_renamed_keyword_inheritance():
d = DerivedClassWithMoreKeywordAliases(x=1, y=2).to_dict()
x = d.pop("x")
y = d.pop("y")
d["old_x"] = x
d["old_y"] = y
obj = DerivedClassWithMoreKeywordAliases.from_dict(d)
eq_(obj.x, 1)
eq_(obj.y, 2)
assert not hasattr(obj, "old_x")
assert not hasattr(obj, "old_y")
|