blob: 541210c87b713f9d84b638a8639e187c7b256bc6 [file] [log] [blame]
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001#!/usr/bin/python2.4
2#
Joe Gregorio6d5e94f2010-08-25 23:49:30 -04003# 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 Gregorioba9ea7f2010-08-19 15:49:04 -040017
18"""Discovery document tests
19
20Unit tests for objects created from discovery documents.
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040025import httplib2
26import os
27import unittest
Joe Gregorio00cf1d92010-09-27 09:22:03 -040028import urlparse
ade@google.comc5eb46f2010-09-27 23:35:39 +010029try:
30 from urlparse import parse_qs
31except ImportError:
32 from cgi import parse_qs
Joe Gregorio00cf1d92010-09-27 09:22:03 -040033
34from apiclient.discovery import build, key2param
Joe Gregorioaf276d22010-12-09 14:26:58 -050035from tests.util import HttpMock
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040036
37
Joe Gregorioc5c5a372010-09-22 11:42:32 -040038class 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 Gregorioba9ea7f2010-08-19 15:49:04 -040044class Discovery(unittest.TestCase):
Joe Gregorio2379ecc2010-10-26 10:51:28 -040045
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040046 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.com850cf552010-08-20 23:24:56 +010078 def test_buzz_resources(self):
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040079 self.http = HttpMock('buzz.json', {'status': '200'})
80 buzz = build('buzz', 'v1', self.http)
81 self.assertTrue(getattr(buzz, 'activities'))
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040082 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 Gregorioc5c5a372010-09-22 11:42:32 -040089 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 Gregorio2379ecc2010-10-26 10:51:28 -040095 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
Joe Gregorio3fada332011-01-07 17:07:45 -0500107 def test_nested_resources(self):
108 self.http = HttpMock('zoo.json', {'status': '200'})
109 zoo = build('zoo', 'v1', self.http)
110 self.assertTrue(getattr(zoo, 'animals'))
111 request = zoo.my().favorites().list(max_results="5")
112 parsed = urlparse.urlparse(request.uri)
113 q = parse_qs(parsed[4])
114 self.assertEqual(q['max-results'], ['5'])
115
Joe Gregorio7a6df3a2011-01-31 21:55:21 -0500116 def test_top_level_functions(self):
117 self.http = HttpMock('zoo.json', {'status': '200'})
118 zoo = build('zoo', 'v1', self.http)
119 self.assertTrue(getattr(zoo, 'query'))
120 request = zoo.query(q="foo")
121 parsed = urlparse.urlparse(request.uri)
122 q = parse_qs(parsed[4])
123 self.assertEqual(q['q'], ['foo'])
Joe Gregorio2379ecc2010-10-26 10:51:28 -0400124
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400125
126class Next(unittest.TestCase):
Joe Gregorioc9359072010-10-25 15:26:13 -0400127 def test_next_for_people_liked(self):
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400128 self.http = HttpMock('buzz.json', {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400129 buzz = build('buzz', 'v1', self.http)
130 people = {'links':
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400131 {'next':
132 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400133 request = buzz.people().liked_next(people)
134 self.assertEqual(request.uri, 'http://www.googleapis.com/next-link')
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400135
136
137class DeveloperKey(unittest.TestCase):
138 def test_param(self):
139 self.http = HttpMock('buzz.json', {'status': '200'})
140 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
141 activities = {'links':
142 {'next':
143 [{'href': 'http://www.googleapis.com/next-link'}]}}
144 request = buzz.activities().list_next(activities)
145 parsed = urlparse.urlparse(request.uri)
ade@google.comc5eb46f2010-09-27 23:35:39 +0100146 q = parse_qs(parsed[4])
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400147 self.assertEqual(q['key'], ['foobie_bletch'])
148
Joe Gregorioc9359072010-10-25 15:26:13 -0400149 def test_next_for_activities_list(self):
ade@google.com2ab0de72010-09-27 23:26:54 +0100150 self.http = HttpMock('buzz.json', {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400151 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
152 activities = {'links':
ade@google.com2ab0de72010-09-27 23:26:54 +0100153 {'next':
154 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400155 request = buzz.activities().list_next(activities)
156 self.assertEqual(request.uri,
157 'http://www.googleapis.com/next-link?key=foobie_bletch')
158
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400159
160if __name__ == '__main__':
161 unittest.main()