Commit 6a6175c0 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Unittest full kamaki.clients.Client

parent 2e247767
...@@ -183,9 +183,12 @@ class Client(object): ...@@ -183,9 +183,12 @@ class Client(object):
E.g. in most queries the 'X-Auth-Token' header might be the same for E.g. in most queries the 'X-Auth-Token' header might be the same for
all, but the 'Range' header might be different from request to request. all, but the 'Range' header might be different from request to request.
""" """
assert isinstance(method, str) or isinstance(method, unicode)
assert method
assert isinstance(path, str) or isinstance(path, unicode)
assert path
try: try:
success = kwargs.pop('success', 200) success = kwargs.pop('success', 200)
data = kwargs.pop('data', None) data = kwargs.pop('data', None)
self.http_client.headers.setdefault('X-Auth-Token', self.token) self.http_client.headers.setdefault('X-Auth-Token', self.token)
...@@ -236,9 +239,9 @@ class Client(object): ...@@ -236,9 +239,9 @@ class Client(object):
errstr = ('%s' % type(err))[7:-2] errstr = ('%s' % type(err))[7:-2]
status = getattr(err, 'status', getattr(err, 'errno', 0)) status = getattr(err, 'status', getattr(err, 'errno', 0))
raise ClientError('%s\n' % errstr, status=status) raise ClientError('%s\n' % errstr, status=status)
self.http_client.reset_headers() self.http_client.reset_headers()
self.http_client.reset_params() self.http_client.reset_params()
if success is not None: if success is not None:
# Success can either be an int or a collection # Success can either be an int or a collection
...@@ -150,30 +150,42 @@ class SilentEvent(TestCase): ...@@ -150,30 +150,42 @@ class SilentEvent(TestCase):
self.assertFalse(t.exception) self.assertFalse(t.exception)
class FR(object):
json = None
text = None
headers = dict()
content = json
status = None
status_code = 200
def release(self):
class FakeConnection(object): class FakeConnection(object):
"""A fake Connection class""" """A fake Connection class"""
headers = dict()
params = dict()
def __init__(self): def __init__(self):
pass pass
def set_header(self, name, value): def set_header(self, name, value):
pass pass
def set_param(self, name, value): def reset_headers(self):
pass self.headers = {}
class FR(object): def set_param(self, name, value):
json = None self.params = {}
text = None
headers = {}
content = json
status = None
status_code = 200
def release(self): def reset_params(self):
pass pass
def perform_request(self, *args):
return FR()
class Client(TestCase): class Client(TestCase):
...@@ -197,6 +209,8 @@ class Client(TestCase): ...@@ -197,6 +209,8 @@ class Client(TestCase):
FR.text = None FR.text = None
FR.status = None FR.status = None
FR.status_code = 200 FR.status_code = 200
FakeConnection.headers = dict()
self.client.token = self.token
def test___init__(self): def test___init__(self):
self.assertEqual(self.client.base_url, self.base_url) self.assertEqual(self.client.base_url, self.base_url)
...@@ -299,6 +313,75 @@ class Client(TestCase): ...@@ -299,6 +313,75 @@ class Client(TestCase):
else: else:
self.assertEqual(num_of_calls, len(SP.mock_calls)) self.assertEqual(num_of_calls, len(SP.mock_calls))
@patch('%s.FakeConnection.perform_request' % __name__, return_value=FR())
def test_request(self, PR):
for args in product(
('get', '', dict(method='get')),
('/some/path', None, ['some', 'path']),
(dict(), dict(h1='v1'), dict(h1='v2', h2='v2')),
(dict(), dict(p1='v1'), dict(p1='v2', p2=None, p3='v3')),
(dict(), dict(data='some data'), dict(
json=dict(k2='v2', k1='v1')))):
method, path, kwargs = args[0], args[1], args[-1]
args = args[:-1]
if not (isinstance(method, str) and method and isinstance(
path, str) and path):
*args, **kwargs)
atoken = 'a70k3n_%s' % randint(1, 30)
self.client.token = atoken
if 'success' in kwargs:
*args, **kwargs)
FR.status_code = kwargs['success']
FR.status_code = 200
self.client.request(*args, **kwargs)
data = kwargs.get(
'{"k2": "v2", "k1": "v1"}' if 'json' in kwargs else None)
self.assertEqual(self.client.http_client.url, self.base_url)
self.assertEqual(self.client.http_client.path, path)
call(method, data, *args[2:]))
self.assertEqual(self.client.http_client.headers, dict())
self.assertEqual(self.client.http_client.params, dict())
@patch('kamaki.clients.Client.request', return_value='lala')
def _test_foo(self, foo, request):
method = getattr(self.client, foo)
r = method('path', k='v')
self.assertEqual(r, 'lala')
request.assert_called_once_with(foo, 'path', k='v')
def test_delete(self):
def test_get(self):
def test_head(self):
def test_post(self):
def test_put(self):
def test_copy(self):
def test_move(self):
# TestCase auxiliary methods # TestCase auxiliary methods
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment