File: test_keyword_aliases.py

package info (click to toggle)
python-serializable 0.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 470; makefile: 7
file content (94 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from serializable import Serializable
from .common 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")