|
| 1 | +""" |
| 2 | +Pytest configuration and fixtures for Redis MCP Server tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from unittest.mock import Mock, patch |
| 7 | +import redis |
| 8 | +from redis.exceptions import RedisError, ConnectionError, TimeoutError |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_redis(): |
| 13 | + """Create a mock Redis connection.""" |
| 14 | + mock = Mock(spec=redis.Redis) |
| 15 | + return mock |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_redis_cluster(): |
| 20 | + """Create a mock Redis Cluster connection.""" |
| 21 | + mock = Mock(spec=redis.cluster.RedisCluster) |
| 22 | + return mock |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def mock_redis_connection_manager(): |
| 27 | + """Mock the RedisConnectionManager to return a mock Redis connection.""" |
| 28 | + with patch('src.common.connection.RedisConnectionManager.get_connection') as mock_get_conn: |
| 29 | + mock_redis = Mock(spec=redis.Redis) |
| 30 | + mock_get_conn.return_value = mock_redis |
| 31 | + yield mock_redis |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def redis_config(): |
| 36 | + """Sample Redis configuration for testing.""" |
| 37 | + return { |
| 38 | + "host": "localhost", |
| 39 | + "port": 6379, |
| 40 | + "db": 0, |
| 41 | + "username": None, |
| 42 | + "password": "", |
| 43 | + "ssl": False, |
| 44 | + "ssl_ca_path": None, |
| 45 | + "ssl_keyfile": None, |
| 46 | + "ssl_certfile": None, |
| 47 | + "ssl_cert_reqs": "required", |
| 48 | + "ssl_ca_certs": None, |
| 49 | + "cluster_mode": False, |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def redis_uri_samples(): |
| 55 | + """Sample Redis URIs for testing.""" |
| 56 | + return { |
| 57 | + "basic": "redis://localhost:6379/0", |
| 58 | + "with_auth": "redis://user:pass@localhost:6379/0", |
| 59 | + "ssl": "rediss://user:pass@localhost:6379/0", |
| 60 | + "with_query": "redis://localhost:6379/0?ssl_cert_reqs=required", |
| 61 | + "cluster": "redis://localhost:6379/0?cluster_mode=true", |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture |
| 66 | +def sample_vector(): |
| 67 | + """Sample vector for testing vector operations.""" |
| 68 | + return [0.1, 0.2, 0.3, 0.4, 0.5] |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture |
| 72 | +def sample_json_data(): |
| 73 | + """Sample JSON data for testing.""" |
| 74 | + return { |
| 75 | + "name": "John Doe", |
| 76 | + "age": 30, |
| 77 | + "city": "New York", |
| 78 | + "hobbies": ["reading", "swimming"] |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def redis_error_scenarios(): |
| 84 | + """Common Redis error scenarios for testing.""" |
| 85 | + return { |
| 86 | + "connection_error": ConnectionError("Connection refused"), |
| 87 | + "timeout_error": TimeoutError("Operation timed out"), |
| 88 | + "generic_error": RedisError("Generic Redis error"), |
| 89 | + "auth_error": RedisError("NOAUTH Authentication required"), |
| 90 | + "wrong_type": RedisError("WRONGTYPE Operation against a key holding the wrong kind of value"), |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +@pytest.fixture(autouse=True) |
| 95 | +def reset_connection_manager(): |
| 96 | + """Reset the RedisConnectionManager singleton before each test.""" |
| 97 | + from src.common.connection import RedisConnectionManager |
| 98 | + RedisConnectionManager._instance = None |
| 99 | + yield |
| 100 | + RedisConnectionManager._instance = None |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture |
| 104 | +def mock_numpy_array(): |
| 105 | + """Mock numpy array for vector testing.""" |
| 106 | + with patch('numpy.array') as mock_array: |
| 107 | + mock_array.return_value.tobytes.return_value = b'mock_binary_data' |
| 108 | + yield mock_array |
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture |
| 112 | +def mock_numpy_frombuffer(): |
| 113 | + """Mock numpy frombuffer for vector testing.""" |
| 114 | + with patch('numpy.frombuffer') as mock_frombuffer: |
| 115 | + mock_frombuffer.return_value.tolist.return_value = [0.1, 0.2, 0.3] |
| 116 | + yield mock_frombuffer |
| 117 | + |
| 118 | + |
| 119 | +# Async test helpers |
| 120 | +@pytest.fixture |
| 121 | +def event_loop(): |
| 122 | + """Create an event loop for async tests.""" |
| 123 | + import asyncio |
| 124 | + loop = asyncio.new_event_loop() |
| 125 | + yield loop |
| 126 | + loop.close() |
| 127 | + |
| 128 | + |
| 129 | +# Mark configurations |
| 130 | +def pytest_configure(config): |
| 131 | + """Configure pytest markers.""" |
| 132 | + config.addinivalue_line("markers", "unit: mark test as a unit test") |
| 133 | + config.addinivalue_line("markers", "integration: mark test as an integration test") |
| 134 | + config.addinivalue_line("markers", "slow: mark test as slow running") |
0 commit comments