Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 3 | # 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 Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 17 | |
| 18 | """Discovery document tests |
| 19 | |
| 20 | Unit tests for objects created from discovery documents. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 25 | import httplib2 |
| 26 | import os |
| 27 | import unittest |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 28 | import urlparse |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 29 | try: |
| 30 | from urlparse import parse_qs |
| 31 | except ImportError: |
| 32 | from cgi import parse_qs |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 33 | |
| 34 | from apiclient.discovery import build, key2param |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 35 | from apiclient.http import HttpMock |
| 36 | |
| 37 | |
| 38 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 39 | |
| 40 | def datafile(filename): |
| 41 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 42 | |
| 43 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 44 | class 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 Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 50 | class Discovery(unittest.TestCase): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 51 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 52 | def test_method_error_checking(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 53 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 54 | 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 Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 65 | buzz.activities().list(scope='@myself', userId='me') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 66 | self.fail() |
| 67 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 68 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 69 | |
| 70 | # Parameter doesn't match regex |
| 71 | try: |
| 72 | buzz.activities().list(scope='not@', userId='foo') |
| 73 | self.fail() |
| 74 | except TypeError, e: |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 75 | self.assertTrue('not an allowed value' in str(e)) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 76 | |
| 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 Gregorio | bee8683 | 2011-02-22 10:00:19 -0500 | [diff] [blame] | 84 | 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 | |
Joe Gregorio | 1321795 | 2011-02-22 15:37:38 -0500 | [diff] [blame] | 106 | def test_optional_stack_query_parameters(self): |
| 107 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
| 108 | zoo = build('zoo', 'v1', self.http) |
| 109 | request = zoo.query(trace='html') |
| 110 | |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 111 | parsed = urlparse.urlparse(request.uri) |
| 112 | q = parse_qs(parsed[4]) |
| 113 | self.assertEqual(q['trace'], ['html']) |
| 114 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 115 | def test_buzz_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 116 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 117 | buzz = build('buzz', 'v1', self.http) |
| 118 | self.assertTrue(getattr(buzz, 'activities')) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 119 | self.assertTrue(getattr(buzz, 'photos')) |
| 120 | self.assertTrue(getattr(buzz, 'people')) |
| 121 | self.assertTrue(getattr(buzz, 'groups')) |
| 122 | self.assertTrue(getattr(buzz, 'comments')) |
| 123 | self.assertTrue(getattr(buzz, 'related')) |
| 124 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 125 | def test_auth(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 126 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 127 | buzz = build('buzz', 'v1', self.http) |
| 128 | auth = buzz.auth_discovery() |
| 129 | self.assertTrue('request' in auth) |
| 130 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 131 | def test_full_featured(self): |
| 132 | # Zoo should exercise all discovery facets |
| 133 | # and should also have no future.json file. |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 134 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 135 | zoo = build('zoo', 'v1', self.http) |
| 136 | self.assertTrue(getattr(zoo, 'animals')) |
| 137 | request = zoo.animals().list(name="bat", projection="size") |
| 138 | parsed = urlparse.urlparse(request.uri) |
| 139 | q = parse_qs(parsed[4]) |
| 140 | self.assertEqual(q['name'], ['bat']) |
| 141 | self.assertEqual(q['projection'], ['size']) |
| 142 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 143 | def test_nested_resources(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 144 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 145 | zoo = build('zoo', 'v1', self.http) |
| 146 | self.assertTrue(getattr(zoo, 'animals')) |
| 147 | request = zoo.my().favorites().list(max_results="5") |
| 148 | parsed = urlparse.urlparse(request.uri) |
| 149 | q = parse_qs(parsed[4]) |
| 150 | self.assertEqual(q['max-results'], ['5']) |
| 151 | |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 152 | def test_top_level_functions(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 153 | self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | 7a6df3a | 2011-01-31 21:55:21 -0500 | [diff] [blame] | 154 | zoo = build('zoo', 'v1', self.http) |
| 155 | self.assertTrue(getattr(zoo, 'query')) |
| 156 | request = zoo.query(q="foo") |
| 157 | parsed = urlparse.urlparse(request.uri) |
| 158 | q = parse_qs(parsed[4]) |
| 159 | self.assertEqual(q['q'], ['foo']) |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 160 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 161 | |
| 162 | class Next(unittest.TestCase): |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 163 | def test_next_for_people_liked(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 164 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 165 | buzz = build('buzz', 'v1', self.http) |
| 166 | people = {'links': |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 167 | {'next': |
| 168 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 169 | request = buzz.people().liked_next(people) |
| 170 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 171 | |
| 172 | |
| 173 | class DeveloperKey(unittest.TestCase): |
| 174 | def test_param(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 175 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 176 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 177 | activities = {'links': |
| 178 | {'next': |
| 179 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 180 | request = buzz.activities().list_next(activities) |
| 181 | parsed = urlparse.urlparse(request.uri) |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 182 | q = parse_qs(parsed[4]) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 183 | self.assertEqual(q['key'], ['foobie_bletch']) |
| 184 | |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 185 | def test_next_for_activities_list(self): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 186 | self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 187 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 188 | activities = {'links': |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 189 | {'next': |
| 190 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 191 | request = buzz.activities().list_next(activities) |
| 192 | self.assertEqual(request.uri, |
| 193 | 'http://www.googleapis.com/next-link?key=foobie_bletch') |
| 194 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 195 | |
| 196 | if __name__ == '__main__': |
| 197 | unittest.main() |