Skip to main content

More easier to use fakeredis in django.

Project description

CircleCI

More easier to 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.

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

Project details


Download files

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

Source Distribution

django-fakeredis-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_fakeredis-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file django-fakeredis-0.1.0.tar.gz.

File metadata

  • Download URL: django-fakeredis-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.5

File hashes

Hashes for django-fakeredis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e8988e54962c1755b3267d8a6609f97369f2327f16e238d4fd51fd357092ceb
MD5 144c94abec8bba9ce6efe5982cf15911
BLAKE2b-256 008ca69f9d4633d43d721ddc26a13e2b9e02d4c0c2adbf1af751b12975b24691

See more details on using hashes here.

File details

Details for the file django_fakeredis-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_fakeredis-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.5

File hashes

Hashes for django_fakeredis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5218286763ad8950ccf6a34119332cff024e64008d5a28546ad0e12be4fbf64b
MD5 f9b7ae23ecdc517922048ee98b856b8a
BLAKE2b-256 0d4c58d7967ccd1298124087657a8c815db57efbc5af7df052d85b4549aee275

See more details on using hashes here.

Supported by

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