blob: 374e57ca2260703c57dacddd65deb8bb87266507 [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 Gregorioc5c5a372010-09-22 11:42:32 -040025from apiclient.discovery import build, key2param
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040026import httplib2
27import os
28import unittest
29
30DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
31
32class 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 Gregorioc5c5a372010-09-22 11:42:32 -040044class 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 Gregorioba9ea7f2010-08-19 15:49:04 -040050class 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.com850cf552010-08-20 23:24:56 +010083 def test_buzz_resources(self):
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040084 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 Gregorioc5c5a372010-09-22 11:42:32 -040095 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
102class Next(unittest.TestCase):
ade@google.com2ab0de72010-09-27 23:26:54 +0100103 def test_next_for_activities_list(self):
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400104 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.com2ab0de72010-09-27 23:26:54 +0100112 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 Gregorioba9ea7f2010-08-19 15:49:04 -0400120
121if __name__ == '__main__':
122 unittest.main()