blob: 91450ee6ba119c61e0057de8b710129a088cbeda [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
24from apiclient.discovery import HttpError
25from apiclient.discovery import build
26from apiclient.http import RequestMockBuilder
27from tests.util import HttpMock
28
29import unittest
30import httplib2
31
32
33class Mocks(unittest.TestCase):
34 def setUp(self):
35 self.http = HttpMock('buzz.json', {'status': '200'})
36
37 def test_default_response(self):
38 requestBuilder = RequestMockBuilder({})
39 buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder)
40 activity = buzz.activities().get(postId='tag:blah', userId='@me').execute()
41 self.assertEqual({}, activity)
42
43 def test_simple_response(self):
44 requestBuilder = RequestMockBuilder({
45 'chili.activities.get': (None, '{"data": {"foo": "bar"}}')
46 })
47 buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder)
48
49 activity = buzz.activities().get(postId='tag:blah', userId='@me').execute()
50 self.assertEqual({"foo": "bar"}, activity)
51
52
53 def test_errors(self):
54 errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
55 requestBuilder = RequestMockBuilder({
56 'chili.activities.list': (errorResponse, '{}')
57 })
58 buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder)
59
60 try:
61 activity = buzz.activities().list(scope='@self', userId='@me').execute()
62 self.fail('An exception should have been thrown')
63 except HttpError, e:
Joe Gregoriod4e14562011-01-04 09:51:45 -050064 self.assertEqual('{}', e.content)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050065 self.assertEqual(500, e.resp.status)
66 self.assertEqual('Server Error', e.resp.reason)
67
68
69
70
71if __name__ == '__main__':
72 unittest.main()