blob: f8c45c1800fb78571e34e027adbbf7bddde68e67 [file] [log] [blame]
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001#!/usr/bin/python2.4
Joe Gregoriof863f7a2011-02-24 03:24:44 -05002# -*- coding: utf-8 -*-
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04003#
Joe Gregorio6d5e94f2010-08-25 23:49:30 -04004# Copyright 2010 Google Inc.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040018
19"""Discovery document tests
20
21Unit tests for objects created from discovery documents.
22"""
23
24__author__ = 'jcgregorio@google.com (Joe Gregorio)'
25
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040026import httplib2
27import os
28import unittest
Joe Gregorio00cf1d92010-09-27 09:22:03 -040029import urlparse
ade@google.comc5eb46f2010-09-27 23:35:39 +010030try:
31 from urlparse import parse_qs
32except ImportError:
33 from cgi import parse_qs
Joe Gregorio00cf1d92010-09-27 09:22:03 -040034
35from apiclient.discovery import build, key2param
Joe Gregoriocb8103d2011-02-11 23:20:52 -050036from apiclient.http import HttpMock
Joe Gregoriof4153422011-03-18 22:45:18 -040037from apiclient.http import tunnel_patch
38from apiclient.http import HttpMockSequence
Joe Gregorioc0e0fe92011-03-04 16:16:55 -050039from apiclient.errors import HttpError
Joe Gregorio49396552011-03-08 10:39:00 -050040from apiclient.errors import InvalidJsonError
Joe Gregoriofdf7c802011-06-30 12:33:38 -040041from apiclient.errors import MediaUploadSizeError
42from apiclient.errors import UnacceptableMimeTypeError
Joe Gregoriocb8103d2011-02-11 23:20:52 -050043
44
45DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
46
47def datafile(filename):
48 return os.path.join(DATA_DIR, filename)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040049
50
Joe Gregorioc5c5a372010-09-22 11:42:32 -040051class Utilities(unittest.TestCase):
52 def test_key2param(self):
53 self.assertEqual('max_results', key2param('max-results'))
54 self.assertEqual('x007_bond', key2param('007-bond'))
55
56
Joe Gregorioc0e0fe92011-03-04 16:16:55 -050057class DiscoveryErrors(unittest.TestCase):
58
59 def test_failed_to_parse_discovery_json(self):
60 self.http = HttpMock(datafile('malformed.json'), {'status': '200'})
61 try:
62 buzz = build('buzz', 'v1', self.http)
63 self.fail("should have raised an exception over malformed JSON.")
Joe Gregorio49396552011-03-08 10:39:00 -050064 except InvalidJsonError:
65 pass
Joe Gregorioc0e0fe92011-03-04 16:16:55 -050066
67
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040068class Discovery(unittest.TestCase):
Joe Gregorio2379ecc2010-10-26 10:51:28 -040069
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040070 def test_method_error_checking(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -050071 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040072 buzz = build('buzz', 'v1', self.http)
73
74 # Missing required parameters
75 try:
76 buzz.activities().list()
77 self.fail()
78 except TypeError, e:
79 self.assertTrue('Missing' in str(e))
80
81 # Parameter doesn't match regex
82 try:
Joe Gregoriobee86832011-02-22 10:00:19 -050083 buzz.activities().list(scope='@myself', userId='me')
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040084 self.fail()
85 except TypeError, e:
Joe Gregorioca876e42011-02-22 19:39:42 -050086 self.assertTrue('not an allowed value' in str(e))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040087
88 # Parameter doesn't match regex
89 try:
90 buzz.activities().list(scope='not@', userId='foo')
91 self.fail()
92 except TypeError, e:
Joe Gregorioca876e42011-02-22 19:39:42 -050093 self.assertTrue('not an allowed value' in str(e))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040094
95 # Unexpected parameter
96 try:
97 buzz.activities().list(flubber=12)
98 self.fail()
99 except TypeError, e:
100 self.assertTrue('unexpected' in str(e))
101
Joe Gregoriobee86832011-02-22 10:00:19 -0500102 def _check_query_types(self, request):
103 parsed = urlparse.urlparse(request.uri)
104 q = parse_qs(parsed[4])
105 self.assertEqual(q['q'], ['foo'])
106 self.assertEqual(q['i'], ['1'])
107 self.assertEqual(q['n'], ['1.0'])
108 self.assertEqual(q['b'], ['false'])
109 self.assertEqual(q['a'], ['[1, 2, 3]'])
110 self.assertEqual(q['o'], ['{\'a\': 1}'])
111 self.assertEqual(q['e'], ['bar'])
112
113 def test_type_coercion(self):
Joe Gregoriof4153422011-03-18 22:45:18 -0400114 http = HttpMock(datafile('zoo.json'), {'status': '200'})
115 zoo = build('zoo', 'v1', http)
Joe Gregoriobee86832011-02-22 10:00:19 -0500116
117 request = zoo.query(q="foo", i=1.0, n=1.0, b=0, a=[1,2,3], o={'a':1}, e='bar')
118 self._check_query_types(request)
119 request = zoo.query(q="foo", i=1, n=1, b=False, a=[1,2,3], o={'a':1}, e='bar')
120 self._check_query_types(request)
Joe Gregoriof863f7a2011-02-24 03:24:44 -0500121
Joe Gregoriobee86832011-02-22 10:00:19 -0500122 request = zoo.query(q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar')
123 self._check_query_types(request)
124
Joe Gregorio13217952011-02-22 15:37:38 -0500125 def test_optional_stack_query_parameters(self):
Joe Gregoriof4153422011-03-18 22:45:18 -0400126 http = HttpMock(datafile('zoo.json'), {'status': '200'})
127 zoo = build('zoo', 'v1', http)
128 request = zoo.query(trace='html', fields='description')
Joe Gregorio13217952011-02-22 15:37:38 -0500129
Joe Gregorioca876e42011-02-22 19:39:42 -0500130 parsed = urlparse.urlparse(request.uri)
131 q = parse_qs(parsed[4])
132 self.assertEqual(q['trace'], ['html'])
Joe Gregoriof4153422011-03-18 22:45:18 -0400133 self.assertEqual(q['fields'], ['description'])
134
135 def test_patch(self):
136 http = HttpMock(datafile('zoo.json'), {'status': '200'})
137 zoo = build('zoo', 'v1', http)
138 request = zoo.animals().patch(name='lion', body='{"description": "foo"}')
139
140 self.assertEqual(request.method, 'PATCH')
141
142 def test_tunnel_patch(self):
143 http = HttpMockSequence([
144 ({'status': '200'}, file(datafile('zoo.json'), 'r').read()),
145 ({'status': '200'}, 'echo_request_headers_as_json'),
146 ])
147 http = tunnel_patch(http)
148 zoo = build('zoo', 'v1', http)
149 resp = zoo.animals().patch(name='lion', body='{"description": "foo"}').execute()
150
151 self.assertTrue('x-http-method-override' in resp)
Joe Gregorioca876e42011-02-22 19:39:42 -0500152
ade@google.com850cf552010-08-20 23:24:56 +0100153 def test_buzz_resources(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500154 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400155 buzz = build('buzz', 'v1', self.http)
156 self.assertTrue(getattr(buzz, 'activities'))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400157 self.assertTrue(getattr(buzz, 'photos'))
158 self.assertTrue(getattr(buzz, 'people'))
159 self.assertTrue(getattr(buzz, 'groups'))
160 self.assertTrue(getattr(buzz, 'comments'))
161 self.assertTrue(getattr(buzz, 'related'))
162
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400163 def test_auth(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500164 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400165 buzz = build('buzz', 'v1', self.http)
166 auth = buzz.auth_discovery()
167 self.assertTrue('request' in auth)
168
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400169 def test_full_featured(self):
170 # Zoo should exercise all discovery facets
171 # and should also have no future.json file.
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500172 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400173 zoo = build('zoo', 'v1', self.http)
174 self.assertTrue(getattr(zoo, 'animals'))
Joe Gregoriof863f7a2011-02-24 03:24:44 -0500175
Joe Gregoriof4153422011-03-18 22:45:18 -0400176 request = zoo.animals().list(name='bat', projection="full")
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400177 parsed = urlparse.urlparse(request.uri)
178 q = parse_qs(parsed[4])
179 self.assertEqual(q['name'], ['bat'])
Joe Gregoriof4153422011-03-18 22:45:18 -0400180 self.assertEqual(q['projection'], ['full'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400181
Joe Gregorio3fada332011-01-07 17:07:45 -0500182 def test_nested_resources(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500183 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio3fada332011-01-07 17:07:45 -0500184 zoo = build('zoo', 'v1', self.http)
185 self.assertTrue(getattr(zoo, 'animals'))
186 request = zoo.my().favorites().list(max_results="5")
187 parsed = urlparse.urlparse(request.uri)
188 q = parse_qs(parsed[4])
189 self.assertEqual(q['max-results'], ['5'])
190
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500191 def test_top_level_functions(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500192 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500193 zoo = build('zoo', 'v1', self.http)
194 self.assertTrue(getattr(zoo, 'query'))
195 request = zoo.query(q="foo")
196 parsed = urlparse.urlparse(request.uri)
197 q = parse_qs(parsed[4])
198 self.assertEqual(q['q'], ['foo'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400199
Joe Gregoriofdf7c802011-06-30 12:33:38 -0400200 def test_simple_media_uploads(self):
201 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
202 zoo = build('zoo', 'v1', self.http)
203 doc = getattr(zoo.animals().insert, '__doc__')
204 self.assertTrue('media_body' in doc)
205
206 def test_simple_media_raise_correct_exceptions(self):
207 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
208 zoo = build('zoo', 'v1', self.http)
209
210 try:
211 zoo.animals().insert(media_body=datafile('smiley.png'))
212 self.fail("should throw exception if media is too large.")
213 except MediaUploadSizeError:
214 pass
215
216 try:
217 zoo.animals().insert(media_body=datafile('small.jpg'))
218 self.fail("should throw exception if mimetype is unacceptable.")
219 except UnacceptableMimeTypeError:
220 pass
221
222 def test_simple_media_good_upload(self):
223 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
224 zoo = build('zoo', 'v1', self.http)
225
226 request = zoo.animals().insert(media_body=datafile('small.png'))
227 self.assertEquals('image/png', request.headers['content-type'])
228 self.assertEquals('PNG', request.body[1:4])
229
230 def test_multipart_media_raise_correct_exceptions(self):
231 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
232 zoo = build('zoo', 'v1', self.http)
233
234 try:
235 zoo.animals().insert(media_body=datafile('smiley.png'), body={})
236 self.fail("should throw exception if media is too large.")
237 except MediaUploadSizeError:
238 pass
239
240 try:
241 zoo.animals().insert(media_body=datafile('small.jpg'), body={})
242 self.fail("should throw exception if mimetype is unacceptable.")
243 except UnacceptableMimeTypeError:
244 pass
245
246 def test_multipart_media_good_upload(self):
247 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
248 zoo = build('zoo', 'v1', self.http)
249
250 request = zoo.animals().insert(media_body=datafile('small.png'), body={})
251 self.assertTrue(request.headers['content-type'].startswith('multipart/related'))
252 self.assertEquals('--==', request.body[0:4])
253
254 def test_media_capable_method_without_media(self):
255 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
256 zoo = build('zoo', 'v1', self.http)
257
258 request = zoo.animals().insert(body={})
259 self.assertTrue(request.headers['content-type'], 'application/json')
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400260
261class Next(unittest.TestCase):
Joe Gregorioc9359072010-10-25 15:26:13 -0400262 def test_next_for_people_liked(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500263 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400264 buzz = build('buzz', 'v1', self.http)
265 people = {'links':
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400266 {'next':
267 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400268 request = buzz.people().liked_next(people)
269 self.assertEqual(request.uri, 'http://www.googleapis.com/next-link')
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400270
271
272class DeveloperKey(unittest.TestCase):
273 def test_param(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500274 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400275 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
276 activities = {'links':
277 {'next':
278 [{'href': 'http://www.googleapis.com/next-link'}]}}
279 request = buzz.activities().list_next(activities)
280 parsed = urlparse.urlparse(request.uri)
ade@google.comc5eb46f2010-09-27 23:35:39 +0100281 q = parse_qs(parsed[4])
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400282 self.assertEqual(q['key'], ['foobie_bletch'])
283
Joe Gregorioc9359072010-10-25 15:26:13 -0400284 def test_next_for_activities_list(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500285 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400286 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
287 activities = {'links':
ade@google.com2ab0de72010-09-27 23:26:54 +0100288 {'next':
289 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400290 request = buzz.activities().list_next(activities)
291 self.assertEqual(request.uri,
292 'http://www.googleapis.com/next-link?key=foobie_bletch')
293
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400294
295if __name__ == '__main__':
296 unittest.main()