Fake implementation of redis API for testing purposes.
Project description
fakeredis: A fake version of a redis-py
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.
Alternatives
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.
How to Use
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.
Porting to fakeredis 1.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).
Unimplemented Commands
All of the redis commands are implemented in fakeredis with these exceptions:
connection
auth
quit
server
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
string
bitfield
bitop
bitpos
sorted_set
bzpopmin
bzpopmax
zpopmax
zpopmin
cluster
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
generic
dump
migrate
object
restore
touch
unlink
wait
geo
geoadd
geohash
geopos
geodist
georadius
georadiusbymember
pubsub
pubsub
scripting
script debug
script exists
script flush
script kill
stream
xinfo
xadd
xtrim
xdel
xrange
xrevrange
xlen
xread
xgroup
xreadgroup
xack
xclaim
xpending
Other limitations
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, 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 redis 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 during iteration. They also won’t necessarily iterate in the same chunk sizes or the same order as redis.
Contributing
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.
Running the Tests
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
Revision history
1.0.3
#235 Support for redis==3.2
1.0.2
#235 Depend on redis<3.2
1.0.1
Fix crash when a connection closes without unsubscribing and there is a subsequent PUBLISH
1.0
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.
1.0rc1
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
1.0b1
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.
0.16.0
#224 Add __delitem__
Restrict to redis<3
0.15.0
0.14.0
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:
0.13.1
0.13.0.1
Fix a typo in the Trove classifiers
0.13.0
0.12.0
0.11.0
0.10.3
This is a minor bug-fix release.
#189 Add ‘System’ to the list of libc equivalents
0.10.2
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
0.10.1
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
fakenewsredis 0.10.0
fakenewsredis 0.9.5
This release makes a start on supporting Lua scripting: - #9 Add support for StrictRedis.eval for Lua scripts
fakenewsredis 0.9.4
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
fakenewsredis 0.9.3
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
fakenewsredis 0.9.2
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fakeredis-1.0.3.tar.gz
.
File metadata
- Download URL: fakeredis-1.0.3.tar.gz
- Upload date:
- Size: 72.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94c98b320e9d64535e9ffea360512ad8181129d4b07439168feaf5efc412711f |
|
MD5 | 361680e5e82d01a43192bb2971cc24a7 |
|
BLAKE2b-256 | a298e522a70987b35052f2776f654b014c01218ce6a473711277d3a7c2570931 |
File details
Details for the file fakeredis-1.0.3-py2.py3-none-any.whl
.
File metadata
- Download URL: fakeredis-1.0.3-py2.py3-none-any.whl
- Upload date:
- Size: 29.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e87dd5be186aad89679e4c64b9510d223f0390c23ed44aaf84ab4cde225c60a7 |
|
MD5 | 02c7afb1f7903ffa04fe79e867bc07f7 |
|
BLAKE2b-256 | d4148f2ee2331e35a5d8c6a9be1fb46087c31d89d03c63912cb1355e8d4685d6 |