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_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("abcdefghijklmnopqrstuvwyz")
        self.assertEqual(mockhttp.method_calls[0][0], 'request')
        self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/feature/%s.json' % (API_VERSION, "abcdefghijklmnopqrstuvwyz"))
        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, dict), (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("abcdefghijklmnopqrstuvwyz")
        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/feature/%s.json' % (API_VERSION, "abcdefghijklmnopqrstuvwyz"))
        self.assertEqual(mockhttp.method_calls[0][1][1], 'GET')
 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')