Esempio n. 1
0
 def test_org_quota_overrides_user_quota(self):
     redis_conn = MockRedis()
     redis_conn.hset('rails:users:my_username', 'mapzen_routing_quota', 42)
     redis_conn.hset('rails:orgs:acme', 'mapzen_routing_quota', 31415)
     redis_config = ServicesRedisConfig(redis_conn).build('my_username', 'acme')
     assert 'mapzen_routing_quota' in redis_config
     assert int(redis_config['mapzen_routing_quota']) == 31415
 def test_it_picks_mapzen_routing_quota_from_redis(self):
     redis_conn = MockRedis()
     redis_conn.hset('rails:users:my_username', 'mapzen_routing_quota', 42)
     redis_config = ServicesRedisConfig(redis_conn).build(
         'my_username', None)
     assert 'mapzen_routing_quota' in redis_config
     assert int(redis_config['mapzen_routing_quota']) == 42
    def test_can_get_a_config_field(self):
        connection = MockRedis()
        connection.hset(self.CONFIG_HASH_KEY, 'field1', 42)
        redis_config = RedisConfigStorage(connection, self.CONFIG_HASH_KEY)

        value = redis_config.get('field1')
        assert type(value) == str # this is something to take into account, redis always returns strings
        assert value == '42'
Esempio n. 4
0
class TestRedisPubSub(object):
    def setup(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_publish(self):
        channel = 'ch#1'
        msg = 'test message'
        self.redis.publish(channel, msg)
        eq_(self.redis.pubsub()[channel], [msg])
    def test_can_get_a_config_field(self):
        connection = MockRedis()
        connection.hset(self.CONFIG_HASH_KEY, 'field1', 42)
        redis_config = RedisConfigStorage(connection, self.CONFIG_HASH_KEY)

        value = redis_config.get('field1')
        assert type(
            value
        ) == str  # this is something to take into account, redis always returns strings
        assert value == '42'
Esempio n. 6
0
class TestRedisPubSub(object):

    def setup(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_publish(self):
        channel = 'ch#1'
        msg = 'test message'
        self.redis.publish(channel, msg)
        eq_(self.redis.pubsub[channel], [msg])
Esempio n. 7
0
 def test_routing_quota_check_passes_when_enough_quota(self):
     user_service_config = RoutingConfigMock(
         username=self.username,
         organization=None,
         service_type=self.service_type,
         monthly_quota=1000,
         period_end_date=datetime.today(),
         soft_limit=False)
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.day, 999)
     assert QuotaChecker(user_service_config, redis_conn).check() == True
Esempio n. 8
0
class TestRedisPubSub(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_publish(self):
        channel = 'ch#1'
        msg = 'test message'
        self.redis.publish(channel, msg)
        self.assertListEqual(self.redis.pubsub[channel], [msg])
Esempio n. 9
0
 def test_routing_quota_check_passes_when_enough_quota(self):
     user_service_config = RoutingConfigMock(
         username = self.username,
         organization = None,
         service_type = self.service_type,
         monthly_quota = 1000,
         period_end_date = datetime.today(),
         soft_limit = False
     )
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.day, 999)
     assert QuotaChecker(user_service_config, redis_conn).check() == True
Esempio n. 10
0
 def test_routing_quota_check_passes_if_no_quota_but_soft_limit(self):
     user_service_config = RoutingConfigMock(
         username=self.username,
         organization=None,
         service_type=self.service_type,
         monthly_quota=1000,
         period_end_date=datetime.today(),
         soft_limit=True)
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.day, 1001)
     checker = QuotaChecker(user_service_config, redis_conn)
     assert checker.check() == True
Esempio n. 11
0
 def test_routing_quota_check_passes_if_no_quota_but_soft_limit(self):
     user_service_config = RoutingConfigMock(
         username = self.username,
         organization = None,
         service_type = self.service_type,
         monthly_quota = 1000,
         period_end_date = datetime.today(),
         soft_limit = True
     )
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.day, 1001)
     checker = QuotaChecker(user_service_config, redis_conn)
     assert checker.check() == True
Esempio n. 12
0
 def test_routing_quota_check_fails_when_quota_exhausted(self):
     user_service_config = RoutingConfigMock(
         username=self.username,
         organization=None,
         service_type=self.service_type,
         monthly_quota=1000,
         period_end_date=datetime.today(),
         soft_limit=False)
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.strftime('%d'),
                        1001)
     checker = QuotaChecker(user_service_config, redis_conn)
     assert checker.check() == False
Esempio n. 13
0
def models():
    """Create some sample machine learning models."""
    rng = np.random.RandomState(42)
    X = rng.random_sample((20, 5))
    y = rng.random_sample(20)
    lm = linear_model.LinearRegression()
    dt = tree.DecisionTreeRegressor()
    svr = svm.SVR()
    lm.fit(X, y)
    dt.fit(X, y)
    svr.fit(X, y)
    r = MockRedis()
    r.set("1.0.0", pickle.dumps(lm))
    r.set("1.1.0", pickle.dumps(dt))
    r.set("1.2.0", pickle.dumps(svr))
    return r
Esempio n. 14
0
 def test_routing_quota_check_fails_right_in_the_limit(self):
     """
     I have 1000 credits and I just spent 1000 today. I should not pass
     the check to perform the 1001th routing operation.
     """
     user_service_config = RoutingConfigMock(
         username=self.username,
         organization=None,
         service_type=self.service_type,
         monthly_quota=1000,
         period_end_date=datetime.today(),
         soft_limit=False)
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.day, 1000)
     checker = QuotaChecker(user_service_config, redis_conn)
     assert checker.check() == False
Esempio n. 15
0
 def test_routing_quota_check_fails_right_in_the_limit(self):
     """
     I have 1000 credits and I just spent 1000 today. I should not pass
     the check to perform the 1001th routing operation.
     """
     user_service_config = RoutingConfigMock(
         username = self.username,
         organization = None,
         service_type = self.service_type,
         monthly_quota = 1000,
         period_end_date = datetime.today(),
         soft_limit = False
     )
     redis_conn = MockRedis()
     redis_conn.zincrby(self.redis_key, self.period_end_date.day, 1000)
     checker = QuotaChecker(user_service_config, redis_conn)
     assert checker.check() == False
Esempio n. 16
0
 def setUp(self):
     user_geocoder_config = UserGeocoderConfig(
         username='******',
         organization=None,
         period_end_date=datetime.date.today())
     redis_conn = MockRedis()
     self.user_metrics_service = UserMetricsService(user_geocoder_config,
                                                    redis_conn)
Esempio n. 17
0
 def init_app(self, app):
     if app.config.get("TESTING", False):
         from mockredis import MockRedis
         self.redis_cli = MockRedis()
     else:
         self.redis_cli = Redis(host=app.config['REDIS_HOST'],
                                port=app.config.get('REDIS_PORT', 6379),
                                db=app.config.get('REDIS_DB', 0))
Esempio n. 18
0
    def test_register_script_client(self):
        # lpush two values in LIST1 in first instance of redis
        self.redis.lpush(LIST1, VAL2, VAL1)

        # create script on first instance of redis
        script_content = LPOP_SCRIPT
        script = self.redis.register_script(script_content)

        # lpush two values in LIST1 in redis2 (second instance of redis)
        redis2 = MockRedis()
        redis2.lpush(LIST1, VAL4, VAL3)

        # execute LPOP script on redis2 instance
        list_item = script(keys=[LIST1], client=redis2)

        # validate lpop from LIST1 in redis2
        eq_(VAL3, list_item)
        eq_([VAL4], redis2.lrange(LIST1, 0, -1))
        eq_([VAL1, VAL2], self.redis.lrange(LIST1, 0, -1))
Esempio n. 19
0
    def test_register_script_client(self):
        # lpush two values in LIST1 in first instance of redis
        self.redis.lpush(LIST1, VAL2, VAL1)

        # create script on first instance of redis
        script_content = LPOP_SCRIPT
        script = self.redis.register_script(script_content)

        # lpush two values in LIST1 in redis2 (second instance of redis)
        redis2 = MockRedis()
        redis2.lpush(LIST1, VAL4, VAL3)

        # execute LPOP script on redis2 instance
        list_item = script(keys=[LIST1], client=redis2)

        # validate lpop from LIST1 in redis2
        eq_(VAL3, list_item)
        eq_([VAL4], redis2.lrange(LIST1, 0, -1))
        eq_([VAL1, VAL2], self.redis.lrange(LIST1, 0, -1))
    def setUp(self):
        self.pubsub = MockRedis()
        self.trans = connection.begin()
        self.session = Session(connection)

        currency = Currency(name='Pesos', code='ARG')
        self.exchange = Exchange(name='Merval', code='MERV', currency=currency)
        self.owner = Owner(name='poor owner')
        self.broker = Broker(name='broker1')
        self.account = Account(owner=self.owner, broker=self.broker)
        self.account.deposit(Money(amount=10000, currency=currency))
Esempio n. 21
0
    def setUp(self):
        self.pubsub = MockRedis()
        self.config = PyConfig("test_config.ini")
        self.symbols = ['GOOG']
        self.strategy = StrategyFactory.create_strategy(
            self.config.get(CONF_ANALYZER_SECTION, CONF_STRATEGY_NAME),
            self.symbols, self.config)

        self.strategy2 = StrategyFactory.create_strategy(
            self.config.get(CONF_ANALYZER_SECTION, CONF_STRATEGY_NAME),
            self.symbols, self.config.getSection(CONF_ANALYZER_SECTION))
Esempio n. 22
0
def test_main_view_assert_error():
    """Test main view with fake redis."""
    import pytest
    import shortmagnet
    from mockredis import MockRedis

    redis = MockRedis()
    with patch('shortmagnet.request') as request:
        with pytest.raises(AssertionError):
            request.method = "POST"
            request.form = {'magnet': 'foobar'}
            shortmagnet.index("foobar", redis)
 def setUp(self):
     self._redis_connection = MockRedis()
     self._server_config = InMemoryConfigStorage({
         "server_conf": {
             "environment": "testing"
         },
         "mapzen_conf": {
             "geocoder": {
                 "api_key": "search-xxxxxxx",
                 "monthly_quota": 1500000
             }
         },
         "logger_conf": {}
     })
     self._username = '******'
     self._user_key = "rails:users:{0}".format(self._username)
     self._user_config = RedisUserConfigStorageBuilder(
         self._redis_connection, self._username).get()
     self._org_config = RedisOrgConfigStorageBuilder(
         self._redis_connection, None).get()
     self._set_default_config_values()
Esempio n. 24
0
 def test_org_quota_overrides_user_quota(self):
     redis_conn = MockRedis()
     redis_conn.hset('rails:users:my_username', 'mapzen_routing_quota', 42)
     redis_conn.hset('rails:orgs:acme', 'mapzen_routing_quota', 31415)
     redis_config = ServicesRedisConfig(redis_conn).build('my_username', 'acme')
     assert 'mapzen_routing_quota' in redis_config
     assert int(redis_config['mapzen_routing_quota']) == 31415
Esempio n. 25
0
def create_redis(_app):
    global redis_client

    # loading redis
    from flask_redis import FlaskRedis

    if app.config['TESTING']:
        # loading mockredis
        from mockredis import MockRedis
        redis_client = MockRedis(FlaskRedis.from_custom_provider(MockRedis))
    else:
        # loading the real redis instance
        redis_client = FlaskRedis(app)
Esempio n. 26
0
 def setUp(self):
     self.mock = mox.Mox()
     self.pubsub = MockRedis()
     self.pesos = Currency(name='Pesos', code='ARG')
     self.exchange = Exchange(name='Merval', currency=self.pesos)
     self.stock_one = Stock(symbol='PBR',
                            description='Petrobras BR',
                            ISIN='US71654V4086',
                            exchange=self.exchange)
     self.stock_two = Stock(symbol='YPF',
                            description='YPF S.A',
                            ISIN='US9842451000',
                            exchange=self.exchange)
Esempio n. 27
0
class StupidRedis(DefaultClient):
    _fake = MockRedis()

    def __init__(self, server, params, backend):
        self._options = {}
        self._backend = backend
        self._serializer = PickleSerializer({})
        self._compressor = IdentityCompressor({})

    def get_client(self, *args, **kwargs):
        return self._fake

    def close(self, **kw):
        return
Esempio n. 28
0
def test_main_view():
    """Test main view with fake redis."""
    import shortmagnet
    from mockredis import MockRedis

    redis = MockRedis()
    with patch('shortmagnet.request') as request:
        request.method = "POST"
        request.form = {'magnet': 'magnet:foobar'}
        result = shortmagnet.index("magnet:foobar", redis)
        assert result
        request.method = "GET"
        response = shortmagnet.index(result, redis)
        assert response.status_code == 302
        assert response.location == "magnet:foobar"
 def setUp(self):
     plpy_mock_config()
     cartodb_services.init(plpy_mock, _GD={})
     self.username = '******'
     self.orgname = 'test_org'
     self.redis_conn = MockRedis()
     build_redis_user_config(self.redis_conn, self.username, 'geocoding')
     build_redis_org_config(self.redis_conn,
                            self.orgname,
                            'geocoding',
                            provider='mapzen')
     self.environment = 'production'
     plpy_mock._define_result("CDB_Conf_GetConf\('server_conf'\)",
                              [{
                                  'conf': '{"environment": "production"}'
                              }])
     plpy_mock._define_result(
         "CDB_Conf_GetConf\('redis_metadata_config'\)",
         [{
             'conf': '{"redis_host":"localhost","redis_port":"6379"}'
         }])
     plpy_mock._define_result(
         "CDB_Conf_GetConf\('redis_metrics_config'\)",
         [{
             'conf': '{"redis_host":"localhost","redis_port":"6379"}'
         }])
     basic_server_conf = {
         "server_conf": {
             "environment": "testing"
         },
         "mapzen_conf": {
             "geocoder": {
                 "api_key": "search-xxxxxxx",
                 "monthly_quota": 1500000,
                 "service": {
                     "base_url": "http://base"
                 }
             }
         },
         "logger_conf": {}
     }
     self.empty_server_config = InMemoryConfigStorage(basic_server_conf)
     self.empty_redis_config = InMemoryConfigStorage({})
     self.user_config = RedisUserConfigStorageBuilder(
         self.redis_conn, self.username).get()
     self.org_config = RedisOrgConfigStorageBuilder(self.redis_conn,
                                                    self.orgname).get()
     self.server_config = ServerConfigBackendFactory().get()
Esempio n. 30
0
class TestPipeline(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_pipeline(self):
        """
        Pipeline execution returns all of the saved up values.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.echo("foo")
            pipeline.echo("bar")

            eq_(["foo", "bar"], pipeline.execute())

    def test_set_and_get(self):
        """
        Pipeline execution returns the pipeline, not the intermediate value.
        """
        with self.redis.pipeline() as pipeline:
            eq_(pipeline, pipeline.set("foo", "bar"))
            eq_(pipeline, pipeline.get("foo"))

            eq_([True, "bar"], pipeline.execute())

    def test_scripts(self):
        """
        Verify that script calls work across pipelines.

        This test basically ensures that the pipeline shares
        state with the mock redis instance.
        """
        script_content = "redis.call('PING')"
        sha = sha1(script_content).hexdigest()

        self.redis.register_script(script_content)

        # Script exists in mock redis
        eq_([True], self.redis.script_exists(sha))

        # Script exists in pipeline
        eq_([True], self.redis.pipeline().script_exists(sha).execute()[0])
Esempio n. 31
0
class TestPipeline(TestCase):
    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_pipeline(self):
        """
        Pipeline execution returns all of the saved up values.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.echo("foo")
            pipeline.echo("bar")

            eq_(["foo", "bar"], pipeline.execute())

    def test_set_and_get(self):
        """
        Pipeline execution returns the pipeline, not the intermediate value.
        """
        with self.redis.pipeline() as pipeline:
            eq_(pipeline, pipeline.set("foo", "bar"))
            eq_(pipeline, pipeline.get("foo"))

            eq_([True, "bar"], pipeline.execute())

    def test_scripts(self):
        """
        Verify that script calls work across pipelines.

        This test basically ensures that the pipeline shares
        state with the mock redis instance.
        """
        script_content = "redis.call('PING')"
        sha = sha1(script_content).hexdigest()

        self.redis.register_script(script_content)

        # Script exists in mock redis
        eq_([True], self.redis.script_exists(sha))

        # Script exists in pipeline
        eq_([True], self.redis.pipeline().script_exists(sha).execute()[0])
Esempio n. 32
0
def models():
    """Create some sample machine learning models."""
    rng = np.random.RandomState(42)
    X = rng.random_sample((20, 5))
    y = rng.random_sample(20)
    lm = linear_model.LinearRegression()
    dt = tree.DecisionTreeRegressor()
    svr = svm.SVR()
    lm.fit(X, y)
    dt.fit(X, y)
    svr.fit(X, y)
    r = MockRedis()
    r.set("1.0.0", pickle.dumps(lm))
    r.set("1.1.0", pickle.dumps(dt))
    r.set("1.2.0", pickle.dumps(svr))
    return r
Esempio n. 33
0
class TestRedis(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_get(self):
        self.assertEqual('', self.redis.get('key'))

        self.redis.redis['key'] = 'value'
        self.assertEqual('value', self.redis.get('key'))

    def test_set(self):
        self.assertEqual(None, self.redis.redis.get('key'))
        
        self.redis.set('key', 'value')
        self.assertEqual('value', self.redis.redis.get('key'))
 def setUp(self):
     self.redis_conn = MockRedis()
     plpy_mock_config()
class TestRoutingConfig(TestCase):
    def setUp(self):
        self._redis_conn = MockRedis()
        self._db_conn = plpy_mock
        self._username = '******'
        self._user_key = "rails:users:{0}".format(self._username)
        self._redis_conn.hset(self._user_key, 'period_end_date', '2016-10-10')

    def test_should_pick_quota_from_redis_if_present(self):
        self._redis_conn.hset(self._user_key, 'mapzen_routing_quota', 1000)
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username,
                               orgname)
        assert config.routing_quota == 1000

    def test_org_quota_overrides_user_quota(self):
        self._redis_conn.hset(self._user_key, 'mapzen_routing_quota', 1000)
        orgname = 'my_test_org'
        orgname_key = "rails:orgs:{0}".format(orgname)
        self._redis_conn.hset(orgname_key, 'period_end_date', '2016-05-31')
        self._redis_conn.hset(orgname_key, 'mapzen_routing_quota', 5000)

        # TODO: these are not too relevant for the routing config
        self._redis_conn.hset(orgname_key, 'geocoding_quota', 0)
        self._redis_conn.hset(orgname_key, 'here_isolines_quota', 0)

        config = RoutingConfig(self._redis_conn, self._db_conn, self._username,
                               orgname)
        assert config.routing_quota == 5000

    def test_should_have_soft_limit_false_by_default(self):
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username,
                               orgname)
        assert config.soft_limit == False

    def test_can_set_soft_limit_in_user_conf(self):
        self._redis_conn.hset(self._user_key, 'soft_mapzen_routing_limit',
                              True)
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username,
                               orgname)
        assert config.soft_limit == True
Esempio n. 36
0
class TestRedis(TestCase):
    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_get_types(self):
        '''
        testing type conversions for set/get, hset/hget, sadd/smembers

        Python bools, lists, dicts are returned as strings by
        redis-py/redis.
        '''

        values = list([
            True,
            False,
            [1, '2'],
            {
                'a': 1,
                'b': 'c'
            },
        ])

        self.assertEqual(None, self.redis.get('key'))

        for value in values:
            self.redis.set('key', value)
            self.assertEqual(str(value), self.redis.get('key'), "redis.get")

            self.redis.hset('hkey', 'item', value)
            self.assertEqual(str(value), self.redis.hget('hkey', 'item'))

            self.redis.sadd('skey', value)
            self.assertEqual(set([str(value)]), self.redis.smembers('skey'))

            self.redis.flushdb()

    def test_incr(self):
        '''
        incr, hincr when keys exist
        '''

        values = list([
            (1, '2'),
            ('1', '2'),
        ])

        for value in values:
            self.redis.set('key', value[0])
            self.redis.incr('key')
            self.assertEqual(value[1], self.redis.get('key'), "redis.incr")

            self.redis.hset('hkey', 'attr', value[0])
            self.redis.hincrby('hkey', 'attr')
            self.assertEqual(value[1], self.redis.hget('hkey', 'attr'),
                             "redis.hincrby")

            self.redis.flushdb()

    def test_incr_init(self):
        '''
        incr, hincr, decr when keys do NOT exist
        '''

        self.redis.incr('key')
        self.assertEqual('1', self.redis.get('key'))

        self.redis.hincrby('hkey', 'attr')
        self.assertEqual('1', self.redis.hget('hkey', 'attr'))

        self.redis.decr('dkey')
        self.assertEqual('-1', self.redis.get('dkey'))

    def test_ttl(self):
        self.redis.set('key', 'key')
        self.redis.expire('key', 30)

        result = self.redis.ttl('key')
        # should be an int
        self.assertTrue(isinstance(result, int))
        # should be less than the timeout originally set
        self.assertTrue(result <= 30)

    def test_ttl_when_key_absent(self):
        """Test whether, like the redis-py lib, ttl returns None if the key is absent"""

        self.assertEqual(self.redis.ttl('invalid_key'), None)

    def test_ttl_no_timeout(self):
        """
        Test whether, like the redis-py lib, ttl returns None if the key has no timeout set.
        """
        self.redis.set('key', 'key')
        self.assertEqual(self.redis.ttl('key'), None)

    def test_push_pop_returns_str(self):
        key = 'l'
        values = ['5', 5, [], {}]
        for v in values:
            self.redis.rpush(key, v)
            self.assertEquals(self.redis.lpop(key), str(v))
Esempio n. 37
0
class TestScript(object):
    """
    Tests for MockRedis scripting operations
    """

    def setup(self):
        self.redis = MockRedis(load_lua_dependencies=False)
        self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT.encode("utf-8")).hexdigest()

        try:
            lua, lua_globals = MockRedisScript._import_lua(load_dependencies=False)
        except RuntimeError:
            raise SkipTest("mockredispy was not installed with lua support")

        self.lua = lua
        self.lua_globals = lua_globals

        assert_equal_list = """
        function compare_list(list1, list2)
            if #list1 ~= #list2 then
                return false
            end
            for i, item1 in ipairs(list1) do
                if item1 ~= list2[i] then
                    return false
                end
            end
            return true
        end

        function assert_equal_list(list1, list2)
            assert(compare_list(list1, list2))
        end
        return assert_equal_list
        """
        self.lua_assert_equal_list = self.lua.execute(assert_equal_list)

        assert_equal_list_with_pairs = """
        function pair_exists(list1, key, value)
            i = 1
            for i, item1 in ipairs(list1) do
                if i%2 == 1 then
                    if (list1[i] == key) and (list1[i + 1] == value) then
                        return true
                    end
                end
            end
            return false
        end

        function compare_list_with_pairs(list1, list2)
            if #list1 ~= #list2 or #list1 % 2 == 1 then
                return false
            end
            for i = 1, #list1, 2 do
                if not pair_exists(list2, list1[i], list1[i + 1]) then
                    return false
                end
            end
            return true
        end

        function assert_equal_list_with_pairs(list1, list2)
            assert(compare_list_with_pairs(list1, list2))
        end
        return assert_equal_list_with_pairs
        """
        self.lua_assert_equal_list_with_pairs = self.lua.execute(assert_equal_list_with_pairs)

        compare_val = """
        function compare_val(var1, var2)
            return var1 == var2
        end
        return compare_val
        """
        self.lua_compare_val = self.lua.execute(compare_val)

    def test_register_script_lpush(self):
        # lpush two values
        script_content = "redis.call('LPUSH', KEYS[1], ARGV[1], ARGV[2])"
        script = self.redis.register_script(script_content)
        script(keys=[LIST1], args=[VAL1, VAL2])

        # validate insertion
        eq_([VAL2, VAL1], self.redis.lrange(LIST1, 0, -1))

    def test_register_script_lpop(self):
        self.redis.lpush(LIST1, VAL2, VAL1)

        # lpop one value
        script_content = "return redis.call('LPOP', KEYS[1])"
        script = self.redis.register_script(script_content)
        list_item = script(keys=[LIST1])

        # validate lpop
        eq_(VAL1, list_item)
        eq_([VAL2], self.redis.lrange(LIST1, 0, -1))

    def test_register_script_rpoplpush(self):
        self.redis.lpush(LIST1, VAL2, VAL1)
        self.redis.lpush(LIST2, VAL4, VAL3)

        # rpoplpush
        script_content = "redis.call('RPOPLPUSH', KEYS[1], KEYS[2])"
        script = self.redis.register_script(script_content)
        script(keys=[LIST1, LIST2])

        # validate rpoplpush
        eq_([VAL1], self.redis.lrange(LIST1, 0, -1))
        eq_([VAL2, VAL3, VAL4], self.redis.lrange(LIST2, 0, -1))

    def test_register_script_rpop_lpush(self):
        self.redis.lpush(LIST1, VAL2, VAL1)
        self.redis.lpush(LIST2, VAL4, VAL3)

        # rpop from LIST1 and lpush the same value to LIST2
        script_content = """
        local tmp_item = redis.call('RPOP', KEYS[1])
        redis.call('LPUSH', KEYS[2], tmp_item)
        """
        script = self.redis.register_script(script_content)
        script(keys=[LIST1, LIST2])

        # validate rpop and then lpush
        eq_([VAL1], self.redis.lrange(LIST1, 0, -1))
        eq_([VAL2, VAL3, VAL4], self.redis.lrange(LIST2, 0, -1))

    def test_register_script_client(self):
        # lpush two values in LIST1 in first instance of redis
        self.redis.lpush(LIST1, VAL2, VAL1)

        # create script on first instance of redis
        script_content = LPOP_SCRIPT
        script = self.redis.register_script(script_content)

        # lpush two values in LIST1 in redis2 (second instance of redis)
        redis2 = MockRedis()
        redis2.lpush(LIST1, VAL4, VAL3)

        # execute LPOP script on redis2 instance
        list_item = script(keys=[LIST1], client=redis2)

        # validate lpop from LIST1 in redis2
        eq_(VAL3, list_item)
        eq_([VAL4], redis2.lrange(LIST1, 0, -1))
        eq_([VAL1, VAL2], self.redis.lrange(LIST1, 0, -1))

    def test_eval_lpush(self):
        # lpush two values
        script_content = "redis.call('LPUSH', KEYS[1], ARGV[1], ARGV[2])"
        self.redis.eval(script_content, 1, LIST1, VAL1, VAL2)

        # validate insertion
        eq_([VAL2, VAL1], self.redis.lrange(LIST1, 0, -1))

    def test_eval_lpop(self):
        self.redis.lpush(LIST1, VAL2, VAL1)

        # lpop one value
        script_content = "return redis.call('LPOP', KEYS[1])"
        list_item = self.redis.eval(script_content, 1, LIST1)

        # validate lpop
        eq_(VAL1, list_item)
        eq_([VAL2], self.redis.lrange(LIST1, 0, -1))

    def test_eval_lrem(self):
        self.redis.delete(LIST1)
        self.redis.lpush(LIST1, VAL1)

        # lrem one value
        script_content = "return redis.call('LREM', KEYS[1], 0, ARGV[1])"
        value = self.redis.eval(script_content, 1, LIST1, VAL1)

        eq_(value, 1)

    def test_eval_zadd(self):
        # The score and member are reversed when the client is not strict.
        self.redis.strict = False
        script_content = "return redis.call('zadd', KEYS[1], ARGV[1], ARGV[2])"
        self.redis.eval(script_content, 1, SET1, 42, VAL1)

        eq_(42, self.redis.zscore(SET1, VAL1))

    def test_eval_zrangebyscore(self):
        # Make sure the limit is removed.
        script = "return redis.call('zrangebyscore',KEYS[1],ARGV[1],ARGV[2])"
        self.eval_zrangebyscore(script)

    def test_eval_zrangebyscore_with_limit(self):
        # Make sure the limit is removed.
        script = ("return redis.call('zrangebyscore', "
                  "KEYS[1], ARGV[1], ARGV[2], 'LIMIT', 0, 2)")

        self.eval_zrangebyscore(script)

    def eval_zrangebyscore(self, script):
        self.redis.strict = False
        self.redis.zadd(SET1, VAL1, 1)
        self.redis.zadd(SET1, VAL2, 2)

        eq_([],           self.redis.eval(script, 1, SET1, 0, 0))
        eq_([VAL1],       self.redis.eval(script, 1, SET1, 0, 1))
        eq_([VAL1, VAL2], self.redis.eval(script, 1, SET1, 0, 2))
        eq_([VAL2],       self.redis.eval(script, 1, SET1, 2, 2))

    def test_table_type(self):
        self.redis.lpush(LIST1, VAL2, VAL1)
        script_content = """
        local items = redis.call('LRANGE', KEYS[1], ARGV[1], ARGV[2])
        return type(items)
        """
        script = self.redis.register_script(script_content)
        itemType = script(keys=[LIST1], args=[0, -1])
        eq_('table', itemType)

    def test_script_hgetall(self):
        myhash = {"k1": "v1"}
        self.redis.hmset("myhash", myhash)
        script_content = """
        return redis.call('HGETALL', KEYS[1])
        """
        script = self.redis.register_script(script_content)
        item = script(keys=["myhash"])
        ok_(isinstance(item, list))
        eq_(["k1", "v1"], item)

    def test_evalsha(self):
        self.redis.lpush(LIST1, VAL1)
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA

        # validator error when script not registered
        with assert_raises(RedisError) as redis_error:
            self.redis.evalsha(self.LPOP_SCRIPT_SHA, 1, LIST1)

        eq_("Sha not registered", str(redis_error.exception))

        with assert_raises(RedisError):
            self.redis.evalsha(self.LPOP_SCRIPT_SHA, 1, LIST1)

        # load script and then evalsha
        eq_(sha, self.redis.script_load(script))
        eq_(VAL1, self.redis.evalsha(sha, 1, LIST1))
        eq_(0, self.redis.llen(LIST1))

    def test_script_exists(self):
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA
        eq_([False], self.redis.script_exists(sha))
        self.redis.register_script(script)
        eq_([True], self.redis.script_exists(sha))

    def test_script_flush(self):
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA
        self.redis.register_script(script)
        eq_([True], self.redis.script_exists(sha))
        self.redis.script_flush()
        eq_([False], self.redis.script_exists(sha))

    def test_script_load(self):
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA
        eq_([False], self.redis.script_exists(sha))
        eq_(sha, self.redis.script_load(script))
        eq_([True], self.redis.script_exists(sha))

    def test_lua_to_python_none(self):
        lval = self.lua.eval("")
        pval = MockRedisScript._lua_to_python(lval)
        ok_(pval is None)

    def test_lua_to_python_list(self):
        lval = self.lua.eval('{"val1", "val2"}')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, list))
        eq_(["val1", "val2"], pval)

    def test_lua_to_python_long(self):
        lval = self.lua.eval('22')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, long))
        eq_(22, pval)

    def test_lua_to_python_flota(self):
        lval = self.lua.eval('22.2')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, float))
        eq_(22.2, pval)

    def test_lua_to_python_string(self):
        lval = self.lua.eval('"somestring"')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, str))
        eq_("somestring", pval)

    def test_lua_to_python_bool(self):
        lval = self.lua.eval('true')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, bool))
        eq_(True, pval)

    def test_python_to_lua_none(self):
        pval = None
        lval = MockRedisScript._python_to_lua(pval)
        is_null = """
        function is_null(var1)
            return var1 == nil
        end
        return is_null
        """
        lua_is_null = self.lua.execute(is_null)
        ok_(MockRedisScript._lua_to_python(lua_is_null(lval)))

    def test_python_to_lua_string(self):
        pval = "somestring"
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('"somestring"')
        eq_("string", self.lua_globals.type(lval))
        eq_(lval_expected, lval)

    def test_python_to_lua_list(self):
        pval = ["abc", "xyz"]
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('{"abc", "xyz"}')
        self.lua_assert_equal_list(lval_expected, lval)

    def test_python_to_lua_dict(self):
        pval = {"k1": "v1", "k2": "v2"}
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('{"k1", "v1", "k2", "v2"}')
        self.lua_assert_equal_list_with_pairs(lval_expected, lval)

    def test_python_to_lua_long(self):
        pval = long(10)
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('10')
        eq_("number", self.lua_globals.type(lval))
        ok_(MockRedisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))

    def test_python_to_lua_float(self):
        pval = 10.1
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('10.1')
        eq_("number", self.lua_globals.type(lval))
        ok_(MockRedisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))

    def test_python_to_lua_boolean(self):
        pval = True
        lval = MockRedisScript._python_to_lua(pval)
        eq_("boolean", self.lua_globals.type(lval))
        ok_(MockRedisScript._lua_to_python(lval))

    def test_lua_ok_return(self):
        script_content = "return {ok='OK'}"
        script = self.redis.register_script(script_content)
        eq_('OK', script())

    @raises_response_error
    def test_lua_err_return(self):
        script_content = "return {err='ERROR Some message'}"
        script = self.redis.register_script(script_content)
        script()

    def test_concurrent_lua(self):
        script_content = """
local entry = redis.call('HGETALL', ARGV[1])
redis.call('HSET', ARGV[1], 'kk', 'vv')
return entry
"""
        script = self.redis.register_script(script_content)

        for i in range(500):
            self.redis.hmset(i, {'k1': 'v1', 'k2': 'v2', 'k3': 'v3'})

        def lua_thread():
            for i in range(500):
                script(args=[i])

        active_threads = []
        for i in range(10):
            thread = threading.Thread(target=lua_thread)
            active_threads.append(thread)
            thread.start()

        for thread in active_threads:
            thread.join()
Esempio n. 38
0
 def setUp(self):
     self._redis_conn = MockRedis()
     self._db_conn = plpy_mock
     self._username = '******'
     self._user_key = "rails:users:{0}".format(self._username)
     self._redis_conn.hset(self._user_key, 'period_end_date', '2016-10-10')
Esempio n. 39
0
class TestRedis(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_get_types(self):
        '''
        testing type conversions for set/get, hset/hget, sadd/smembers

        Python bools, lists, dicts are returned as strings by
        redis-py/redis.
        '''

        values = list([
            True,
            False,
            [1, '2'],
            {
                'a': 1,
                'b': 'c'
            },
        ])

        self.assertEqual(None, self.redis.get('key'))

        for value in values:
            self.redis.set('key', value)
            self.assertEqual(str(value),
                             self.redis.get('key'),
                             "redis.get")

            self.redis.hset('hkey', 'item', value)
            self.assertEqual(str(value),
                             self.redis.hget('hkey', 'item'))

            self.redis.sadd('skey', value)
            self.assertEqual(set([str(value)]),
                             self.redis.smembers('skey'))

            self.redis.flushdb()

    def test_incr(self):
        '''
        incr, hincr when keys exist
        '''

        values = list([
            (1, '2'),
            ('1', '2'),
        ])

        for value in values:
            self.redis.set('key', value[0])
            self.redis.incr('key')
            self.assertEqual(value[1],
                             self.redis.get('key'),
                             "redis.incr")

            self.redis.hset('hkey', 'attr', value[0])
            self.redis.hincrby('hkey', 'attr')
            self.assertEqual(value[1],
                             self.redis.hget('hkey', 'attr'),
                             "redis.hincrby")

            self.redis.flushdb()

    def test_incr_init(self):
        '''
        incr, hincr, decr when keys do NOT exist
        '''

        self.redis.incr('key')
        self.assertEqual('1', self.redis.get('key'))

        self.redis.hincrby('hkey', 'attr')
        self.assertEqual('1', self.redis.hget('hkey', 'attr'))

        self.redis.decr('dkey')
        self.assertEqual('-1', self.redis.get('dkey'))

    def test_ttl(self):
        self.redis.set('key', 'key')
        self.redis.expire('key', 30)

        result = self.redis.ttl('key')
        # should be an int
        self.assertTrue(isinstance(result, int))
        # should be less than the timeout originally set
        self.assertTrue(result <= 30)

    def test_ttl_when_key_absent(self):
        """Test whether, like the redis-py lib, ttl returns None if the key is absent"""

        self.assertEqual(self.redis.ttl('invalid_key'), None)

    def test_ttl_no_timeout(self):
        """
        Test whether, like the redis-py lib, ttl returns None if the key has no timeout set.
        """
        self.redis.set('key', 'key')
        self.assertEqual(self.redis.ttl('key'), None)

    def test_push_pop_returns_str(self):
        key = 'l'
        values = ['5', 5, [], {}]
        for v in values:
            self.redis.rpush(key, v)
            self.assertEquals(self.redis.lpop(key),
                              str(v))
Esempio n. 40
0
class TestRedisSet(TestCase):
    """set tests"""

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_sadd(self):
        key = "set"
        values = ["one", "uno", "two", "three"]
        for value in values:
            self.assertEquals(1, self.redis.sadd(key, value))

    def test_sadd_multiple(self):
        key = "set"
        values = ["one", "uno", "two", "three"]
        self.assertEquals(4, self.redis.sadd(key, *values))

    def test_sadd_duplicate_key(self):
        key = "set"
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(0, self.redis.sadd(key, "one"))

    def test_scard(self):
        key = "set"
        self.assertEquals(0, self.redis.scard(key))
        self.assertFalse(key in self.redis.redis)
        values = ["one", "uno", "two", "three"]
        self.assertEquals(4, self.redis.sadd(key, *values))
        self.assertEquals(4, self.redis.scard(key))

    def test_sdiff(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sdiff([])

        self.assertEquals(set(), self.redis.sdiff("w"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sdiff("x"))
        self.assertEquals(set(["two", "three"]), self.redis.sdiff("x", "y"))
        self.assertEquals(set(["two", "three"]), self.redis.sdiff(["x", "y"]))
        self.assertEquals(set(["three"]), self.redis.sdiff("x", "y", "z"))
        self.assertEquals(set(["three"]), self.redis.sdiff(["x", "y"], "z"))

    def test_sdiffstore(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sdiffstore("w", [])

        self.assertEquals(3, self.redis.sdiffstore("w", "x"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(2, self.redis.sdiffstore("w", "x", "y"))
        self.assertEquals(set(["two", "three"]), self.redis.smembers("w"))
        self.assertEquals(2, self.redis.sdiffstore("w", ["x", "y"]))
        self.assertEquals(set(["two", "three"]), self.redis.smembers("w"))
        self.assertEquals(1, self.redis.sdiffstore("w", "x", "y", "z"))
        self.assertEquals(set(["three"]), self.redis.smembers("w"))
        self.assertEquals(1, self.redis.sdiffstore("w", ["x", "y"], "z"))
        self.assertEquals(set(["three"]), self.redis.smembers("w"))

    def test_sinter(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sinter([])

        self.assertEquals(set(), self.redis.sinter("w"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sinter("x"))
        self.assertEquals(set(["one"]), self.redis.sinter("x", "y"))
        self.assertEquals(set(["two"]), self.redis.sinter(["x", "z"]))
        self.assertEquals(set(), self.redis.sinter("x", "y", "z"))
        self.assertEquals(set(), self.redis.sinter(["x", "y"], "z"))

    def test_sinterstore(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sinterstore("w", [])

        self.assertEquals(3, self.redis.sinterstore("w", "x"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(1, self.redis.sinterstore("w", "x", "y"))
        self.assertEquals(set(["one"]), self.redis.smembers("w"))
        self.assertEquals(1, self.redis.sinterstore("w", ["x", "z"]))
        self.assertEquals(set(["two"]), self.redis.smembers("w"))
        self.assertEquals(0, self.redis.sinterstore("w", "x", "y", "z"))
        self.assertEquals(set(), self.redis.smembers("w"))
        self.assertEquals(0, self.redis.sinterstore("w", ["x", "y"], "z"))
        self.assertEquals(set(), self.redis.smembers("w"))

    def test_sismember(self):
        key = "set"
        self.assertFalse(self.redis.sismember(key, "one"))
        self.assertFalse(key in self.redis.redis)
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertTrue(self.redis.sismember(key, "one"))
        self.assertFalse(self.redis.sismember(key, "two"))

    def test_ismember_numeric(self):
        """
        Verify string conversion.
        """
        key = "set"
        self.assertEquals(1, self.redis.sadd(key,  1))
        self.assertEquals(set(["1"]), self.redis.smembers(key))
        self.assertTrue(self.redis.sismember(key, "1"))
        self.assertTrue(self.redis.sismember(key, 1))

    def test_smembers(self):
        key = "set"
        self.assertEquals(set(), self.redis.smembers(key))
        self.assertFalse(key in self.redis.redis)
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(set(["one"]), self.redis.smembers(key))
        self.assertEquals(1, self.redis.sadd(key, "two"))
        self.assertEquals(set(["one", "two"]), self.redis.smembers(key))

    def test_smove(self):
        self.assertEquals(0, self.redis.smove("x", "y", "one"))

        self.assertEquals(2, self.redis.sadd("x", "one", "two"))
        self.assertEquals(set(["one", "two"]), self.redis.smembers("x"))
        self.assertEquals(set(), self.redis.smembers("y"))

        self.assertEquals(0, self.redis.smove("x", "y", "three"))
        self.assertEquals(set(["one", "two"]), self.redis.smembers("x"))
        self.assertEquals(set(), self.redis.smembers("y"))

        self.assertEquals(1, self.redis.smove("x", "y", "one"))
        self.assertEquals(set(["two"]), self.redis.smembers("x"))
        self.assertEquals(set(["one"]), self.redis.smembers("y"))

    def test_spop(self):
        key = "set"
        self.assertEquals(None, self.redis.spop(key))
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals("one", self.redis.spop(key))
        self.assertEquals(0, self.redis.scard(key))
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(1, self.redis.sadd(key, "two"))
        first = self.redis.spop(key)
        self.assertTrue(first in ["one", "two"])
        self.assertEquals(1, self.redis.scard(key))
        second = self.redis.spop(key)
        self.assertEquals("one" if first == "two" else "two", second)
        self.assertEquals(0, self.redis.scard(key))

    def test_srandmember(self):
        key = "set"
        # count is None
        self.assertEquals(None, self.redis.srandmember(key))
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals("one", self.redis.srandmember(key))
        self.assertEquals(1, self.redis.scard(key))
        self.assertEquals(1, self.redis.sadd(key, "two"))
        self.assertTrue(self.redis.srandmember(key) in ["one", "two"])
        self.assertEquals(2, self.redis.scard(key))
        # count > 0
        self.assertEquals([], self.redis.srandmember("empty", 1))
        self.assertTrue(self.redis.srandmember(key, 1)[0] in ["one", "two"])
        self.assertEquals(set(["one", "two"]), set(self.redis.srandmember(key, 2)))
        # count < 0
        self.assertEquals([], self.redis.srandmember("empty", -1))
        self.assertTrue(self.redis.srandmember(key, -1)[0] in ["one", "two"])
        members = self.redis.srandmember(key, -2)
        self.assertEquals(2, len(members))
        for member in members:
            self.assertTrue(member in ["one", "two"])

    def test_srem(self):
        key = "set"
        self.assertEquals(0, self.redis.srem(key, "one"))
        self.assertEquals(3, self.redis.sadd(key, "one", "two", "three"))
        self.assertEquals(0, self.redis.srem(key, "four"))
        self.assertEquals(2, self.redis.srem(key, "one", "three"))
        self.assertEquals(1, self.redis.srem(key, "two", "four"))

    def test_sunion(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sunion([])

        self.assertEquals(set(), self.redis.sunion("v"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sunion("x"))
        self.assertEquals(set(["one"]), self.redis.sunion("v", "y"))
        self.assertEquals(set(["one", "two"]), self.redis.sunion(["y", "z"]))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sunion("x", "y", "z"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sunion(["x", "y"], "z"))

    def test_sunionstore(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sunionstore("w", [])

        self.assertEquals(0, self.redis.sunionstore("w", "v"))
        self.assertEquals(set(), self.redis.smembers("w"))

        self.assertEquals(3, self.redis.sunionstore("w", "x"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(1, self.redis.sunionstore("w", "v", "y"))
        self.assertEquals(set(["one"]), self.redis.smembers("w"))

        self.assertEquals(2, self.redis.sunionstore("w", ["y", "z"]))
        self.assertEquals(set(["one", "two"]), self.redis.smembers("w"))

        self.assertEquals(3, self.redis.sunionstore("w", "x", "y", "z"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(3, self.redis.sunionstore("w", ["x", "y"], "z"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))
Esempio n. 41
0
    def setUp(self):
        from redis import StrictRedis as Redis

        self.redis = Redis(db=15)
        self.mockredis = MockRedis()
        self.logger = logging.getLogger("mockredis.TestReads")
Esempio n. 42
0
class TestReads(TestCase):

    def setUp(self):
        from redis import StrictRedis as Redis

        self.redis = Redis(db=15)
        self.mockredis = MockRedis()
        self.logger = logging.getLogger("mockredis.TestReads")

    def tearDown(self):

        test_keys = self.redis.keys("MOCKREDIS*")
        if len(test_keys) > 0:
            self.redis.delete(*test_keys)
        self.mockredis.flushdb()

    def connections(self):
        self.assertEqual("testing_redis",
                         self.redis.echo("testing_redis"))
        self.assertEqual("testing_redis",
                         self.mockredis.echo("testing_redis"))

    def execute(self, method, params):
        '''
        Execute command on redis and mockredis
        '''

        getattr(self.redis, method)(*params)
        getattr(self.mockredis, method)(*params)

    def assert_execute(self, method, params):
        '''
        Assert that method response is the same
        for redis and mockredis
        '''

        self.logger.debug("method={method},params={params}"
                          .format(method=method, params=params))
        self.assertEqual(
            getattr(self.redis, method)(*params),
            getattr(self.mockredis, method)(*params),
            "testing {method}".format(method=method)
        )

    def write(self):
        '''
        successful writes for redis/mockredis (responses NOT compared)
        '''

        for method, details in redis_writes.items():
            for m, params in self.gather_params(method, details):
                self.execute(method, params)

    def test_reads(self):
        '''
        equality of responses for redis/mockredis
        '''

        self.write()

        for method, details in redis_reads.items():
            for m, params in self.gather_params(method, details):
                    self.assert_execute(method, params)

    def gather_params(self, method, details):
        for param in details["params"]:
            for use_key in details["keys"]:
                if "values" in details:
                    values = details["values"]
                else:
                    values = [""]
                for i in range(len(values)):
                    param = [p.format(key=key.format(test=use_key),
                                      attr=attr.format(test=use_key),
                                      value=values[i],
                                      index=i)
                             for p in param]

                    yield method, param

    def test_reads_clean(self):
        '''
        equality of responses on clean db for redis/mockredis
        '''

        for method, details in redis_reads.items():
            for m, params in self.gather_params(method, details):
                    self.assert_execute(method, params)
Esempio n. 43
0
    tree = Tree()
    node_added = signal('node_added')
    for parent, child, created_at in stream:
        candidate = Edge(parent, child, created_at)
        if tree.should_add(candidate):
            tree.add(candidate)
            if tree.is_root(candidate):
                node_added.send('grow_trees', tree_name=tree.name, edge=candidate) 
            node_added.send('grow_trees', tree_name=tree.name, edge=candidate)
    return tree

"""
In this case we use a "redis cache" (mock redis) to aggregate our stats. Redis provides the required atomicity, 
but you may want something else for production (see redis' persistence features, AOF vs RDB)
"""
conn = StrictRedis()

def online_size(sender, tree_name=None, edge=None):
    conn.incr('{0}.size'.format(tree_name)) 
     
def online_depth(sender, tree_name=None, edge=None):
    with conn.lock('{0}.depth'.format(tree_name)):
        current_depth = conn.get('{0}.depth'.format(tree_name)) or 0
        if int(current_depth) < edge.generation:
            conn.set('{0}.depth'.format(tree_name), edge.generation)

"""
Subscribe our online methods to the node_added event.
"""
signal('node_added').connect(online_size)
signal('node_added').connect(online_depth)
Esempio n. 44
0
class TestRedis(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_get(self):
        self.assertEqual(None, self.redis.get('key'))

        self.redis.redis['key'] = 'value'
        self.assertEqual('value', self.redis.get('key'))

    def test_set(self):
        self.assertEqual(None, self.redis.redis.get('key'))

        self.redis.set('key', 'value')
        self.assertEqual('value', self.redis.redis.get('key'))

    def test_get_types(self):
        '''
        testing type coversions for set/get, hset/hget, sadd/smembers

        Python bools, lists, dicts are returned as strings by
        redis-py/redis.
        '''

        values = list([
            True,
            False,
            [1, '2'],
            {
                'a': 1,
                'b': 'c'
            },
        ])

        self.assertEqual(None, self.redis.get('key'))

        for value in values:
            self.redis.set('key', value)
            self.assertEqual(str(value),
                             self.redis.get('key'),
                             "redis.get")

            self.redis.hset('hkey', 'item', value)
            self.assertEqual(str(value),
                             self.redis.hget('hkey', 'item'))

            self.redis.sadd('skey', value)
            self.assertEqual(set([str(value)]),
                             self.redis.smembers('skey'))

            self.redis.flushdb()

    def test_incr(self):
        '''
        incr, hincr when keys exist
        '''

        values = list([
            (1, '2'),
            ('1', '2'),
        ])

        for value in values:
            self.redis.set('key', value[0])
            self.redis.incr('key')
            self.assertEqual(value[1],
                             self.redis.get('key'),
                             "redis.incr")

            self.redis.hset('hkey', 'attr', value[0])
            self.redis.hincrby('hkey', 'attr')
            self.assertEqual(value[1],
                             self.redis.hget('hkey', 'attr'),
                             "redis.hincrby")

            self.redis.flushdb()

    def test_incr_init(self):
        '''
        incr, hincr, decr when keys do NOT exist
        '''

        self.redis.incr('key')
        self.assertEqual('1', self.redis.get('key'))

        self.redis.hincrby('hkey', 'attr')
        self.assertEqual('1', self.redis.hget('hkey', 'attr'))

        self.redis.decr('dkey')
        self.assertEqual('-1', self.redis.get('dkey'))

    def test_ttl(self):
        self.redis.set('key', 'key')
        self.redis.expire('key', 30)

        assert self.redis.ttl('key') <= 30
        self.assertEqual(self.redis.ttl('invalid_key'), -1)

    def test_push_pop_returns_str(self):
        key = 'l'
        values = ['5', 5, [], {}]
        for v in values:
            self.redis.rpush(key, v)
            self.assertEquals(self.redis.lpop(key),
                              str(v))

    #### SET TESTS ####

    def test_sadd(self):
        key = "set"
        values = ["one", "uno", "two", "three"]
        for value in values:
            self.assertEquals(1, self.redis.sadd(key, value))

    def test_sadd_multiple(self):
        key = "set"
        values = ["one", "uno", "two", "three"]
        self.assertEquals(4, self.redis.sadd(key, *values))

    def test_sadd_duplicate_key(self):
        key = "set"
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(0, self.redis.sadd(key, "one"))

    def test_scard(self):
        key = "set"
        self.assertEquals(0, self.redis.scard(key))
        self.assertFalse(key in self.redis.redis)
        values = ["one", "uno", "two", "three"]
        self.assertEquals(4, self.redis.sadd(key, *values))
        self.assertEquals(4, self.redis.scard(key))

    def test_sdiff(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sdiff([])

        self.assertEquals(set(), self.redis.sdiff("w"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sdiff("x"))
        self.assertEquals(set(["two", "three"]), self.redis.sdiff("x", "y"))
        self.assertEquals(set(["two", "three"]), self.redis.sdiff(["x", "y"]))
        self.assertEquals(set(["three"]), self.redis.sdiff("x", "y", "z"))
        self.assertEquals(set(["three"]), self.redis.sdiff(["x", "y"], "z"))

    def test_sdiffstore(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sdiffstore("w", [])

        self.assertEquals(3, self.redis.sdiffstore("w", "x"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(2, self.redis.sdiffstore("w", "x", "y"))
        self.assertEquals(set(["two", "three"]), self.redis.smembers("w"))
        self.assertEquals(2, self.redis.sdiffstore("w", ["x", "y"]))
        self.assertEquals(set(["two", "three"]), self.redis.smembers("w"))
        self.assertEquals(1, self.redis.sdiffstore("w", "x", "y", "z"))
        self.assertEquals(set(["three"]), self.redis.smembers("w"))
        self.assertEquals(1, self.redis.sdiffstore("w", ["x", "y"], "z"))
        self.assertEquals(set(["three"]), self.redis.smembers("w"))

    def test_sinter(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sinter([])

        self.assertEquals(set(), self.redis.sinter("w"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sinter("x"))
        self.assertEquals(set(["one"]), self.redis.sinter("x", "y"))
        self.assertEquals(set(["two"]), self.redis.sinter(["x", "z"]))
        self.assertEquals(set(), self.redis.sinter("x", "y", "z"))
        self.assertEquals(set(), self.redis.sinter(["x", "y"], "z"))

    def test_sinterstore(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sinterstore("w", [])

        self.assertEquals(3, self.redis.sinterstore("w", "x"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(1, self.redis.sinterstore("w", "x", "y"))
        self.assertEquals(set(["one"]), self.redis.smembers("w"))
        self.assertEquals(1, self.redis.sinterstore("w", ["x", "z"]))
        self.assertEquals(set(["two"]), self.redis.smembers("w"))
        self.assertEquals(0, self.redis.sinterstore("w", "x", "y", "z"))
        self.assertEquals(set(), self.redis.smembers("w"))
        self.assertEquals(0, self.redis.sinterstore("w", ["x", "y"], "z"))
        self.assertEquals(set(), self.redis.smembers("w"))

    def test_sismember(self):
        key = "set"
        self.assertEquals(0, self.redis.sismember(key, "one"))
        self.assertFalse(key in self.redis.redis)
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(1, self.redis.sismember(key, "one"))
        self.assertEquals(0, self.redis.sismember(key, "two"))

    def test_smembers(self):
        key = "set"
        self.assertEquals(set(), self.redis.smembers(key))
        self.assertFalse(key in self.redis.redis)
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(set(["one"]), self.redis.smembers(key))
        self.assertEquals(1, self.redis.sadd(key, "two"))
        self.assertEquals(set(["one", "two"]), self.redis.smembers(key))

    def test_smove(self):
        self.assertEquals(0, self.redis.smove("x", "y", "one"))

        self.assertEquals(2, self.redis.sadd("x", "one", "two"))
        self.assertEquals(set(["one", "two"]), self.redis.smembers("x"))
        self.assertEquals(set(), self.redis.smembers("y"))

        self.assertEquals(0, self.redis.smove("x", "y", "three"))
        self.assertEquals(set(["one", "two"]), self.redis.smembers("x"))
        self.assertEquals(set(), self.redis.smembers("y"))

        self.assertEquals(1, self.redis.smove("x", "y", "one"))
        self.assertEquals(set(["two"]), self.redis.smembers("x"))
        self.assertEquals(set(["one"]), self.redis.smembers("y"))

    def test_spop(self):
        key = "set"
        self.assertEquals(None, self.redis.spop(key))
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals("one", self.redis.spop(key))
        self.assertEquals(0, self.redis.scard(key))
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals(1, self.redis.sadd(key, "two"))
        first = self.redis.spop(key)
        self.assertTrue(first in ["one", "two"])
        self.assertEquals(1, self.redis.scard(key))
        second = self.redis.spop(key)
        self.assertEquals("one" if first == "two" else "two", second)
        self.assertEquals(0, self.redis.scard(key))

    def test_srandmember(self):
        key = "set"
        # count is None
        self.assertEquals(None, self.redis.srandmember(key))
        self.assertEquals(1, self.redis.sadd(key, "one"))
        self.assertEquals("one", self.redis.srandmember(key))
        self.assertEquals(1, self.redis.scard(key))
        self.assertEquals(1, self.redis.sadd(key, "two"))
        self.assertTrue(self.redis.srandmember(key) in ["one", "two"])
        self.assertEquals(2, self.redis.scard(key))
        # count > 0
        self.assertEquals([], self.redis.srandmember("empty", 1))
        self.assertTrue(self.redis.srandmember(key, 1)[0] in ["one", "two"])
        self.assertEquals(set(["one", "two"]), set(self.redis.srandmember(key, 2)))
        # count < 0
        self.assertEquals([], self.redis.srandmember("empty", -1))
        self.assertTrue(self.redis.srandmember(key, -1)[0] in ["one", "two"])
        members = self.redis.srandmember(key, -2)
        self.assertEquals(2, len(members))
        for member in members:
            self.assertTrue(member in ["one", "two"])

    def test_srem(self):
        key = "set"
        self.assertEquals(0, self.redis.srem(key, "one"))
        self.assertEquals(3, self.redis.sadd(key, "one", "two", "three"))
        self.assertEquals(0, self.redis.srem(key, "four"))
        self.assertEquals(2, self.redis.srem(key, "one", "three"))
        self.assertEquals(1, self.redis.srem(key, "two", "four"))

    def test_sunion(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sunion([])

        self.assertEquals(set(), self.redis.sunion("v"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sunion("x"))
        self.assertEquals(set(["one"]), self.redis.sunion("v", "y"))
        self.assertEquals(set(["one", "two"]), self.redis.sunion(["y", "z"]))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sunion("x", "y", "z"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.sunion(["x", "y"], "z"))

    def test_sunionstore(self):
        self.redis.sadd("x", "one", "two", "three")
        self.redis.sadd("y", "one")
        self.redis.sadd("z", "two")

        with self.assertRaises(Exception):
            self.redis.sunionstore("w", [])

        self.assertEquals(0, self.redis.sunionstore("w", "v"))
        self.assertEquals(set(), self.redis.smembers("w"))

        self.assertEquals(3, self.redis.sunionstore("w", "x"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(1, self.redis.sunionstore("w", "v", "y"))
        self.assertEquals(set(["one"]), self.redis.smembers("w"))

        self.assertEquals(2, self.redis.sunionstore("w", ["y", "z"]))
        self.assertEquals(set(["one", "two"]), self.redis.smembers("w"))

        self.assertEquals(3, self.redis.sunionstore("w", "x", "y", "z"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

        self.assertEquals(3, self.redis.sunionstore("w", ["x", "y"], "z"))
        self.assertEquals(set(["one", "two", "three"]), self.redis.smembers("w"))

    #### SORTED SET TESTS ####

    def test_zadd(self):
        key = "zset"
        values = [("one", 1), ("uno", 1), ("two", 2), ("three", 3)]
        for member, score in values:
            self.assertEquals(1, self.redis.zadd(key, member, score))

    def test_zadd_strict(self):
        """Argument order for zadd depends on strictness"""
        self.redis.strict = True
        key = "zset"
        values = [("one", 1), ("uno", 1), ("two", 2), ("three", 3)]
        for member, score in values:
            self.assertEquals(1, self.redis.zadd(key, score, member))

    def test_zadd_duplicate_key(self):
        key = "zset"
        self.assertEquals(1, self.redis.zadd(key, "one", 1.0))
        self.assertEquals(0, self.redis.zadd(key, "one", 2.0))

    def test_zadd_wrong_type(self):
        key = "zset"
        self.redis.set(key, "value")
        with self.assertRaises(Exception):
            self.redis.zadd(key, "one", 2.0)

    def test_zadd_multiple_bad_args(self):
        key = "zset"
        args = ["one", 1, "two"]
        with self.assertRaises(Exception):
            self.redis.zadd(key, *args)

    def test_zadd_multiple_bad_score(self):
        key = "zset"
        with self.assertRaises(Exception):
            self.redis.zadd(key, "one", "two")

    def test_zadd_multiple_args(self):
        key = "zset"
        args = ["one", 1, "uno", 1, "two", 2, "three", 3]
        self.assertEquals(4, self.redis.zadd(key, *args))

    def test_zadd_multiple_kwargs(self):
        key = "zset"
        kwargs = {"one": 1, "uno": 1, "two": 2, "three": 3}
        self.assertEquals(4, self.redis.zadd(key, **kwargs))

    def test_zcard(self):
        key = "zset"
        self.assertEquals(0, self.redis.zcard(key))
        self.redis.zadd(key, "one", 1)
        self.assertEquals(1, self.redis.zcard(key))
        self.redis.zadd(key, "one", 2)
        self.assertEquals(1, self.redis.zcard(key))
        self.redis.zadd(key, "two", 2)
        self.assertEquals(2, self.redis.zcard(key))

    def test_zincrby(self):
        key = "zset"
        self.assertEquals(1.0, self.redis.zincrby(key, "member1"))
        self.assertEquals(2.0, self.redis.zincrby(key, "member2", 2))
        self.assertEquals(-1.0, self.redis.zincrby(key, "member1", -2))

    def test_zrange(self):
        key = "zset"
        self.assertEquals([], self.redis.zrange(key, 0, -1))
        self.redis.zadd(key, "one", 1.5)
        self.redis.zadd(key, "two", 2.5)
        self.redis.zadd(key, "three", 3.5)

        # full range
        self.assertEquals(["one", "two", "three"],
                          self.redis.zrange(key, 0, -1))
        # withscores
        self.assertEquals([("one", 1.5), ("two", 2.5), ("three", 3.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))
        # score_cast_func
        self.assertEquals([("one", 1), ("two", 2), ("three", 3)],
                          self.redis.zrange(key, 0, -1, withscores=True, score_cast_func=int))

        # positive ranges
        self.assertEquals(["one"], self.redis.zrange(key, 0, 0))
        self.assertEquals(["one", "two"], self.redis.zrange(key, 0, 1))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, 0, 2))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, 0, 3))
        self.assertEquals(["two", "three"], self.redis.zrange(key, 1, 2))
        self.assertEquals(["three"], self.redis.zrange(key, 2, 3))

        # negative ends
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, 0, -1))
        self.assertEquals(["one", "two"], self.redis.zrange(key, 0, -2))
        self.assertEquals(["one"], self.redis.zrange(key, 0, -3))
        self.assertEquals([], self.redis.zrange(key, 0, -4))

        # negative starts
        self.assertEquals([], self.redis.zrange(key, -1, 0))
        self.assertEquals(["three"], self.redis.zrange(key, -1, -1))
        self.assertEquals(["two", "three"], self.redis.zrange(key, -2, -1))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, -3, -1))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, -4, -1))

        # desc
        self.assertEquals(["three", "two", "one"], self.redis.zrange(key, 0, 2, desc=True))
        self.assertEquals(["two", "one"], self.redis.zrange(key, 1, 2, desc=True))
        self.assertEquals(["three", "two"], self.redis.zrange(key, 0, 1, desc=True))

    def test_zrem(self):
        key = "zset"
        self.assertFalse(self.redis.zrem(key, "two"))

        self.redis.zadd(key, "one", 1.0)
        self.assertEquals(1, self.redis.zcard(key))

        self.assertTrue(self.redis.zrem(key, "one"))
        self.assertEquals(0, self.redis.zcard(key))

    def test_zscore(self):
        key = "zset"
        self.assertEquals(None, self.redis.zscore(key, "one"))

        self.redis.zadd(key, "one", 1.0)
        self.assertEquals(1.0, self.redis.zscore(key, "one"))

    def test_zrank(self):
        key = "zset"
        self.assertEquals(None, self.redis.zrank(key, "two"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.assertEquals(0, self.redis.zrank(key, "one"))
        self.assertEquals(1, self.redis.zrank(key, "two"))

    def test_zcount(self):
        key = "zset"
        self.assertEquals(0, self.redis.zcount(key, "-inf", "inf"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)

        self.assertEquals(2, self.redis.zcount(key, "-inf", "inf"))
        self.assertEquals(1, self.redis.zcount(key, "-inf", 1.0))
        self.assertEquals(1, self.redis.zcount(key, "-inf", 1.5))
        self.assertEquals(2, self.redis.zcount(key, "-inf", 2.0))
        self.assertEquals(2, self.redis.zcount(key, "-inf", 2.5))
        self.assertEquals(1, self.redis.zcount(key, 0.5, 1.0))
        self.assertEquals(1, self.redis.zcount(key, 0.5, 1.5))
        self.assertEquals(2, self.redis.zcount(key, 0.5, 2.0))
        self.assertEquals(2, self.redis.zcount(key, 0.5, 2.5))
        self.assertEquals(2, self.redis.zcount(key, 0.5, "inf"))

        self.assertEquals(0, self.redis.zcount(key, "inf", "-inf"))
        self.assertEquals(0, self.redis.zcount(key, 2.0, 0.5))

    def test_zrangebyscore(self):
        key = "zset"
        self.assertEquals([], self.redis.zrangebyscore(key, "-inf", "inf"))
        self.redis.zadd(key, "one", 1.5)
        self.redis.zadd(key, "two", 2.5)
        self.redis.zadd(key, "three", 3.5)

        self.assertEquals(["one", "two", "three"],
                          self.redis.zrangebyscore(key, "-inf", "inf"))
        self.assertEquals([("one", 1.5), ("two", 2.5), ("three", 3.5)],
                          self.redis.zrangebyscore(key, "-inf", "inf", withscores=True))
        self.assertEquals([("one", 1), ("two", 2), ("three", 3)],
                          self.redis.zrangebyscore(key, "-inf", "inf", withscores=True, score_cast_func=int))

        self.assertEquals(["one"],
                          self.redis.zrangebyscore(key, 1.0, 2.0))
        self.assertEquals(["one", "two"],
                          self.redis.zrangebyscore(key, 1.0, 3.0))
        self.assertEquals(["one"],
                          self.redis.zrangebyscore(key, 1.0, 3.0, start=0, num=1))
        self.assertEquals(["two"],
                          self.redis.zrangebyscore(key, 1.0, 3.0, start=1, num=1))
        self.assertEquals(["two", "three"],
                          self.redis.zrangebyscore(key, 1.0, 3.5, start=1, num=4))
        self.assertEquals([],
                          self.redis.zrangebyscore(key, 1.0, 3.5, start=3, num=4))

    def test_zremrank(self):
        key = "zset"
        self.assertEquals(None, self.redis.zrevrank(key, "two"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.assertEquals(1, self.redis.zrevrank(key, "one"))
        self.assertEquals(0, self.redis.zrevrank(key, "two"))

    def test_zrevrangebyscore(self):
        key = "zset"
        self.assertEquals([], self.redis.zrevrangebyscore(key, "inf", "-inf"))
        self.redis.zadd(key, "one", 1.5)
        self.redis.zadd(key, "two", 2.5)
        self.redis.zadd(key, "three", 3.5)

        self.assertEquals(["three", "two", "one"],
                          self.redis.zrevrangebyscore(key, "inf", "-inf"))
        self.assertEquals([("three", 3.5), ("two", 2.5), ("one", 1.5)],
                          self.redis.zrevrangebyscore(key, "inf", "-inf", withscores=True))
        self.assertEquals([("three", 3), ("two", 2), ("one", 1)],
                          self.redis.zrevrangebyscore(key, "inf", "-inf", withscores=True, score_cast_func=int))

        self.assertEquals(["one"],
                          self.redis.zrevrangebyscore(key, 2.0, 1.0))
        self.assertEquals(["two", "one"],
                          self.redis.zrevrangebyscore(key, 3.0, 1.0))
        self.assertEquals(["two"],
                          self.redis.zrevrangebyscore(key, 3.0, 1.0, start=0, num=1))
        self.assertEquals(["one"],
                          self.redis.zrevrangebyscore(key, 3.0, 1.0, start=1, num=1))
        self.assertEquals(["two", "one"],
                          self.redis.zrevrangebyscore(key, 3.5, 1.0, start=1, num=4))
        self.assertEquals([],
                          self.redis.zrevrangebyscore(key, 3.5, 1.0, start=3, num=4))

    def test_zremrangebyrank(self):
        key = "zset"
        self.assertEquals(0, self.redis.zremrangebyrank(key, 0, -1))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.redis.zadd(key, "three", 3.0)

        self.assertEquals(2, self.redis.zremrangebyrank(key, 0, 1))

        self.assertEquals(["three"], self.redis.zrange(key, 0, -1))
        self.assertEquals(1, self.redis.zremrangebyrank(key, 0, -1))

        self.assertEquals([], self.redis.zrange(key, 0, -1))

    def test_zremrangebyscore(self):
        key = "zset"
        self.assertEquals(0, self.redis.zremrangebyscore(key, "-inf", "inf"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.redis.zadd(key, "three", 3.0)

        self.assertEquals(1, self.redis.zremrangebyscore(key, 0, 1))

        self.assertEquals(["two", "three"], self.redis.zrange(key, 0, -1))
        self.assertEquals(2, self.redis.zremrangebyscore(key, 2.0, "inf"))

        self.assertEquals([], self.redis.zrange(key, 0, -1))

    def test_zunionstore(self):
        key = "zset"

        # no keys
        self.assertEquals(0, self.redis.zunionstore(key, ["zset1", "zset2"]))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # sum (default)
        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"]))
        self.assertEquals([("one", 1.0), ("three", 3.0), ("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # sum (explicit)
        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="sum"))
        self.assertEquals([("one", 1.0), ("three", 3.0), ("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # min
        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="min"))
        self.assertEquals([("one", 1.0), ("two", 2.0), ("three", 3.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # max
        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="max"))
        self.assertEquals([("one", 1.0), ("two", 2.5), ("three", 3.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore(self):
        key = "zset"

        # no keys
        self.assertEquals(0, self.redis.zinterstore(key, ["zset1", "zset2"]))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # sum (default)
        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"]))
        self.assertEquals([("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # sum (explicit)
        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="sum"))
        self.assertEquals([("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # min
        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="min"))
        self.assertEquals([("two", 2.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        # max
        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="max"))
        self.assertEquals([("two", 2.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))
Esempio n. 45
0
class TestScript(object):
    """
    Tests for MockRedis scripting operations
    """

    def setup(self):
        self.redis = MockRedis(load_lua_dependencies=False)
        self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT.encode("utf-8")).hexdigest()

        try:
            lua, lua_globals = MockRedisScript._import_lua(load_dependencies=False)
        except RuntimeError:
            raise SkipTest("mockredispy was not installed with lua support")

        self.lua = lua
        self.lua_globals = lua_globals

        assert_equal_list = """
        function compare_list(list1, list2)
            if #list1 ~= #list2 then
                return false
            end
            for i, item1 in ipairs(list1) do
                if item1 ~= list2[i] then
                    return false
                end
            end
            return true
        end

        function assert_equal_list(list1, list2)
            assert(compare_list(list1, list2))
        end
        return assert_equal_list
        """
        self.lua_assert_equal_list = self.lua.execute(assert_equal_list)

        assert_equal_list_with_pairs = """
        function pair_exists(list1, key, value)
            i = 1
            for i, item1 in ipairs(list1) do
                if i%2 == 1 then
                    if (list1[i] == key) and (list1[i + 1] == value) then
                        return true
                    end
                end
            end
            return false
        end

        function compare_list_with_pairs(list1, list2)
            if #list1 ~= #list2 or #list1 % 2 == 1 then
                return false
            end
            for i = 1, #list1, 2 do
                if not pair_exists(list2, list1[i], list1[i + 1]) then
                    return false
                end
            end
            return true
        end

        function assert_equal_list_with_pairs(list1, list2)
            assert(compare_list_with_pairs(list1, list2))
        end
        return assert_equal_list_with_pairs
        """
        self.lua_assert_equal_list_with_pairs = self.lua.execute(assert_equal_list_with_pairs)

        compare_val = """
        function compare_val(var1, var2)
            return var1 == var2
        end
        return compare_val
        """
        self.lua_compare_val = self.lua.execute(compare_val)

    def test_register_script_lpush(self):
        # lpush two values
        script_content = "redis.call('LPUSH', KEYS[1], ARGV[1], ARGV[2])"
        script = self.redis.register_script(script_content)
        script(keys=[LIST1], args=[VAL1, VAL2])

        # validate insertion
        eq_([VAL2, VAL1], self.redis.lrange(LIST1, 0, -1))

    def test_register_script_lpop(self):
        self.redis.lpush(LIST1, VAL2, VAL1)

        # lpop one value
        script_content = "return redis.call('LPOP', KEYS[1])"
        script = self.redis.register_script(script_content)
        list_item = script(keys=[LIST1])

        # validate lpop
        eq_(VAL1, list_item)
        eq_([VAL2], self.redis.lrange(LIST1, 0, -1))

    def test_register_script_rpoplpush(self):
        self.redis.lpush(LIST1, VAL2, VAL1)
        self.redis.lpush(LIST2, VAL4, VAL3)

        # rpoplpush
        script_content = "redis.call('RPOPLPUSH', KEYS[1], KEYS[2])"
        script = self.redis.register_script(script_content)
        script(keys=[LIST1, LIST2])

        # validate rpoplpush
        eq_([VAL1], self.redis.lrange(LIST1, 0, -1))
        eq_([VAL2, VAL3, VAL4], self.redis.lrange(LIST2, 0, -1))

    def test_register_script_rpop_lpush(self):
        self.redis.lpush(LIST1, VAL2, VAL1)
        self.redis.lpush(LIST2, VAL4, VAL3)

        # rpop from LIST1 and lpush the same value to LIST2
        script_content = """
        local tmp_item = redis.call('RPOP', KEYS[1])
        redis.call('LPUSH', KEYS[2], tmp_item)
        """
        script = self.redis.register_script(script_content)
        script(keys=[LIST1, LIST2])

        # validate rpop and then lpush
        eq_([VAL1], self.redis.lrange(LIST1, 0, -1))
        eq_([VAL2, VAL3, VAL4], self.redis.lrange(LIST2, 0, -1))

    def test_register_script_client(self):
        # lpush two values in LIST1 in first instance of redis
        self.redis.lpush(LIST1, VAL2, VAL1)

        # create script on first instance of redis
        script_content = LPOP_SCRIPT
        script = self.redis.register_script(script_content)

        # lpush two values in LIST1 in redis2 (second instance of redis)
        redis2 = MockRedis()
        redis2.lpush(LIST1, VAL4, VAL3)

        # execute LPOP script on redis2 instance
        list_item = script(keys=[LIST1], client=redis2)

        # validate lpop from LIST1 in redis2
        eq_(VAL3, list_item)
        eq_([VAL4], redis2.lrange(LIST1, 0, -1))
        eq_([VAL1, VAL2], self.redis.lrange(LIST1, 0, -1))

    def test_eval_lpush(self):
        # lpush two values
        script_content = "redis.call('LPUSH', KEYS[1], ARGV[1], ARGV[2])"
        self.redis.eval(script_content, 1, LIST1, VAL1, VAL2)

        # validate insertion
        eq_([VAL2, VAL1], self.redis.lrange(LIST1, 0, -1))

    def test_eval_lpop(self):
        self.redis.lpush(LIST1, VAL2, VAL1)

        # lpop one value
        script_content = "return redis.call('LPOP', KEYS[1])"
        list_item = self.redis.eval(script_content, 1, LIST1)

        # validate lpop
        eq_(VAL1, list_item)
        eq_([VAL2], self.redis.lrange(LIST1, 0, -1))

    def test_eval_lrem(self):
        self.redis.delete(LIST1)
        self.redis.lpush(LIST1, VAL1)

        # lrem one value
        script_content = "return redis.call('LREM', KEYS[1], 0, ARGV[1])"
        value = self.redis.eval(script_content, 1, LIST1, VAL1)

        eq_(value, 1)

    def test_eval_zadd(self):
        # The score and member are reversed when the client is not strict.
        self.redis.strict = False
        script_content = "return redis.call('zadd', KEYS[1], ARGV[1], ARGV[2])"
        self.redis.eval(script_content, 1, SET1, 42, VAL1)

        eq_(42, self.redis.zscore(SET1, VAL1))

    def test_eval_zrangebyscore(self):
        # Make sure the limit is removed.
        script = "return redis.call('zrangebyscore',KEYS[1],ARGV[1],ARGV[2])"
        self.eval_zrangebyscore(script)

    def test_eval_zrangebyscore_with_limit(self):
        # Make sure the limit is removed.
        script = "return redis.call('zrangebyscore', " "KEYS[1], ARGV[1], ARGV[2], 'LIMIT', 0, 2)"

        self.eval_zrangebyscore(script)

    def eval_zrangebyscore(self, script):
        self.redis.strict = False
        self.redis.zadd(SET1, VAL1, 1)
        self.redis.zadd(SET1, VAL2, 2)

        eq_([], self.redis.eval(script, 1, SET1, 0, 0))
        eq_([VAL1], self.redis.eval(script, 1, SET1, 0, 1))
        eq_([VAL1, VAL2], self.redis.eval(script, 1, SET1, 0, 2))
        eq_([VAL2], self.redis.eval(script, 1, SET1, 2, 2))

    def test_table_type(self):
        self.redis.lpush(LIST1, VAL2, VAL1)
        script_content = """
        local items = redis.call('LRANGE', KEYS[1], ARGV[1], ARGV[2])
        return type(items)
        """
        script = self.redis.register_script(script_content)
        itemType = script(keys=[LIST1], args=[0, -1])
        eq_("table", itemType)

    def test_script_hgetall(self):
        myhash = {"k1": "v1"}
        self.redis.hmset("myhash", myhash)
        script_content = """
        return redis.call('HGETALL', KEYS[1])
        """
        script = self.redis.register_script(script_content)
        item = script(keys=["myhash"])
        ok_(isinstance(item, list))
        eq_(["k1", "v1"], item)

    def test_evalsha(self):
        self.redis.lpush(LIST1, VAL1)
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA

        # validator error when script not registered
        with assert_raises(RedisError) as redis_error:
            self.redis.evalsha(self.LPOP_SCRIPT_SHA, 1, LIST1)

        eq_("Sha not registered", str(redis_error.exception))

        with assert_raises(RedisError):
            self.redis.evalsha(self.LPOP_SCRIPT_SHA, 1, LIST1)

        # load script and then evalsha
        eq_(sha, self.redis.script_load(script))
        eq_(VAL1, self.redis.evalsha(sha, 1, LIST1))
        eq_(0, self.redis.llen(LIST1))

    def test_script_exists(self):
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA
        eq_([False], self.redis.script_exists(sha))
        self.redis.register_script(script)
        eq_([True], self.redis.script_exists(sha))

    def test_script_flush(self):
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA
        self.redis.register_script(script)
        eq_([True], self.redis.script_exists(sha))
        self.redis.script_flush()
        eq_([False], self.redis.script_exists(sha))

    def test_script_load(self):
        script = LPOP_SCRIPT
        sha = self.LPOP_SCRIPT_SHA
        eq_([False], self.redis.script_exists(sha))
        eq_(sha, self.redis.script_load(script))
        eq_([True], self.redis.script_exists(sha))

    def test_lua_to_python_none(self):
        lval = self.lua.eval("")
        pval = MockRedisScript._lua_to_python(lval)
        ok_(pval is None)

    def test_lua_to_python_list(self):
        lval = self.lua.eval('{"val1", "val2"}')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, list))
        eq_(["val1", "val2"], pval)

    def test_lua_to_python_long(self):
        lval = self.lua.eval("22")
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, long))
        eq_(22, pval)

    def test_lua_to_python_flota(self):
        lval = self.lua.eval("22.2")
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, float))
        eq_(22.2, pval)

    def test_lua_to_python_string(self):
        lval = self.lua.eval('"somestring"')
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, str))
        eq_("somestring", pval)

    def test_lua_to_python_bool(self):
        lval = self.lua.eval("true")
        pval = MockRedisScript._lua_to_python(lval)
        ok_(isinstance(pval, bool))
        eq_(True, pval)

    def test_python_to_lua_none(self):
        pval = None
        lval = MockRedisScript._python_to_lua(pval)
        is_null = """
        function is_null(var1)
            return var1 == nil
        end
        return is_null
        """
        lua_is_null = self.lua.execute(is_null)
        ok_(MockRedisScript._lua_to_python(lua_is_null(lval)))

    def test_python_to_lua_string(self):
        pval = "somestring"
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('"somestring"')
        eq_("string", self.lua_globals.type(lval))
        eq_(lval_expected, lval)

    def test_python_to_lua_list(self):
        pval = ["abc", "xyz"]
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('{"abc", "xyz"}')
        self.lua_assert_equal_list(lval_expected, lval)

    def test_python_to_lua_dict(self):
        pval = {"k1": "v1", "k2": "v2"}
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval('{"k1", "v1", "k2", "v2"}')
        self.lua_assert_equal_list_with_pairs(lval_expected, lval)

    def test_python_to_lua_long(self):
        pval = long(10)
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval("10")
        eq_("number", self.lua_globals.type(lval))
        ok_(MockRedisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))

    def test_python_to_lua_float(self):
        pval = 10.1
        lval = MockRedisScript._python_to_lua(pval)
        lval_expected = self.lua.eval("10.1")
        eq_("number", self.lua_globals.type(lval))
        ok_(MockRedisScript._lua_to_python(self.lua_compare_val(lval_expected, lval)))

    def test_python_to_lua_boolean(self):
        pval = True
        lval = MockRedisScript._python_to_lua(pval)
        eq_("boolean", self.lua_globals.type(lval))
        ok_(MockRedisScript._lua_to_python(lval))

    def test_lua_ok_return(self):
        script_content = "return {ok='OK'}"
        script = self.redis.register_script(script_content)
        eq_("OK", script())

    @raises_response_error
    def test_lua_err_return(self):
        script_content = "return {err='ERROR Some message'}"
        script = self.redis.register_script(script_content)
        script()

    def test_concurrent_lua(self):
        script_content = """
local entry = redis.call('HGETALL', ARGV[1])
redis.call('HSET', ARGV[1], 'kk', 'vv')
return entry
"""
        script = self.redis.register_script(script_content)

        for i in range(500):
            self.redis.hmset(i, {"k1": "v1", "k2": "v2", "k3": "v3"})

        def lua_thread():
            for i in range(500):
                script(args=[i])

        active_threads = []
        for i in range(10):
            thread = threading.Thread(target=lua_thread)
            active_threads.append(thread)
            thread.start()

        for thread in active_threads:
            thread.join()
Esempio n. 46
0
class TestRedisHash(TestCase):
    """hash tests"""

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_hexists(self):
        hashkey = "hash"
        self.assertFalse(self.redis.hexists(hashkey, "key"))
        self.redis.hset(hashkey, "key", "value")
        self.assertTrue(self.redis.hexists(hashkey, "key"))
        self.assertFalse(self.redis.hexists(hashkey, "key2"))

    def test_hgetall(self):
        hashkey = "hash"
        self.assertEquals({}, self.redis.hgetall(hashkey))
        self.redis.hset(hashkey, "key", "value")
        self.assertEquals({"key": "value"}, self.redis.hgetall(hashkey))

    def test_hdel(self):
        hashkey = "hash"
        self.redis.hmset(hashkey, {1: 1, 2: 2, 3: 3})
        self.assertEquals(0, self.redis.hdel(hashkey, "foo"))
        self.assertEquals({"1": "1", "2": "2", "3": "3"}, self.redis.hgetall(hashkey))
        self.assertEquals(2, self.redis.hdel(hashkey, "1", 2))
        self.assertEquals({"3": "3"}, self.redis.hgetall(hashkey))
        self.assertEquals(1, self.redis.hdel(hashkey, "3", 4))
        self.assertEquals({}, self.redis.hgetall(hashkey))
        self.assertFalse(self.redis.exists(hashkey))

    def test_hlen(self):
        hashkey = "hash"
        self.assertEquals(0, self.redis.hlen(hashkey))
        self.redis.hset(hashkey, "key", "value")
        self.assertEquals(1, self.redis.hlen(hashkey))

    def test_hset(self):
        hashkey = "hash"
        self.redis.hset(hashkey, "key", "value")
        self.assertEquals("value", self.redis.hget(hashkey, "key"))

    def test_hget(self):
        hashkey = "hash"
        self.assertEquals(None, self.redis.hget(hashkey, "key"))

    def test_hset_integral(self):
        hashkey = "hash"
        self.redis.hset(hashkey, 1, 2)
        self.assertEquals("2", self.redis.hget(hashkey, 1))
        self.assertEquals("2", self.redis.hget(hashkey, "1"))

    def test_hsetnx(self):
        hashkey = "hash"
        self.assertEquals(1, self.redis.hsetnx(hashkey, "key", "value1"))
        self.assertEquals("value1", self.redis.hget(hashkey, "key"))
        self.assertEquals(0, self.redis.hsetnx(hashkey, "key", "value2"))
        self.assertEquals("value1", self.redis.hget(hashkey, "key"))

    def test_hmset(self):
        hashkey = "hash"
        self.redis.hmset(hashkey, {"key1": "value1", "key2": "value2"})
        self.assertEquals("value1", self.redis.hget(hashkey, "key1"))
        self.assertEquals("value2", self.redis.hget(hashkey, "key2"))

    def test_hmset_integral(self):
        hashkey = "hash"
        self.redis.hmset(hashkey, {1: 2, 3: 4})
        self.assertEquals("2", self.redis.hget(hashkey, "1"))
        self.assertEquals("2", self.redis.hget(hashkey, 1))
        self.assertEquals("4", self.redis.hget(hashkey, "3"))
        self.assertEquals("4", self.redis.hget(hashkey, 3))

    def test_hmget(self):
        hashkey = "hash"
        self.redis.hmset(hashkey, {1: 2, 3: 4})
        self.assertEquals(["2", None, "4"], self.redis.hmget(hashkey, "1", "2", "3"))
        self.assertEquals(["2", None, "4"], self.redis.hmget(hashkey, ["1", "2", "3"]))
        self.assertEquals(["2", None, "4"], self.redis.hmget(hashkey, [1, 2, 3]))

    def test_hincrby(self):
        hashkey = "hash"
        self.assertEquals(1, self.redis.hincrby(hashkey, "key", 1))
        self.assertEquals(3, self.redis.hincrby(hashkey, "key", 2))
        self.assertEquals("3", self.redis.hget(hashkey, "key"))

    def test_hincrbyfloat(self):
        hashkey = "hash"
        self.assertEquals(1.2, self.redis.hincrbyfloat(hashkey, "key", 1.2))
        self.assertEquals(3.5, self.redis.hincrbyfloat(hashkey, "key", 2.3))
        self.assertEquals("3.5", self.redis.hget(hashkey, "key"))

    def test_hkeys(self):
        hashkey = "hash"
        self.redis.hmset(hashkey, {1: 2, 3: 4})
        self.assertEquals(["1", "3"], sorted(self.redis.hkeys(hashkey)))

    def test_hvals(self):
        hashkey = "hash"
        self.redis.hmset(hashkey, {1: 2, 3: 4})
        self.assertEquals(["2", "4"], sorted(self.redis.hvals(hashkey)))
Esempio n. 47
0
 def test_it_picks_mapzen_routing_quota_from_redis(self):
     redis_conn = MockRedis()
     redis_conn.hset('rails:users:my_username', 'mapzen_routing_quota', 42)
     redis_config = ServicesRedisConfig(redis_conn).build('my_username', None)
     assert 'mapzen_routing_quota' in redis_config
     assert int(redis_config['mapzen_routing_quota']) == 42
Esempio n. 48
0
class TestRoutingConfig(TestCase):

    def setUp(self):
        self._redis_conn = MockRedis()
        self._db_conn = plpy_mock
        self._username = '******'
        self._user_key = "rails:users:{0}".format(self._username)
        self._redis_conn.hset(self._user_key, 'period_end_date', '2016-10-10')

    def test_should_pick_quota_from_server_by_default(self):
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
        assert config.monthly_quota == 1500000

    def test_should_pick_quota_from_redis_if_present(self):
        self._redis_conn.hset(self._user_key, 'mapzen_routing_quota', 1000)
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
        assert config.monthly_quota == 1000

    def test_org_quota_overrides_user_quota(self):
        self._redis_conn.hset(self._user_key, 'mapzen_routing_quota', 1000)
        orgname = 'my_test_org'
        orgname_key = "rails:orgs:{0}".format(orgname)
        self._redis_conn.hset(orgname_key, 'period_end_date', '2016-05-31')
        self._redis_conn.hset(orgname_key, 'mapzen_routing_quota', 5000)

        # TODO: these are not too relevant for the routing config
        self._redis_conn.hset(orgname_key, 'geocoding_quota', 0)
        self._redis_conn.hset(orgname_key, 'here_isolines_quota', 0)

        config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
        assert config.monthly_quota == 5000


    def test_should_have_soft_limit_false_by_default(self):
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
        assert config.soft_limit == False

    def test_can_set_soft_limit_in_user_conf(self):
        self._redis_conn.hset(self._user_key, 'soft_mapzen_routing_limit', True)
        orgname = None
        config = RoutingConfig(self._redis_conn, self._db_conn, self._username, orgname)
        assert config.soft_limit == True
Esempio n. 49
0
class TestPipeline(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_pipeline(self):
        """
        Pipeline execution returns all of the saved up values.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.echo("foo")
            pipeline.echo("bar")

            eq_(["foo", "bar"], pipeline.execute())

    def test_set_and_get(self):
        """
        Pipeline execution returns the pipeline, not the intermediate value.
        """
        with self.redis.pipeline() as pipeline:
            eq_(pipeline, pipeline.set("foo", "bar"))
            eq_(pipeline, pipeline.get("foo"))

            eq_([True, "bar"], pipeline.execute())

    def test_scripts(self):
        """
        Verify that script calls work across pipelines.

        This test basically ensures that the pipeline shares
        state with the mock redis instance.
        """
        script_content = "redis.call('PING')"
        sha = sha1(script_content).hexdigest()

        self.redis.register_script(script_content)

        # Script exists in mock redis
        eq_([True], self.redis.script_exists(sha))

        # Script exists in pipeline
        eq_([True], self.redis.pipeline().script_exists(sha).execute()[0])

    def test_watch(self):
        """
        Verify watch puts the pipeline in immediate execution mode.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.watch("key1", "key2")
            eq_(True, pipeline.set("foo", "bar"))
            eq_("bar", pipeline.get("foo"))

    def test_multi(self):
        """
        Test explicit transaction with multi command.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.multi()
            eq_(pipeline, pipeline.set("foo", "bar"))
            eq_(pipeline, pipeline.get("foo"))

            eq_([True, "bar"], pipeline.execute())

    def test_multi_with_watch(self):
        """
        Test explicit transaction with watched keys.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.watch("foo")
            eq_(True, pipeline.set("foo", "bar"))
            eq_("bar", pipeline.get("foo"))

            pipeline.multi()
            eq_(pipeline, pipeline.set("foo", "baz"))
            eq_(pipeline, pipeline.get("foo"))

            eq_([True, "baz"], pipeline.execute())

    def test_watch_after_multi(self):
        """
        Cannot watch after multi.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.multi()
            with self.assertRaises(RedisError):
                pipeline.watch()

    def test_multiple_multi_calls(self):
        """
        Cannot call multi mutliple times.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.multi()
            with self.assertRaises(RedisError):
                pipeline.multi()

    def test_multi_on_implicit_transaction(self):
        """
        Cannot start an explicit transaction when commands have already been issued.
        """
        with self.redis.pipeline() as pipeline:
            pipeline.set("foo", "bar")
            with self.assertRaises(RedisError):
                pipeline.multi()
Esempio n. 50
0
    def setup(self):
        self.redis = MockRedis(load_lua_dependencies=False)
        self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT.encode("utf-8")).hexdigest()

        try:
            lua, lua_globals = MockRedisScript._import_lua(load_dependencies=False)
        except RuntimeError:
            raise SkipTest("mockredispy was not installed with lua support")

        self.lua = lua
        self.lua_globals = lua_globals

        assert_equal_list = """
        function compare_list(list1, list2)
            if #list1 ~= #list2 then
                return false
            end
            for i, item1 in ipairs(list1) do
                if item1 ~= list2[i] then
                    return false
                end
            end
            return true
        end

        function assert_equal_list(list1, list2)
            assert(compare_list(list1, list2))
        end
        return assert_equal_list
        """
        self.lua_assert_equal_list = self.lua.execute(assert_equal_list)

        assert_equal_list_with_pairs = """
        function pair_exists(list1, key, value)
            i = 1
            for i, item1 in ipairs(list1) do
                if i%2 == 1 then
                    if (list1[i] == key) and (list1[i + 1] == value) then
                        return true
                    end
                end
            end
            return false
        end

        function compare_list_with_pairs(list1, list2)
            if #list1 ~= #list2 or #list1 % 2 == 1 then
                return false
            end
            for i = 1, #list1, 2 do
                if not pair_exists(list2, list1[i], list1[i + 1]) then
                    return false
                end
            end
            return true
        end

        function assert_equal_list_with_pairs(list1, list2)
            assert(compare_list_with_pairs(list1, list2))
        end
        return assert_equal_list_with_pairs
        """
        self.lua_assert_equal_list_with_pairs = self.lua.execute(assert_equal_list_with_pairs)

        compare_val = """
        function compare_val(var1, var2)
            return var1 == var2
        end
        return compare_val
        """
        self.lua_compare_val = self.lua.execute(compare_val)
 def test_returns_nothing_if_field_not_present(self):
     connection = MockRedis()
     connection.hmset(self.CONFIG_HASH_KEY, {'field1': 42, 'field2': 43})
     redis_config = RedisConfigStorage(connection, self.CONFIG_HASH_KEY)
     redis_config.get('whatever_field')
Esempio n. 52
0
 def setUp(self):
     self.redis = MockRedis()
     self.redis.flushdb()
Esempio n. 53
0
class TestRedis(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_get(self):
        self.assertEqual(None, self.redis.get('key'))

        self.redis.redis['key'] = 'value'
        self.assertEqual('value', self.redis.get('key'))

    def test_set(self):
        self.assertEqual(None, self.redis.redis.get('key'))

        self.redis.set('key', 'value')
        self.assertEqual('value', self.redis.redis.get('key'))

    def test_get_types(self):
        '''
        Python bools, lists, dicts are returned as strings by
        redis-py/redis.
        '''

        values = list([
            True,
            False,
            [1, '2'],
            {
                'a': 1,
                'b': 'c'
            },
        ])

        self.assertEqual(None, self.redis.get('key'))

        for value in values:
            self.redis.set('key', value)
            self.assertEqual(str(value),
                             self.redis.get('key'),
                             "redis.get")

            self.redis.hset('hkey', 'item', value)
            self.assertEqual(str(value),
                             self.redis.hget('hkey', 'item'))

            self.redis.sadd('skey', value)
            self.assertEqual(set([str(value)]),
                             self.redis.smembers('skey'))

            self.redis.flushdb()

    def test_incr(self):

        values = list([
            (1, '2'),
            ('1', '2'),
        ])

        for value in values:
            self.redis.set('key', value[0])
            self.redis.incr('key')
            self.assertEqual(value[1],
                             self.redis.get('key'),
                             "redis.incr")

            self.redis.hset('hkey', 'attr', value[0])
            self.redis.hincrby('hkey', 'attr')
            print self.redis.hget('hkey', 'attr')
            self.assertEqual(value[1],
                             self.redis.hget('hkey', 'attr'),
                             "redis.hincrby")

            self.redis.flushdb()

    def test_ttl(self):
        self.redis.set('key', 'key')
        self.redis.expire('key', 30)

        assert self.redis.ttl('key') <= 30
        self.assertEqual(self.redis.ttl('invalid_key'), -1)

    def test_push_pop_returns_str(self):
        key = 'l'
        values = ['5', 5, [], {}]
        for v in values:
            self.redis.rpush(key, v)
            self.assertEquals(self.redis.lpop(key),
                              str(v))
Esempio n. 54
0
 def setUp(self):
     self.redis = MockRedis()
     self.redis.flushdb()
Esempio n. 55
0
class TestRedis(TestCase):

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_get(self):
        self.assertEqual(None, self.redis.get('key'))

        self.redis.redis['key'] = 'value'
        self.assertEqual('value', self.redis.get('key'))

    def test_set(self):
        self.assertEqual(None, self.redis.redis.get('key'))

        self.redis.set('key', 'value')
        self.assertEqual('value', self.redis.redis.get('key'))

    def test_get_types(self):
        '''
        Python bools, lists, dicts are returned as strings by
        redis-py/redis.
        '''

        values = list([
            True,
            False,
            [1, '2'],
            {
                'a': 1,
                'b': 'c'
            },
        ])

        self.assertEqual(None, self.redis.get('key'))

        for value in values:
            self.redis.set('key', value)
            self.assertEqual(str(value),
                             self.redis.get('key'),
                             "redis.get")

            self.redis.hset('hkey', 'item', value)
            self.assertEqual(str(value),
                             self.redis.hget('hkey', 'item'))

            self.redis.sadd('skey', value)
            self.assertEqual(set([str(value)]),
                             self.redis.smembers('skey'))

            self.redis.flushdb()

    def test_incr(self):
        '''
        incr, hincr when keys exist
        '''

        values = list([
            (1, '2'),
            ('1', '2'),
        ])

        for value in values:
            self.redis.set('key', value[0])
            self.redis.incr('key')
            self.assertEqual(value[1],
                             self.redis.get('key'),
                             "redis.incr")

            self.redis.hset('hkey', 'attr', value[0])
            self.redis.hincrby('hkey', 'attr')
            self.assertEqual(value[1],
                             self.redis.hget('hkey', 'attr'),
                             "redis.hincrby")

            self.redis.flushdb()

    def test_incr_init(self):
        '''
        incr, hincr, decr when keys do NOT exist
        '''

        self.redis.incr('key')
        self.assertEqual('1', self.redis.get('key'))

        self.redis.hincrby('hkey', 'attr')
        self.assertEqual('1', self.redis.hget('hkey', 'attr'))

        self.redis.decr('dkey')
        self.assertEqual('-1', self.redis.get('dkey'))

    def test_ttl(self):
        self.redis.set('key', 'key')
        self.redis.expire('key', 30)

        assert self.redis.ttl('key') <= 30
        self.assertEqual(self.redis.ttl('invalid_key'), -1)

    def test_push_pop_returns_str(self):
        key = 'l'
        values = ['5', 5, [], {}]
        for v in values:
            self.redis.rpush(key, v)
            self.assertEquals(self.redis.lpop(key),
                              str(v))

    def test_llen(self):
        self.assertEqual(self.redis.llen('key'), 0)
        self.assertFalse(self.redis.exists('key'))
        self.redis.rpush('key', 'value')
        self.assertEqual(self.redis.llen('key'), 1)

    def test_lrange(self):
        self.assertEqual(self.redis.lrange('key', 0, 100), [])
        self.assertFalse(self.redis.exists('key'))
        for i in range(3):
            self.redis.rpush('key', i)
        self.assertEqual(self.redis.lrange('key', 2, 0), [])
        self.assertEqual(self.redis.lrange('key', 0, 100), ['0', '1', '2'])
        self.assertEqual(self.redis.lrange('key', 0, 2), ['0', '1', '2'])
        self.assertEqual(self.redis.lrange('key', 1, 1), ['1'])
        self.assertEqual(self.redis.lrange('key', 1, -1), ['1', '2'])
        self.assertEqual(self.redis.lrange('key', 1, -2), ['1'])
        self.assertEqual(self.redis.lrange('key', -3, -2), ['0', '1'])
        self.assertEqual(self.redis.lrange('key', -1, -1), ['2'])
 def setUp(self):
     self._redis_conn = MockRedis()
     self._db_conn = plpy_mock
     self._username = '******'
     self._user_key = "rails:users:{0}".format(self._username)
     self._redis_conn.hset(self._user_key, 'period_end_date', '2016-10-10')
Esempio n. 57
0
    def setup(self):
        self.redis = MockRedis(load_lua_dependencies=False)
        self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT.encode("utf-8")).hexdigest()

        try:
            lua, lua_globals = MockRedisScript._import_lua(load_dependencies=False)
        except RuntimeError:
            raise SkipTest("mockredispy was not installed with lua support")

        self.lua = lua
        self.lua_globals = lua_globals

        assert_equal_list = """
        function compare_list(list1, list2)
            if #list1 ~= #list2 then
                return false
            end
            for i, item1 in ipairs(list1) do
                if item1 ~= list2[i] then
                    return false
                end
            end
            return true
        end

        function assert_equal_list(list1, list2)
            assert(compare_list(list1, list2))
        end
        return assert_equal_list
        """
        self.lua_assert_equal_list = self.lua.execute(assert_equal_list)

        assert_equal_list_with_pairs = """
        function pair_exists(list1, key, value)
            i = 1
            for i, item1 in ipairs(list1) do
                if i%2 == 1 then
                    if (list1[i] == key) and (list1[i + 1] == value) then
                        return true
                    end
                end
            end
            return false
        end

        function compare_list_with_pairs(list1, list2)
            if #list1 ~= #list2 or #list1 % 2 == 1 then
                return false
            end
            for i = 1, #list1, 2 do
                if not pair_exists(list2, list1[i], list1[i + 1]) then
                    return false
                end
            end
            return true
        end

        function assert_equal_list_with_pairs(list1, list2)
            assert(compare_list_with_pairs(list1, list2))
        end
        return assert_equal_list_with_pairs
        """
        self.lua_assert_equal_list_with_pairs = self.lua.execute(assert_equal_list_with_pairs)

        compare_val = """
        function compare_val(var1, var2)
            return var1 == var2
        end
        return compare_val
        """
        self.lua_compare_val = self.lua.execute(compare_val)
Esempio n. 58
0
def broken_model():
    """Create a non-functional model object."""
    r = MockRedis()
    r.set("1.2.0", pickle.dumps("lol"))
    return lambda: r
Esempio n. 59
0
class TestRedisZset(TestCase):
    """zset tests"""

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_zadd(self):
        key = "zset"
        values = [("one", 1), ("uno", 1), ("two", 2), ("three", 3)]
        for member, score in values:
            self.assertEquals(1, self.redis.zadd(key, member, score))

    def test_zadd_strict(self):
        """Argument order for zadd depends on strictness"""
        self.redis.strict = True
        key = "zset"
        values = [("one", 1), ("uno", 1), ("two", 2), ("three", 3)]
        for member, score in values:
            self.assertEquals(1, self.redis.zadd(key, score, member))

    def test_zadd_duplicate_key(self):
        key = "zset"
        self.assertEquals(1, self.redis.zadd(key, "one", 1.0))
        self.assertEquals(0, self.redis.zadd(key, "one", 2.0))

    def test_zadd_wrong_type(self):
        key = "zset"
        self.redis.set(key, "value")
        with self.assertRaises(Exception):
            self.redis.zadd(key, "one", 2.0)

    def test_zadd_multiple_bad_args(self):
        key = "zset"
        args = ["one", 1, "two"]
        with self.assertRaises(Exception):
            self.redis.zadd(key, *args)

    def test_zadd_multiple_bad_score(self):
        key = "zset"
        with self.assertRaises(Exception):
            self.redis.zadd(key, "one", "two")

    def test_zadd_multiple_args(self):
        key = "zset"
        args = ["one", 1, "uno", 1, "two", 2, "three", 3]
        self.assertEquals(4, self.redis.zadd(key, *args))

    def test_zadd_multiple_kwargs(self):
        key = "zset"
        kwargs = {"one": 1, "uno": 1, "two": 2, "three": 3}
        self.assertEquals(4, self.redis.zadd(key, **kwargs))

    def test_zcard(self):
        key = "zset"
        self.assertEquals(0, self.redis.zcard(key))
        self.redis.zadd(key, "one", 1)
        self.assertEquals(1, self.redis.zcard(key))
        self.redis.zadd(key, "one", 2)
        self.assertEquals(1, self.redis.zcard(key))
        self.redis.zadd(key, "two", 2)
        self.assertEquals(2, self.redis.zcard(key))

    def test_zincrby(self):
        key = "zset"
        self.assertEquals(1.0, self.redis.zincrby(key, "member1"))
        self.assertEquals(2.0, self.redis.zincrby(key, "member2", 2))
        self.assertEquals(-1.0, self.redis.zincrby(key, "member1", -2))

    def test_zrange(self):
        key = "zset"
        self.assertEquals([], self.redis.zrange(key, 0, -1))
        self.redis.zadd(key, "one", 1.5)
        self.redis.zadd(key, "two", 2.5)
        self.redis.zadd(key, "three", 3.5)

        # full range
        self.assertEquals(["one", "two", "three"],
                          self.redis.zrange(key, 0, -1))
        # withscores
        self.assertEquals([("one", 1.5), ("two", 2.5), ("three", 3.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))
        # score_cast_func
        self.assertEquals([("one", 1), ("two", 2), ("three", 3)],
                          self.redis.zrange(key, 0, -1, withscores=True, score_cast_func=int))

        # positive ranges
        self.assertEquals(["one"], self.redis.zrange(key, 0, 0))
        self.assertEquals(["one", "two"], self.redis.zrange(key, 0, 1))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, 0, 2))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, 0, 3))
        self.assertEquals(["two", "three"], self.redis.zrange(key, 1, 2))
        self.assertEquals(["three"], self.redis.zrange(key, 2, 3))

        # negative ends
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, 0, -1))
        self.assertEquals(["one", "two"], self.redis.zrange(key, 0, -2))
        self.assertEquals(["one"], self.redis.zrange(key, 0, -3))
        self.assertEquals([], self.redis.zrange(key, 0, -4))

        # negative starts
        self.assertEquals([], self.redis.zrange(key, -1, 0))
        self.assertEquals(["three"], self.redis.zrange(key, -1, -1))
        self.assertEquals(["two", "three"], self.redis.zrange(key, -2, -1))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, -3, -1))
        self.assertEquals(["one", "two", "three"], self.redis.zrange(key, -4, -1))

        # desc
        self.assertEquals(["three", "two", "one"], self.redis.zrange(key, 0, 2, desc=True))
        self.assertEquals(["two", "one"], self.redis.zrange(key, 1, 2, desc=True))
        self.assertEquals(["three", "two"], self.redis.zrange(key, 0, 1, desc=True))

    def test_zrem(self):
        key = "zset"
        self.assertFalse(self.redis.zrem(key, "two"))

        self.redis.zadd(key, "one", 1.0)
        self.assertEquals(1, self.redis.zcard(key))

        self.assertTrue(self.redis.zrem(key, "one"))
        self.assertEquals(0, self.redis.zcard(key))

    def test_zscore(self):
        key = "zset"
        self.assertEquals(None, self.redis.zscore(key, "one"))

        self.redis.zadd(key, "one", 1.0)
        self.assertEquals(1.0, self.redis.zscore(key, "one"))

    def test_zrank(self):
        key = "zset"
        self.assertEquals(None, self.redis.zrank(key, "two"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.assertEquals(0, self.redis.zrank(key, "one"))
        self.assertEquals(1, self.redis.zrank(key, "two"))

    def test_zcount(self):
        key = "zset"
        self.assertEquals(0, self.redis.zcount(key, "-inf", "inf"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)

        self.assertEquals(2, self.redis.zcount(key, "-inf", "inf"))
        self.assertEquals(1, self.redis.zcount(key, "-inf", 1.0))
        self.assertEquals(1, self.redis.zcount(key, "-inf", 1.5))
        self.assertEquals(2, self.redis.zcount(key, "-inf", 2.0))
        self.assertEquals(2, self.redis.zcount(key, "-inf", 2.5))
        self.assertEquals(1, self.redis.zcount(key, 0.5, 1.0))
        self.assertEquals(1, self.redis.zcount(key, 0.5, 1.5))
        self.assertEquals(2, self.redis.zcount(key, 0.5, 2.0))
        self.assertEquals(2, self.redis.zcount(key, 0.5, 2.5))
        self.assertEquals(2, self.redis.zcount(key, 0.5, "inf"))

        self.assertEquals(0, self.redis.zcount(key, "inf", "-inf"))
        self.assertEquals(0, self.redis.zcount(key, 2.0, 0.5))

    def test_zrangebyscore(self):
        key = "zset"
        self.assertEquals([], self.redis.zrangebyscore(key, "-inf", "inf"))
        self.redis.zadd(key, "one", 1.5)
        self.redis.zadd(key, "two", 2.5)
        self.redis.zadd(key, "three", 3.5)

        self.assertEquals(["one", "two", "three"],
                          self.redis.zrangebyscore(key, "-inf", "inf"))
        self.assertEquals([("one", 1.5), ("two", 2.5), ("three", 3.5)],
                          self.redis.zrangebyscore(key, "-inf", "inf", withscores=True))
        self.assertEquals([("one", 1), ("two", 2), ("three", 3)],
                          self.redis.zrangebyscore(key,
                                                   "-inf",
                                                   "inf",
                                                   withscores=True,
                                                   score_cast_func=int))

        self.assertEquals(["one"],
                          self.redis.zrangebyscore(key, 1.0, 2.0))
        self.assertEquals(["one", "two"],
                          self.redis.zrangebyscore(key, 1.0, 3.0))
        self.assertEquals(["one"],
                          self.redis.zrangebyscore(key, 1.0, 3.0, start=0, num=1))
        self.assertEquals(["two"],
                          self.redis.zrangebyscore(key, 1.0, 3.0, start=1, num=1))
        self.assertEquals(["two", "three"],
                          self.redis.zrangebyscore(key, 1.0, 3.5, start=1, num=4))
        self.assertEquals([],
                          self.redis.zrangebyscore(key, 1.0, 3.5, start=3, num=4))

    def test_zremrank(self):
        key = "zset"
        self.assertEquals(None, self.redis.zrevrank(key, "two"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.assertEquals(1, self.redis.zrevrank(key, "one"))
        self.assertEquals(0, self.redis.zrevrank(key, "two"))

    def test_zrevrangebyscore(self):
        key = "zset"
        self.assertEquals([], self.redis.zrevrangebyscore(key, "inf", "-inf"))
        self.redis.zadd(key, "one", 1.5)
        self.redis.zadd(key, "two", 2.5)
        self.redis.zadd(key, "three", 3.5)

        self.assertEquals(["three", "two", "one"],
                          self.redis.zrevrangebyscore(key, "inf", "-inf"))
        self.assertEquals([("three", 3.5), ("two", 2.5), ("one", 1.5)],
                          self.redis.zrevrangebyscore(key, "inf", "-inf", withscores=True))
        self.assertEquals([("three", 3), ("two", 2), ("one", 1)],
                          self.redis.zrevrangebyscore(key,
                                                      "inf",
                                                      "-inf",
                                                      withscores=True,
                                                      score_cast_func=int))

        self.assertEquals(["one"],
                          self.redis.zrevrangebyscore(key, 2.0, 1.0))
        self.assertEquals(["two", "one"],
                          self.redis.zrevrangebyscore(key, 3.0, 1.0))
        self.assertEquals(["two"],
                          self.redis.zrevrangebyscore(key, 3.0, 1.0, start=0, num=1))
        self.assertEquals(["one"],
                          self.redis.zrevrangebyscore(key, 3.0, 1.0, start=1, num=1))
        self.assertEquals(["two", "one"],
                          self.redis.zrevrangebyscore(key, 3.5, 1.0, start=1, num=4))
        self.assertEquals([],
                          self.redis.zrevrangebyscore(key, 3.5, 1.0, start=3, num=4))

    def test_zremrangebyrank(self):
        key = "zset"
        self.assertEquals(0, self.redis.zremrangebyrank(key, 0, -1))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.redis.zadd(key, "three", 3.0)

        self.assertEquals(2, self.redis.zremrangebyrank(key, 0, 1))

        self.assertEquals(["three"], self.redis.zrange(key, 0, -1))
        self.assertEquals(1, self.redis.zremrangebyrank(key, 0, -1))

        self.assertEquals([], self.redis.zrange(key, 0, -1))

    def test_zremrangebyscore(self):
        key = "zset"
        self.assertEquals(0, self.redis.zremrangebyscore(key, "-inf", "inf"))

        self.redis.zadd(key, "one", 1.0)
        self.redis.zadd(key, "two", 2.0)
        self.redis.zadd(key, "three", 3.0)

        self.assertEquals(1, self.redis.zremrangebyscore(key, 0, 1))

        self.assertEquals(["two", "three"], self.redis.zrange(key, 0, -1))
        self.assertEquals(2, self.redis.zremrangebyscore(key, 2.0, "inf"))

        self.assertEquals([], self.redis.zrange(key, 0, -1))

    def test_zunionstore_no_keys(self):
        key = "zset"

        self.assertEquals(0, self.redis.zunionstore(key, ["zset1", "zset2"]))

    def test_zunionstore_default(self):
        # sum is default
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"]))
        self.assertEquals([("one", 1.0), ("three", 3.0), ("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zunionstore_sum(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="sum"))
        self.assertEquals([("one", 1.0), ("three", 3.0), ("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zunionstore_SUM(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="SUM"))
        self.assertEquals([("one", 1.0), ("three", 3.0), ("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zunionstore_min(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="min"))
        self.assertEquals([("one", 1.0), ("two", 2.0), ("three", 3.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zunionstore_MIN(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="MIN"))
        self.assertEquals([("one", 1.0), ("two", 2.0), ("three", 3.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zunionstore_max(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        key = "zset"
        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="max"))
        self.assertEquals([("one", 1.0), ("two", 2.5), ("three", 3.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zunionstore_MAX(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        key = "zset"
        self.assertEquals(3, self.redis.zunionstore(key, ["zset1", "zset2"], aggregate="MAX"))
        self.assertEquals([("one", 1.0), ("two", 2.5), ("three", 3.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_no_keys(self):
        key = "zset"

        # no keys
        self.assertEquals(0, self.redis.zinterstore(key, ["zset1", "zset2"]))

    def test_zinterstore_default(self):
        # sum is default
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"]))
        self.assertEquals([("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_sum(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="sum"))
        self.assertEquals([("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_SUM(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="SUM"))
        self.assertEquals([("two", 4.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_min(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="min"))
        self.assertEquals([("two", 2.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_MIN(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="MIN"))
        self.assertEquals([("two", 2.0)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_max(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="max"))
        self.assertEquals([("two", 2.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))

    def test_zinterstore_MAX(self):
        key = "zset"
        self.redis.zadd("zset1", "one", 1.0)
        self.redis.zadd("zset1", "two", 2.0)
        self.redis.zadd("zset2", "two", 2.5)
        self.redis.zadd("zset2", "three", 3.0)

        self.assertEquals(1, self.redis.zinterstore(key, ["zset1", "zset2"], aggregate="MAX"))
        self.assertEquals([("two", 2.5)],
                          self.redis.zrange(key, 0, -1, withscores=True))