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 19Unit tests for the Mocks. 20""" 21from __future__ import absolute_import 22 23__author__ = "jcgregorio@google.com (Joe Gregorio)" 24 25import httplib2 26import os 27import unittest2 as unittest 28 29from 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 35 36 37DATA_DIR = os.path.join(os.path.dirname(__file__), "data") 38 39 40def datafile(filename): 41 return os.path.join(DATA_DIR, filename) 42 43 44class Mocks(unittest.TestCase): 45 def setUp(self): 46 self.http = HttpMock(datafile("plus.json"), {"status": "200"}) 47 self.zoo_http = HttpMock(datafile("zoo.json"), {"status": "200"}) 48 49 def test_default_response(self): 50 requestBuilder = RequestMockBuilder({}) 51 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder) 52 activity = plus.activities().get(activityId="tag:blah").execute() 53 self.assertEqual({}, activity) 54 55 def test_simple_response(self): 56 requestBuilder = RequestMockBuilder( 57 {"plus.activities.get": (None, '{"foo": "bar"}')} 58 ) 59 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder) 60 61 activity = plus.activities().get(activityId="tag:blah").execute() 62 self.assertEqual({"foo": "bar"}, activity) 63 64 def test_unexpected_call(self): 65 requestBuilder = RequestMockBuilder({}, check_unexpected=True) 66 67 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder) 68 69 try: 70 plus.activities().get(activityId="tag:blah").execute() 71 self.fail("UnexpectedMethodError should have been raised") 72 except UnexpectedMethodError: 73 pass 74 75 def test_simple_unexpected_body(self): 76 requestBuilder = RequestMockBuilder( 77 {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', None)} 78 ) 79 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder) 80 81 try: 82 zoo.animals().insert(body="{}").execute() 83 self.fail("UnexpectedBodyError should have been raised") 84 except UnexpectedBodyError: 85 pass 86 87 def test_simple_expected_body(self): 88 requestBuilder = RequestMockBuilder( 89 {"zoo.animals.insert": (None, '{"data": {"foo": "bar"}}', "{}")} 90 ) 91 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder) 92 93 try: 94 zoo.animals().insert(body="").execute() 95 self.fail("UnexpectedBodyError should have been raised") 96 except UnexpectedBodyError: 97 pass 98 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 ) 109 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder) 110 111 try: 112 zoo.animals().insert(body='{"data": {"foo": "blah"}}').execute() 113 self.fail("UnexpectedBodyError should have been raised") 114 except UnexpectedBodyError: 115 pass 116 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 ) 127 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder) 128 129 activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute() 130 self.assertEqual({"foo": "bar"}, activity) 131 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 ) 142 zoo = build("zoo", "v1", http=self.zoo_http, requestBuilder=requestBuilder) 143 144 activity = zoo.animals().insert(body={"data": {"foo": "bar"}}).execute() 145 self.assertEqual({"foo": "bar"}, activity) 146 147 def test_errors(self): 148 errorResponse = httplib2.Response({"status": 500, "reason": "Server Error"}) 149 requestBuilder = RequestMockBuilder( 150 {"plus.activities.list": (errorResponse, b"{}")} 151 ) 152 plus = build("plus", "v1", http=self.http, requestBuilder=requestBuilder) 153 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) 163 164 165if __name__ == "__main__": 166 unittest.main() 167