More easier to use fakeredis in django.
Project description
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.
Pros:
- One fakeredis server with mutiple connections for tests like the way of using redis.
- Combine override settings to DummyCACHE and fake get_redis_connection
- To disable the fake action with passing env: "NOFAKE_REDIS=1"
NOFAKE_REDIS=1 python manage.py test
Before you use django_fakeredis
, your everty test 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.fakeredis import FakeRedis
@FakeRedis("yourpath.get_redis_connection")
def test_foo():
...
from django_fakeredis.fakeredis import FakeRedis
with FakeRedis("yourpath.get_redis_connection"):
foo()
from django_fakeredis.fakeredis import FakeRedis
with FakeRedis("yourpath.cache"):
foo()
NOTE
If you want to mock django.core.cache.cache
with fakeredis, you should notice that there are internal cast in django cache.
i.e:
from django.core.cache import cache
cache.set("key", 2)
assert cache.get("key") == 2
but if you use fakeredis, you have to cast by hand:
con.set("key", 2)
concache.get("key").decode('utf8') == "2"
So I suggest to use django_redis with fakeredis in django.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for django_fakeredis-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d3de9ee3c05670d635690eeffcb5945a88a532d280a9ef5d1612d0916326455 |
|
MD5 | 2fab24ccf593cadb524011a679212aa9 |
|
BLAKE2b-256 | 095a5722bea4f214300f27c6a152c3efd621c7408710931b4683b96fe93237c0 |