Debian Codegraph @

tests/test_mocks.py

raw
1 #!/usr/bin/env python
2 #
3 # Copyright 2014 Google Inc. All Rights Reserved.
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
19 Unit tests for the Mocks.
20 """
21 from __future__ import absolute_import
22
23 __author__ = "[email protected] (Joe Gregorio)"
24
25 import os
26 import unittest
27
28 import httplib2
29
30 from googleapiclient.discovery import build
31 from googleapiclient.errors import HttpError, UnexpectedBodyError, UnexpectedMethodError
32 from googleapiclient.http import HttpMock, RequestMockBuilder
33
34 DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
35
36
37 def datafile(filename):
38 return os.path.join(DATA_DIR, filename)
39
40
41 class Mocks(unittest.TestCase):
42 def setUp(self):
43 self.http = HttpMock(datafile("plus.json"), {"status": "200"})
44 self.zoo_http = HttpMock(datafile("zoo.json"), {"status": "200"})
45
46 def test_default_response(self):
47 requestBuilder = RequestMockBuilder({})
48 plus = build(
49 "plus",
50 "v1",
51 http=self.http,
52 requestBuilder=requestBuilder,
53 static_discovery=False,
54 )
55 activity = plus.activities().get(activityId="tag:blah").execute()
56 self.assertEqual({}, activity)
57
58 def test_simple_response(self):
59 requestBuilder = RequestMockBuilder(
60 {"plus.activities.get": (None, '{"foo": "bar"}')}
61 )
62 plus = build(
63 "plus",
64 "v1",
65 http=self.http,
66 requestBuilder=requestBuilder,
67 static_discovery=False,
68 )
69
70 activity = plus.activities().get(activityId="tag:blah").execute()
71 self.assertEqual({"foo": "bar"}, activity)
72
73 def test_unexpected_call(self):
74 requestBuilder = RequestMockBuilder({}, check_unexpected=True)
75
76 plus = build(
77 "plus",
78 "v1",
79 http=self.http,
80 requestBuilder=requestBuilder,
81 static_discovery=False,
82 )
83
84 try:
85 plus.activities().get(activityId="tag:blah").execute()
86 self.fail("UnexpectedMethodError should have been raised")
87 except UnexpectedMethodError:
88 pass
89
90 def test_simple_unexpected_body(self):
91 requestBuilder = RequestMockBuilder(
92 {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', None)}
93 )
94 zoo = build(
95 "zoo",
96 "v1",
97 http=self.zoo_http,
98 requestBuilder=requestBuilder,
99 static_discovery=False,
100 )
101
102 try:
103 zoo.animals().insert(body="{}").execute()
104 self.fail("UnexpectedBodyError should have been raised")
105 except UnexpectedBodyError:
106 pass
107
108 def test_simple_expected_body(self):
109 requestBuilder = RequestMockBuilder(
110 {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', "{}")}
111 )
112 zoo = build(
113 "zoo",
114 "v1",
115 http=self.zoo_http,
116 requestBuilder=requestBuilder,
117 static_discovery=False,
118 )
119
120 try:
121 zoo.animals().insert(body="").execute()
122 self.fail("UnexpectedBodyError should have been raised")
123 except UnexpectedBodyError:
124 pass
125
126 def test_simple_wrong_body(self):
127 requestBuilder = RequestMockBuilder(
128 {
129 "zoo.animals.insert": (
130 None,
131 '{"data": {"foo": "bar"}}',
132 '{"data": {"foo": "bar"}}',
133 )
134 }
135 )
136 zoo = build(
137 "zoo",
138 "v1",
139 http=self.zoo_http,
140 requestBuilder=requestBuilder,
141 static_discovery=False,
142 )
143
144 try:
145 zoo.animals().insert(body='{"data": {"foo": "blah"}}').execute()
146 self.fail("UnexpectedBodyError should have been raised")
147 except UnexpectedBodyError:
148 pass
149
150 def test_simple_matching_str_body(self):
151 requestBuilder = RequestMockBuilder(
152 {
153 "zoo.animals.insert": (
154 None,
155 '{"data": {"foo": "bar"}}',
156 '{"data": {"foo": "bar"}}',
157 )
158 }
159 )
160 zoo = build(
161 "zoo",
162 "v1",
163 http=self.zoo_http,
164 requestBuilder=requestBuilder,
165 static_discovery=False,
166 )
167
168 activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute()
169 self.assertEqual({"foo": "bar"}, activity)
170
171 def test_simple_matching_dict_body(self):
172 requestBuilder = RequestMockBuilder(
173 {
174 "zoo.animals.insert": (
175 None,
176 '{"data": {"foo": "bar"}}',
177 {"data": {"foo": "bar"}},
178 )
179 }
180 )
181 zoo = build(
182 "zoo",
183 "v1",
184 http=self.zoo_http,
185 requestBuilder=requestBuilder,
186 static_discovery=False,
187 )
188
189 activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute()
190 self.assertEqual({"foo": "bar"}, activity)
191
192 def test_errors(self):
193 errorResponse = httplib2.Response({"status": 500, "reason": "Server Error"})
194 requestBuilder = RequestMockBuilder(
195 {"plus.activities.list": (errorResponse, b"{}")}
196 )
197 plus = build(
198 "plus",
199 "v1",
200 http=self.http,
201 requestBuilder=requestBuilder,
202 static_discovery=False,
203 )
204
205 try:
206 activity = (
207 plus.activities().list(collection="public", userId="me").execute()
208 )
209 self.fail("An exception should have been thrown")
210 except HttpError as e:
211 self.assertEqual(b"{}", e.content)
212 self.assertEqual(500, e.resp.status)
213 self.assertEqual("Server Error", e.resp.reason)
214
215
216 if __name__ == "__main__":
217 unittest.main()