blob: 6822335dbdc6355a6d56592035cea19799dfb24c [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 Gregorioa388ce32011-09-09 17:19:13 -040029from apiclient.errors import UnexpectedBodyError
30from apiclient.errors import UnexpectedMethodError
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050031from apiclient.discovery import build
32from apiclient.http import RequestMockBuilder
Joe Gregoriocb8103d2011-02-11 23:20:52 -050033from apiclient.http import HttpMock
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050034
Joe Gregoriocb8103d2011-02-11 23:20:52 -050035
36DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
37
38def datafile(filename):
39 return os.path.join(DATA_DIR, filename)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050040
41
42class Mocks(unittest.TestCase):
43 def setUp(self):
Joe Gregorio7b70f432011-11-09 10:18:51 -050044 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
45 self.zoo_http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050046
47 def test_default_response(self):
48 requestBuilder = RequestMockBuilder({})
Joe Gregorio7b70f432011-11-09 10:18:51 -050049 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
50 activity = plus.activities().get(activityId='tag:blah').execute()
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050051 self.assertEqual({}, activity)
52
53 def test_simple_response(self):
54 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -050055 'plus.activities.get': (None, '{"data": {"foo": "bar"}}')
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050056 })
Joe Gregorio7b70f432011-11-09 10:18:51 -050057 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050058
Joe Gregorio7b70f432011-11-09 10:18:51 -050059 activity = plus.activities().get(activityId='tag:blah').execute()
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050060 self.assertEqual({"foo": "bar"}, activity)
61
Joe Gregorioa388ce32011-09-09 17:19:13 -040062 def test_unexpected_call(self):
63 requestBuilder = RequestMockBuilder({}, check_unexpected=True)
64
Joe Gregorio7b70f432011-11-09 10:18:51 -050065 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -040066
67 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -050068 plus.activities().get(activityId='tag:blah').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -040069 self.fail('UnexpectedMethodError should have been raised')
70 except UnexpectedMethodError:
71 pass
72
73 def test_simple_unexpected_body(self):
74 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -050075 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', None)
Joe Gregorioa388ce32011-09-09 17:19:13 -040076 })
Joe Gregorio7b70f432011-11-09 10:18:51 -050077 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -040078
79 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -050080 zoo.animals().insert(body='{}').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -040081 self.fail('UnexpectedBodyError should have been raised')
82 except UnexpectedBodyError:
83 pass
84
85 def test_simple_expected_body(self):
86 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -050087 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', '{}')
Joe Gregorioa388ce32011-09-09 17:19:13 -040088 })
Joe Gregorio7b70f432011-11-09 10:18:51 -050089 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -040090
91 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -050092 zoo.animals().insert(body='').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -040093 self.fail('UnexpectedBodyError should have been raised')
94 except UnexpectedBodyError:
95 pass
96
97 def test_simple_wrong_body(self):
98 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -050099 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
Joe Gregorioa388ce32011-09-09 17:19:13 -0400100 '{"data": {"foo": "bar"}}')
101 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500102 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400103
104 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500105 zoo.animals().insert(
106 body='{"data": {"foo": "blah"}}').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -0400107 self.fail('UnexpectedBodyError should have been raised')
108 except UnexpectedBodyError:
109 pass
110
111 def test_simple_matching_str_body(self):
112 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500113 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
Joe Gregorioa388ce32011-09-09 17:19:13 -0400114 '{"data": {"foo": "bar"}}')
115 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500116 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400117
Joe Gregorio7b70f432011-11-09 10:18:51 -0500118 activity = zoo.animals().insert(
119 body={'data': {'foo': 'bar'}}).execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -0400120 self.assertEqual({'foo': 'bar'}, activity)
121
122 def test_simple_matching_dict_body(self):
123 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500124 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
Joe Gregorioa388ce32011-09-09 17:19:13 -0400125 {'data': {'foo': 'bar'}})
126 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500127 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400128
Joe Gregorio7b70f432011-11-09 10:18:51 -0500129 activity = zoo.animals().insert(
130 body={'data': {'foo': 'bar'}}).execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -0400131 self.assertEqual({'foo': 'bar'}, activity)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500132
133 def test_errors(self):
134 errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
135 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500136 'plus.activities.list': (errorResponse, '{}')
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500137 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500138 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500139
140 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500141 activity = plus.activities().list(collection='public', userId='me').execute()
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500142 self.fail('An exception should have been thrown')
143 except HttpError, e:
Joe Gregoriod4e14562011-01-04 09:51:45 -0500144 self.assertEqual('{}', e.content)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500145 self.assertEqual(500, e.resp.status)
146 self.assertEqual('Server Error', e.resp.reason)
147
148
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500149if __name__ == '__main__':
150 unittest.main()