75
75
from googleapiclient .http import MediaUploadProgress
76
76
from googleapiclient .http import tunnel_patch
77
77
from googleapiclient .model import JsonModel
78
+ from googleapiclient .schema import Schemas
78
79
from oauth2client import GOOGLE_TOKEN_URI
79
80
from oauth2client .client import OAuth2Credentials , GoogleCredentials
80
81
@@ -122,18 +123,21 @@ def setUp(self):
122
123
self .zoo_get_method_desc = self .zoo_root_desc ['methods' ]['query' ]
123
124
self .zoo_animals_resource = self .zoo_root_desc ['resources' ]['animals' ]
124
125
self .zoo_insert_method_desc = self .zoo_animals_resource ['methods' ]['insert' ]
126
+ self .zoo_schema = Schemas (self .zoo_root_desc )
125
127
126
128
def test_key2param (self ):
127
129
self .assertEqual ('max_results' , key2param ('max-results' ))
128
130
self .assertEqual ('x007_bond' , key2param ('007-bond' ))
129
131
130
- def _base_fix_up_parameters_test (self , method_desc , http_method , root_desc ):
132
+ def _base_fix_up_parameters_test (
133
+ self , method_desc , http_method , root_desc , schema ):
131
134
self .assertEqual (method_desc ['httpMethod' ], http_method )
132
135
133
136
method_desc_copy = copy .deepcopy (method_desc )
134
137
self .assertEqual (method_desc , method_desc_copy )
135
138
136
- parameters = _fix_up_parameters (method_desc_copy , root_desc , http_method )
139
+ parameters = _fix_up_parameters (method_desc_copy , root_desc , http_method ,
140
+ schema )
137
141
138
142
self .assertNotEqual (method_desc , method_desc_copy )
139
143
@@ -147,14 +151,14 @@ def _base_fix_up_parameters_test(self, method_desc, http_method, root_desc):
147
151
return parameters
148
152
149
153
def test_fix_up_parameters_get (self ):
150
- parameters = self ._base_fix_up_parameters_test (self . zoo_get_method_desc ,
151
- 'GET' , self .zoo_root_desc )
154
+ parameters = self ._base_fix_up_parameters_test (
155
+ self . zoo_get_method_desc , 'GET' , self .zoo_root_desc , self . zoo_schema )
152
156
# Since http_method is 'GET'
153
157
self .assertFalse ('body' in parameters )
154
158
155
159
def test_fix_up_parameters_insert (self ):
156
- parameters = self ._base_fix_up_parameters_test (self . zoo_insert_method_desc ,
157
- 'POST' , self .zoo_root_desc )
160
+ parameters = self ._base_fix_up_parameters_test (
161
+ self . zoo_insert_method_desc , 'POST' , self .zoo_root_desc , self . zoo_schema )
158
162
body = {
159
163
'description' : 'The request body.' ,
160
164
'type' : 'object' ,
@@ -165,35 +169,58 @@ def test_fix_up_parameters_insert(self):
165
169
166
170
def test_fix_up_parameters_check_body (self ):
167
171
dummy_root_desc = {}
172
+ dummy_schema = {
173
+ 'Request' : {
174
+ 'properties' : {
175
+ "description" : "Required. Dummy parameter." ,
176
+ "type" : "string"
177
+ }
178
+ }
179
+ }
168
180
no_payload_http_method = 'DELETE'
169
181
with_payload_http_method = 'PUT'
170
182
171
183
invalid_method_desc = {'response' : 'Who cares' }
172
- valid_method_desc = {'request' : {'key1' : 'value1' , 'key2' : 'value2' }}
184
+ valid_method_desc = {
185
+ 'request' : {
186
+ 'key1' : 'value1' ,
187
+ 'key2' : 'value2' ,
188
+ '$ref' : 'Request'
189
+ }
190
+ }
173
191
174
192
parameters = _fix_up_parameters (invalid_method_desc , dummy_root_desc ,
175
- no_payload_http_method )
193
+ no_payload_http_method , dummy_schema )
176
194
self .assertFalse ('body' in parameters )
177
195
178
196
parameters = _fix_up_parameters (valid_method_desc , dummy_root_desc ,
179
- no_payload_http_method )
197
+ no_payload_http_method , dummy_schema )
180
198
self .assertFalse ('body' in parameters )
181
199
182
200
parameters = _fix_up_parameters (invalid_method_desc , dummy_root_desc ,
183
- with_payload_http_method )
201
+ with_payload_http_method , dummy_schema )
184
202
self .assertFalse ('body' in parameters )
185
203
186
204
parameters = _fix_up_parameters (valid_method_desc , dummy_root_desc ,
187
- with_payload_http_method )
205
+ with_payload_http_method , dummy_schema )
188
206
body = {
189
207
'description' : 'The request body.' ,
190
208
'type' : 'object' ,
191
209
'required' : True ,
210
+ '$ref' : 'Request' ,
192
211
'key1' : 'value1' ,
193
212
'key2' : 'value2' ,
194
213
}
195
214
self .assertEqual (parameters ['body' ], body )
196
215
216
+ def test_fix_up_parameters_optional_body (self ):
217
+ # Request with no parameters
218
+ dummy_schema = {'Request' : {'properties' : {}}}
219
+ method_desc = {'request' : {'$ref' : 'Request' }}
220
+
221
+ parameters = _fix_up_parameters (method_desc , {}, 'POST' , dummy_schema )
222
+ self .assertFalse (parameters ['body' ]['required' ])
223
+
197
224
def _base_fix_up_method_description_test (
198
225
self , method_desc , initial_parameters , final_parameters ,
199
226
final_accept , final_max_size , final_media_path_url ):
@@ -260,7 +287,7 @@ def test_fix_up_media_upload_with_initial_valid_full(self):
260
287
261
288
def test_fix_up_method_description_get (self ):
262
289
result = _fix_up_method_description (self .zoo_get_method_desc ,
263
- self .zoo_root_desc )
290
+ self .zoo_root_desc , self . zoo_schema )
264
291
path_url = 'query'
265
292
http_method = 'GET'
266
293
method_id = 'bigquery.query'
@@ -272,7 +299,7 @@ def test_fix_up_method_description_get(self):
272
299
273
300
def test_fix_up_method_description_insert (self ):
274
301
result = _fix_up_method_description (self .zoo_insert_method_desc ,
275
- self .zoo_root_desc )
302
+ self .zoo_root_desc , self . zoo_schema )
276
303
path_url = 'animals'
277
304
http_method = 'POST'
278
305
method_id = 'zoo.animals.insert'
0 commit comments