-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_iko.py
144 lines (100 loc) · 3.43 KB
/
test_iko.py
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
import base64
import iko
async def test_context():
class PrivateField(iko.Field):
async def post_load(self, value, context):
value = bytes(
(char + context['private_offset']) % 256
for char in value.encode('utf-8')
)
return base64.encodebytes(value).decode('utf-8').strip()
async def post_dump(self, value, context):
value = bytes(
(bt - context['private_offset']) % 256
for bt in base64.decodebytes(value.encode('utf-8'))
)
return value.decode('utf-8').strip()
class Schema(iko.Schema):
secret = PrivateField()
ctx = {'private_offset': 1}
load_data = {'secret': 'moon'}
dump_data = {'secret': 'bnBwbw=='}
assert await Schema.load(load_data, context=ctx) == dump_data
assert await Schema.dump(dump_data, context=ctx) == load_data
async def test_dump_to():
class Schema(iko.Schema):
_id = iko.Field(dump_to='id')
assert await Schema.dump({'_id': 42}) == {'id': 42}
async def test_load_from():
class Schema(iko.Schema):
_id = iko.Field(load_from='id')
assert await Schema.load({'id': 42}) == {'_id': 42}
async def test_outer_name():
class Schema(iko.Schema):
_id = iko.Field(outer_name='id')
assert await Schema.dump({'_id': 42}) == {'id': 42}
assert await Schema.load({'id': 42}) == {'_id': 42}
async def test_many():
class Schema(iko.Schema):
field = iko.Field
data = {'field': '42'}
assert await Schema.dump_many([data]) == [data]
assert await Schema.load_many([data]) == [data]
async def test_nested():
class Named(iko.Schema):
name = iko.Field
class Schema(iko.Schema):
owner = iko.Nested(Named)
friend = iko.Nested(Named())
data = {
'owner': {'name': 'Alice'},
'friend': {'name': 'Bob'},
}
assert await Schema.load(data) == data
assert await Schema.dump(data) == data
async def test_list():
class Named(iko.Schema):
name = iko.Field
class Schema(iko.Schema):
owners = iko.List(Named)
friends = iko.List(Named())
data = {
'owners': [{'name': 'Alice'}, {'name': 'Bob'}],
'friends': [{'name': 'Eve'}],
}
assert await Schema.load(data) == data
assert await Schema.dump(data) == data
async def test_list_field():
class Name(iko.Field):
pass
class Schema(iko.Schema):
owners = iko.List(field=Name)
friends = iko.List(field=Name())
data = {
'owners': [
'Alice',
'Bob',
],
'friends': ['Eve'],
}
assert await Schema.load(data) == data
assert await Schema.dump(data) == data
async def test_default():
class Schema(iko.Schema):
field42 = iko.Field(default=42)
field43 = iko.Field(default=lambda: 43)
expected_data = {
'field42': 42,
'field43': 43,
}
assert await Schema.dump({}) == expected_data
assert await Schema.load({}) == expected_data
async def test_optional():
class NullOptional(iko.Field):
OPTIONAL_LIST = (None, iko.OPTIONAL)
class Schema(iko.Schema):
field = NullOptional
assert await Schema.dump({'field': 43}) == {'field': 43}
assert await Schema.load({'field': 43}) == {'field': 43}
assert await Schema.dump({'field': None}) == {}
assert await Schema.load({'field': None}) == {}