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