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 | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 25 | from apiclient.discovery import build, key2param |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 26 | import httplib2 |
| 27 | import os |
| 28 | import unittest |
| 29 | |
| 30 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 31 | |
| 32 | class HttpMock(object): |
| 33 | |
| 34 | def __init__(self, filename, headers): |
| 35 | f = file(os.path.join(DATA_DIR, filename), 'r') |
| 36 | self.data = f.read() |
| 37 | f.close() |
| 38 | self.headers = headers |
| 39 | |
| 40 | def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): |
| 41 | return httplib2.Response(self.headers), self.data |
| 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): |
| 51 | def test_method_error_checking(self): |
| 52 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 53 | buzz = build('buzz', 'v1', self.http) |
| 54 | |
| 55 | # Missing required parameters |
| 56 | try: |
| 57 | buzz.activities().list() |
| 58 | self.fail() |
| 59 | except TypeError, e: |
| 60 | self.assertTrue('Missing' in str(e)) |
| 61 | |
| 62 | # Parameter doesn't match regex |
| 63 | try: |
| 64 | buzz.activities().list(scope='@self', userId='') |
| 65 | self.fail() |
| 66 | except TypeError, e: |
| 67 | self.assertTrue('does not match' in str(e)) |
| 68 | |
| 69 | # Parameter doesn't match regex |
| 70 | try: |
| 71 | buzz.activities().list(scope='not@', userId='foo') |
| 72 | self.fail() |
| 73 | except TypeError, e: |
| 74 | self.assertTrue('does not match' in str(e)) |
| 75 | |
| 76 | # Unexpected parameter |
| 77 | try: |
| 78 | buzz.activities().list(flubber=12) |
| 79 | self.fail() |
| 80 | except TypeError, e: |
| 81 | self.assertTrue('unexpected' in str(e)) |
| 82 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 83 | def test_buzz_resources(self): |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 84 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 85 | buzz = build('buzz', 'v1', self.http) |
| 86 | self.assertTrue(getattr(buzz, 'activities')) |
| 87 | self.assertTrue(getattr(buzz, 'search')) |
| 88 | self.assertTrue(getattr(buzz, 'feeds')) |
| 89 | self.assertTrue(getattr(buzz, 'photos')) |
| 90 | self.assertTrue(getattr(buzz, 'people')) |
| 91 | self.assertTrue(getattr(buzz, 'groups')) |
| 92 | self.assertTrue(getattr(buzz, 'comments')) |
| 93 | self.assertTrue(getattr(buzz, 'related')) |
| 94 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 95 | def test_auth(self): |
| 96 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 97 | buzz = build('buzz', 'v1', self.http) |
| 98 | auth = buzz.auth_discovery() |
| 99 | self.assertTrue('request' in auth) |
| 100 | |
| 101 | |
| 102 | class Next(unittest.TestCase): |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame^] | 103 | def test_next_for_activities_list(self): |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 104 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 105 | buzz = build('buzz', 'v1', self.http) |
| 106 | activities = {'links': |
| 107 | {'next': |
| 108 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 109 | request = buzz.activities().list_next(activities) |
| 110 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
| 111 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame^] | 112 | def test_next_for_people_liked(self): |
| 113 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 114 | buzz = build('buzz', 'v1', self.http) |
| 115 | people = {'links': |
| 116 | {'next': |
| 117 | [{'href': 'http://www.googleapis.com/next-link'}]}} |
| 118 | request = buzz.people().liked_next(people) |
| 119 | self.assertEqual(request.uri, 'http://www.googleapis.com/next-link') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 120 | |
| 121 | if __name__ == '__main__': |
| 122 | unittest.main() |