Easier to use fakeredis in Django.
Project description
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
- 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"
- 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
Built Distribution
File details
Details for the file django-fakeredis-0.1.2.tar.gz
.
File metadata
- Download URL: django-fakeredis-0.1.2.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b135731b161837c20bca9db20b561462bee5d248c41a6b8ae7c70e9d7a1d527a |
|
MD5 | e77350b283b8b1a1a0af1a24e47f07b3 |
|
BLAKE2b-256 | 764a01fdaec3b03dde52128de835c2ae17f1a130e922d118ed544c9e39ce8bfc |
File details
Details for the file django_fakeredis-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: django_fakeredis-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a4372bb9459b2332a387ae06e0d117b982280206a74f54d79864cc2e3a8a869 |
|
MD5 | c93d91f2604ebc5f50a5cac397276eb0 |
|
BLAKE2b-256 | 296b7f788cb8d6e52dcf9e719bc9b163c1be974e0b148c3f6e4f4e729dfa4dff |