|
5 | 5 | import pytest |
6 | 6 | from redis.exceptions import RedisError |
7 | 7 |
|
8 | | -from src.tools.list import llen, lpop, lpush, lrange, rpop, rpush |
| 8 | +from src.tools.list import llen, lpop, lpush, lrange, rpop, rpush, lrem |
9 | 9 |
|
10 | 10 |
|
11 | 11 | class TestListOperations: |
@@ -278,3 +278,62 @@ async def test_push_operations_return_new_length( |
278 | 278 | # Results should indicate successful push regardless of return value |
279 | 279 | assert "pushed to the left of list" in result1 |
280 | 280 | assert "pushed to the right of list" in result2 |
| 281 | + |
| 282 | + @pytest.mark.asyncio |
| 283 | + async def test_lrem_success_single_removal(self, mock_redis_connection_manager): |
| 284 | + """Test successful removal of a single matching element.""" |
| 285 | + mock_redis = mock_redis_connection_manager |
| 286 | + mock_redis.lrem.return_value = 1 |
| 287 | + |
| 288 | + result = await lrem("test_list", 1, "value1") |
| 289 | + |
| 290 | + mock_redis.lrem.assert_called_once_with("test_list", 1, "value1") |
| 291 | + assert "Removed 1 occurrence(s) of 'value1' from list 'test_list'" in result |
| 292 | + |
| 293 | + @pytest.mark.asyncio |
| 294 | + async def test_lrem_success_multiple_removal(self, mock_redis_connection_manager): |
| 295 | + """Test successful removal of multiple matching elements.""" |
| 296 | + mock_redis = mock_redis_connection_manager |
| 297 | + mock_redis.lrem.return_value = 3 |
| 298 | + |
| 299 | + result = await lrem("test_list", 0, "value1") |
| 300 | + |
| 301 | + mock_redis.lrem.assert_called_once_with("test_list", 0, "value1") |
| 302 | + assert "Removed 3 occurrence(s) of 'value1' from list 'test_list'" in result |
| 303 | + |
| 304 | + @pytest.mark.asyncio |
| 305 | + async def test_lrem_no_elements_removed(self, mock_redis_connection_manager): |
| 306 | + """Test when no elements are removed because the element is not found.""" |
| 307 | + mock_redis = mock_redis_connection_manager |
| 308 | + mock_redis.lrem.return_value = 0 |
| 309 | + |
| 310 | + result = await lrem("test_list", 2, "missing_value") |
| 311 | + |
| 312 | + mock_redis.lrem.assert_called_once_with("test_list", 2, "missing_value") |
| 313 | + assert ( |
| 314 | + "Element 'missing_value' not found in list 'test_list' or list does not exist." |
| 315 | + in result |
| 316 | + ) |
| 317 | + |
| 318 | + @pytest.mark.asyncio |
| 319 | + async def test_lrem_negative_count(self, mock_redis_connection_manager): |
| 320 | + """Test removal with a negative count (from tail).""" |
| 321 | + mock_redis = mock_redis_connection_manager |
| 322 | + mock_redis.lrem.return_value = 2 |
| 323 | + |
| 324 | + result = await lrem("test_list", -2, "value_tail") |
| 325 | + |
| 326 | + mock_redis.lrem.assert_called_once_with("test_list", -2, "value_tail") |
| 327 | + assert "Removed 2 occurrence(s) of 'value_tail' from list 'test_list'" in result |
| 328 | + |
| 329 | + @pytest.mark.asyncio |
| 330 | + async def test_lrem_redis_error(self, mock_redis_connection_manager): |
| 331 | + """Test error handling during lrem operation.""" |
| 332 | + mock_redis = mock_redis_connection_manager |
| 333 | + mock_redis.lrem.side_effect = RedisError("Connection failed") |
| 334 | + |
| 335 | + result = await lrem("test_list", 1, "value1") |
| 336 | + |
| 337 | + assert ( |
| 338 | + "Error removing element from list 'test_list': Connection failed" in result |
| 339 | + ) |
0 commit comments