blob: 34cc8a9747ed5f8e88cc633b4cd6c67dd4f15282 [file] [log] [blame]
Craig Citro15744b12015-03-02 13:34:32 -08001#!/usr/bin/env python
Joe Gregoriod27ae3e2010-12-09 15:01:27 -05002#
Craig Citro751b7fb2014-09-23 11:20:38 -07003# Copyright 2014 Google Inc. All Rights Reserved.
Joe Gregoriod27ae3e2010-12-09 15:01:27 -05004#
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"""
INADA Naokid898a372015-03-04 03:52:46 +090021from __future__ import absolute_import
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050022
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregorioe1de4162011-02-23 11:30:29 -050025import httplib2
26import os
27import unittest
28
John Asmuth864311d2014-04-24 15:46:08 -040029from googleapiclient.errors import HttpError
30from googleapiclient.errors import UnexpectedBodyError
31from googleapiclient.errors import UnexpectedMethodError
32from googleapiclient.discovery import build
33from googleapiclient.http import RequestMockBuilder
34from googleapiclient.http import HttpMock
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050035
Joe Gregoriocb8103d2011-02-11 23:20:52 -050036
37DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
38
39def datafile(filename):
40 return os.path.join(DATA_DIR, filename)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050041
42
43class Mocks(unittest.TestCase):
44 def setUp(self):
Joe Gregorio7b70f432011-11-09 10:18:51 -050045 self.http = HttpMock(datafile('plus.json'), {'status': '200'})
46 self.zoo_http = HttpMock(datafile('zoo.json'), {'status': '200'})
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050047
48 def test_default_response(self):
49 requestBuilder = RequestMockBuilder({})
Joe Gregorio7b70f432011-11-09 10:18:51 -050050 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
51 activity = plus.activities().get(activityId='tag:blah').execute()
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050052 self.assertEqual({}, activity)
53
54 def test_simple_response(self):
55 requestBuilder = RequestMockBuilder({
Ali Afshar81fde8e2012-10-23 11:14:28 -070056 'plus.activities.get': (None, '{"foo": "bar"}')
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050057 })
Joe Gregorio7b70f432011-11-09 10:18:51 -050058 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050059
Joe Gregorio7b70f432011-11-09 10:18:51 -050060 activity = plus.activities().get(activityId='tag:blah').execute()
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050061 self.assertEqual({"foo": "bar"}, activity)
62
Joe Gregorioa388ce32011-09-09 17:19:13 -040063 def test_unexpected_call(self):
64 requestBuilder = RequestMockBuilder({}, check_unexpected=True)
65
Joe Gregorio7b70f432011-11-09 10:18:51 -050066 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -040067
68 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -050069 plus.activities().get(activityId='tag:blah').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -040070 self.fail('UnexpectedMethodError should have been raised')
71 except UnexpectedMethodError:
72 pass
73
74 def test_simple_unexpected_body(self):
75 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -050076 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', None)
Joe Gregorioa388ce32011-09-09 17:19:13 -040077 })
Joe Gregorio7b70f432011-11-09 10:18:51 -050078 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -040079
80 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -050081 zoo.animals().insert(body='{}').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -040082 self.fail('UnexpectedBodyError should have been raised')
83 except UnexpectedBodyError:
84 pass
85
86 def test_simple_expected_body(self):
87 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -050088 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', '{}')
Joe Gregorioa388ce32011-09-09 17:19:13 -040089 })
Joe Gregorio7b70f432011-11-09 10:18:51 -050090 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -040091
92 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -050093 zoo.animals().insert(body='').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -040094 self.fail('UnexpectedBodyError should have been raised')
95 except UnexpectedBodyError:
96 pass
97
98 def test_simple_wrong_body(self):
99 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500100 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
Joe Gregorioa388ce32011-09-09 17:19:13 -0400101 '{"data": {"foo": "bar"}}')
102 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500103 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400104
105 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500106 zoo.animals().insert(
107 body='{"data": {"foo": "blah"}}').execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -0400108 self.fail('UnexpectedBodyError should have been raised')
109 except UnexpectedBodyError:
110 pass
111
112 def test_simple_matching_str_body(self):
113 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500114 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
Joe Gregorioa388ce32011-09-09 17:19:13 -0400115 '{"data": {"foo": "bar"}}')
116 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500117 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400118
Joe Gregorio7b70f432011-11-09 10:18:51 -0500119 activity = zoo.animals().insert(
120 body={'data': {'foo': 'bar'}}).execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -0400121 self.assertEqual({'foo': 'bar'}, activity)
122
123 def test_simple_matching_dict_body(self):
124 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500125 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}',
Joe Gregorioa388ce32011-09-09 17:19:13 -0400126 {'data': {'foo': 'bar'}})
127 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500128 zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400129
Joe Gregorio7b70f432011-11-09 10:18:51 -0500130 activity = zoo.animals().insert(
131 body={'data': {'foo': 'bar'}}).execute()
Joe Gregorioa388ce32011-09-09 17:19:13 -0400132 self.assertEqual({'foo': 'bar'}, activity)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500133
134 def test_errors(self):
135 errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'})
136 requestBuilder = RequestMockBuilder({
Joe Gregorio7b70f432011-11-09 10:18:51 -0500137 'plus.activities.list': (errorResponse, '{}')
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500138 })
Joe Gregorio7b70f432011-11-09 10:18:51 -0500139 plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500140
141 try:
Joe Gregorio7b70f432011-11-09 10:18:51 -0500142 activity = plus.activities().list(collection='public', userId='me').execute()
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500143 self.fail('An exception should have been thrown')
INADA Naokic1505df2014-08-20 15:19:53 +0900144 except HttpError as e:
Joe Gregoriod4e14562011-01-04 09:51:45 -0500145 self.assertEqual('{}', e.content)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500146 self.assertEqual(500, e.resp.status)
147 self.assertEqual('Server Error', e.resp.reason)
148
149
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500150if __name__ == '__main__':
151 unittest.main()