| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3ad5e9a commit d27ae3e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,72 @@ | |||
| 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 | + | ||
| 24 | + from apiclient.discovery import HttpError | ||
| 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: | ||
| 64 | + self.assertEqual('500 Server Error', e.detail) | ||
| 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() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + #!/usr/bin/python2.4 | ||
| 2 | + # | ||
| 3 | + # Copyright 2010 Google Inc. All Rights Reserved. | ||
| 4 | + | ||
| 5 | + """One-line documentation for util module. | ||
| 6 | + | ||
| 7 | + A detailed description of util. | ||
| 8 | + """ | ||
| 9 | + | ||
| 10 | + __author__ = 'jcgregorio@google.com (Joe Gregorio)' | ||
| 11 | + | ||
| 12 | + import httplib2 | ||
| 13 | + import os | ||
| 14 | + | ||
| 15 | + DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + class HttpMock(object): | ||
| 19 | + | ||
| 20 | + def __init__(self, filename, headers): | ||
| 21 | + f = file(os.path.join(DATA_DIR, filename), 'r') | ||
| 22 | + self.data = f.read() | ||
| 23 | + f.close() | ||
| 24 | + self.headers = headers | ||
| 25 | + | ||
| 26 | + def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): | ||
| 27 | + return httplib2.Response(self.headers), self.data | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments