From cb8103dbd3c15177cca4584ea60348662a66f5df Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Fri, 11 Feb 2011 23:20:52 -0500 Subject: [PATCH] Move HttpMock out of tests and into apiclient.http. Update tests that used HttpMock --- apiclient/http.py | 21 ++++++++++++++++++++- docs/apiclient.http.html | 32 +++++++++++++++++++++++++++++--- tests/test_discovery.py | 26 ++++++++++++++++---------- tests/test_mocks.py | 13 ++++++++++--- tests/util.py | 27 --------------------------- 5 files changed, 75 insertions(+), 44 deletions(-) delete mode 100644 tests/util.py diff --git a/apiclient/http.py b/apiclient/http.py index 85ff93a..e9fd7e0 100644 --- a/apiclient/http.py +++ b/apiclient/http.py @@ -9,10 +9,12 @@ actuall HTTP request. __author__ = 'jcgregorio@google.com (Joe Gregorio)' __all__ = [ - 'HttpRequest', 'RequestMockBuilder' + 'HttpRequest', 'RequestMockBuilder', 'HttpMock' ] import httplib2 +import os + from model import JsonModel @@ -147,3 +149,20 @@ class RequestMockBuilder(object): else: model = JsonModel() return HttpRequestMock(None, '{}', model.response) + +class HttpMock(object): + """Mock of httplib2.Http""" + + def __init__(self, filename, headers): + """ + Args: + filename: string, absolute filename to read response from + headers: dict, header to return with response + """ + f = file(filename, 'r') + self.data = f.read() + f.close() + self.headers = headers + + def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): + return httplib2.Response(self.headers), self.data diff --git a/docs/apiclient.http.html b/docs/apiclient.http.html index a378677..b18e2de 100644 --- a/docs/apiclient.http.html +++ b/docs/apiclient.http.html @@ -22,7 +22,8 @@ actuall HTTP request.

       
httplib2
-

+os
+

 
@@ -33,7 +34,8 @@ actuall HTTP request.

__builtin__.object
-
HttpRequest +
HttpMock +
HttpRequest
RequestMockBuilder
@@ -42,6 +44,30 @@ actuall HTTP request.

+ + + + +
 
+class HttpMock(__builtin__.object)
   Mock of httplib2.Http
 
 Methods defined here:
+
__init__(self, filename, headers)
Args:
+  filename: string, absolute filename to read response from
+  headers: dict, header to return with response
+ +
request(self, uri, method='GET', body=None, headers=None, redirections=1, connection_type=None)
+ +
+Data descriptors defined here:
+
__dict__
+
dictionary for instance variables (if defined)
+
+
__weakref__
+
list of weak references to the object (if defined)
+
+

+ + + @@ -143,7 +169,7 @@ Data descriptors defined here:
Data -
 
class HttpRequest(__builtin__.object)
   
       __all__ = ['HttpRequest', 'RequestMockBuilder']
+
__all__ = ['HttpRequest', 'RequestMockBuilder', 'HttpMock']
__author__ = 'jcgregorio@google.com (Joe Gregorio)'

diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 541210c..7c00bb9 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -32,7 +32,13 @@ except ImportError: from cgi import parse_qs from apiclient.discovery import build, key2param -from tests.util import HttpMock +from apiclient.http import HttpMock + + +DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') + +def datafile(filename): + return os.path.join(DATA_DIR, filename) class Utilities(unittest.TestCase): @@ -44,7 +50,7 @@ class Utilities(unittest.TestCase): class Discovery(unittest.TestCase): def test_method_error_checking(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http) # Missing required parameters @@ -76,7 +82,7 @@ class Discovery(unittest.TestCase): self.assertTrue('unexpected' in str(e)) def test_buzz_resources(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http) self.assertTrue(getattr(buzz, 'activities')) self.assertTrue(getattr(buzz, 'feeds')) @@ -87,7 +93,7 @@ class Discovery(unittest.TestCase): self.assertTrue(getattr(buzz, 'related')) def test_auth(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http) auth = buzz.auth_discovery() self.assertTrue('request' in auth) @@ -95,7 +101,7 @@ class Discovery(unittest.TestCase): def test_full_featured(self): # Zoo should exercise all discovery facets # and should also have no future.json file. - self.http = HttpMock('zoo.json', {'status': '200'}) + self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) zoo = build('zoo', 'v1', self.http) self.assertTrue(getattr(zoo, 'animals')) request = zoo.animals().list(name="bat", projection="size") @@ -105,7 +111,7 @@ class Discovery(unittest.TestCase): self.assertEqual(q['projection'], ['size']) def test_nested_resources(self): - self.http = HttpMock('zoo.json', {'status': '200'}) + self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) zoo = build('zoo', 'v1', self.http) self.assertTrue(getattr(zoo, 'animals')) request = zoo.my().favorites().list(max_results="5") @@ -114,7 +120,7 @@ class Discovery(unittest.TestCase): self.assertEqual(q['max-results'], ['5']) def test_top_level_functions(self): - self.http = HttpMock('zoo.json', {'status': '200'}) + self.http = HttpMock(datafile('zoo.json'), {'status': '200'}) zoo = build('zoo', 'v1', self.http) self.assertTrue(getattr(zoo, 'query')) request = zoo.query(q="foo") @@ -125,7 +131,7 @@ class Discovery(unittest.TestCase): class Next(unittest.TestCase): def test_next_for_people_liked(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http) people = {'links': {'next': @@ -136,7 +142,7 @@ class Next(unittest.TestCase): class DeveloperKey(unittest.TestCase): def test_param(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') activities = {'links': {'next': @@ -147,7 +153,7 @@ class DeveloperKey(unittest.TestCase): self.assertEqual(q['key'], ['foobie_bletch']) def test_next_for_activities_list(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) buzz = build('buzz', 'v1', self.http, developerKey='foobie_bletch') activities = {'links': {'next': diff --git a/tests/test_mocks.py b/tests/test_mocks.py index ee8ef26..2c66aab 100644 --- a/tests/test_mocks.py +++ b/tests/test_mocks.py @@ -24,15 +24,22 @@ __author__ = 'jcgregorio@google.com (Joe Gregorio)' from apiclient.errors import HttpError from apiclient.discovery import build from apiclient.http import RequestMockBuilder -from tests.util import HttpMock +from apiclient.http import HttpMock -import unittest import httplib2 +import os +import unittest + + +DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') + +def datafile(filename): + return os.path.join(DATA_DIR, filename) class Mocks(unittest.TestCase): def setUp(self): - self.http = HttpMock('buzz.json', {'status': '200'}) + self.http = HttpMock(datafile('buzz.json'), {'status': '200'}) def test_default_response(self): requestBuilder = RequestMockBuilder({}) diff --git a/tests/util.py b/tests/util.py deleted file mode 100644 index 101079f..0000000 --- a/tests/util.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/python2.4 -# -# Copyright 2010 Google Inc. All Rights Reserved. - -"""One-line documentation for util module. - -A detailed description of util. -""" - -__author__ = 'jcgregorio@google.com (Joe Gregorio)' - -import httplib2 -import os - -DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') - - -class HttpMock(object): - - def __init__(self, filename, headers): - f = file(os.path.join(DATA_DIR, filename), 'r') - self.data = f.read() - f.close() - self.headers = headers - - def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): - return httplib2.Response(self.headers), self.data