blob: 44dbd6aec062502a9c3dbacc7ffccf02e23cbcd2 [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
107
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400108
109class Next(unittest.TestCase):
Joe Gregorioc9359072010-10-25 15:26:13 -0400110 def test_next_for_people_liked(self):
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400111 self.http = HttpMock('buzz.json', {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400112 buzz = build('buzz', 'v1', self.http)
113 people = {'links':
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400114 {'next':
115 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400116 request = buzz.people().liked_next(people)
117 self.assertEqual(request.uri, 'http://www.googleapis.com/next-link')
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400118
119
120class 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.comc5eb46f2010-09-27 23:35:39 +0100129 q = parse_qs(parsed[4])
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400130 self.assertEqual(q['key'], ['foobie_bletch'])
131
Joe Gregorioc9359072010-10-25 15:26:13 -0400132 def test_next_for_activities_list(self):
ade@google.com2ab0de72010-09-27 23:26:54 +0100133 self.http = HttpMock('buzz.json', {'status': '200'})
Joe Gregorioc9359072010-10-25 15:26:13 -0400134 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
135 activities = {'links':
ade@google.com2ab0de72010-09-27 23:26:54 +0100136 {'next':
137 [{'href': 'http://www.googleapis.com/next-link'}]}}
Joe Gregorioc9359072010-10-25 15:26:13 -0400138 request = buzz.activities().list_next(activities)
139 self.assertEqual(request.uri,
140 'http://www.googleapis.com/next-link?key=foobie_bletch')
141
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400142
143if __name__ == '__main__':
144 unittest.main()