blob: f2da9d966cdd6adb32963d9303cd369d0adfe7ed [file] [log] [blame]
Joe Gregoriod27ae3e2010-12-09 15:01:27 -05001#!/usr/bin/python2.4
2#
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
17"""Mock tests
18
19Unit tests for the Mocks.
20"""
21
22__author__ = 'jcgregorio@google.com (Joe Gregorio)'
23
Joe Gregorioe1de4162011-02-23 11:30:29 -050024import httplib2
25import os
26import unittest
27
Joe Gregorio3fada332011-01-07 17:07:45 -050028from apiclient.errors import HttpError
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050029from apiclient.discovery import build
30from apiclient.http import RequestMockBuilder
Joe Gregoriocb8103d2011-02-11 23:20:52 -050031from apiclient.http import HttpMock
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050032
Joe Gregoriocb8103d2011-02-11 23:20:52 -050033
34DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
35
36def datafile(filename):
37 return os.path.join(DATA_DIR, filename)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050038
39
40class Mocks(unittest.TestCase):
41 def setUp(self):
Joe Gregoriocb8103d2011-02-11 23:20:52 -050042 self.http = HttpMock(datafile('buzz.json'), {'status': '200'})
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050043
44 def test_default_response(self):
45 requestBuilder = RequestMockBuilder({})
46 buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder)
47 activity = buzz.activities().get(postId='tag:blah', userId='@me').execute()
48 self.assertEqual({}, activity)
49
50 def test_simple_response(self):
51 requestBuilder = RequestMockBuilder({
52 'chili.activities.get': (None, '{"data": {"foo": "bar"}}')
53 })
54 buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder)
55
56 activity = buzz.activities().get(postId='tag:blah', userId='@me').execute()
57 self.assertEqual({"foo": "bar"}, activity)
58
59
60 def test_errors(self):
61 errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
62 requestBuilder = RequestMockBuilder({
63 'chili.activities.list': (errorResponse, '{}')
64 })
65 buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder)
66
67 try:
68 activity = buzz.activities().list(scope='@self', userId='@me').execute()
69 self.fail('An exception should have been thrown')
70 except HttpError, e:
Joe Gregoriod4e14562011-01-04 09:51:45 -050071 self.assertEqual('{}', e.content)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050072 self.assertEqual(500, e.resp.status)
73 self.assertEqual('Server Error', e.resp.reason)
74
75
76
77
78if __name__ == '__main__':
79 unittest.main()