forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_json_obj.py
More file actions
220 lines (154 loc) · 4.74 KB
/
test_json_obj.py
File metadata and controls
220 lines (154 loc) · 4.74 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import morepath
from webtest import TestApp as Client
def test_json_obj_dump():
class app(morepath.App):
pass
@app.path(path='/models/{x}')
class Model(object):
def __init__(self, x):
self.x = x
@app.json(model=Model)
def default(self, request):
return self
@app.dump_json(model=Model)
def dump_model_json(self, request):
return {'x': self.x}
c = Client(app())
response = c.get('/models/foo')
assert response.json == {'x': 'foo'}
def test_json_obj_dump_app_arg():
class App(morepath.App):
pass
@App.path(path='/models/{x}')
class Model(object):
def __init__(self, x):
self.x = x
@App.json(model=Model)
def default(self, request):
return self
@App.dump_json(model=Model)
def dump_model_json(app, obj, request):
assert isinstance(app, App)
return {'x': obj.x}
c = Client(App())
response = c.get('/models/foo')
assert response.json == {'x': 'foo'}
def test_json_obj_load():
class app(morepath.App):
pass
class Collection(object):
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
collection = Collection()
@app.path(path='/', model=Collection)
def get_collection():
return collection
@app.json(model=Collection, request_method='POST')
def default(self, request):
self.add(request.body_obj)
return 'done'
class Item(object):
def __init__(self, value):
self.value = value
@app.load_json()
def load_json(json, request):
return Item(json['x'])
c = Client(app())
c.post_json('/', {'x': 'foo'})
assert len(collection.items) == 1
assert isinstance(collection.items[0], Item)
assert collection.items[0].value == 'foo'
def test_json_obj_load_app_arg():
class App(morepath.App):
pass
class Collection(object):
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
collection = Collection()
@App.path(path='/', model=Collection)
def get_collection():
return collection
@App.json(model=Collection, request_method='POST')
def default(self, request):
self.add(request.body_obj)
return 'done'
class Item(object):
def __init__(self, value):
self.value = value
@App.load_json()
def load_json(app, json, request):
assert isinstance(app, App)
return Item(json['x'])
c = Client(App())
c.post_json('/', {'x': 'foo'})
assert len(collection.items) == 1
assert isinstance(collection.items[0], Item)
assert collection.items[0].value == 'foo'
def test_json_obj_load_default():
class app(morepath.App):
pass
class Root(object):
pass
@app.path(path='/', model=Root)
def get_root():
return Root()
@app.json(model=Root, request_method='POST')
def default(self, request):
assert request.body_obj == request.json
return 'done'
c = Client(app())
c.post_json('/', {'x': 'foo'})
def test_json_body_model():
class app(morepath.App):
pass
class Collection(object):
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
class Item1(object):
def __init__(self, value):
self.value = value
class Item2(object):
def __init__(self, value):
self.value = value
collection = Collection()
@app.path(path='/', model=Collection)
def get_collection():
return collection
@app.json(model=Collection, request_method='POST',
body_model=Item1)
def default(self, request):
self.add(request.body_obj)
return 'done'
@app.load_json()
def load_json(json, request):
if json['@type'] == 'Item1':
return Item1(json['x'])
elif json['@type'] == 'Item2':
return Item2(json['x'])
c = Client(app())
c.post_json('/', {'@type': 'Item1', 'x': 'foo'})
assert len(collection.items) == 1
assert isinstance(collection.items[0], Item1)
assert collection.items[0].value == 'foo'
c.post_json('/', {'@type': 'Item2', 'x': 'foo'}, status=422)
def test_json_obj_load_no_json_post():
class app(morepath.App):
pass
class Root(object):
pass
@app.path(path='/', model=Root)
def get_root():
return Root()
@app.json(model=Root, request_method='POST')
def default(self, request):
assert request.body_obj is None
return 'done'
c = Client(app())
response = c.post('/', {'x': 'foo'})
assert response.json == 'done'