blob: d0a0ff04e06a14652c3ca67c4076920a4ef380b0 [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
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070023__author__ = "jcgregorio@google.com (Joe Gregorio)"
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050024
Joe Gregorioe1de4162011-02-23 11:30:29 -050025import httplib2
26import os
Pat Ferate497a90f2015-03-09 09:52:54 -070027import unittest2 as unittest
Joe Gregorioe1de4162011-02-23 11:30:29 -050028
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
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070037DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
38
Joe Gregoriocb8103d2011-02-11 23:20:52 -050039
40def datafile(filename):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070041 return os.path.join(DATA_DIR, filename)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050042
43
44class Mocks(unittest.TestCase):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070045 def setUp(self):
46 self.http = HttpMock(datafile("plus.json"), {"status": "200"})
47 self.zoo_http = HttpMock(datafile("zoo.json"), {"status": "200"})
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050048
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070049 def test_default_response(self):
50 requestBuilder = RequestMockBuilder({})
Anthonios Partheniou32d1c592021-01-14 18:48:59 -050051 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False)
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070052 activity = plus.activities().get(activityId="tag:blah").execute()
53 self.assertEqual({}, activity)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050054
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070055 def test_simple_response(self):
56 requestBuilder = RequestMockBuilder(
57 {"plus.activities.get": (None, '{"foo": "bar"}')}
58 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -050059 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050060
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070061 activity = plus.activities().get(activityId="tag:blah").execute()
62 self.assertEqual({"foo": "bar"}, activity)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -050063
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070064 def test_unexpected_call(self):
65 requestBuilder = RequestMockBuilder({}, check_unexpected=True)
Joe Gregorioa388ce32011-09-09 17:19:13 -040066
Anthonios Partheniou32d1c592021-01-14 18:48:59 -050067 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregorioa388ce32011-09-09 17:19:13 -040068
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070069 try:
70 plus.activities().get(activityId="tag:blah").execute()
71 self.fail("UnexpectedMethodError should have been raised")
72 except UnexpectedMethodError:
73 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040074
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070075 def test_simple_unexpected_body(self):
76 requestBuilder = RequestMockBuilder(
77 {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', None)}
78 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -050079 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregorioa388ce32011-09-09 17:19:13 -040080
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070081 try:
82 zoo.animals().insert(body="{}").execute()
83 self.fail("UnexpectedBodyError should have been raised")
84 except UnexpectedBodyError:
85 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040086
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070087 def test_simple_expected_body(self):
88 requestBuilder = RequestMockBuilder(
89 {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', "{}")}
90 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -050091 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregorioa388ce32011-09-09 17:19:13 -040092
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070093 try:
94 zoo.animals().insert(body="").execute()
95 self.fail("UnexpectedBodyError should have been raised")
96 except UnexpectedBodyError:
97 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -040098
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070099 def test_simple_wrong_body(self):
100 requestBuilder = RequestMockBuilder(
101 {
102 "zoo.animals.insert": (
103 None,
104 '{"data": {"foo": "bar"}}',
105 '{"data": {"foo": "bar"}}',
106 )
107 }
108 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -0500109 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400110
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700111 try:
112 zoo.animals().insert(body='{"data": {"foo": "blah"}}').execute()
113 self.fail("UnexpectedBodyError should have been raised")
114 except UnexpectedBodyError:
115 pass
Joe Gregorioa388ce32011-09-09 17:19:13 -0400116
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700117 def test_simple_matching_str_body(self):
118 requestBuilder = RequestMockBuilder(
119 {
120 "zoo.animals.insert": (
121 None,
122 '{"data": {"foo": "bar"}}',
123 '{"data": {"foo": "bar"}}',
124 )
125 }
126 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -0500127 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400128
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700129 activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute()
130 self.assertEqual({"foo": "bar"}, activity)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400131
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700132 def test_simple_matching_dict_body(self):
133 requestBuilder = RequestMockBuilder(
134 {
135 "zoo.animals.insert": (
136 None,
137 '{"data": {"foo": "bar"}}',
138 {"data": {"foo": "bar"}},
139 )
140 }
141 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -0500142 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregorioa388ce32011-09-09 17:19:13 -0400143
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700144 activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute()
145 self.assertEqual({"foo": "bar"}, activity)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500146
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700147 def test_errors(self):
148 errorResponse = httplib2.Response({"status": 500, "reason": "Server Error"})
149 requestBuilder = RequestMockBuilder(
150 {"plus.activities.list": (errorResponse, b"{}")}
151 )
Anthonios Partheniou32d1c592021-01-14 18:48:59 -0500152 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500153
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700154 try:
155 activity = (
156 plus.activities().list(collection="public", userId="me").execute()
157 )
158 self.fail("An exception should have been thrown")
159 except HttpError as e:
160 self.assertEqual(b"{}", e.content)
161 self.assertEqual(500, e.resp.status)
162 self.assertEqual("Server Error", e.resp.reason)
Joe Gregoriod27ae3e2010-12-09 15:01:27 -0500163
164
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700165if __name__ == "__main__":
166 unittest.main()