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 | |
| 38 | class HttpMock(object): |
| 39 | |
| 40 | def __init__(self, filename, headers): |
| 41 | f = file(os.path.join(DATA_DIR, filename), 'r') |
| 42 | self.data = f.read() |
| 43 | f.close() |
| 44 | self.headers = headers |
| 45 | |
| 46 | def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): |
| 47 | return httplib2.Response(self.headers), self.data |
| 48 | |
| 49 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 50 | class Utilities(unittest.TestCase): |
| 51 | def test_key2param(self): |
| 52 | self.assertEqual('max_results', key2param('max-results')) |
| 53 | self.assertEqual('x007_bond', key2param('007-bond')) |
| 54 | |
| 55 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 56 | class Discovery(unittest.TestCase): |
| 57 | def test_method_error_checking(self): |
| 58 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 59 | buzz = build('buzz', 'v1', self.http) |
| 60 | |
| 61 | # Missing required parameters |
| 62 | try: |
| 63 | buzz.activities().list() |
| 64 | self.fail() |
| 65 | except TypeError, e: |
| 66 | self.assertTrue('Missing' in str(e)) |
| 67 | |
| 68 | # Parameter doesn't match regex |
| 69 | try: |
| 70 | buzz.activities().list(scope='@self', userId='') |
| 71 | self.fail() |
| 72 | except TypeError, e: |
| 73 | self.assertTrue('does not match' in str(e)) |
| 74 | |
| 75 | # Parameter doesn't match regex |
| 76 | try: |
| 77 | buzz.activities().list(scope='not@', userId='foo') |
| 78 | self.fail() |
| 79 | except TypeError, e: |
| 80 | self.assertTrue('does not match' in str(e)) |
| 81 | |
| 82 | # Unexpected parameter |
| 83 | try: |
| 84 | buzz.activities().list(flubber=12) |
| 85 | self.fail() |
| 86 | except TypeError, e: |
| 87 | self.assertTrue('unexpected' in str(e)) |
| 88 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 89 | def test_buzz_resources(self): |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 90 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 91 | buzz = build('buzz', 'v1', self.http) |
| 92 | self.assertTrue(getattr(buzz, 'activities')) |
| 93 | self.assertTrue(getattr(buzz, 'search')) |
| 94 | self.assertTrue(getattr(buzz, 'feeds')) |
| 95 | self.assertTrue(getattr(buzz, 'photos')) |
| 96 | self.assertTrue(getattr(buzz, 'people')) |
| 97 | self.assertTrue(getattr(buzz, 'groups')) |
| 98 | self.assertTrue(getattr(buzz, 'comments')) |
| 99 | self.assertTrue(getattr(buzz, 'related')) |
| 100 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 101 | def test_auth(self): |
| 102 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 103 | buzz = build('buzz', 'v1', self.http) |
| 104 | auth = buzz.auth_discovery() |
| 105 | self.assertTrue('request' in auth) |
| 106 | |
| 107 | |
| 108 | class Next(unittest.TestCase): |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 109 | def test_next_for_activities_list(self): |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 110 | self.http = HttpMock('buzz.json', {'status': '200'}) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 111 | buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 112 | activities = {'links': |
| 113 | {'next': |
| 114 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 115 | request = buzz.activities().list_next(activities) |
Joe Gregorio | 00cf1d9 | 2010-09-27 09:22:03 -0400 | [diff] [blame] | 116 | self.assertEqual(request.uri, |
| 117 | 'http://www.googleapis.com/next-link?key=foobie_bletch') |
| 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 | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 132 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 133 | def test_next_for_people_liked(self): |
| 134 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 135 | buzz = build('buzz', 'v1', self.http) |
| 136 | people = {'links': |
| 137 | {'next': |
| 138 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 139 | request = buzz.people().liked_next(people) |
| 140 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 141 | |
| 142 | if __name__ == '__main__': |
| 143 | unittest.main() |