mockredis.MockRedis

Here are the examples of the python api mockredis.MockRedis taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

4 Examples 7

Example 1

Project: mockredis Source File: test_script.py
    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))

Example 2

Project: sync-engine Source File: test_sync_start_logic.py
@pytest.fixture(scope='function')
def mock_queue_client(monkeypatch):
    # Stop mockredis from trying to run Lua imports that aren't really needed.
    monkeypatch.setattr('mockredis.script.Script._import_lua_dependencies',
                        staticmethod(lambda *args, **kwargs: None))
    cl = QueueClient(zone='testzone')
    cl.redis = mockredis.MockRedis()
    return cl

Example 3

Project: mockredis Source File: test_pubsub.py
Function: set_up
    def setup(self):
        self.redis = MockRedis()
        self.redis.flushdb()

Example 4

Project: mockredis Source File: test_script.py
    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)