Skip to content

Commit

Permalink
Merge pull request #9 from bottlepy/cgloeckner-master
Browse files Browse the repository at this point in the history
merge clean #8
  • Loading branch information
Thiago Avelino committed Jun 3, 2015
2 parents 534fb02 + 7779fe0 commit 231a0a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
11 changes: 4 additions & 7 deletions bottle_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
31 changes: 19 additions & 12 deletions test.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,30 +20,37 @@ 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())

@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())
Expand All @@ -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()

0 comments on commit 231a0a8

Please sign in to comment.