| 1 |
from typing import Any |
| 2 |
|
| 3 |
from django.test import TestCase |
| 4 |
from redis import Redis |
| 5 |
|
| 6 |
|
| 7 |
class DjangoRQTestCase(TestCase): |
| 8 |
"""Base test case for django-rq tests with common assertion helpers.""" |
| 9 |
|
| 10 |
def assert_connection_kwargs(self, connection: Redis, config: dict[str, Any]) -> None: |
| 11 |
""" |
| 12 |
Assert that connection pool kwargs match expected configuration. |
| 13 |
|
| 14 |
Args: |
| 15 |
connection: Redis connection object |
| 16 |
config: Dict with config keys (either HOST/PORT/DB or lowercase equivalents) |
| 17 |
|
| 18 |
Examples: |
| 19 |
# With settings-based config (HOST/PORT/DB keys) |
| 20 |
config = QUEUES['default'] |
| 21 |
queue = get_queue('default') |
| 22 |
self.assert_connection_kwargs(queue.connection, config) |
| 23 |
|
| 24 |
# With explicit kwargs |
| 25 |
self.assert_connection_kwargs(connection, { |
| 26 |
'host': 'localhost', |
| 27 |
'port': 6379, |
| 28 |
'db': 0 |
| 29 |
}) |
| 30 |
""" |
| 31 |
connection_kwargs = connection.connection_pool.connection_kwargs |
| 32 |
|
| 33 |
# If config has HOST/PORT/DB keys (from settings.RQ_QUEUES) |
| 34 |
if 'HOST' in config: |
| 35 |
self.assertEqual(connection_kwargs['host'], config['HOST']) |
| 36 |
self.assertEqual(connection_kwargs['port'], config['PORT']) |
| 37 |
self.assertEqual(connection_kwargs['db'], config.get('DB', 0)) |
| 38 |
else: |
| 39 |
# Direct comparison for explicit kwargs |
| 40 |
for key, value in config.items(): |
| 41 |
self.assertEqual(connection_kwargs[key], value) |