Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. |
| 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 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 23 | |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 24 | import httplib2 |
| 25 | import os |
| 26 | import unittest |
| 27 | |
Joe Gregorio | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 28 | from apiclient.errors import HttpError |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 29 | from apiclient.errors import UnexpectedBodyError |
| 30 | from apiclient.errors import UnexpectedMethodError |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 31 | from apiclient.discovery import build |
| 32 | from apiclient.http import RequestMockBuilder |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 33 | from apiclient.http import HttpMock |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 34 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 35 | |
| 36 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 37 | |
| 38 | def datafile(filename): |
| 39 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class Mocks(unittest.TestCase): |
| 43 | def setUp(self): |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 44 | self.http = HttpMock(datafile('plus.json'), {'status': '200'}) |
| 45 | self.zoo_http = HttpMock(datafile('zoo.json'), {'status': '200'}) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 46 | |
| 47 | def test_default_response(self): |
| 48 | requestBuilder = RequestMockBuilder({}) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 49 | plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder) |
| 50 | activity = plus.activities().get(activityId='tag:blah').execute() |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 51 | self.assertEqual({}, activity) |
| 52 | |
| 53 | def test_simple_response(self): |
| 54 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 55 | 'plus.activities.get': (None, '{"data": {"foo": "bar"}}') |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 56 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 57 | plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 58 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 59 | activity = plus.activities().get(activityId='tag:blah').execute() |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 60 | self.assertEqual({"foo": "bar"}, activity) |
| 61 | |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 62 | def test_unexpected_call(self): |
| 63 | requestBuilder = RequestMockBuilder({}, check_unexpected=True) |
| 64 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 65 | plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 66 | |
| 67 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 68 | plus.activities().get(activityId='tag:blah').execute() |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 69 | self.fail('UnexpectedMethodError should have been raised') |
| 70 | except UnexpectedMethodError: |
| 71 | pass |
| 72 | |
| 73 | def test_simple_unexpected_body(self): |
| 74 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 75 | 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', None) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 76 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 77 | zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 78 | |
| 79 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 80 | zoo.animals().insert(body='{}').execute() |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 81 | self.fail('UnexpectedBodyError should have been raised') |
| 82 | except UnexpectedBodyError: |
| 83 | pass |
| 84 | |
| 85 | def test_simple_expected_body(self): |
| 86 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 87 | 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', '{}') |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 88 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 89 | zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 90 | |
| 91 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 92 | zoo.animals().insert(body='').execute() |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 93 | self.fail('UnexpectedBodyError should have been raised') |
| 94 | except UnexpectedBodyError: |
| 95 | pass |
| 96 | |
| 97 | def test_simple_wrong_body(self): |
| 98 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 99 | 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 100 | '{"data": {"foo": "bar"}}') |
| 101 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 102 | zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 103 | |
| 104 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 105 | zoo.animals().insert( |
| 106 | body='{"data": {"foo": "blah"}}').execute() |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 107 | self.fail('UnexpectedBodyError should have been raised') |
| 108 | except UnexpectedBodyError: |
| 109 | pass |
| 110 | |
| 111 | def test_simple_matching_str_body(self): |
| 112 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 113 | 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 114 | '{"data": {"foo": "bar"}}') |
| 115 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 116 | zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 117 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 118 | activity = zoo.animals().insert( |
| 119 | body={'data': {'foo': 'bar'}}).execute() |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 120 | self.assertEqual({'foo': 'bar'}, activity) |
| 121 | |
| 122 | def test_simple_matching_dict_body(self): |
| 123 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 124 | 'zoo.animals.insert': (None, '{"data": {"foo": "bar"}}', |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 125 | {'data': {'foo': 'bar'}}) |
| 126 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 127 | zoo = build('zoo', 'v1', http=self.zoo_http, requestBuilder=requestBuilder) |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 128 | |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 129 | activity = zoo.animals().insert( |
| 130 | body={'data': {'foo': 'bar'}}).execute() |
Joe Gregorio | a388ce3 | 2011-09-09 17:19:13 -0400 | [diff] [blame] | 131 | self.assertEqual({'foo': 'bar'}, activity) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 132 | |
| 133 | def test_errors(self): |
| 134 | errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'}) |
| 135 | requestBuilder = RequestMockBuilder({ |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 136 | 'plus.activities.list': (errorResponse, '{}') |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 137 | }) |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 138 | plus = build('plus', 'v1', http=self.http, requestBuilder=requestBuilder) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 139 | |
| 140 | try: |
Joe Gregorio | 7b70f43 | 2011-11-09 10:18:51 -0500 | [diff] [blame] | 141 | activity = plus.activities().list(collection='public', userId='me').execute() |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 142 | self.fail('An exception should have been thrown') |
| 143 | except HttpError, e: |
Joe Gregorio | d4e1456 | 2011-01-04 09:51:45 -0500 | [diff] [blame] | 144 | self.assertEqual('{}', e.content) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 145 | self.assertEqual(500, e.resp.status) |
| 146 | self.assertEqual('Server Error', e.resp.reason) |
| 147 | |
| 148 | |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 149 | if __name__ == '__main__': |
| 150 | unittest.main() |