From 6157de18e488df27049ed2f7daa44caad5f4324c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Wed, 3 Jun 2015 15:30:10 +0200 Subject: [PATCH 1/4] Made plugin ctor more flexible + Modified unit testing --- bottle_redis.py | 11 ++++------- test.py | 9 ++++++++- 2 files changed, 12 insertions(+), 8 deletions(-) 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..15f34f3 100644 --- a/test.py +++ b/test.py @@ -37,11 +37,18 @@ def test_kw(**kw): 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.assertTrue(rdb.connection_pool.connection_kwargs['decode_responses'] == True) + rdb.set('test', 'bottle') + if py3k: + self.assertEqual(rdb.get('test'), 'bottle') + else: + self.assertEqual(rdb.get('test'), u'bottle') self.app({'PATH_INFO':'/db/1', 'REQUEST_METHOD':'GET'}, lambda x,y: None) From 2c6b0cfc2c69b4c683e7c7a317e9b1f7a1a411a8 Mon Sep 17 00:00:00 2001 From: Thiago Avelino Date: Wed, 3 Jun 2015 15:39:11 -0300 Subject: [PATCH 2/4] fix py3k support (3.2) #8 --- test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.py b/test.py index 15f34f3..c32153a 100644 --- a/test.py +++ b/test.py @@ -46,9 +46,9 @@ def test_db_arg(rdb): self.assertTrue(rdb.connection_pool.connection_kwargs['decode_responses'] == True) rdb.set('test', 'bottle') if py3k: - self.assertEqual(rdb.get('test'), 'bottle') + self.assertEqual(rdb.get('test'), b'bottle') else: - self.assertEqual(rdb.get('test'), u'bottle') + self.assertEqual(rdb.get('test'), 'bottle') self.app({'PATH_INFO':'/db/1', 'REQUEST_METHOD':'GET'}, lambda x,y: None) From a25abeda30f8ff5efb870f94097c9ab73d63eff4 Mon Sep 17 00:00:00 2001 From: Thiago Avelino Date: Wed, 3 Jun 2015 15:42:04 -0300 Subject: [PATCH 3/4] fix pep8 on tests --- test.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/test.py b/test.py index c32153a..7ea41ac 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,29 +29,31 @@ 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(db=1, - decode_responses=True)) + 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.assertTrue(rdb.connection_pool.connection_kwargs['decode_responses'] == True) + pool = rdb.connection_pool + self.assertTrue(pool.connection_kwargs['db'] == 1) + self.assertTrue(pool.connection_kwargs['decode_responses'] == True) rdb.set('test', 'bottle') if py3k: self.assertEqual(rdb.get('test'), b'bottle') else: self.assertEqual(rdb.get('test'), 'bottle') - self.app({'PATH_INFO':'/db/1', 'REQUEST_METHOD':'GET'}, - lambda x,y: None) - + 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()) @@ -65,8 +67,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() From 7779fe010fd7133694ad27088c96600dc17fc974 Mon Sep 17 00:00:00 2001 From: Thiago Avelino Date: Wed, 3 Jun 2015 15:57:21 -0300 Subject: [PATCH 4/4] test equal return in rdb --- test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test.py b/test.py index 7ea41ac..73d824f 100644 --- a/test.py +++ b/test.py @@ -48,10 +48,7 @@ def test_db_arg(rdb): self.assertTrue(pool.connection_kwargs['db'] == 1) self.assertTrue(pool.connection_kwargs['decode_responses'] == True) rdb.set('test', 'bottle') - if py3k: - self.assertEqual(rdb.get('test'), b'bottle') - else: - self.assertEqual(rdb.get('test'), 'bottle') + self.assertEqual(rdb.get('test'), 'bottle') self.app({'PATH_INFO': '/db/1', 'REQUEST_METHOD': 'GET'}, lambda x, y: None)