Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Allows for easier and faster unit testing when using the Basecamp API. For details see:
class TestBasecamp(Basecamp):
"""Subclass of Basecamp which records network transactions.
Transactions are serialized to JSON on disk and used in lieu
of network usage on second and subsequent requests.
Basecamp class found here:
def __init__(self, baseURL, username, password):
self.__test_responses = { 'GET': {}, 'POST': {} }
super(TestBasecamp, self).__init__(baseURL, username, password)
def _request(self, path, data=None):
result = self.__test_request_local(path, data)
except KeyError:
result = super(TestBasecamp, self)._request(path, data)
self.__cache_result(path, data, result)
return result
def __cache_result(self, path, data, result):
if data:
data = ET.tostring(data)
if not self.__test_responses['POST'].has_key(path):
self.__test_responses['POST'][path] = {}
self.__test_responses['POST'][path][data] = result
self.__test_responses['GET'][path] = result
return result
def __test_request_local(self, path, data):
if data:
data = ET.tostring(data)
return self.__test_responses['POST'][path][data]
except KeyError:
print "Cache miss: POST %s\n\t%s\n" % (path, data)
return self.__test_responses['GET'][path]
except KeyError:
print "Cache miss: GET %s" % (path)
print "Cache: %s" % (pprint.pformat(self.__test_responses['GET']))
def load_test_fixtures(self, path):
contents = file(path, 'r').read()
self.__test_responses = simplejson.loads(contents)
return self.__test_responses
except IOError, e:
print "Warning fixture file %s not found. No fixtures loaded." % (path)
return False
def save_test_fixtures(self, path):
contents = simplejson.dumps(self.__test_responses, indent=4)
file(path, 'w').write(contents)
return contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment