|
| 1 | +import pytest |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +class TestFalkorDB: |
| 5 | + @pytest.fixture |
| 6 | + def falkor_db(self): |
| 7 | + # Mocking the FalkorDB instance |
| 8 | + class MockFalkorDB: |
| 9 | + def __init__(self): |
| 10 | + self.connection = MagicMock() |
| 11 | + self.sentinel = None |
| 12 | + self.service_name = "mymaster" |
| 13 | + |
| 14 | + return MockFalkorDB() |
| 15 | + |
| 16 | + def test_get_replica_connections_sentinel(self, falkor_db): |
| 17 | + # Set up sentinel mode |
| 18 | + falkor_db.sentinel = MagicMock() |
| 19 | + falkor_db.sentinel.discover_slaves.return_value = [("127.0.0.1", 6380), ("127.0.0.2", 6381)] |
| 20 | + |
| 21 | + result = falkor_db.get_replica_connections() |
| 22 | + |
| 23 | + assert result == [("127.0.0.1", 6380), ("127.0.0.2", 6381)] |
| 24 | + falkor_db.sentinel.discover_slaves.assert_called_once_with(service_name="mymaster") |
| 25 | + |
| 26 | + def test_get_replica_connections_cluster(self, falkor_db): |
| 27 | + # Set up cluster mode |
| 28 | + falkor_db.connection.execute_command.return_value = {"redis_mode": "cluster"} |
| 29 | + falkor_db.connection.cluster_nodes.return_value = { |
| 30 | + "127.0.0.1:6379": {"hostname": "127.0.0.1", "flags": "master"}, |
| 31 | + "127.0.0.2:6380": {"hostname": "127.0.0.2", "flags": "slave"}, |
| 32 | + "127.0.0.3:6381": {"hostname": "127.0.0.3", "flags": "slave"}, |
| 33 | + } |
| 34 | + |
| 35 | + result = falkor_db.get_replica_connections() |
| 36 | + |
| 37 | + assert result == [("127.0.0.2", 6380), ("127.0.0.3", 6381)] |
| 38 | + falkor_db.connection.cluster_nodes.assert_called_once() |
| 39 | + |
| 40 | + def test_get_replica_connections_unsupported_mode(self, falkor_db): |
| 41 | + # Set up unsupported mode |
| 42 | + falkor_db.connection.execute_command.return_value = {"redis_mode": "unknown"} |
| 43 | + |
| 44 | + with pytest.raises(ValueError, match="Unsupported Redis mode: unknown"): |
| 45 | + falkor_db.get_replica_connections() |
| 46 | + |
| 47 | + def test_get_replica_connections_connection_error(self, falkor_db): |
| 48 | + # Simulate connection error |
| 49 | + falkor_db.connection.execute_command.side_effect = ConnectionError("Connection failed") |
| 50 | + |
| 51 | + with pytest.raises(ConnectionError, match="Connection failed"): |
| 52 | + falkor_db.get_replica_connections() |
0 commit comments