fakeredis is a pure-Python implementation of the redis-py python client that simulates talking to a redis server. This was created for a single purpose: to write unittests. Setting up redis is not hard, but many times you want to write unittests that do not talk to an external server (such as redis). This module now allows tests to simply use this module as a reasonable substitute for redis.
Although fakeredis is pure Python, you will need lupa if you want to run Lua
scripts. If you install fakeredis with pip install fakeredis[lua]
it will
be automatically installed.
Consider using birdisle instead of fakeredis. It embeds the redis codebase into a Python extension, so it implements the full redis command set and behaves far more closely to a real redis implementation. The disadvantage is that it currently only works on Linux.
The intent is for fakeredis to act as though you're talking to a real redis server. It does this by storing state internally. For example:
>>> import fakeredis
>>> r = fakeredis.FakeStrictRedis()
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'
>>> r.lpush('bar', 1)
1
>>> r.lpush('bar', 2)
2
>>> r.lrange('bar', 0, -1)
[2, 1]
The state is stored in an instance of FakeServer. If one is not provided at construction, a new instance is automatically created for you, but you can explicitly create one to share state:
>>> import fakeredis
>>> server = fakeredis.FakeServer()
>>> r1 = fakeredis.FakeStrictRedis(server=server)
>>> r1.set('foo', 'bar')
True
>>> r2 = fakeredis.FakeStrictRedis(server=server)
>>> r2.get('foo')
'bar'
>>> r2.set('bar', 'baz')
True
>>> r1.get('bar')
'baz'
>>> r2.get('bar')
'baz'
It is also possible to mock connection errors so you can effectively test your error handling. Simply set the connected attribute of the server to False after initialization.
>>> import fakeredis
>>> server = fakeredis.FakeServer()
>>> server.connected = False
>>> r = fakeredis.FakeStrictRedis(server=server)
>>> r.set('foo', 'bar')
ConnectionError: FakeRedis is emulating a connection error.
>>> server.connected = True
>>> r.set('foo', 'bar')
True
Fakeredis implements the same interface as redis-py, the popular redis client for python, and models the responses of redis 5.0.
Version 1.0 is an almost total rewrite, intended to support redis-py 3.x and improve the Lua scripting emulation. It has a few backwards incompatibilities that may require changes to your code:
- By default, each FakeRedis or FakeStrictRedis instance contains its own state. This is equivalent to the singleton=True option to previous versions of fakeredis. This change was made to improve isolation between tests. If you need to share state between instances, create a FakeServer, as described above.
- FakeRedis is now a subclass of FakeStrictRedis, and similarly FakeStrictRedis is a subclass of StrictRedis. Code that uses isinstance may behave differently.
- The connected attribute is now a property of FakeServer, rather than FakeRedis or FakeStrictRedis. You can still pass the property to the constructor of the latter (provided no server is provided).
All of the redis commands are implemented in fakeredis with these exceptions:
- auth
- quit
- bgrewriteaof
- client id
- client kill
- client list
- client getname
- client pause
- client reply
- client setname
- client unblock
- command
- command count
- command getkeys
- command info
- config get
- config rewrite
- config set
- config resetstat
- debug object
- debug segfault
- info
- memory doctor
- memory help
- memory malloc-stats
- memory purge
- memory stats
- memory usage
- monitor
- role
- shutdown
- slaveof
- replicaof
- slowlog
- sync
- time
- bitfield
- bitop
- bitpos
- bzpopmin
- bzpopmax
- zpopmax
- zpopmin
- cluster addslots
- cluster count-failure-reports
- cluster countkeysinslot
- cluster delslots
- cluster failover
- cluster forget
- cluster getkeysinslot
- cluster info
- cluster keyslot
- cluster meet
- cluster nodes
- cluster replicate
- cluster reset
- cluster saveconfig
- cluster set-config-epoch
- cluster setslot
- cluster slaves
- cluster replicas
- cluster slots
- readonly
- readwrite
- dump
- migrate
- object
- restore
- touch
- unlink
- wait
- geoadd
- geohash
- geopos
- geodist
- georadius
- georadiusbymember
- pubsub
- script debug
- script exists
- script flush
- script kill
- xinfo
- xadd
- xtrim
- xdel
- xrange
- xrevrange
- xlen
- xread
- xgroup
- xreadgroup
- xack
- xclaim
- xpending
Apart from unimplemented commands, there are a number of cases where fakeredis won't give identical results to real redis. The following are differences that are unlikely to ever be fixed; there are also differences that are fixable (such as commands that do not support all features) which should be filed as bugs in Github.
- Hyperloglogs are implemented using sets underneath. This means that the type command will return the wrong answer, you can't use get to retrieve the encoded value, and counts will be slightly different (they will in fact be exact).
- When a command has multiple error conditions, such as operating on a key of the wrong type and an integer argument is not well-formed, the choice of error to return may not match redis.
- The incrbyfloat and hincrbyfloat commands in redis use the C long double type, which typically has more precision than Python's float type.
- Redis makes guarantees about the order in which clients blocked on blocking commands are woken up. Fakeredis does not honour these guarantees.
- Where redis contains bugs, fakeredis generally does not try to provide exact bug-compatibility. It's not practical for fakeredis to try to match the set of bugs in your specific version of redis.
- There are a number of cases where the behaviour of redis is undefined, such as the order of elements returned by set and hash commands. Fakeredis will generally not produce the same results, and in Python versions before 3.6 may produce different results each time the process is re-run.
- SCAN/ZSCAN/HSCAN/SSCAN will not necessary iterate all items if items are deleted or renamed during iteration. They also won't necessarily iterate in the same chunk sizes or the same order as redis.
Contributions are welcome. Please see the contributing guide for more details. The maintainer generally has very little time to work on fakeredis, so the best way to get a bug fixed is to contribute a pull request.
If you'd like to help out, you can start with any of the issues labeled with HelpWanted.
To ensure parity with the real redis, there are a set of integration tests that mirror the unittests. For every unittest that is written, the same test is run against a real redis instance using a real redis-py client instance. In order to run these tests you must have a redis server running on localhost, port 6379 (the default settings). The integration tests use db=10 in order to minimize collisions with an existing redis instance.
To run all the tests, install the requirements file:
pip install -r requirements.txt
If you just want to run the unittests:
nosetests test_fakeredis.py:TestFakeStrictRedis test_fakeredis.py:TestFakeRedis
Because this module is attempting to provide the same interface as redis-py, the python bindings to redis, a reasonable way to test this to to take each unittest and run it against a real redis server. fakeredis and the real redis server should give the same result. This ensures parity between the two. You can run these "integration" tests like this:
nosetests test_fakeredis.py:TestRealStrictRedis test_fakeredis.py:TestRealRedis test_fakeredis_hypothesis.py
In terms of implementation, TestRealRedis
is a subclass of
TestFakeRedis
that overrides a factory method to create
an instance of redis.Redis
(an actual python client for redis)
instead of fakeredis.FakeStrictRedis
.
To run both the unittests and the "integration" tests, run:
nosetests
If redis is not running and you try to run tests against a real redis server, these tests will have a result of 'S' for skipped.
There are some tests that test redis blocking operations that are somewhat slow. If you want to skip these tests during day to day development, they have all been tagged as 'slow' so you can skip them by running:
nosetests -a '!slow' test_fakeredis.py
- #235 Support for
redis==3.2
- #235 Depend on
redis<3.2
- Fix crash when a connection closes without unsubscribing and there is a subsequent PUBLISH
Version 1.0 is a major rewrite. It works at the redis protocol level, rather than at the redis-py level. This allows for many improvements and bug fixes.
- #225 Support redis-py 3.0
- #65 Support execute_command method
- #206 Drop Python 2.6 support
- #141 Support strings in integer arguments
- #218 Watches checks commands rather than final value
- #220 Better support for calling into redis from Lua
- #158 Better timestamp handling
- Support for register_script function.
- Fixes for race conditions caused by keys expiring mid-command
- Disallow certain commands in scripts
- Fix handling of blocking commands inside transactions
- Fix handling of PING inside pubsub connections
It also has new unit tests based on hypothesis, which has identified many corner cases that are now handled correctly.
Compared to 1.0b1:
- #231 Fix setup.py, fakeredis is directory/package now
- Fix some corner case handling of +0 vs -0
- Fix pubsub get_message with a timeout
- Disallow certain commands in scripts
- Fix handling of blocking commands inside transactions
- Fix handling of PING inside pubsub connections
- Make hypothesis tests skip if redis is not running
- Minor optimisations to zset
Version 1.0 is a major rewrite. It works at the redis protocol level, rather than at the redis-py level. This allows for many improvements and bug fixes.
- #225 Support redis-py 3.0
- #65 Support execute_command method
- #206 Drop Python 2.6 support
- #141 Support strings in integer arguments
- #218 Watches checks commands rather than final value
- #220 Better support for calling into redis from Lua
- #158 Better timestamp handling
- Support for register_script function.
- Fixes for race conditions caused by keys expiring mid-command
It also has new unit tests based on hypothesis, which has identified many corner cases that are now handled correctly.
- #224 Add __delitem__
- Restrict to redis<3
This release greatly improves support for threads: the bulk of commands are now
thread-safe, lock
has been rewritten to more closely match redis-py, and
pubsub now supports run_in_thread
:
- #213 pipeline.watch runs transaction even if no commands are queued
- #214 Added pubsub.run_in_thread as it is implemented in redis-py
- #215 Keep pace with redis-py for zrevrange method
- #216 Update behavior of lock to behave closer to redis lock
- #208 eval's KEYS and ARGV are now lua tables
- #209 Redis operation that returns dict now converted to Lua table when called inside eval operation
- #212 Optimize
_scan()
- Fix a typo in the Trove classifiers
- #197 Mock connection error
- #195 Align bool/len behaviour of pipeline
- #199 future.types.newbytes does not encode correctly
- #194 Support
score_cast_func
in zset functions - #192 Make
__getitem__
raise a KeyError for missing keys
This is a minor bug-fix release.
- #189 Add 'System' to the list of libc equivalents
This is a bug-fix release.
- #181 Upgrade twine & other packaging dependencies
- #106 randomkey method is not implemented, but is not in the list of unimplemented commands
- #170 Prefer readthedocs.io instead of readthedocs.org for doc links
- #180 zadd with no member-score pairs should fail
- #145 expire / _expire: accept 'long' also as time
- #182 Pattern matching does not match redis behaviour
- #135 Scan includes expired keys
- #185 flushall() doesn't clean everything
- #186 Fix psubscribe with handlers
- Run CI on PyPy
- Fix coverage measurement
This release merges the fakenewsredis fork back into fakeredis. The version number is chosen to be larger than any fakenewsredis release, so version numbers between the forks are comparable. All the features listed under fakenewsredis version numbers below are thus included in fakeredis for the first time in this release.
Additionally, the following was added: - #169 Fix set-bit
- #14 Add option to create an instance with non-shared data
- #13 Improve emulation of redis -> Lua returns
- #12 Update tox.ini: py35/py36 and extras for eval tests
- #11 Fix typo in private method name
This release makes a start on supporting Lua scripting: - #9 Add support for StrictRedis.eval for Lua scripts
This is a minor bugfix and optimization release: - #5 Update to match redis-py 2.10.6 - #7 Set with invalid expiry time should not set key - Avoid storing useless expiry times in hashes and sorted sets - Improve the performance of bulk zadd
This is a minor bugfix release: - #6 Fix iteration over pubsub list - #3 Preserve expiry time when mutating keys - Fixes to typos and broken links in documentation
This is the first release of fakenewsredis, based on fakeredis 0.9.0, with the following features and fixes:
- fakeredis #78 Behaviour of transaction() does not match redis-py
- fakeredis #79 Implement redis-py's .lock()
- fakeredis #90 HINCRBYFLOAT changes hash value type to float
- fakeredis #101 Should raise an error when attempting to get a key holding a list)
- fakeredis #146 Pubsub messages and channel names are forced to be ASCII strings on Python 2
- fakeredis #163 getset does not to_bytes the value
- fakeredis #165 linsert implementation is incomplete
- fakeredis #128 Remove _ex_keys mapping
- fakeredis #139 Fixed all flake8 errors and added flake8 to Travis CI
- fakeredis #166 Add type checking
- fakeredis #168 Use repr to encode floats in to_bytes