diff --git a/bottle_redis.py b/bottle_redis.py index cca3627..ae1a6fb 100644 --- a/bottle_redis.py +++ b/bottle_redis.py @@ -8,12 +8,11 @@ class RedisPlugin(object): name = 'redis' api = 2 - def __init__(self, host='localhost', port=6379, database=0, keyword='rdb'): - self.host = host - self.port = port - self.database = database + def __init__(self, keyword='rdb', *args, **kwargs): self.keyword = keyword self.redisdb = None + self.args = args + self.kwargs = kwargs def setup(self, app): for other in app.plugins: @@ -23,9 +22,7 @@ def setup(self, app): raise PluginError("Found another redis plugin with "\ "conflicting settings (non-unique keyword).") if self.redisdb is None: - self.redisdb = redis.ConnectionPool(host=self.host, - port=self.port, - db=self.database) + self.redisdb = redis.ConnectionPool(*self.args, **self.kwargs) def apply(self, callback, route): # hack to support bottle v0.9.x diff --git a/test.py b/test.py index f955c4f..73d824f 100644 --- a/test.py +++ b/test.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import unittest -import os import sys import bottle from bottle.ext import redis as redis_plugin @@ -21,7 +20,8 @@ def test_with_keyword(self): @self.app.get('/') def test(rdb): self.assertEqual(type(rdb), type(redis.client.Redis())) - self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None) + self.app({'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'}, + lambda x, y: None) def test_without_keyword(self): self.plugin = self.app.install(redis_plugin.Plugin()) @@ -29,22 +29,28 @@ def test_without_keyword(self): @self.app.get('/') def test(): pass - self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None) + self.app({'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'}, + lambda x, y: None) @self.app.get('/2') def test_kw(**kw): self.assertFalse('rdb' in kw) - self.app({'PATH_INFO':'/2', 'REQUEST_METHOD':'GET'}, lambda x, y: None) - + self.app({'PATH_INFO': '/2', 'REQUEST_METHOD': 'GET'}, + lambda x, y: None) + def test_optional_args(self): - self.plugin = self.app.install(redis_plugin.Plugin(database=1)) + self.plugin = self.app.install(redis_plugin.Plugin( + db=1, decode_responses=True)) @self.app.get('/db/1') def test_db_arg(rdb): - self.assertTrue(rdb.connection_pool.connection_kwargs['db'] == 1) - self.app({'PATH_INFO':'/db/1', 'REQUEST_METHOD':'GET'}, - lambda x,y: None) - + pool = rdb.connection_pool + self.assertTrue(pool.connection_kwargs['db'] == 1) + self.assertTrue(pool.connection_kwargs['decode_responses'] == True) + rdb.set('test', 'bottle') + self.assertEqual(rdb.get('test'), 'bottle') + self.app({'PATH_INFO': '/db/1', 'REQUEST_METHOD': 'GET'}, + lambda x, y: None) def test_save(self): self.plugin = self.app.install(redis_plugin.Plugin()) @@ -58,8 +64,9 @@ def test(rdb): else: self.assertEqual(rdb.get('test'), 'bottle') self.assertEqual(rdb.get('test'), r.get('test')) - self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x,y: None) - + self.app({'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'}, + lambda x, y: None) + if __name__ == '__main__': unittest.main()