Skip to main content

Fake implementation of redis API for testing purposes.

Project description

fakeredis: A fake version of a redis-py

https://secure.travis-ci.org/jamesls/fakeredis.png?branch=master https://pypip.in/version/fakeredis/badge.svg https://pypip.in/py_versions/fakeredis/badge.svg https://pypip.in/license/fakeredis/badge.svg https://coveralls.io/repos/jamesls/fakeredis/badge.png?branch=master

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'

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:

set

  • sscan

hash

  • hscan

hyperloglog

  • pfcount

  • pfadd

  • pfmerge

generic

  • restore

  • dump

  • pexpireat

  • pexpire

  • migrate

  • object

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

sorted_set

  • zrangebylex

  • zscan

  • zrevrangebylex

  • zremrangebylex

  • zlexcount

connection

  • echo

  • select

  • quit

  • auth

scripting

  • script flush

  • script kill

  • script load

  • evalsha

  • eval

  • script exists

string

  • incrbyfloat

  • bitop

  • bitpos

pubsub

  • punsubscribe

  • subscribe

  • publish

  • pubsub

  • psubscribe

  • unsubscribe

Contributing

Contributions are welcome. Please see the contributing guide for more details.

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'

Project details


Release history Release notifications | RSS feed

This version

0.6.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fakeredis-0.6.2.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

fakeredis-0.6.2-py2.py3-none-any.whl (16.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fakeredis-0.6.2.tar.gz.

File metadata

  • Download URL: fakeredis-0.6.2.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fakeredis-0.6.2.tar.gz
Algorithm Hash digest
SHA256 26f00771753f23848b83b22fa492ebdfcbe399c3bf15fb59f21126f95e33bde4
MD5 7b182f827b261e8bda035c2533cd339d
BLAKE2b-256 d4ae7038b4d65589fbe94814eb2a9815b063ce60c12d38eddf3c98b614eb9857

See more details on using hashes here.

File details

Details for the file fakeredis-0.6.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fakeredis-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a6e17ccbef9b6092f569615267cfd8d32b564980f3c622d40ed90caa0f3a3e6f
MD5 5dbebcd30624c43a5210eb6b192ad70d
BLAKE2b-256 0f3df830df938cb0cb2571bf05541b34cc6731a931f7364b99974a1c40dd6c98

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page