Skip to main content

CircleCI

Easier use fakeredis in Django.

Install

pip install django-fakeredis

Why use it?

I have experienced many times to find bugs which is caused by mutiple fakeredis instances in tests. We just want to use fakerredis like redis with one redis-server and different connections and can debug with MONITOR command in redis

Features:

  • One fakeredis server with mutiple connections for tests like the way of using redis.
  • Combine override settings to Local-memory and fake get_redis_connection, django's cache
  • To disable the fake action with passing env: "NOFAKE_REDIS=1" NOFAKE_REDIS=1 python manage.py test

Before you use django_fakeredis, your tests code maybe like that:

server = fakeredis.FakeServer()
@override_settings(CACHES={"default": {"BACKEND": "django.core.cache.backends.dummy.DummyCache" }})
@patch('foo.get_redis_connection', fakeredis.FakeRedis(server=server)
def test_sth():
    ....

Now your can just:

@FakeRedis("yourpath.get_redis_connection")
def test_sth():
    ...

Usage

from django_fakeredis import FakeRedis
@FakeRedis("yourpath.get_redis_connection")
def test_foo():
    ...
from django_fakeredis import FakeRedis
with FakeRedis("yourpath.get_redis_connection"):
    foo()
from django_fakeredis import FakeRedis
with FakeRedis("yourpath.cache"):
    foo()

NOTE

  1. If you want to mock django.core.cache.cache with fakeredis, django-fakeredis do nothing but just override CACHE settings into Local-Memory for using the internal cast. So there are two mocked redis instance for django.cache and get_redis_connection .

If you want to use more redis commands, such as: sets, list..., you may need use django_redis, and cast the result by hand.

django.cache:

from django.core.cache import cache
cache.set("key", 2)
assert cache.get("key") == 2

you have to cast by hand, when using fakeredis or django_redis directly, you have to cast by hand:

import fakeredis
con = fakeredis.FakeStrictRedis()
con.set("key", 2)
assert con.get("key").decode('utf8') == "2"
  1. if you have a problem that mock is not worked, you may should to see where to patch

Metadata

Release files for django-fakeredis 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-fakeredis 0.1.2
File Size Uploaded
django-fakeredis-0.1.2.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-fakeredis 0.1.2
File Interpreter ABI Platform
django_fakeredis-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / django-fakeredis-0.1.2.tar.gz

Download URL django-fakeredis-0.1.2.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b135731b161837c20bca9db20b561462bee5d248c41a6b8ae7c70e9d7a1d527a
BLAKE2b-256 checksum
How to use checksums
764a01fdaec3b03dde52128de835c2ae17f1a130e922d118ed544c9e39ce8bfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2

Release files / django_fakeredis-0.1.2-py3-none-any.whl

Download URL django_fakeredis-0.1.2-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a4372bb9459b2332a387ae06e0d117b982280206a74f54d79864cc2e3a8a869
BLAKE2b-256 checksum
How to use checksums
296b7f788cb8d6e52dcf9e719bc9b163c1be974e0b148c3f6e4f4e729dfa4dff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page