Skip to content

Commit

Permalink
fix:fix connection method override for django-rq
Browse files Browse the repository at this point in the history
Fix #204
  • Loading branch information
cunla committed Jul 2, 2023
1 parent b4c3b92 commit 997fbe7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 106 deletions.
4 changes: 4 additions & 0 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ description: Change log of all fakeredis releases

- Implemented support for `JSON.MSET` #174, `JSON.MERGE` #181

### 🧰 Maintenance

- Updated how to test django_rq #204

## v2.15.0

### 🚀 Features
Expand Down
5 changes: 3 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ There is a need to override `django_rq.queues.get_redis_connection` with
a method returning the same connection.

```python
from fakeredis import FakeRedisConnSingleton
import django_rq
from fakeredis import get_fake_connection

django_rq.queues.get_redis_connection = FakeRedisConnSingleton()
django_rq.queues.get_redis_connection = get_fake_connection
```

## Known Limitations
Expand Down
4 changes: 2 additions & 2 deletions fakeredis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._server import FakeServer, FakeRedis, FakeStrictRedis, FakeConnection, FakeRedisConnSingleton
from ._server import FakeServer, FakeRedis, FakeStrictRedis, FakeConnection, get_fake_connection

try:
from importlib import metadata
Expand All @@ -7,4 +7,4 @@
__version__ = metadata.version("fakeredis")


__all__ = ["FakeServer", "FakeRedis", "FakeStrictRedis", "FakeConnection", "FakeRedisConnSingleton"]
__all__ = ["FakeServer", "FakeRedis", "FakeStrictRedis", "FakeConnection", "get_fake_connection"]
26 changes: 15 additions & 11 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import warnings
import weakref
from collections import defaultdict
from typing import Dict, Tuple
from typing import Dict, Tuple, Any

import redis

Expand Down Expand Up @@ -213,13 +213,17 @@ class FakeRedis(FakeRedisMixin, redis.Redis):
# Set up the connection before RQ Django reads the settings.
# The connection must be the same because in fakeredis connections
# do not share the state. Therefore, we define a singleton object to reuse it.
class FakeRedisConnSingleton:
"""Singleton FakeRedis connection."""

def __init__(self):
self.conn = None

def __call__(self, _, strict):
if not self.conn:
self.conn = FakeStrictRedis() if strict else FakeRedis()
return self.conn
def get_fake_connection(config: Dict[str, Any], strict: bool):
redis_cls = FakeStrictRedis if strict else FakeRedis
if 'URL' in config:
return redis_cls.from_url(
config['URL'],
db=config.get('DB'),
)
return redis_cls(
host=config['HOST'],
port=config['PORT'],
db=config.get('DB', 0),
username=config.get('USERNAME', None),
password=config.get('PASSWORD'),
)
Loading

0 comments on commit 997fbe7

Please sign in to comment.