-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connection factory goes to cache options
- Loading branch information
1 parent
3619218
commit e245d5d
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
SECRET_KEY = "django_tests_secret_key" | ||
|
||
SENTINELS = [("127.0.0.1", 26379)] | ||
|
||
conn_factory = "django_redis.pool.SentinelConnectionFactory" | ||
|
||
CACHES = { | ||
"default": { | ||
"BACKEND": "django_redis.cache.RedisCache", | ||
"LOCATION": ["redis://default_service?db=5"], | ||
"OPTIONS": { | ||
"CLIENT_CLASS": "django_redis.client.DefaultClient", | ||
"SENTINELS": SENTINELS, | ||
"CONNECTION_FACTORY": conn_factory, | ||
}, | ||
}, | ||
"doesnotexist": { | ||
"BACKEND": "django_redis.cache.RedisCache", | ||
"LOCATION": "redis://missing_service?db=1", | ||
"OPTIONS": { | ||
"CLIENT_CLASS": "django_redis.client.DefaultClient", | ||
"SENTINELS": SENTINELS, | ||
"CONNECTION_FACTORY": conn_factory, | ||
}, | ||
}, | ||
"sample": { | ||
"BACKEND": "django_redis.cache.RedisCache", | ||
"LOCATION": "redis://default_service?db=1", | ||
"OPTIONS": { | ||
"CLIENT_CLASS": "django_redis.client.SentinelClient", | ||
"SENTINELS": SENTINELS, | ||
"CONNECTION_FACTORY": conn_factory, | ||
}, | ||
}, | ||
"with_prefix": { | ||
"BACKEND": "django_redis.cache.RedisCache", | ||
"LOCATION": "redis://default_service?db=1", | ||
"KEY_PREFIX": "test-prefix", | ||
"OPTIONS": { | ||
"CLIENT_CLASS": "django_redis.client.DefaultClient", | ||
"SENTINELS": SENTINELS, | ||
"CONNECTION_FACTORY": conn_factory, | ||
}, | ||
}, | ||
} | ||
|
||
INSTALLED_APPS = ["django.contrib.sessions"] | ||
|
||
USE_TZ = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from django_redis import pool | ||
|
||
|
||
def test_connection_factory_redefine_from_opts(): | ||
cf = pool.get_connection_factory( | ||
path="django_redis.pool.ConnectionFactory", | ||
options={ | ||
"CONNECTION_FACTORY": "django_redis.pool.SentinelConnectionFactory", | ||
"SENTINELS": [("127.0.0.1", "26739")], | ||
}, | ||
) | ||
assert cf.__class__.__name__ == "SentinelConnectionFactory" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"conn_factory,expected", | ||
[ | ||
("django_redis.pool.SentinelConnectionFactory", pool.SentinelConnectionFactory), | ||
("django_redis.pool.ConnectionFactory", pool.ConnectionFactory), | ||
], | ||
) | ||
def test_connection_factory_opts(conn_factory: str, expected): | ||
cf = pool.get_connection_factory( | ||
path=None, | ||
options={ | ||
"CONNECTION_FACTORY": conn_factory, | ||
"SENTINELS": [("127.0.0.1", "26739")], | ||
}, | ||
) | ||
assert isinstance(cf, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"conn_factory,expected", | ||
[ | ||
("django_redis.pool.SentinelConnectionFactory", pool.SentinelConnectionFactory), | ||
("django_redis.pool.ConnectionFactory", pool.ConnectionFactory), | ||
], | ||
) | ||
def test_connection_factory_path(conn_factory: str, expected): | ||
cf = pool.get_connection_factory( | ||
path=conn_factory, | ||
options={ | ||
"SENTINELS": [("127.0.0.1", "26739")], | ||
}, | ||
) | ||
assert isinstance(cf, expected) | ||
|
||
|
||
def test_connection_factory_no_sentinels(): | ||
with pytest.raises(ImproperlyConfigured): | ||
pool.get_connection_factory( | ||
path=None, | ||
options={ | ||
"CONNECTION_FACTORY": "django_redis.pool.SentinelConnectionFactory", | ||
}, | ||
) |