Browse Source

update tests to properly use pool rather than urllib3

master
Brett Langdon 12 years ago
parent
commit
fd7f72835d
2 changed files with 179 additions and 409 deletions
  1. +1
    -1
      riakcached/clients.py
  2. +178
    -408
      riakcached/tests/test_riakclient.py

+ 1
- 1
riakcached/clients.py View File

@ -28,7 +28,7 @@ class RiakClient(object):
self.pool = pool self.pool = pool
self.bucket = bucket self.bucket = bucket
self.base_url = pool.url
self.base_url = self.pool.url.rstrip("/")
self._serializers = { self._serializers = {
"application/json": json.dumps, "application/json": json.dumps,
} }


+ 178
- 408
riakcached/tests/test_riakclient.py View File

@ -1,30 +1,24 @@
import mock import mock
import unittest2 import unittest2
import urllib3.exceptions
from riakcached import exceptions from riakcached import exceptions
from riakcached.clients import RiakClient from riakcached.clients import RiakClient
from riakcached.tests.utils import InlineClass
import riakcached.pools
class TestRiakClient(unittest2.TestCase): class TestRiakClient(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
def tearDown(self):
self.patched_connection_from_url.stop()
def test_client_strips_trailing_url_slash(self): def test_client_strips_trailing_url_slash(self):
client = RiakClient("test_bucket", url="http://127.0.0.1:8098/", auto_connect=False)
self.assertEqual(client.url, "http://127.0.0.1:8098")
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.url = "http://127.0.0.1:8098/"
client = RiakClient("test_bucket", pool=pool)
self.assertEqual(client.base_url, "http://127.0.0.1:8098")
def test_client_adds_serializer(self): def test_client_adds_serializer(self):
serializer = mock.Mock() serializer = mock.Mock()
client = RiakClient("test_bucket", auto_connect=False)
client = RiakClient("test_bucket", pool=mock.Mock(spec=riakcached.pools.Pool))
client.add_serializer("application/test", serializer) client.add_serializer("application/test", serializer)
self.assertEqual(client._serializers["application/test"], serializer) self.assertEqual(client._serializers["application/test"], serializer)
client.serialize("test", "application/test") client.serialize("test", "application/test")
@ -33,577 +27,353 @@ class TestRiakClient(unittest2.TestCase):
def test_client_adds_deserializer(self): def test_client_adds_deserializer(self):
deserializer = mock.Mock() deserializer = mock.Mock()
client = RiakClient("test_bucket", auto_connect=False)
client = RiakClient("test_bucket", pool=mock.Mock(spec=riakcached.pools.Pool))
client.add_deserializer("application/test", deserializer) client.add_deserializer("application/test", deserializer)
self.assertEqual(client._deserializers["application/test"], deserializer) self.assertEqual(client._deserializers["application/test"], deserializer)
client.deserialize("test", "application/test") client.deserialize("test", "application/test")
deserializer.assert_called() deserializer.assert_called()
def test_client_calls_pool_close(self):
client = RiakClient("test_bucket", auto_connect=False)
client._pool = mock.Mock()
client.close()
self.assertTrue(client._pool.close.called)
def test_client_calls_pool_connect_with_auto_connect_set(self):
RiakClient("test_bucket", url="http://127.0.0.1:8098", auto_connect=True)
self.assertTrue(self.connection_from_url.called)
self.connection_from_url.assert_called_once_with("http://127.0.0.1:8098")
def test_client_calls_pool_connect_with_auto_connect_default(self):
RiakClient("test_bucket", url="http://127.0.0.1:8098")
self.assertTrue(self.connection_from_url.called)
self.connection_from_url.assert_called_once_with("http://127.0.0.1:8098")
def test_client_doesnt_call_pool_connect_with_auto_connect_false(self):
RiakClient("test_bucket", url="http://127.0.0.1:8098", auto_connect=False)
self.assertFalse(self.connection_from_url.called)
def test_client_request_timeout_raises_timeout(self):
client = RiakClient("test_bucket")
timeout_error = urllib3.exceptions.TimeoutError(InlineClass({}), "url", "timeout")
client._pool.urlopen.side_effect = timeout_error
self.assertRaises(
exceptions.RiakcachedTimeout,
client._request,
"GET",
"http://127.0.0.1:9080/stats",
)
def test_client_request_http_error_raises_connection_error(self):
client = RiakClient("test_bucket")
http_error = urllib3.exceptions.HTTPError("http error")
client._pool.urlopen.side_effect = http_error
self.assertRaises(
exceptions.RiakcachedConnectionError,
client._request,
"GET",
"http://127.0.0.1:9080/stats",
)
class TestGet(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
def tearDown(self):
self.patched_connection_from_url.stop()
class TestRiakClientGet(unittest2.TestCase):
def test_get_calls_pool_request_for_counters(self):
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = None, None, None
pool.url = "http://127.0.0.1:8098"
def test_get_calls_pool_urlopen(self):
client = RiakClient("test_bucket")
client.get("test")
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="GET",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
)
def test_get_calls_pool_urlopen_for_counters(self):
client = RiakClient("test_bucket")
client = RiakClient("test_bucket", pool=pool)
client.get("test", counter=True) client.get("test", counter=True)
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
self.assertTrue(pool.request.called)
pool.request.assert_called_once_with(
method="GET", method="GET",
url="http://127.0.0.1:8098/buckets/test_bucket/counters/test", url="http://127.0.0.1:8098/buckets/test_bucket/counters/test",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
) )
def test_get_invalid_status_code(self): def test_get_invalid_status_code(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 302,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 302, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertIsNone(client.get("test")) self.assertIsNone(client.get("test"))
def test_get_400_raises_bad_request(self): def test_get_400_raises_bad_request(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 400,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 400, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedBadRequest, client.get, "test") self.assertRaises(exceptions.RiakcachedBadRequest, client.get, "test")
def test_get_503_raises_unavailable(self): def test_get_503_raises_unavailable(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 503,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 503, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedServiceUnavailable, client.get, "test") self.assertRaises(exceptions.RiakcachedServiceUnavailable, client.get, "test")
def test_get_uses_deserializer(self): def test_get_uses_deserializer(self):
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, "some data", {"content-type": "application/test"}
pool.url = "http://127.0.0.1:8098"
def deserializer(data): def deserializer(data):
return "deserialized" return "deserialized"
client = RiakClient("test_bucket")
client = RiakClient("test_bucket", pool=pool)
client.add_deserializer("application/test", deserializer) client.add_deserializer("application/test", deserializer)
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": "some data",
"getheader": (lambda header: "application/test"),
})
result = client.get("test") result = client.get("test")
self.assertEqual("deserialized", result) self.assertEqual("deserialized", result)
def test_get_many(self): def test_get_many(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": "result",
"getheader": (lambda header: "text/plain"),
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, "result", {"content-type": "text/plain"}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
results = client.get_many(["test1", "test2"]) results = client.get_many(["test1", "test2"])
self.assertEqual(results, { self.assertEqual(results, {
"test1": "result", "test1": "result",
"test2": "result", "test2": "result",
}) })
self.assertEqual(2, client._pool.urlopen.call_count)
client._pool.urlopen.assert_any_call(
self.assertEqual(2, pool.request.call_count)
pool.request.assert_any_call(
method="GET", method="GET",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test1", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test1",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
) )
client._pool.urlopen.assert_any_call(
pool.request.assert_any_call(
method="GET", method="GET",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test2", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test2",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
) )
class TestSet(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
class TestRiakClientSet(unittest2.TestCase):
def test_set_calls_pool_request_with_correct_content_type(self):
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = None, None, None
pool.url = "http://127.0.0.1:8098"
def tearDown(self):
self.patched_connection_from_url.stop()
def test_set_calls_pool_urlopen(self):
client = RiakClient("test_bucket")
client.set("test", "value")
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test",
body="value",
headers={
"Content-Type": "text/plain",
},
timeout=client._timeout,
redirect=False,
)
def test_set_calls_pool_urlopen_with_correct_content_type(self):
client = RiakClient("test_bucket")
client = RiakClient("test_bucket", pool=pool)
client.set("test", "value", content_type="application/test") client.set("test", "value", content_type="application/test")
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
self.assertTrue(pool.request.called)
pool.request.assert_called_once_with(
method="POST", method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test",
body="value", body="value",
headers={ headers={
"Content-Type": "application/test", "Content-Type": "application/test",
}, },
timeout=client._timeout,
redirect=False,
) )
def test_set_invalid_status_code(self): def test_set_invalid_status_code(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 302,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 302, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertFalse(client.get("test")) self.assertFalse(client.get("test"))
def test_set_400_raises_bad_request(self): def test_set_400_raises_bad_request(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 400,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 400, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedBadRequest, client.set, "test", "value") self.assertRaises(exceptions.RiakcachedBadRequest, client.set, "test", "value")
def test_set_412_raises_precondition_failed(self): def test_set_412_raises_precondition_failed(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 412,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 412, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedPreconditionFailed, client.set, "test", "value") self.assertRaises(exceptions.RiakcachedPreconditionFailed, client.set, "test", "value")
def test_set_uses_serializer(self): def test_set_uses_serializer(self):
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
def serializer(data): def serializer(data):
return "serialized" return "serialized"
client = RiakClient("test_bucket")
client = RiakClient("test_bucket", pool=pool)
client.add_serializer("application/test", serializer) client.add_serializer("application/test", serializer)
client._pool.urlopen.return_value = InlineClass({
"status": 204,
"data": "",
})
client.set("test", "value", content_type="application/test") client.set("test", "value", content_type="application/test")
client._pool.urlopen.assert_called_once_with(
pool.request.assert_called_once_with(
method="POST", method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test",
body="serialized", body="serialized",
headers={ headers={
"Content-Type": "application/test", "Content-Type": "application/test",
}, },
timeout=client._timeout,
redirect=False,
) )
def test_set_many(self): def test_set_many(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": "",
"getheader": (lambda header: "text/plain"),
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, "", {"content-type": "text/plain"}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.set_many({ client.set_many({
"test1": "value1", "test1": "value1",
"test2": "value2", "test2": "value2",
}) })
self.assertEqual(2, client._pool.urlopen.call_count)
client._pool.urlopen.assert_any_call(
self.assertEqual(2, pool.request.call_count)
pool.request.assert_any_call(
method="POST", method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test1", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test1",
body="value1", body="value1",
headers={ headers={
"Content-Type": "text/plain", "Content-Type": "text/plain",
}, },
timeout=client._timeout,
redirect=False,
) )
client._pool.urlopen.assert_any_call(
pool.request.assert_any_call(
method="POST", method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test2", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test2",
body="value2", body="value2",
headers={ headers={
"Content-Type": "text/plain", "Content-Type": "text/plain",
}, },
timeout=client._timeout,
redirect=False,
) )
class TestDelete(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
def tearDown(self):
self.patched_connection_from_url.stop()
def test_delete_calls_pool_urlopen(self):
client = RiakClient("test_bucket")
client.delete("test")
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="DELETE",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
)
class TestRiakClientDelete(unittest2.TestCase):
def test_delete_invalid_status(self): def test_delete_invalid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertFalse(client.delete("test")) self.assertFalse(client.delete("test"))
def test_delete_valid_status(self): def test_delete_valid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 204,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertTrue(client.delete("test")) self.assertTrue(client.delete("test"))
def test_delete_400_raises_bad_request(self): def test_delete_400_raises_bad_request(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 400,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 400, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedBadRequest, client.delete, "test") self.assertRaises(exceptions.RiakcachedBadRequest, client.delete, "test")
def test_delete_many_calls_pool_urlopen(self):
client = RiakClient("test_bucket")
def test_delete_many_calls_pool_request(self):
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.delete_many(["test1", "test2"]) client.delete_many(["test1", "test2"])
self.assertEqual(2, client._pool.urlopen.call_count)
client._pool.urlopen.assert_any_call(
self.assertEqual(2, pool.request.call_count)
pool.request.assert_any_call(
method="DELETE", method="DELETE",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test1", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test1",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
) )
client._pool.urlopen.assert_any_call(
pool.request.assert_any_call(
method="DELETE", method="DELETE",
url="http://127.0.0.1:8098/buckets/test_bucket/keys/test2", url="http://127.0.0.1:8098/buckets/test_bucket/keys/test2",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
) )
class TestCounter(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
def tearDown(self):
self.patched_connection_from_url.stop()
def test_incr_calls_pool_urlopen(self):
client = RiakClient("test_bucket")
client.incr("test")
self.assertTrue(client._pool.urlopen.called)
self.assertTrue(client._pool.urlopen.called_with(
method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/counters/test",
body="1",
headers=None,
timeout=client._timeout,
redirect=False,
))
class TestRiakClientCounter(unittest2.TestCase):
def test_incr_calls_pool_urlopen_with_value(self): def test_incr_calls_pool_urlopen_with_value(self):
client = RiakClient("test_bucket")
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = None, None, None
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.incr("test", value=5) client.incr("test", value=5)
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
self.assertTrue(pool.request.called)
pool.request.assert_called_once_with(
method="POST", method="POST",
url="http://127.0.0.1:8098/buckets/test_bucket/counters/test", url="http://127.0.0.1:8098/buckets/test_bucket/counters/test",
body="5", body="5",
headers=None,
timeout=client._timeout,
redirect=False,
) )
def test_incr_409_raises_conflict(self): def test_incr_409_raises_conflict(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 409,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 409, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedConflict, client.incr, "test") self.assertRaises(exceptions.RiakcachedConflict, client.incr, "test")
def test_incr_400_raises_bad_request(self): def test_incr_400_raises_bad_request(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 400,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 400, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertRaises(exceptions.RiakcachedBadRequest, client.incr, "test") self.assertRaises(exceptions.RiakcachedBadRequest, client.incr, "test")
def test_incr_invalid_status(self): def test_incr_invalid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 302,
"data": "",
})
self.assertFalse(client.incr("test"))
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 302, "", {}
pool.url = "http://127.0.0.1:8098"
class TestPing(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
client = RiakClient("test_bucket", pool=pool)
self.assertFalse(client.incr("test"))
def tearDown(self):
self.patched_connection_from_url.stop()
def test_ping(self):
client = RiakClient("test_bucket")
client.ping()
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="GET",
url="http://127.0.0.1:8098/ping",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
)
class TestRiakClientPing(unittest2.TestCase):
def test_ping_valid_status(self): def test_ping_valid_status(self):
client = RiakClient("test_bucket")
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.ping() client.ping()
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": "",
})
self.assertTrue(client.ping()) self.assertTrue(client.ping())
def test_ping_invalid_status(self): def test_ping_invalid_status(self):
client = RiakClient("test_bucket")
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.ping() client.ping()
client._pool.urlopen.return_value = InlineClass({
"status": 204,
"data": "",
})
self.assertFalse(client.ping()) self.assertFalse(client.ping())
class TestStats(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
def tearDown(self):
self.patched_connection_from_url.stop()
def test_stats(self):
client = RiakClient("test_bucket")
client.stats()
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="GET",
url="http://127.0.0.1:8098/stats",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
)
class TestRiakClientStats(unittest2.TestCase):
def test_stats_valid_status(self): def test_stats_valid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": '{"test": "stats"}',
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, '{"test": "stats"}', {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertEqual(client.stats(), { self.assertEqual(client.stats(), {
"test": "stats", "test": "stats",
}) })
def test_stats_invalid_status(self): def test_stats_invalid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 204,
"data": "",
})
self.assertIsNone(client.stats())
class TestProps(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
def tearDown(self):
self.patched_connection_from_url.stop()
client = RiakClient("test_bucket", pool=pool)
self.assertIsNone(client.stats())
def test_props(self):
client = RiakClient("test_bucket")
client.props()
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="GET",
url="http://127.0.0.1:8098/buckets/test_bucket/props",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
)
class TestRiakClientProps(unittest2.TestCase):
def test_props_valid_status(self): def test_props_valid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": '{"test": "props"}',
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, '{"test": "props"}', {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertEqual(client.props(), { self.assertEqual(client.props(), {
"test": "props", "test": "props",
}) })
def test_props_invalid_status(self): def test_props_invalid_status(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 204,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertIsNone(client.props()) self.assertIsNone(client.props())
def test_set_props(self): def test_set_props(self):
client = RiakClient("test_bucket")
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": "",
})
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
self.assertTrue(client.set_props({ self.assertTrue(client.set_props({
"some": "props" "some": "props"
})) }))
client._pool.urlopen.assert_called_once_with(
pool.request.assert_called_once_with(
method="PUT", method="PUT",
url="http://127.0.0.1:8098/buckets/test_bucket/props", url="http://127.0.0.1:8098/buckets/test_bucket/props",
body='{"some": "props"}', body='{"some": "props"}',
headers={ headers={
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
timeout=client._timeout,
redirect=False,
) )
class TestKeys(unittest2.TestCase):
def setUp(self):
self.patched_connection_from_url = mock.patch("urllib3.connection_from_url")
self.connection_from_url = self.patched_connection_from_url.start()
def tearDown(self):
self.patched_connection_from_url.stop()
def test_keys(self):
client = RiakClient("test_bucket")
client.keys()
self.assertTrue(client._pool.urlopen.called)
client._pool.urlopen.assert_called_once_with(
method="GET",
url="http://127.0.0.1:8098/buckets/test_bucket/keys?keys=true",
body=None,
headers=None,
timeout=client._timeout,
redirect=False,
)
class TestRiakClientKeys(unittest2.TestCase):
def test_keys_valid_status(self): def test_keys_valid_status(self):
client = RiakClient("test_bucket")
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 200, '["key1", "key2"]', {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.ping() client.ping()
client._pool.urlopen.return_value = InlineClass({
"status": 200,
"data": '["key1", "key2"]',
})
self.assertEqual(client.keys(), ["key1", "key2"]) self.assertEqual(client.keys(), ["key1", "key2"])
def test_ping_invalid_status(self): def test_ping_invalid_status(self):
client = RiakClient("test_bucket")
pool = mock.Mock(spec=riakcached.pools.Pool)
pool.request.return_value = 204, "", {}
pool.url = "http://127.0.0.1:8098"
client = RiakClient("test_bucket", pool=pool)
client.ping() client.ping()
client._pool.urlopen.return_value = InlineClass({
"status": 204,
"data": "",
})
self.assertIsNone(client.keys()) self.assertIsNone(client.keys())

Loading…
Cancel
Save