class FlaskRedisTestCase(unittest.TestCase): def setUp(self): """ Create a sample Flask Application """ self.redis = Redis() self.app = flask.Flask(__name__) def test_init_app(self): """ Test the initation of our Redis extension """ self.redis.init_app(self.app) assert self.redis.get('potato') is None def test_custom_prefix(self): """ Test the use of custom config prefixes """ self.db1_redis = Redis(config_prefix='DB1') self.app.config['DB1_URL'] = "redis://localhost:6379" self.app.config['DB1_DATABASE'] = 0 self.db1_redis.init_app(self.app) self.db2_redis = Redis(config_prefix='DB2') self.app.config['DB2_URL'] = "redis://localhost:6379" self.app.config['DB2_DATABASE'] = 1 self.db2_redis.init_app(self.app) self.db3_redis = Redis(config_prefix='DB3') self.app.config['DB3_URL'] = "redis://localhost:6379" self.db3_redis.init_app(self.app) self.db4_redis = Redis(config_prefix='DB4') self.app.config['DB4_URL'] = "redis://localhost:6379/5" self.db4_redis.init_app(self.app) assert self.db1_redis.get('potato') is None assert self.db2_redis.get('potato') is None assert self.db3_redis.get('potato') is None assert self.db4_redis.get('potato') is None
class FlaskRedisTestCase(unittest.TestCase): def setUp(self): """ Create a sample Flask Application """ self.redis = Redis() self.app = flask.Flask('test-flask-redis') def test_init_app(self): """ Test the initation of our Redis extension """ self.redis.init_app(self.app) assert self.redis.get('potato') is None
def test_init_app_behaviour(self): redis = Redis() self.assertRaises(AttributeError, getattr, redis, 'ping') app = self.create_app(REDIS_HOST=TEST_REDIS_HOST, REDIS_PORT=TEST_REDIS_PORT, REDIS_DB=TEST_REDIS_DB) redis.init_app(app) redis.ping()
def test_custom_connection_class_using_string(app): """Test that Redis can be instructed to use a different Redis client using a class reference""" app.config['REDIS_CONNECTION_CLASS'] = 'redis.StrictRedis' redis = Redis() assert redis.client is None redis.init_app(app) assert redis.client is not None assert isinstance(redis.client, rd.StrictRedis)
def test_init_app_behaviour(self): redis = Redis() self.assertRaises(AttributeError, getattr, redis, 'ping') app = self.create_app(REDIS_HOST=TEST_REDIS_HOST, REDIS_PORT=TEST_REDIS_PORT, REDIS_DB=TEST_REDIS_DB) redis.init_app(app) # Extensions initialized by init_app are not bound to any app # So you need an application context to access it self.assertRaises(RuntimeError, redis.ping) with get_context(app): redis.ping()
def test_custom_connection_class_using_class(app): """Test that Redis can be instructed to use a different Redis client using a class reference""" class FakeProvider(object): @classmethod def from_url(cls, *args, **kwargs): return cls() app.config['REDIS_CONNECTION_CLASS'] = FakeProvider redis = Redis() assert redis.client is None redis.init_app(app) assert redis.client is not None assert isinstance(redis.client, FakeProvider)
def test_init_app(app): """Test that a constructor without app instance will not initialize the connection. After Redis.init_app(app) is called, the connection will be initialized. """ redis = Redis() assert redis.client is None redis.init_app(app) assert redis.client is not None assert hasattr(redis.client, 'connection_pool') if hasattr(app, 'extensions'): assert 'redis' in app.extensions assert app.extensions['redis'] == redis
def create_app(object_name): # config app.config.from_object(object_name) # app.config.update( # DEBUG=True, # SECRET_KEY='secret_xxx' # ) # Redis Configs redis_instance = Redis() app.register_blueprint(login_app) app.register_blueprint(dashboard_app) app.register_blueprint(task_app) redis_instance.init_app(app) login_manager.init_app(app) return app
class FlaskRedisTestCase(unittest.TestCase): def setUp(self): """ Create a sample Flask Application """ self.redis = Redis() self.app = flask.Flask(__name__) def test_init_app(self): """ Test the initation of our Redis extension """ self.redis.init_app(self.app) assert self.redis.get("potato") is None def test_custom_prefix(self): """ Test the use of custom config prefixes """ self.db1_redis = Redis(config_prefix="DB1") self.app.config["DB1_URL"] = "redis://localhost" self.app.config["DB1_DATABASE"] = 0 self.db1_redis.init_app(self.app) self.db2_redis = Redis(config_prefix="DB2") self.app.config["DB2_URL"] = "redis://localhost" self.app.config["DB2_DATABASE"] = 1 self.db2_redis.init_app(self.app) assert self.db1_redis.get("potato") is None assert self.db2_redis.get("potato") is None
# All rights reserved # @Author: 'Wu Dong <*****@*****.**>' # @Time: '2020-06-28 17:00' from flask import Flask from flask_redis import Redis, K_RDS_DEFAULT_BIND redis = Redis() app = Flask(__name__) app.config[K_RDS_DEFAULT_BIND] = "default" app.config["REDIS_BINDS"] = { "default": { "REDIS_PREFIX": "DEFAULT:", "REDIS_URL": "redis://:[email protected]:16379/12", }, "DB12": { "REDIS_PREFIX": "EG12:", "REDIS_URL": "redis://:[email protected]:16379/12", }, "DB13": { "REDIS_PREFIX": "EG13:", "REDIS_URL": "redis://:[email protected]:16379/13", } } redis.init_app(app) if __name__ == "__main__": redis.set("EXP2:SET", "EXP2:VALUE1", 60) print(redis.get("EXP2:SET"))
app = Flask(__name__) login_manager = LoginManager() db = SQLAlchemy() socketio = SocketIO() oauth = OAuth(app) redis = Redis() moment = Moment() with app.app_context(): config_name = os.getenv('FLASK_CONFIG') or 'development' app.config.from_object(config[config_name]) config[config_name].init_app(app) db.app = app db.init_app(app) login_manager.init_app(app) login_manager.session_protection = 'strong' login_manager.login_view = 'login' login_manager.login_message = u'请先登陆系统,若遗忘密码,请联系管理员' login_manager.login_message_category = 'warning' socketio.init_app(app, async_mode=async_mode) redis.init_app(app) moment.init_app(app) from .views import * # noqa from .models import * # noqa