Skip to content

Commit d4a37f3

Browse files
Fix connections.py: allow to connect to Redis using a Unix socket URL… (#392)
* Fix connections.py: allow to connect to Redis using a Unix socket URL without specifying the database number * add tests --------- Co-authored-by: Samuel Colvin <[email protected]>
1 parent 8fe4fc5 commit d4a37f3

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

arq/connections.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def from_dsn(cls, dsn: str) -> 'RedisSettings':
6161
if query_db:
6262
# e.g. redis://localhost:6379?db=1
6363
database = int(query_db[0])
64-
else:
64+
elif conf.scheme != 'unix':
6565
database = int(conf.path.lstrip('/')) if conf.path else 0
66+
else:
67+
database = 0
6668
return RedisSettings(
6769
host=conf.hostname or 'localhost',
6870
port=conf.port or 6379,

tests/test_utils.py

+56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import re
33
import sys
4+
from dataclasses import asdict
45
from datetime import timedelta
56

67
import pytest
@@ -197,3 +198,58 @@ def test_import_string_invalid_short():
197198
def test_import_string_invalid_missing():
198199
with pytest.raises(ImportError, match='Module "math" does not define a "foobar" attribute'):
199200
arq.utils.import_string('math.foobar')
201+
202+
203+
def test_settings_plain():
204+
settings = RedisSettings()
205+
assert asdict(settings) == {
206+
'host': 'localhost',
207+
'port': 6379,
208+
'unix_socket_path': None,
209+
'database': 0,
210+
'username': None,
211+
'password': None,
212+
'ssl': False,
213+
'ssl_keyfile': None,
214+
'ssl_certfile': None,
215+
'ssl_cert_reqs': 'required',
216+
'ssl_ca_certs': None,
217+
'ssl_ca_data': None,
218+
'ssl_check_hostname': False,
219+
'conn_timeout': 1,
220+
'conn_retries': 5,
221+
'conn_retry_delay': 1,
222+
'sentinel': False,
223+
'sentinel_master': 'mymaster',
224+
'retry_on_timeout': False,
225+
'retry_on_error': None,
226+
'retry': None,
227+
}
228+
229+
230+
def test_settings_from_socket_dsn():
231+
settings = RedisSettings.from_dsn('unix:///run/redis/redis.sock')
232+
# insert_assert(asdict(settings))
233+
assert asdict(settings) == {
234+
'host': 'localhost',
235+
'port': 6379,
236+
'unix_socket_path': '/run/redis/redis.sock',
237+
'database': 0,
238+
'username': None,
239+
'password': None,
240+
'ssl': False,
241+
'ssl_keyfile': None,
242+
'ssl_certfile': None,
243+
'ssl_cert_reqs': 'required',
244+
'ssl_ca_certs': None,
245+
'ssl_ca_data': None,
246+
'ssl_check_hostname': False,
247+
'conn_timeout': 1,
248+
'conn_retries': 5,
249+
'conn_retry_delay': 1,
250+
'sentinel': False,
251+
'sentinel_master': 'mymaster',
252+
'retry_on_timeout': False,
253+
'retry_on_error': None,
254+
'retry': None,
255+
}

0 commit comments

Comments
 (0)