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