Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 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 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 21 | from __future__ import absolute_import |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 22 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 23 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 24 | |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 25 | import httplib2 |
| 26 | import os |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 27 | import unittest2 as unittest |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 28 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 29 | from googleapiclient.errors import HttpError |
| 30 | from googleapiclient.errors import UnexpectedBodyError |
| 31 | from googleapiclient.errors import UnexpectedMethodError |
| 32 | from googleapiclient.discovery import build |
| 33 | from googleapiclient.http import RequestMockBuilder |
| 34 | from googleapiclient.http import HttpMock |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 35 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 36 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 37 | DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
| 38 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 39 | |
| 40 | def datafile(filename): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 41 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 42 | |
| 43 | |
| 44 | class Mocks(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 45 | def setUp(self): |
| 46 | self.http = HttpMock(datafile("plus.json"), {"status": "200"}) |
| 47 | self.zoo_http = HttpMock(datafile("zoo.json"), {"status": "200"}) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 48 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 49 | def test_default_response(self): |
| 50 | requestBuilder = RequestMockBuilder({}) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 51 | plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 52 | activity = plus.activities().get(activityId="tag:blah").execute() |
| 53 | self.assertEqual({}, activity) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 54 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 55 | def test_simple_response(self): |
| 56 | requestBuilder = RequestMockBuilder( |
| 57 | {"plus.activities.get": (None, '{"foo": "bar"}')} |
| 58 | ) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 59 | plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 60 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 61 | activity = plus.activities().get(activityId="tag:blah").execute() |
| 62 | self.assertEqual({"foo": "bar"}, activity) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 63 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 64 | def test_unexpected_call(self): |
| 65 | requestBuilder = RequestMockBuilder({}, check_unexpected=True) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 66 | |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 67 | plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 68 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 69 | try: |
| 70 | plus.activities().get(activityId="tag:blah").execute() |
| 71 | self.fail("UnexpectedMethodError should have been raised") |
| 72 | except UnexpectedMethodError: |
| 73 | pass |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 74 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 75 | def test_simple_unexpected_body(self): |
| 76 | requestBuilder = RequestMockBuilder( |
| 77 | {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', None)} |
| 78 | ) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 79 | zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 80 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 81 | try: |
| 82 | zoo.animals().insert(body="{}").execute() |
| 83 | self.fail("UnexpectedBodyError should have been raised") |
| 84 | except UnexpectedBodyError: |
| 85 | pass |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 86 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 87 | def test_simple_expected_body(self): |
| 88 | requestBuilder = RequestMockBuilder( |
| 89 | {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', "{}")} |
| 90 | ) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 91 | zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 92 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 93 | try: |
| 94 | zoo.animals().insert(body="").execute() |
| 95 | self.fail("UnexpectedBodyError should have been raised") |
| 96 | except UnexpectedBodyError: |
| 97 | pass |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 98 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 99 | 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 Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 109 | zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 110 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 111 | try: |
| 112 | zoo.animals().insert(body='{"data": {"foo": "blah"}}').execute() |
| 113 | self.fail("UnexpectedBodyError should have been raised") |
| 114 | except UnexpectedBodyError: |
| 115 | pass |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 116 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 117 | 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 Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 127 | zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 128 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 129 | activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute() |
| 130 | self.assertEqual({"foo": "bar"}, activity) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 131 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 132 | 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 Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 142 | zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 143 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 144 | activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute() |
| 145 | self.assertEqual({"foo": "bar"}, activity) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 146 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 147 | def test_errors(self): |
| 148 | errorResponse = httplib2.Response({"status": 500, "reason": "Server Error"}) |
| 149 | requestBuilder = RequestMockBuilder( |
| 150 | {"plus.activities.list": (errorResponse, b"{}")} |
| 151 | ) |
Anthonios Partheniou | 32d1c59 | 2021-01-14 18:48:59 -0500 | [diff] [blame] | 152 | plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder, static_discovery=False) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 153 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 154 | 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 Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 163 | |
| 164 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 165 | if __name__ == "__main__": |
| 166 | unittest.main() |