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 | 3fada33 | 2011-01-07 17:07:45 -0500 | [diff] [blame] | 24 | from apiclient.errors import HttpError |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 25 | from apiclient.discovery import build |
| 26 | from apiclient.http import RequestMockBuilder |
| 27 | from tests.util import HttpMock |
| 28 | |
| 29 | import unittest |
| 30 | import httplib2 |
| 31 | |
| 32 | |
| 33 | class Mocks(unittest.TestCase): |
| 34 | def setUp(self): |
| 35 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 36 | |
| 37 | def test_default_response(self): |
| 38 | requestBuilder = RequestMockBuilder({}) |
| 39 | buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder) |
| 40 | activity = buzz.activities().get(postId='tag:blah', userId='@me').execute() |
| 41 | self.assertEqual({}, activity) |
| 42 | |
| 43 | def test_simple_response(self): |
| 44 | requestBuilder = RequestMockBuilder({ |
| 45 | 'chili.activities.get': (None, '{"data": {"foo": "bar"}}') |
| 46 | }) |
| 47 | buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder) |
| 48 | |
| 49 | activity = buzz.activities().get(postId='tag:blah', userId='@me').execute() |
| 50 | self.assertEqual({"foo": "bar"}, activity) |
| 51 | |
| 52 | |
| 53 | def test_errors(self): |
| 54 | errorResponse = httplib2.Response({'status': 500, 'reason': 'Server Error'}) |
| 55 | requestBuilder = RequestMockBuilder({ |
| 56 | 'chili.activities.list': (errorResponse, '{}') |
| 57 | }) |
| 58 | buzz = build('buzz', 'v1', http=self.http, requestBuilder=requestBuilder) |
| 59 | |
| 60 | try: |
| 61 | activity = buzz.activities().list(scope='@self', userId='@me').execute() |
| 62 | self.fail('An exception should have been thrown') |
| 63 | except HttpError, e: |
Joe Gregorio | d4e1456 | 2011-01-04 09:51:45 -0500 | [diff] [blame] | 64 | self.assertEqual('{}', e.content) |
Joe Gregorio | d27ae3e | 2010-12-09 15:01:27 -0500 | [diff] [blame] | 65 | self.assertEqual(500, e.resp.status) |
| 66 | self.assertEqual('Server Error', e.resp.reason) |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | unittest.main() |