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.
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 in the fakeredis module. 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]
By storing state in the fakeredis module, instances can share data:
>>> import fakeredis
>>> r1 = fakeredis.FakeStrictRedis()
>>> r1.set('foo', 'bar')
True
>>> r2 = fakeredis.FakeStrictRedis()
>>> r2.get('foo')
'bar'
>>> r2.set('bar', 'baz')
True
>>> r1.get('bar')
'baz'
>>> r2.get('bar')
'baz'
Because fakeredis stores state at the module level, if you want to ensure that you have a clean slate for every unit test you run, be sure to call r.flushall() in your tearDown method. For example:
def setUp(self): # Setup fake redis for testing. self.r = fakeredis.FakeStrictRedis() def tearDown(self): # Clear data in fakeredis. self.r.flushall()
Alternatively, you can create an instance that does not share data with other instances, by passing singleton=False to the constructor.
Fakeredis implements the same interface as redis-py, the popular redis client for python, and models the responses of redis 2.6.
Unimplemented Commands
All of the redis commands are implemented in fakeredis with these exceptions:
sorted_set
zscan
hash
hstrlen
string
bitop
bitpos
geo
geoadd
geopos
georadius
geohash
georadiusbymember
geodist
generic
restore
dump
migrate
object
wait
server
client list
lastsave
slowlog
debug object
shutdown
debug segfault
command count
monitor
client kill
cluster slots
role
config resetstat
time
config get
config set
save
client setname
command getkeys
config rewrite
sync
client getname
bgrewriteaof
slaveof
info
client pause
bgsave
command
dbsize
command info
cluster
cluster getkeysinslot
cluster info
readwrite
cluster slots
cluster keyslot
cluster addslots
readonly
cluster saveconfig
cluster forget
cluster meet
cluster slaves
cluster nodes
cluster countkeysinslot
cluster setslot
cluster count-failure-reports
cluster reset
cluster failover
cluster set-config-epoch
cluster delslots
cluster replicate
connection
echo
select
quit
auth
scripting
script flush
script kill
script load
evalsha
script exists
Contributing
Contributions are welcome. Please see the contributing guide for more details.
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
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'
Revision history
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-0.10.1.tar.gz
.
File metadata
- Download URL: fakeredis-0.10.1.tar.gz
- Upload date:
- Size: 43.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2be3d50fefe3528547c0e99ab5b62a82f5ae2ff6842b67e9f53f396d7375f3e |
|
MD5 | 465139b04b55eb4a46ae853fc26ee873 |
|
BLAKE2b-256 | 98517966dad6003edf6bc4c53c980a6654e0a54022cdc4f60402c2eed30c9d79 |
File details
Details for the file fakeredis-0.10.1-py2.py3-none-any.whl
.
File metadata
- Download URL: fakeredis-0.10.1-py2.py3-none-any.whl
- Upload date:
- Size: 26.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 684b5cc8ebe1aaff32353daad80a7b40c0ec986f0ad83452c16c6d64683d97e1 |
|
MD5 | 00eb857f4d74c92b51a88792e8dbf9ca |
|
BLAKE2b-256 | a4b0def57e414bfe8d69ee902a247c0deaf4079cf3ce196460f62260a71508d8 |