class TestAnnotations(unittest.TestCase): def setUp(self): self.client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET, API_VERSION, API_HOST, API_PORT) self.handle = 'SG_4H2GqJDZrc0ZAjKGR8qM4D' def test_get_annotations(self): mockhttp = mock.Mock() headers = {'status': '200', 'content-type': 'application/json'} mockhttp.request.return_value = (headers, json.dumps(EXAMPLE_ANNOTATIONS_RESPONSE)) self.client.http = mockhttp res = self.client.get_annotations(self.handle) self.assertEqual(mockhttp.method_calls[0][0], 'request') self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/features/%s/annotations.json' % (API_VERSION, self.handle)) self.assertEqual(mockhttp.method_calls[0][1][1], 'GET') # Make sure client returns a dict. self.failUnless(isinstance(res, dict)) def test_get_annotations_bad_handle(self): try: self.client.get_annotations('bad_handle') except TypeError, e: self.failUnless(str(e).startswith('simplegeohandle is required to match the regex')) else:
def __init__(self, key, secret, api_version=API_VERSION, host="api.simplegeo.com", port=80): SGClient.__init__(self, key, secret, api_version=api_version, host=host, port=port) self.endpoints['context'] = 'context/%(lat)s,%(lon)s.json' self.endpoints['context_by_ip'] = 'context/%(ip)s.json' self.endpoints['context_by_my_ip'] = 'context/ip.json' self.endpoints['context_by_address'] = 'context/address.json?address=%(address)s'
class ClientTest(unittest.TestCase): def setUp(self): self.client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET, API_VERSION, API_HOST, API_PORT) self.query_lat = D('37.8016') self.query_lon = D('-122.4783') def test_wrong_endpoint(self): self.assertRaises(Exception, self.client._endpoint, 'wrongwrong') def test_missing_argument(self): self.assertRaises(Exception, self.client._endpoint, 'feature') def test_get_point_feature(self): mockhttp = mock.Mock() mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', 'thingie': "just to see if you're listening"}, EXAMPLE_POINT_BODY) self.client.http = mockhttp res = self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970") self.assertEqual(mockhttp.method_calls[0][0], 'request') self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")) self.assertEqual(mockhttp.method_calls[0][1][1], 'GET') # the code under test is required to have json-decoded this before handing it back self.failUnless(isinstance(res, Feature), (repr(res), type(res))) self.failUnless(self.client.get_most_recent_http_headers(), {'status': '200', 'content-type': 'application/json', 'thingie': "just to see if you're listening"}) def test_get_polygon_feature(self): mockhttp = mock.Mock() mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, EXAMPLE_BODY) self.client.http = mockhttp res = self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970") self.assertEqual(mockhttp.method_calls[0][0], 'request') self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")) self.assertEqual(mockhttp.method_calls[0][1][1], 'GET') # the code under test is required to have json-decoded this before handing it back self.failUnless(isinstance(res, Feature), (repr(res), type(res))) def test_type_check_request(self): self.failUnlessRaises(TypeError, self.client._request, 'whatever', 'POST', {'bogus': "non string"}) def test_get_feature_bad_json(self): mockhttp = mock.Mock() mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, EXAMPLE_BODY + 'some crap') self.client.http = mockhttp try: self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970") except DecodeError, e: self.failUnlessEqual(e.code,None,repr(e.code)) self.failUnless("Could not decode JSON" in e.msg, repr(e.msg)) erepr = repr(e) self.failUnless('JSONDecodeError' in erepr, erepr) self.assertEqual(mockhttp.method_calls[0][0], 'request') self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")) self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')
def __init__(self, key, secret, api_version=API_VERSION, host="api.simplegeo.com", port=80): SGClient.__init__(self, key, secret, api_version=api_version, host=host, port=port) self.endpoints.update(endpoints)
def setUp(self): self.client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET, API_VERSION, API_HOST, API_PORT) self.query_lat = D('37.8016') self.query_lon = D('-122.4783')
def test_get_feature_useful_validation_error_message(self): c = Client('whatever', 'whatever') try: c.get_feature('wrong thing') except TypeError, e: self.failUnless(str(e).startswith('simplegeohandle is required to match '), str(e))
def setUp(self): self.client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET, API_VERSION, API_HOST, API_PORT) self.handle = 'SG_4H2GqJDZrc0ZAjKGR8qM4D'