blob: 8704e92533fdecb5b20a2ef9343ee31df9bdb831 [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 Gregorioba9ea7f2010-08-19 15:49:04 -040035
36DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
37
38class 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 Gregorioc5c5a372010-09-22 11:42:32 -040050class 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 Gregorioba9ea7f2010-08-19 15:49:04 -040056class 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.com850cf552010-08-20 23:24:56 +010089 def test_buzz_resources(self):
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040090 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 Gregorioc5c5a372010-09-22 11:42:32 -0400101 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
108class Next(unittest.TestCase):
ade@google.com2ab0de72010-09-27 23:26:54 +0100109 def test_next_for_activities_list(self):
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400110 self.http = HttpMock('buzz.json', {'status': '200'})
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400111 buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch')
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400112 activities = {'links':
113 {'next':
114 [{'href': 'http://www.googleapis.com/next-link'}]}}
115 request = buzz.activities().list_next(activities)
Joe Gregorio00cf1d92010-09-27 09:22:03 -0400116 self.assertEqual(request.uri,
117 'http://www.googleapis.com/next-link?key=foobie_bletch')
118
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 Gregorioc5c5a372010-09-22 11:42:32 -0400132
ade@google.com2ab0de72010-09-27 23:26:54 +0100133 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 Gregorioba9ea7f2010-08-19 15:49:04 -0400141
142if __name__ == '__main__':
143 unittest.main()