Skip to content

Commit b82003d

Browse files
authored
Merge pull request #6 from flowdacity/copilot/sub-pr-4
Fix test coverage gaps: unused imports, mock unix socket init, and cleanup
2 parents 182aa0f + a5c0934 commit b82003d

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

tests/test_edge_cases.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ class TestEdgeCases(unittest.IsolatedAsyncioTestCase):
115115
async def asyncSetUp(self):
116116
cwd = os.path.dirname(os.path.realpath(__file__))
117117
self.config_path = os.path.join(cwd, "test.conf")
118+
self.fq_instance = None
119+
120+
async def asyncTearDown(self):
121+
"""Clean up Redis state and close connections after each test."""
122+
# If a test initialized FQ with real Redis, clean up
123+
if self.fq_instance is not None:
124+
try:
125+
if self.fq_instance._r is not None:
126+
await self.fq_instance._r.flushdb()
127+
await self.fq_instance.close()
128+
except Exception:
129+
# Ignore errors during cleanup - tests may have mocked or closed connections
130+
# This prevents tearDown failures from masking test failures
131+
pass
132+
self.fq_instance = None
118133

119134
def test_missing_config_file_raises(self):
120135
with self.assertRaisesRegex(FQException, "Config file not found"):
@@ -159,22 +174,22 @@ async def test_cluster_initialization(self):
159174
async def test_dequeue_payload_none(self):
160175
"""Covers dequeue branch where payload is None (queue.py line 212)."""
161176
fq = FQ(self.config_path)
177+
self.fq_instance = fq
162178
await fq._initialize()
163179
fake_dequeue = FakeLuaDequeue()
164180
fq._lua_dequeue = fake_dequeue
165181
result = await fq.dequeue()
166182
self.assertEqual(result["status"], "failure")
167183
self.assertTrue(fake_dequeue.called)
168-
await fq.close()
169184

170185
async def test_clear_queue_delete_only(self):
171186
"""Covers clear_queue else branch (queue.py lines 499, 502)."""
172187
fq = FQ(self.config_path)
188+
self.fq_instance = fq
173189
await fq._initialize()
174190
await fq._r.flushdb()
175191
response = await fq.clear_queue(queue_type="noqueue", queue_id="missing")
176192
self.assertEqual(response["status"], "Failure")
177-
await fq.close()
178193

179194
async def test_close_fallback_paths(self):
180195
"""Covers close() fallback paths (queue.py lines 528-549)."""
@@ -249,10 +264,10 @@ async def test_get_queue_length_invalid_params(self):
249264
async def test_deep_status_real_redis(self):
250265
"""Covers deep_status with real redis (queue.py line 420)."""
251266
fq = FQ(self.config_path)
267+
self.fq_instance = fq
252268
await fq._initialize()
253269
result = await fq.deep_status()
254270
self.assertTrue(result)
255-
await fq.close()
256271

257272

258273
if __name__ == "__main__":

tests/test_func.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import unittest
99
import msgpack
1010
import tempfile
11-
from unittest.mock import patch, AsyncMock
11+
from unittest.mock import AsyncMock, MagicMock
1212
from fq import FQ
1313
from fq.exceptions import FQException
1414
from fq.utils import generate_epoch, deserialize_payload
@@ -1816,7 +1816,7 @@ async def test_close_with_none_client(self):
18161816
self.assertIsNone(fq._r)
18171817

18181818
async def test_initialize_unix_socket_connection(self):
1819-
"""Test initialization with unix socket connection - tests line 59."""
1819+
"""Test initialization with Unix socket connection - tests line 59."""
18201820
# Create a temporary config with unix_sock
18211821
with tempfile.NamedTemporaryFile(mode='w', suffix='.conf', delete=False) as f:
18221822
f.write("""[fq]
@@ -1833,14 +1833,29 @@ async def test_initialize_unix_socket_connection(self):
18331833
config_path = f.name
18341834

18351835
try:
1836-
fq = FQ(config_path)
1837-
# Mock the Redis ping to avoid actual connection attempt
1838-
mock_redis = AsyncMock()
1839-
mock_redis.ping = AsyncMock(return_value=True)
1840-
fq._r = mock_redis
1841-
# This tests the unix_sock path was configured (line 59)
1842-
self.assertIsNotNone(fq._r)
1843-
await fq.close()
1836+
# Create a mock Redis class to capture initialization parameters
1837+
mock_redis_instance = MagicMock()
1838+
mock_redis_instance.ping = AsyncMock(return_value=True)
1839+
mock_redis_instance.register_script = MagicMock(return_value=MagicMock())
1840+
mock_redis_instance.aclose = AsyncMock()
1841+
1842+
redis_init_kwargs = {}
1843+
1844+
def mock_redis_constructor(**kwargs):
1845+
redis_init_kwargs.update(kwargs)
1846+
return mock_redis_instance
1847+
1848+
# Patch Redis to intercept the initialization
1849+
with unittest.mock.patch('fq.queue.Redis', side_effect=mock_redis_constructor):
1850+
fq = FQ(config_path)
1851+
await fq._initialize()
1852+
1853+
# Verify that Redis was initialized with unix_socket_path
1854+
self.assertIn('unix_socket_path', redis_init_kwargs)
1855+
self.assertEqual(redis_init_kwargs['unix_socket_path'], '/tmp/redis_nonexistent.sock')
1856+
self.assertEqual(int(redis_init_kwargs['db']), 0)
1857+
1858+
await fq.close()
18441859
finally:
18451860
os.unlink(config_path)
18461861

0 commit comments

Comments
 (0)