Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 3 | # |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 4 | # 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 Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 18 | |
| 19 | """Discovery document tests |
| 20 | |
| 21 | Unit tests for objects created from discovery documents. |
| 22 | """ |
| 23 | |
| 24 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 25 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 26 | import httplib2 |
| 27 | import os |
| 28 | import unittest |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 29 | import urlparse |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 30 | try: |
| 31 | from urlparse import parse_qs |
| 32 | except ImportError: |
| 33 | from cgi import parse_qs |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 34 | |
| 35 | from apiclient.discovery import build, key2param |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 36 | from apiclient.http import HttpMock |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 37 | from apiclient.http import tunnel_patch |
| 38 | from apiclient.http import HttpMockSequence |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 39 | from apiclient.errors import HttpError |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 40 | from apiclient.errors import InvalidJsonError |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 41 | |
| 42 | |
| 43 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 44 | |
| 45 | def datafile(filename): |
| 46 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 47 | |
| 48 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 49 | class Utilities(unittest.TestCase): |
| 50 | def test_key2param(self): |
| 51 | self.assertEqual('max_results', key2param('max-results')) |
| 52 | self.assertEqual('x007_bond', key2param('007-bond')) |
| 53 | |
| 54 | |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 55 | class DiscoveryErrors(unittest.TestCase): |
| 56 | |
| 57 | def test_failed_to_parse_discovery_json(self): |
| 58 | self.http = HttpMock(datafile('malformed.json'), {'status': '200'}) |
| 59 | try: |
| 60 | buzz = build('buzz', 'v1', self.http) |
| 61 | self.fail("should have raised an exception over malformed JSON.") |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 62 | except InvalidJsonError: |
| 63 | pass |
Joe Gregorio | c0e0fe9 | 2011-03-04 16:16:55 -0500 | [diff] [blame] | 64 | |
| 65 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 66 | class Discovery(unittest.TestCase): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 67 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 68 | def test_method_error_checking(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 69 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 70 | buzz = build('buzz', 'v1', self.http) |
| 71 | |
| 72 | # Missing required parameters |
| 73 | try: |
| 74 | buzz.activities().list() |
| 75 | self.fail() |
| 76 | except TypeError, e: |
| 77 | self.assertTrue('Missing' in str(e)) |
| 78 | |
| 79 | # Parameter doesn't match regex |
| 80 | try: |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 81 | buzz.activities().list(scope='@myself', userId='me') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 82 | self.fail() |
| 83 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 84 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 85 | |
| 86 | # Parameter doesn't match regex |
| 87 | try: |
| 88 | buzz.activities().list(scope='not@', userId='foo') |
| 89 | self.fail() |
| 90 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 91 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 92 | |
| 93 | # Unexpected parameter |
| 94 | try: |
| 95 | buzz.activities().list(flubber=12) |
| 96 | self.fail() |
| 97 | except TypeError, e: |
| 98 | self.assertTrue('unexpected' in str(e)) |
| 99 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 100 | def _check_query_types(self, request): |
| 101 | parsed = urlparse.urlparse(request.uri) |
| 102 | q = parse_qs(parsed[4]) |
| 103 | self.assertEqual(q['q'], ['foo']) |
| 104 | self.assertEqual(q['i'], ['1']) |
| 105 | self.assertEqual(q['n'], ['1.0']) |
| 106 | self.assertEqual(q['b'], ['false']) |
| 107 | self.assertEqual(q['a'], ['[1, 2, 3]']) |
| 108 | self.assertEqual(q['o'], ['{\'a\': 1}']) |
| 109 | self.assertEqual(q['e'], ['bar']) |
| 110 | |
| 111 | def test_type_coercion(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 112 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 113 | zoo = build('zoo', 'v1', http) |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 114 | |
| 115 | request = zoo.query(q="foo", i=1.0, n=1.0, b=0, a=[1,2,3], o={'a':1}, e='bar') |
| 116 | self._check_query_types(request) |
| 117 | request = zoo.query(q="foo", i=1, n=1, b=False, a=[1,2,3], o={'a':1}, e='bar') |
| 118 | self._check_query_types(request) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 119 | |
Joe Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 120 | request = zoo.query(q="foo", i="1", n="1", b="", a=[1,2,3], o={'a':1}, e='bar') |
| 121 | self._check_query_types(request) |
| 122 | |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 123 | def test_optional_stack_query_parameters(self): |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 124 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 125 | zoo = build('zoo', 'v1', http) |
| 126 | request = zoo.query(trace='html', fields='description') |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 127 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 128 | parsed = urlparse.urlparse(request.uri) |
| 129 | q = parse_qs(parsed[4]) |
| 130 | self.assertEqual(q['trace'], ['html']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 131 | self.assertEqual(q['fields'], ['description']) |
| 132 | |
| 133 | def test_patch(self): |
| 134 | http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 135 | zoo = build('zoo', 'v1', http) |
| 136 | request = zoo.animals().patch(name='lion', body='{"description": "foo"}') |
| 137 | |
| 138 | self.assertEqual(request.method, 'PATCH') |
| 139 | |
| 140 | def test_tunnel_patch(self): |
| 141 | http = HttpMockSequence([ |
| 142 | ({'status': '200'}, file(datafile('zoo.json'), 'r').read()), |
| 143 | ({'status': '200'}, 'echo_request_headers_as_json'), |
| 144 | ]) |
| 145 | http = tunnel_patch(http) |
| 146 | zoo = build('zoo', 'v1', http) |
| 147 | resp = zoo.animals().patch(name='lion', body='{"description": "foo"}').execute() |
| 148 | |
| 149 | self.assertTrue('x-http-method-override' in resp) |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 150 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 151 | def test_buzz_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 152 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 153 | buzz = build('buzz', 'v1', self.http) |
| 154 | self.assertTrue(getattr(buzz, 'activities')) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 155 | self.assertTrue(getattr(buzz, 'photos')) |
| 156 | self.assertTrue(getattr(buzz, 'people')) |
| 157 | self.assertTrue(getattr(buzz, 'groups')) |
| 158 | self.assertTrue(getattr(buzz, 'comments')) |
| 159 | self.assertTrue(getattr(buzz, 'related')) |
| 160 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 161 | def test_auth(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 162 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 163 | buzz = build('buzz', 'v1', self.http) |
| 164 | auth = buzz.auth_discovery() |
| 165 | self.assertTrue('request' in auth) |
| 166 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 167 | def test_full_featured(self): |
| 168 | # Zoo should exercise all discovery facets |
| 169 | # and should also have no future.json file. |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 170 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 171 | zoo = build('zoo', 'v1', self.http) |
| 172 | self.assertTrue(getattr(zoo, 'animals')) |
Joe Gregorio | f863f7a | 2011-02-24 03:24:44 -0500 | [diff] [blame] | 173 | |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 174 | request = zoo.animals().list(name='bat', projection="full") |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 175 | parsed = urlparse.urlparse(request.uri) |
| 176 | q = parse_qs(parsed[4]) |
| 177 | self.assertEqual(q['name'], ['bat']) |
Joe Gregorio | f415342 | 2011-03-18 22:45:18 -0400 | [diff] [blame] | 178 | self.assertEqual(q['projection'], ['full']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 179 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 180 | def test_nested_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 181 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 182 | zoo = build('zoo', 'v1', self.http) |
| 183 | self.assertTrue(getattr(zoo, 'animals')) |
| 184 | request = zoo.my().favorites().list(max_results="5") |
| 185 | parsed = urlparse.urlparse(request.uri) |
| 186 | q = parse_qs(parsed[4]) |
| 187 | self.assertEqual(q['max-results'], ['5']) |
| 188 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 189 | def test_top_level_functions(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 190 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 191 | zoo = build('zoo', 'v1', self.http) |
| 192 | self.assertTrue(getattr(zoo, 'query')) |
| 193 | request = zoo.query(q="foo") |
| 194 | parsed = urlparse.urlparse(request.uri) |
| 195 | q = parse_qs(parsed[4]) |
| 196 | self.assertEqual(q['q'], ['foo']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 197 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 198 | |
| 199 | class Next(unittest.TestCase): |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 200 | def test_next_for_people_liked(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 201 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 202 | buzz = build('buzz', 'v1', self.http) |
| 203 | people = {'links': |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 204 | {'next': |
| 205 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 206 | request = buzz.people().liked_next(people) |
| 207 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 208 | |
| 209 | |
| 210 | class DeveloperKey(unittest.TestCase): |
| 211 | def test_param(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 212 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 213 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 214 | activities = {'links': |
| 215 | {'next': |
| 216 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 217 | request = buzz.activities().list_next(activities) |
| 218 | parsed = urlparse.urlparse(request.uri) |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 219 | q = parse_qs(parsed[4]) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 220 | self.assertEqual(q['key'], ['foobie_bletch']) |
| 221 | |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 222 | def test_next_for_activities_list(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 223 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 224 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 225 | activities = {'links': |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 226 | {'next': |
| 227 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 228 | request = buzz.activities().list_next(activities) |
| 229 | self.assertEqual(request.uri, |
| 230 | 'http://www.googleapis.com/next-link?key=foobie_bletch') |
| 231 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 232 | |
| 233 | if __name__ == '__main__': |
| 234 | unittest.main() |