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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# ==============================================================================
# Copyright (C) 2019 - Philip Paquette, Steven Bocco
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
""" Test changes in a Jsonable schema. """
#pylint: disable=invalid-name
from diplomacy.utils import parsing
from diplomacy.utils.jsonable import Jsonable
def converter_to_int(val):
""" A converter from given value to an integer. Used in Version1. """
try:
return int(val)
except ValueError:
return 0
class Version1(Jsonable):
""" A Jsonable with fields a, b, c, d.
NB: To parse a dict from Version22 to Version1, modified fields a and c must be convertible in Version1.
using ConverterType in Version1.
"""
model = {
'a': parsing.ConverterType(int, converter_to_int),
'b': parsing.OptionalValueType(str),
'c': parsing.ConverterType(float, converter_function=float),
'd': parsing.DefaultValueType(bool, True),
}
def __init__(self, **kwargs):
self.a = None
self.b = None
self.c = None
self.d = None
super(Version1, self).__init__(**kwargs)
class Version20(Jsonable):
""" Version1 with removed fields b and d.
NB: To parse a dict from Version20 to Version1, removed fields b and d must be optional in Version1.
"""
model = {
'a': int,
'c': float,
}
def __init__(self, **kwargs):
self.a = None
self.c = None
super(Version20, self).__init__(**kwargs)
class Version21(Jsonable):
""" Version1 with added fields e and f.
NB: To parse a dict from Version1 to Version21, added fields e and f must be optional in Version21.
"""
model = {
'a': int,
'b': str,
'c': float,
'd': bool,
'e': parsing.DefaultValueType(parsing.EnumerationType([100, 200, 300, 400]), 100),
'f': parsing.DefaultValueType(dict, {'x': 1, 'y': 2})
}
def __init__(self, **kwargs):
self.a = None
self.b = None
self.c = None
self.d = None
self.e = None
self.f = {}
super(Version21, self).__init__(**kwargs)
class Version22(Jsonable):
""" Version1 with modified types for a and c.
NB: To parse a dict from Version1 to Version22, modified fields a and c must be convertible
using ConverterType in Version22.
"""
model = {
'a': parsing.ConverterType(str, converter_function=str),
'b': str,
'c': parsing.ConverterType(bool, converter_function=bool),
'd': bool,
}
def __init__(self, **kwargs):
self.a = None
self.b = None
self.c = None
self.d = None
super(Version22, self).__init__(**kwargs)
class Version3(Jsonable):
""" Version 1 with a modified, b removed, e added.
To parse a dict between Version3 and Version1:
- a must be convertible in both versions.
- b must be optional in Version1.
- e must be optional in Version3.
"""
model = {
'a': parsing.ConverterType(str, converter_function=str),
'c': float,
'd': bool,
'e': parsing.OptionalValueType(parsing.SequenceType(int))
}
def __init__(self, **kwargs):
self.a = None
self.c = None
self.d = None
self.e = None
super(Version3, self).__init__(**kwargs)
def test_jsonable_changes_v1_v20():
""" Test changes from Version1 to Version20. """
v20 = Version20(a=1, c=1.5)
v1 = Version1(a=1, b='b', c=1.5, d=False)
json_v1 = v1.to_dict()
v20_from_v1 = Version20.from_dict(json_v1)
json_v20_from_v1 = v20_from_v1.to_dict()
v1_from_v20_from_v1 = Version1.from_dict(json_v20_from_v1)
assert v1_from_v20_from_v1.b is None
assert v1_from_v20_from_v1.d is True
json_v20 = v20.to_dict()
v1_from_v20 = Version1.from_dict(json_v20)
assert v1_from_v20.b is None
assert v1_from_v20.d is True
def test_jsonable_changes_v1_v21():
""" Test changes from Version1 to Version21. """
v21 = Version21(a=1, b='b21', c=1.5, d=True, e=300, f=dict(x=1, y=2))
v1 = Version1(a=1, b='b', c=1.5, d=False)
json_v1 = v1.to_dict()
v21_from_v1 = Version21.from_dict(json_v1)
assert v21_from_v1.e == 100
assert v21_from_v1.f['x'] == 1
assert v21_from_v1.f['y'] == 2
json_v21_from_v1 = v21_from_v1.to_dict()
v1_from_v21_from_v1 = Version1.from_dict(json_v21_from_v1)
assert v1_from_v21_from_v1.b == 'b'
assert v1_from_v21_from_v1.d is False
json_v21 = v21.to_dict()
v1_from_v21 = Version1.from_dict(json_v21)
assert v1_from_v21.b == 'b21'
assert v1_from_v21.d is True
def test_jsonable_changes_v1_v22():
""" Test changes from Version1 to Version22. """
v22 = Version22(a='a', b='b', c=False, d=False)
v1 = Version1(a=1, b='b', c=1.5, d=False)
json_v1 = v1.to_dict()
v22_from_v1 = Version22.from_dict(json_v1)
assert v22_from_v1.a == '1'
assert v22_from_v1.c is True
json_v22_from_v1 = v22_from_v1.to_dict()
v1_from_v22_from_v1 = Version1.from_dict(json_v22_from_v1)
assert v1_from_v22_from_v1.a == 1
assert v1_from_v22_from_v1.c == 1.0
json_v22 = v22.to_dict()
v1_from_v22 = Version1.from_dict(json_v22)
assert v1_from_v22.a == 0
assert v1_from_v22.c == 0.0
def test_jsonable_changes_v1_v3():
""" Test changes from Version1 to Version3. """
v3 = Version3(a='a', c=1.5, d=False, e=(1, 2, 3))
v1 = Version1(a=1, b='b', c=1.5, d=False)
json_v1 = v1.to_dict()
v3_from_v1 = Version3.from_dict(json_v1)
assert v3_from_v1.a == '1'
assert v3_from_v1.e is None
json_v3_from_v1 = v3_from_v1.to_dict()
v1_from_v3_from_v1 = Version1.from_dict(json_v3_from_v1)
assert v1_from_v3_from_v1.a == 1
assert v1_from_v3_from_v1.b is None
json_v3 = v3.to_dict()
v1_from_v3 = Version1.from_dict(json_v3)
assert v1_from_v3.a == 0
assert v1_from_v3.b is None
|