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.cachewith 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
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)
| File | Size | Uploaded | |
|---|---|---|---|
| django-fakeredis-0.1.2.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|