Giter VIP home page Giter VIP logo

django-fakeredis's Introduction

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

django-fakeredis's People

Contributors

o3o3o avatar pirelle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.