Skip to main content

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 redis client itself. For example:

>>> import fakeredis
>>> r = fakeredis.FakeRedis()
>>> 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]

Unimplemented Commands

All of the redis commands are implemented in fakeredis with these exceptions:

generic

  • object

  • eval

connection

  • echo

  • select

  • quit

  • ping

  • auth

pubsub

  • punsubscribe

  • subscribe

  • psubscribe

  • publish

  • unsubscribe

transactions

  • exec

  • multi

  • discard

server

  • debug object

  • slowlog

  • sync

  • shutdown

  • lastsave

  • debug segfault

  • monitor

  • config resetstat

  • config get

  • save

  • bgsave

  • bgrewriteaof

  • slaveof

  • info

  • config set

  • dbsize

Adding New Commands

Adding support for more redis commands is easy:

  • Add unittests for the new command.

  • Implement new command.

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.

Running the Tests

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:TestFakeRedis

Because this module is attempting to provide the same interface as 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: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.FakeRedis.

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.

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

fakeredis-0.1.1.tar.gz (9.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for fakeredis-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a741f5f9dd0934db2bbf657612e2395104f4956739d8f0e30b1b0ba9c9779b5d
MD5 ba4aed3b510f02ffaf8b7733c0a7bf9a
BLAKE2b-256 65ced4144c2b2a3c472762af3d783842b62432eb5cc66f66575f3fac3095e115

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