Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge clean #8 #9

Merged
merged 4 commits into from
Jun 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()