Python fakeredis.FakeStrictRedis() Examples

The following are 30 code examples of fakeredis.FakeStrictRedis(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fakeredis , or try the search function .
Example #1
Source File: test_redis_tag.py    From tache with MIT License 6 votes vote down vote up
def test_multi_tag_kwargs():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    class A(object):
        @cache.cached(key_func=kwargs_key_generator, tags=["a:{a}", "b:{b}", "c"])
        def add(self, a, b):
            return a + b + random.randint(1, 10000)

    add_result1 = A().add(a=5, b=6)
    add_result2 = A().add(a=5, b=7)
    add_result3 = A().add(a=1, b=8)
    add_result4 = A().add(a=1, b=8)
    add_result5 = A().add(a=2, b=9)
    A().add.invalidate_tag("a:5")
    A().add.invalidate_tag("b:8")
    assert add_result1 != A().add(a=5, b=6)
    assert add_result2 != A().add(a=5, b=7)
    assert add_result3 != A().add(a=1, b=8)
    assert add_result4 != A().add(a=1, b=8)
    assert add_result5 == A().add(a=2, b=9)
    A().add.invalidate_tag("c")
    assert add_result5 != A().add(a=2, b=9) 
Example #2
Source File: conftest.py    From code-coverage with Mozilla Public License 2.0 6 votes vote down vote up
def mock_cache(mock_secrets, mock_bucket, tmpdir, monkeypatch):
    """
    Mock a GCPCache instance, using fakeredis and a mocked GCP bucket
    """
    from code_coverage_backend.gcp import GCPCache

    class MockCache(GCPCache):
        def __init__(self):
            self.redis = fakeredis.FakeStrictRedis()
            self.reports_dir = tmpdir.mkdtemp()
            self.bucket = mock_bucket

    cache = MockCache()

    # Set global cache too
    monkeypatch.setattr(code_coverage_backend.gcp, "__cache", cache)

    return cache 
Example #3
Source File: storage.py    From optuna with MIT License 6 votes vote down vote up
def __enter__(self):
        # type: () -> optuna.storages.BaseStorage

        if self.storage_specifier == "inmemory":
            return optuna.storages.InMemoryStorage()
        elif self.storage_specifier == "sqlite":
            self.tempfile = tempfile.NamedTemporaryFile()
            url = "sqlite:///{}".format(self.tempfile.name)
            return optuna.storages.RDBStorage(
                url, engine_kwargs={"connect_args": {"timeout": SQLITE3_TIMEOUT}},
            )
        elif self.storage_specifier == "cache":
            self.tempfile = tempfile.NamedTemporaryFile()
            url = "sqlite:///{}".format(self.tempfile.name)
            return optuna.storages._CachedStorage(
                optuna.storages.RDBStorage(
                    url, engine_kwargs={"connect_args": {"timeout": SQLITE3_TIMEOUT}},
                )
            )
        elif self.storage_specifier == "redis":
            storage = optuna.storages.RedisStorage("redis://localhost")
            storage._redis = fakeredis.FakeStrictRedis()
            return storage
        else:
            assert False 
Example #4
Source File: test_tagged_cache.py    From cachy with MIT License 6 votes vote down vote up
def test_redis_cache_tags_push_forever_keys_correctly(self):
        store = flexmock(RedisStore(redis_class=FakeStrictRedis))
        tag_set = flexmock(TagSet(store, ['foo', 'bar']))

        tag_set.should_receive('get_namespace').and_return('foo|bar')
        redis = RedisTaggedCache(store, tag_set)

        store.should_receive('get_prefix').and_return('prefix:')
        conn = flexmock()
        store.should_receive('connection').and_return(conn)
        conn.should_receive('lpush').once()\
            .with_args('prefix:foo:forever', 'prefix:%s:key1' % hashlib.sha1(b'foo|bar').hexdigest())
        conn.should_receive('lpush').once()\
            .with_args('prefix:bar:forever', 'prefix:%s:key1' % hashlib.sha1(b'foo|bar').hexdigest())

        store.should_receive('forever').with_args(hashlib.sha1(b'foo|bar').hexdigest() + ':key1', 'key1:value')

        redis.forever('key1', 'key1:value') 
Example #5
Source File: webrecorder_player.py    From conifer with Apache License 2.0 6 votes vote down vote up
def _patch_redis(self):
        redis.StrictRedis = FakeStrictRedis

        if not self.cache_dir:
            return

        if self.inputs:
            self.cache_dir = os.path.join(os.path.dirname(self.inputs[0]), self.cache_dir)
            try:
                os.makedirs(self.cache_dir)
            except OSError:
                pass

            name = os.path.basename(self.inputs[0]) +'-cache.json.gz'
            cache_db = os.path.join(self.cache_dir, name)

            self.serializer = FakeRedisSerializer(cache_db, self.inputs) 
Example #6
Source File: test_redis_tag.py    From tache with MIT License 6 votes vote down vote up
def test_function_tag_kwargs():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    @cache.cached(key_func=kwargs_key_generator, tags=[lambda *args, **kwargs: "add:{0}".format(kwargs['a'] + kwargs['b'])])
    def add(a, b):
        return a + b + random.randint(1, 10000)

    add_result1 = add(a=5, b=6)
    add_result2 = add(a=4, b=7)
    add_result3 = add(a=5, b=8)

    # cache 生效
    assert add(a=5, b=6) == add_result1
    assert add(a=4, b=7) == add_result2
    assert add(a=5, b=8) == add_result3

    add.invalidate_tag("add:11")
    assert add(a=5, b=6) != add_result1
    assert add(a=4, b=7) != add_result2
    assert add(a=5, b=8) == add_result3 
Example #7
Source File: test_redis_tag.py    From tache with MIT License 6 votes vote down vote up
def test_function_tag():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    @cache.cached(tags=[lambda *args, **kwargs: "add:{0}".format(args[0] + args[1])])
    def add(a, b):
        return a + b + random.randint(1, 10000)

    add_result1 = add(5, 6)
    add_result2 = add(4, 7)
    add_result3 = add(5, 8)

    # cache 生效
    assert add(5, 6) == add_result1
    assert add(4, 7) == add_result2
    assert add(5, 8) == add_result3

    add.invalidate_tag("add:11")
    assert add(5, 6) != add_result1
    assert add(4, 7) != add_result2
    assert add(5, 8) == add_result3 
Example #8
Source File: test_redis_tag.py    From tache with MIT License 6 votes vote down vote up
def test_multi_tag():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    class A(object):
        @cache.cached(tags=["a:{0}", "b:{1}", "c"])
        def add(self, a, b):
            return a + b + random.randint(1, 10000)

    add_result1 = A().add(5, 6)
    add_result2 = A().add(5, 7)
    add_result3 = A().add(1, 8)
    add_result4 = A().add(1, 8)
    add_result5 = A().add(2, 9)
    A().add.invalidate_tag("a:5")
    A().add.invalidate_tag("b:8")
    assert add_result1 != A().add(5, 6)
    assert add_result2 != A().add(5, 7)
    assert add_result3 != A().add(1, 8)
    assert add_result4 != A().add(1, 8)
    assert add_result5 == A().add(2, 9)
    A().add.invalidate_tag("c")
    assert add_result5 != A().add(2, 9) 
Example #9
Source File: test_redis_cache.py    From tache with MIT License 6 votes vote down vote up
def test_cache_classmethod():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    class AC(object):

        count = 0

        @cache.cached()
        @classmethod
        def add(cls, a, b):
            return a + b + random.randint(1, 100)

        @cache.cached()
        @classmethod
        def plus(cls, a, b):
            cls.__name__ = 'AD'
            return a + b + random.randint(1, 100)

    assert AC.add(3, 4) == AC.add(3, 4) == AC().add(3, 4)
    assert AC.plus(3, 4) != AC.plus(3, 4) 
Example #10
Source File: test_redis_cache.py    From tache with MIT License 6 votes vote down vote up
def test_not_cache_None():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    global i
    i = 0

    @cache.cached(should_cache_fn=lambda value: value is not None)
    def incr(by):
        global i
        i += 1
        return by

    incr(None)
    incr(None)
    assert i == 2
    incr(1)
    incr(1)
    assert i == 3 
Example #11
Source File: test_redis_batch.py    From tache with MIT License 6 votes vote down vote up
def test_batch_staticmethod():
    r = fakeredis.FakeStrictRedis()
    r.flushall()
    cache = RedisCache(conn=r)

    class ABS(object):

        count = 0

        @cache.batch()
        @staticmethod
        def list(*ids):
            result = []
            for i in ids:
                result.append(i + random.randint(1, 100))
            return result

    assert ABS.list(3, 4) == ABS.list(3, 4) == ABS().list(3, 4) 
Example #12
Source File: redis_store.py    From plaso with Apache License 2.0 6 votes vote down vote up
def _GetRedisClient(self):
    """Creates a Redis client for testing.

    This method will attempt to use a Redis server listening on localhost and
    fallback to a fake Redis client if no server is availble or the connection
    timed out.

    Returns:
      Redis: a Redis client.
    """
    try:
      redis_client = redis.from_url(self._REDIS_URL, socket_timeout=60)
      redis_client.ping()
    except redis.exceptions.ConnectionError:
      redis_client = fakeredis.FakeStrictRedis()

    return redis_client 
Example #13
Source File: test_redis.py    From sentry-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_basic(sentry_init, capture_events):
    sentry_init(integrations=[RedisIntegration()])
    events = capture_events()

    connection = FakeStrictRedis()

    connection.get("foobar")
    capture_message("hi")

    (event,) = events
    (crumb,) = event["breadcrumbs"]

    assert crumb == {
        "category": "redis",
        "message": "GET 'foobar'",
        "data": {"redis.key": "foobar", "redis.command": "GET"},
        "timestamp": crumb["timestamp"],
        "type": "redis",
    } 
Example #14
Source File: test_rq.py    From sentry-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_basic(sentry_init, capture_events):
    sentry_init(integrations=[RqIntegration()])
    events = capture_events()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.SimpleWorker([queue], connection=queue.connection)

    queue.enqueue(crashing_job, foo=42)
    worker.work(burst=True)

    (event,) = events

    (exception,) = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError"
    assert exception["mechanism"]["type"] == "rq"
    assert exception["stacktrace"]["frames"][-1]["vars"]["foo"] == "42"

    assert event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
    assert event["extra"]["rq-job"] == {
        "args": [],
        "description": "tests.integrations.rq.test_rq.crashing_job(foo=42)",
        "func": "tests.integrations.rq.test_rq.crashing_job",
        "job_id": event["extra"]["rq-job"]["job_id"],
        "kwargs": {"foo": 42},
    } 
Example #15
Source File: redis.py    From dino with Apache License 2.0 6 votes vote down vote up
def __init__(self, env, host: str, port: int = 6379, db: int = 0):
        if env.config.get(ConfigKeys.TESTING, False) or host == 'mock':
            from fakeredis import FakeStrictRedis

            self.redis_pool = None
            self.redis_instance = FakeStrictRedis(host=host, port=port, db=db)
        else:
            self.redis_pool = redis.ConnectionPool(host=host, port=port, db=db)
            self.redis_instance = None

        self.cache = MemoryCache()

        args = sys.argv
        for a in ['--bind', '-b']:
            bind_arg_pos = [i for i, x in enumerate(args) if x == a]
            if len(bind_arg_pos) > 0:
                bind_arg_pos = bind_arg_pos[0]
                break

        self.listen_port = 'standalone'
        if bind_arg_pos is not None and not isinstance(bind_arg_pos, list):
            self.listen_port = args[bind_arg_pos + 1].split(':')[1]

        self.listen_host = socket.gethostname().split('.')[0] 
Example #16
Source File: test_rq.py    From sentry-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_transport_shutdown(sentry_init, capture_events_forksafe):
    sentry_init(integrations=[RqIntegration()])

    events = capture_events_forksafe()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.Worker([queue], connection=queue.connection)

    queue.enqueue(crashing_job, foo=42)
    worker.work(burst=True)

    event = events.read_event()
    events.read_flush()

    (exception,) = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError" 
Example #17
Source File: utils.py    From browsertrix with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.redis = fakeredis.FakeStrictRedis(decode_responses=True) 
Example #18
Source File: test_api.py    From browsertrix with Apache License 2.0 5 votes vote down vote up
def test_delete_crawl(self):
        res = self.client.delete(f'/crawl/{self.crawl_id}')

        assert res.json()['success'] == True

        res = self.client.delete(f'/crawl/{self.crawl_id_2}')

        assert res.json()['success'] == True

        assert fakeredis.FakeStrictRedis().keys('a:*') == []

        res = self.client.delete(f'/crawl/{self.crawl_id_2}')

        assert res.json()['detail'] == 'crawl not found' 
Example #19
Source File: test_flask.py    From python-dockerflow with Mozilla Public License 2.0 5 votes vote down vote up
def redis_store(app):
    return FlaskRedis.from_custom_provider(FakeStrictRedis, app) 
Example #20
Source File: test_flask.py    From python-dockerflow with Mozilla Public License 2.0 5 votes vote down vote up
def test_full_redis_check(mocker):
    app = Flask("redis-check")
    app.debug = True
    redis_store = FlaskRedis.from_custom_provider(FakeStrictRedis, app)
    dockerflow = Dockerflow(app, redis=redis_store)
    assert "check_redis_connected" in dockerflow.checks

    with app.test_client() as test_client:
        response = test_client.get("/__heartbeat__")
        assert response.status_code == 200
        assert json.loads(response.data.decode())["status"] == "ok" 
Example #21
Source File: test_flask.py    From python-dockerflow with Mozilla Public License 2.0 5 votes vote down vote up
def test_full_redis_check_error(mocker):
    app = Flask("redis-check")
    redis_store = FlaskRedis.from_custom_provider(FakeStrictRedis, app)
    ping = mocker.patch.object(redis_store, "ping")
    ping.side_effect = redis.ConnectionError
    dockerflow = Dockerflow(app, redis=redis_store)
    assert "check_redis_connected" in dockerflow.checks

    with app.test_client() as test_client:
        response = test_client.get("/__heartbeat__")
        assert response.status_code == 500
        assert json.loads(response.data.decode())["status"] == "error" 
Example #22
Source File: test_redis.py    From lemur with Apache License 2.0 5 votes vote down vote up
def test_write_and_read_from_redis():
    function = f"{__name__}.{sys._getframe().f_code.co_name}"

    red = fakeredis.FakeStrictRedis()
    key = f"{function}.last_success"
    value = int(time.time())
    assert red.set(key, value) is True
    assert (int(red.get(key)) == value) is True 
Example #23
Source File: test_validate_redis.py    From quay with Apache License 2.0 5 votes vote down vote up
def test_validate_redis(unvalidated_config, user, user_password, use_mock, expected, app):
    with patch("redis.StrictRedis" if use_mock else "redis.None", FakeStrictRedis):
        validator = RedisValidator()
        unvalidated_config = ValidatorContext(unvalidated_config)

        unvalidated_config.user = AttrDict(dict(username=user))
        unvalidated_config.user_password = user_password

        if expected is not None:
            with pytest.raises(expected):
                validator.validate(unvalidated_config)
        else:
            validator.validate(unvalidated_config) 
Example #24
Source File: test_subscription_transport.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def pubsub(monkeypatch):
    monkeypatch.setattr(redis, 'StrictRedis', fakeredis.FakeStrictRedis)
    return RedisPubsub() 
Example #25
Source File: test_subscription_manager.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def pubsub(monkeypatch):
    monkeypatch.setattr(redis, 'StrictRedis', fakeredis.FakeStrictRedis)
    return RedisPubsub() 
Example #26
Source File: test_trackers.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def __init__(self, _domain: Domain) -> None:
        self.red = fakeredis.FakeStrictRedis()
        self.record_exp = None

        # added in redis==3.3.0, but not yet in fakeredis
        self.red.connection_pool.connection_class.health_check_interval = 0

        TrackerStore.__init__(self, _domain) 
Example #27
Source File: test_lock_store.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        import fakeredis

        self.red = fakeredis.FakeStrictRedis()

        # added in redis==3.3.0, but not yet in fakeredis
        self.red.connection_pool.connection_class.health_check_interval = 0

        super(RedisLockStore, self).__init__() 
Example #28
Source File: migrate4.0.py    From conifer with Apache License 2.0 5 votes vote down vote up
def __init__(self, old_redis_url, new_redis_url, dry_run=True,
                 per_recording_list=False, s3_import=False, s3_root=None):
        self.old_redis = StrictRedis.from_url(old_redis_url, decode_responses=True)
        self.dry_run = dry_run
        self.per_recording_list = per_recording_list
        self.s3_import = s3_import

        if s3_import:
            assert(s3_root)
            import boto3
            self.s3_root = s3_root
            self.s3 = boto3.client('s3')
        else:
            self.s3_root = None
            self.s3 = None

        if self.dry_run:
            import redis
            redis.StrictRedis = fakeredis.FakeStrictRedis
            self.redis = FakeStrictRedis.from_url(new_redis_url, decode_responses=True)
        else:
            self.redis = StrictRedis.from_url(new_redis_url, decode_responses=True)

        print('Redis Inited')

        self.cli = CLIUserManager(new_redis_url) 
Example #29
Source File: test_redis_store.py    From cachy with MIT License 5 votes vote down vote up
def setUp(self):
        server = FakeServer()
        server.connected = True
        self.store = RedisStore(
            prefix='prefix:', redis_class=FakeStrictRedis, server=server
        )
        self.redis = FakeStrictRedis(server=server)

        super(RedisStoreTestCase, self).setUp() 
Example #30
Source File: redis.py    From dino with Apache License 2.0 5 votes vote down vote up
def __init__(self, env: GNEnvironment, host: str, port: int = 6379, db: int = 0):
        if environ.env.config.get(ConfigKeys.TESTING, False) or host == 'mock':
            from fakeredis import FakeStrictRedis as Redis
        else:
            from redis import Redis

        self.env = env
        self.redis = Redis(host=host, port=port, db=db)
        self.acl_validator = AclValidator()