blob: adc5eb1c761a3c9b9b8d3b3eefcd946eca6b5e31 [file] [log] [blame]
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001#!/usr/bin/python2.4
2#
Joe Gregorio6d5e94f2010-08-25 23:49:30 -04003# Copyright 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040017
18"""Discovery document tests
19
20Unit tests for objects created from discovery documents.
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040025import httplib2
26import os
27import unittest
Joe Gregorio00cf1d92010-09-27 09:22:03 -040028import urlparse
ade@google.comc5eb46f2010-09-27 23:35:39 +010029try:
30 from urlparse import parse_qs
31except ImportError:
32 from cgi import parse_qs
Joe Gregorio00cf1d92010-09-27 09:22:03 -040033
34from apiclient.discovery import build, key2param
Joe Gregoriocb8103d2011-02-11 23:20:52 -050035from apiclient.http import HttpMock
36
37
38DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
39
40def datafile(filename):
41 return os.path.join(DATA_DIR, filename)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040042
43
Joe Gregorioc5c5a372010-09-22 11:42:32 -040044class Utilities(unittest.TestCase):
45 def test_key2param(self):
46 self.assertEqual('max_results', key2param('max-results'))
47 self.assertEqual('x007_bond', key2param('007-bond'))
48
49
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040050class Discovery(unittest.TestCase):
Joe Gregorio2379ecc2010-10-26 10:51:28 -040051
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040052 def test_method_error_checking(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -050053 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040054 buzz = build('buzz', 'v1', self.http)
55
56 # Missing required parameters
57 try:
58 buzz.activities().list()
59 self.fail()
60 except TypeError, e:
61 self.assertTrue('Missing' in str(e))
62
63 # Parameter doesn't match regex
64 try:
Joe Gregoriobee86832011-02-22 10:00:19 -050065 buzz.activities().list(scope='@myself', userId='me')
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040066 self.fail()
67 except TypeError, e:
Joe Gregoriobee86832011-02-22 10:00:19 -050068 self.assertTrue('not in the list' in str(e))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040069
70 # Parameter doesn't match regex
71 try:
72 buzz.activities().list(scope='not@', userId='foo')
73 self.fail()
74 except TypeError, e:
Joe Gregoriobee86832011-02-22 10:00:19 -050075 self.assertTrue('not in the list' in str(e))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040076
77 # Unexpected parameter
78 try:
79 buzz.activities().list(flubber=12)
80 self.fail()
81 except TypeError, e:
82 self.assertTrue('unexpected' in str(e))
83
Joe Gregoriobee86832011-02-22 10:00:19 -050084 def _check_query_types(self, request):
85 parsed = urlparse.urlparse(request.uri)
86 q = parse_qs(parsed[4])
87 self.assertEqual(q['q'], ['foo'])
88 self.assertEqual(q['i'], ['1'])
89 self.assertEqual(q['n'], ['1.0'])
90 self.assertEqual(q['b'], ['false'])
91 self.assertEqual(q['a'], ['[1, 2, 3]'])
92 self.assertEqual(q['o'], ['{\'a\': 1}'])
93 self.assertEqual(q['e'], ['bar'])
94
95 def test_type_coercion(self):
96 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
97 zoo = build('zoo', 'v1', self.http)
98
99 request = zoo.query(q="foo", i=1.0, n=1.0, b=0, a=[1,2,3], o={'a':1}, e='bar')
100 self._check_query_types(request)
101 request = zoo.query(q="foo", i=1, n=1, b=False, a=[1,2,3], o={'a':1}, e='bar')
102 self._check_query_types(request)
103 request = zoo.query(q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar')
104 self._check_query_types(request)
105
ade@google.com850cf552010-08-20 23:24:56 +0100106 def test_buzz_resources(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500107 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400108 buzz = build('buzz', 'v1', self.http)
109 self.assertTrue(getattr(buzz, 'activities'))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400110 self.assertTrue(getattr(buzz, 'photos'))
111 self.assertTrue(getattr(buzz, 'people'))
112 self.assertTrue(getattr(buzz, 'groups'))
113 self.assertTrue(getattr(buzz, 'comments'))
114 self.assertTrue(getattr(buzz, 'related'))
115
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400116 def test_auth(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500117 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400118 buzz = build('buzz', 'v1', self.http)
119 auth = buzz.auth_discovery()
120 self.assertTrue('request' in auth)
121
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400122 def test_full_featured(self):
123 # Zoo should exercise all discovery facets
124 # and should also have no future.json file.
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500125 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400126 zoo = build('zoo', 'v1', self.http)
127 self.assertTrue(getattr(zoo, 'animals'))
128 request = zoo.animals().list(name="bat", projection="size")
129 parsed = urlparse.urlparse(request.uri)
130 q = parse_qs(parsed[4])
131 self.assertEqual(q['name'], ['bat'])
132 self.assertEqual(q['projection'], ['size'])
133
Joe Gregorio3fada332011-01-07 17:07:45 -0500134 def test_nested_resources(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500135 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio3fada332011-01-07 17:07:45 -0500136 zoo = build('zoo', 'v1', self.http)
137 self.assertTrue(getattr(zoo, 'animals'))
138 request = zoo.my().favorites().list(max_results="5")
139 parsed = urlparse.urlparse(request.uri)
140 q = parse_qs(parsed[4])
141 self.assertEqual(q['max-results'], ['5'])
142
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500143 def test_top_level_functions(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500144 self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500145 zoo = build('zoo', 'v1', self.http)
146 self.assertTrue(getattr(zoo, 'query'))
147 request = zoo.query(q="foo")
148 parsed = urlparse.urlparse(request.uri)
149 q = parse_qs(parsed[4])
150 self.assertEqual(q['q'], ['foo'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400151
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400152
153class Next(unittest.TestCase):
Joe Gregorioc9359072010-10-25 15:26:13 -0400154 def test_next_for_people_liked(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500155 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400156 buzz = build('buzz', 'v1', self.http)
157 people = {'links':
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400158 {'next':
159 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400160 request = buzz.people().liked_next(people)
161 self.assertEqual(request.uri, 'http://www.googleapis.com/next-link')
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400162
163
164class DeveloperKey(unittest.TestCase):
165 def test_param(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500166 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400167 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
168 activities = {'links':
169 {'next':
170 [{'href': 'http://www.googleapis.com/next-link'}]}}
171 request = buzz.activities().list_next(activities)
172 parsed = urlparse.urlparse(request.uri)
ade@google.comc5eb46f2010-09-27 23:35:39 +0100173 q = parse_qs(parsed[4])
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400174 self.assertEqual(q['key'], ['foobie_bletch'])
175
Joe Gregorioc9359072010-10-25 15:26:13 -0400176 def test_next_for_activities_list(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -0500177 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400178 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
179 activities = {'links':
ade@google.com2ab0de72010-09-27 23:26:54 +0100180 {'next':
181 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400182 request = buzz.activities().list_next(activities)
183 self.assertEqual(request.uri,
184 'http://www.googleapis.com/next-link?key=foobie_bletch')
185
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400186
187if __name__ == '__main__':
188 unittest.main()