Giter Club home page Giter Club logo

mock_redis's Introduction

MockRedis

Gem Version Build Status Coverage Status

MockRedis provides the same interface as redis-rb, but it stores its data in memory instead of talking to a Redis server. It is intended for use in tests.

The current implementation is tested against Redis 4. Older versions of Redis may return different results or not support some commands.

Getting Started

It's as easy as require 'mock_redis'; mr = MockRedis.new. Then you can call the same methods on it as you can call on a real Redis object.

For example:

>> require 'mock_redis'
>> mr = MockRedis.new
>> mr.set('some key', 'some value')
=> "OK"
>> mr.get('some key')
=> "some value"

Supported Features

mock_redis supports most of the methods that redis-rb does. Examples of supported methods:

  • String methods: get, set, append, incr, etc.
  • List methods: lpush, lpop, lrange, rpoplpush, etc.
  • Set methods: sadd, sinter, sismember, srandmember, etc.
  • Hash methods: hset, hget, hgetall, hmget, hincrby, hincrbyfloat etc.
  • Sorted set methods: zadd, zrank, zunionstore, etc.
  • Expirations: expire, pexpire, ttl, pttl, etc.
  • Transactions: multi, exec, discard
  • Futures

Mostly-Supported Commands

A MockRedis object can't do everything that a real Redis client can since it's an in-memory object confined to a single process. MockRedis makes every attempt to be Redis-compatible, but there are some necessary exceptions.

  • Blocking list commands (#blpop, #brpop, and #brpoplpush) work as expected if there is data for them to retrieve. If you use one of these commands with a nonzero timeout and there is no data for it to retrieve, then the command returns immediately. However, if you ask one of these commands for data with a 0 timeout (means "wait forever") and there is no data available, then a MockRedis::WouldBlock exception is raised. It's not what a real Redis client would do, but it beats hanging your test run forever.

  • #info just returns canned values; they don't update over time.

  • #sort supports ascending and descending sort. ALPHA sort is not yet supported.

Unsupported Commands

Some stuff, we just can't do with a single Ruby object in a single Ruby process.

  • Debugging commands (#debug('object', key) and #debug('segfault')) aren't available.

  • #object isn't available since we don't have any Redis internals to poke at.

  • #monitor isn't available; there's no place for requests to come from, so there's nothing to receive.

  • Pubsub commands (#psubscribe, #publish, #punsubscribe) aren't available.

  • #slowlog isn't available.

  • Scripting commands (#script, #eval, #evalsha) are just stubs—they won't execute anything

Remaining Work

There are some things we want to have in here, but that we just haven't gotten to doing yet. If you're interested in helping out, please submit a pull request with your (tested!) implementation.

  • #config(:get|:set|:resetstat) isn't done. They can just return canned values.

Compatibility

As of version 0.19.0, Ruby 2.2 and above are supported. For older versions of Ruby, use 0.18.0 or older.

Running the Tests

If you want to work on this, you'll probably want to run the tests. (Just kidding! There's no probably about it.) These tests were written with Redis running on localhost without any passwords required. If you're using a different version of Redis, you may see failures due to error message text being different. If you're running a really old version of Redis, you'll definitely see failures due to stuff that doesn't work!

mock_redis's People

Contributors

smerritt avatar sds avatar wincent avatar mjonuschat avatar yaauie avatar polpak avatar rkotov93 avatar cander avatar ecoffey avatar empact avatar snmgian avatar ches avatar chetan avatar dhiemstra avatar ejfinneran avatar jrmhaig avatar madlep avatar thejchap avatar kenchung avatar mattly avatar kcdragon avatar rvashurin avatar ryansch avatar saverio-kantox avatar sinclairtarget avatar tomasv avatar stupidcodefactory avatar adam-involver avatar axos88 avatar adimitrov avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.