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 | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 35 | |
| 36 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 37 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 38 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 39 | class HttpMock(object): |
| 40 | |
| 41 | def __init__(self, filename, headers): |
| 42 | f = file(os.path.join(DATA_DIR, filename), 'r') |
| 43 | self.data = f.read() |
| 44 | f.close() |
| 45 | self.headers = headers |
| 46 | |
| 47 | def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): |
| 48 | return httplib2.Response(self.headers), self.data |
| 49 | |
| 50 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 51 | class 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 Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 57 | class Discovery(unittest.TestCase): |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 58 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 59 | def test_method_error_checking(self): |
| 60 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 61 | buzz = build('buzz', 'v1', self.http) |
| 62 | |
| 63 | # Missing required parameters |
| 64 | try: |
| 65 | buzz.activities().list() |
| 66 | self.fail() |
| 67 | except TypeError, e: |
| 68 | self.assertTrue('Missing' in str(e)) |
| 69 | |
| 70 | # Parameter doesn't match regex |
| 71 | try: |
| 72 | buzz.activities().list(scope='@self', userId='') |
| 73 | self.fail() |
| 74 | except TypeError, e: |
| 75 | self.assertTrue('does not match' in str(e)) |
| 76 | |
| 77 | # Parameter doesn't match regex |
| 78 | try: |
| 79 | buzz.activities().list(scope='not@', userId='foo') |
| 80 | self.fail() |
| 81 | except TypeError, e: |
| 82 | self.assertTrue('does not match' in str(e)) |
| 83 | |
| 84 | # Unexpected parameter |
| 85 | try: |
| 86 | buzz.activities().list(flubber=12) |
| 87 | self.fail() |
| 88 | except TypeError, e: |
| 89 | self.assertTrue('unexpected' in str(e)) |
| 90 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 91 | def test_buzz_resources(self): |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 92 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 93 | buzz = build('buzz', 'v1', self.http) |
| 94 | self.assertTrue(getattr(buzz, 'activities')) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 95 | self.assertTrue(getattr(buzz, 'feeds')) |
| 96 | self.assertTrue(getattr(buzz, 'photos')) |
| 97 | self.assertTrue(getattr(buzz, 'people')) |
| 98 | self.assertTrue(getattr(buzz, 'groups')) |
| 99 | self.assertTrue(getattr(buzz, 'comments')) |
| 100 | self.assertTrue(getattr(buzz, 'related')) |
| 101 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 102 | def test_auth(self): |
| 103 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 104 | buzz = build('buzz', 'v1', self.http) |
| 105 | auth = buzz.auth_discovery() |
| 106 | self.assertTrue('request' in auth) |
| 107 | |
Joe Gregorio | 2379ecc | 2010-10-26 10:51:28 -0400 | [diff] [blame] | 108 | def test_full_featured(self): |
| 109 | # Zoo should exercise all discovery facets |
| 110 | # and should also have no future.json file. |
| 111 | self.http = HttpMock('zoo.json', {'status': '200'}) |
| 112 | zoo = build('zoo', 'v1', self.http) |
| 113 | self.assertTrue(getattr(zoo, 'animals')) |
| 114 | request = zoo.animals().list(name="bat", projection="size") |
| 115 | parsed = urlparse.urlparse(request.uri) |
| 116 | q = parse_qs(parsed[4]) |
| 117 | self.assertEqual(q['name'], ['bat']) |
| 118 | self.assertEqual(q['projection'], ['size']) |
| 119 | |
| 120 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 121 | |
| 122 | class Next(unittest.TestCase): |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 123 | def test_next_for_people_liked(self): |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 124 | self.http = HttpMock('buzz.json', {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 125 | buzz = build('buzz', 'v1', self.http) |
| 126 | people = {'links': |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 127 | {'next': |
| 128 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 129 | request = buzz.people().liked_next(people) |
| 130 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 131 | |
| 132 | |
| 133 | class DeveloperKey(unittest.TestCase): |
| 134 | def test_param(self): |
| 135 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 136 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 137 | activities = {'links': |
| 138 | {'next': |
| 139 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 140 | request = buzz.activities().list_next(activities) |
| 141 | parsed = urlparse.urlparse(request.uri) |
ade@google.com | c5eb46f | 2010-09-27 23:35:39 +0100 | [diff] [blame] | 142 | q = parse_qs(parsed[4]) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 143 | self.assertEqual(q['key'], ['foobie_bletch']) |
| 144 | |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 145 | def test_next_for_activities_list(self): |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 146 | self.http = HttpMock('buzz.json', {'status': '200'}) |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 147 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
| 148 | activities = {'links': |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 149 | {'next': |
| 150 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 151 | request = buzz.activities().list_next(activities) |
| 152 | self.assertEqual(request.uri, |
| 153 | 'http://www.googleapis.com/next-link?key=foobie_bletch') |
| 154 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 155 | |
| 156 | if __name__ == '__main__': |
| 157 | unittest.main() |