|
| 1 | +"""Tests for the RabbitMQ handlers module.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +from mcp_server_rabbitmq.handlers import ( |
| 6 | + handle_delete_exchange, |
| 7 | + handle_delete_queue, |
| 8 | + handle_enqueue, |
| 9 | + handle_fanout, |
| 10 | + handle_get_exchange_info, |
| 11 | + handle_get_queue_info, |
| 12 | + handle_list_exchanges, |
| 13 | + handle_list_queues, |
| 14 | + handle_purge_queue, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TestQueueHandlers: |
| 19 | + """Test the queue-related handler functions.""" |
| 20 | + |
| 21 | + @patch("mcp_server_rabbitmq.handlers.RabbitMQConnection") |
| 22 | + def test_handle_enqueue(self, mock_connection_class): |
| 23 | + """Test that handle_enqueue correctly publishes a message to a queue.""" |
| 24 | + # Setup mocks |
| 25 | + mock_connection = MagicMock() |
| 26 | + mock_channel = MagicMock() |
| 27 | + mock_connection.get_channel.return_value = (mock_connection, mock_channel) |
| 28 | + |
| 29 | + # Call the function |
| 30 | + handle_enqueue(mock_connection, "test-queue", "test-message") |
| 31 | + |
| 32 | + # Verify the expected calls were made |
| 33 | + mock_connection.get_channel.assert_called_once() |
| 34 | + mock_channel.queue_declare.assert_called_once_with("test-queue") |
| 35 | + mock_channel.basic_publish.assert_called_once_with( |
| 36 | + exchange="", routing_key="test-queue", body="test-message" |
| 37 | + ) |
| 38 | + mock_connection.close.assert_called_once() |
| 39 | + |
| 40 | + @patch("mcp_server_rabbitmq.handlers.RabbitMQConnection") |
| 41 | + def test_handle_fanout(self, mock_connection_class): |
| 42 | + """Test that handle_fanout correctly publishes a message to an exchange.""" |
| 43 | + # Setup mocks |
| 44 | + mock_connection = MagicMock() |
| 45 | + mock_channel = MagicMock() |
| 46 | + mock_connection.get_channel.return_value = (mock_connection, mock_channel) |
| 47 | + |
| 48 | + # Call the function |
| 49 | + handle_fanout(mock_connection, "test-exchange", "test-message") |
| 50 | + |
| 51 | + # Verify the expected calls were made |
| 52 | + mock_connection.get_channel.assert_called_once() |
| 53 | + mock_channel.exchange_declare.assert_called_once_with( |
| 54 | + exchange="test-exchange", exchange_type="fanout" |
| 55 | + ) |
| 56 | + mock_channel.basic_publish.assert_called_once_with( |
| 57 | + exchange="test-exchange", routing_key="", body="test-message" |
| 58 | + ) |
| 59 | + mock_connection.close.assert_called_once() |
| 60 | + |
| 61 | + def test_handle_list_queues(self): |
| 62 | + """Test that handle_list_queues correctly returns queue names.""" |
| 63 | + # Setup mock |
| 64 | + mock_admin = MagicMock() |
| 65 | + mock_admin.list_queues.return_value = [ |
| 66 | + {"name": "queue1", "other_field": "value1"}, |
| 67 | + {"name": "queue2", "other_field": "value2"}, |
| 68 | + ] |
| 69 | + |
| 70 | + # Call the function |
| 71 | + result = handle_list_queues(mock_admin) |
| 72 | + |
| 73 | + # Verify the result |
| 74 | + assert result == ["queue1", "queue2"] |
| 75 | + mock_admin.list_queues.assert_called_once() |
| 76 | + |
| 77 | + def test_handle_list_exchanges(self): |
| 78 | + """Test that handle_list_exchanges correctly returns exchange names.""" |
| 79 | + # Setup mock |
| 80 | + mock_admin = MagicMock() |
| 81 | + mock_admin.list_exchanges.return_value = [ |
| 82 | + {"name": "exchange1", "other_field": "value1"}, |
| 83 | + {"name": "exchange2", "other_field": "value2"}, |
| 84 | + ] |
| 85 | + |
| 86 | + # Call the function |
| 87 | + result = handle_list_exchanges(mock_admin) |
| 88 | + |
| 89 | + # Verify the result |
| 90 | + assert result == ["exchange1", "exchange2"] |
| 91 | + mock_admin.list_exchanges.assert_called_once() |
| 92 | + |
| 93 | + def test_handle_get_queue_info(self): |
| 94 | + """Test that handle_get_queue_info correctly returns queue information.""" |
| 95 | + # Setup mock |
| 96 | + mock_admin = MagicMock() |
| 97 | + expected_result = {"name": "test-queue", "messages": 10, "consumers": 2} |
| 98 | + mock_admin.get_queue_info.return_value = expected_result |
| 99 | + |
| 100 | + # Call the function with default vhost |
| 101 | + result = handle_get_queue_info(mock_admin, "test-queue") |
| 102 | + |
| 103 | + # Verify the result |
| 104 | + assert result == expected_result |
| 105 | + mock_admin.get_queue_info.assert_called_once_with("test-queue", "/") |
| 106 | + |
| 107 | + # Reset mock and test with custom vhost |
| 108 | + mock_admin.reset_mock() |
| 109 | + result = handle_get_queue_info(mock_admin, "test-queue", "custom-vhost") |
| 110 | + |
| 111 | + # Verify the result |
| 112 | + assert result == expected_result |
| 113 | + mock_admin.get_queue_info.assert_called_once_with("test-queue", "custom-vhost") |
| 114 | + |
| 115 | + def test_handle_delete_queue(self): |
| 116 | + """Test that handle_delete_queue correctly calls the admin method.""" |
| 117 | + # Setup mock |
| 118 | + mock_admin = MagicMock() |
| 119 | + |
| 120 | + # Call the function with default vhost |
| 121 | + handle_delete_queue(mock_admin, "test-queue") |
| 122 | + |
| 123 | + # Verify the call |
| 124 | + mock_admin.delete_queue.assert_called_once_with("test-queue", "/") |
| 125 | + |
| 126 | + # Reset mock and test with custom vhost |
| 127 | + mock_admin.reset_mock() |
| 128 | + handle_delete_queue(mock_admin, "test-queue", "custom-vhost") |
| 129 | + |
| 130 | + # Verify the call |
| 131 | + mock_admin.delete_queue.assert_called_once_with("test-queue", "custom-vhost") |
| 132 | + |
| 133 | + def test_handle_purge_queue(self): |
| 134 | + """Test that handle_purge_queue correctly calls the admin method.""" |
| 135 | + # Setup mock |
| 136 | + mock_admin = MagicMock() |
| 137 | + |
| 138 | + # Call the function with default vhost |
| 139 | + handle_purge_queue(mock_admin, "test-queue") |
| 140 | + |
| 141 | + # Verify the call |
| 142 | + mock_admin.purge_queue.assert_called_once_with("test-queue", "/") |
| 143 | + |
| 144 | + # Reset mock and test with custom vhost |
| 145 | + mock_admin.reset_mock() |
| 146 | + handle_purge_queue(mock_admin, "test-queue", "custom-vhost") |
| 147 | + |
| 148 | + # Verify the call |
| 149 | + mock_admin.purge_queue.assert_called_once_with("test-queue", "custom-vhost") |
| 150 | + |
| 151 | + |
| 152 | +class TestExchangeHandlers: |
| 153 | + """Test the exchange-related handler functions.""" |
| 154 | + |
| 155 | + def test_handle_delete_exchange(self): |
| 156 | + """Test that handle_delete_exchange correctly calls the admin method.""" |
| 157 | + # Setup mock |
| 158 | + mock_admin = MagicMock() |
| 159 | + |
| 160 | + # Call the function with default vhost |
| 161 | + handle_delete_exchange(mock_admin, "test-exchange") |
| 162 | + |
| 163 | + # Verify the call |
| 164 | + mock_admin.delete_exchange.assert_called_once_with("test-exchange", "/") |
| 165 | + |
| 166 | + # Reset mock and test with custom vhost |
| 167 | + mock_admin.reset_mock() |
| 168 | + handle_delete_exchange(mock_admin, "test-exchange", "custom-vhost") |
| 169 | + |
| 170 | + # Verify the call |
| 171 | + mock_admin.delete_exchange.assert_called_once_with("test-exchange", "custom-vhost") |
| 172 | + |
| 173 | + def test_handle_get_exchange_info(self): |
| 174 | + """Test that handle_get_exchange_info correctly returns exchange information.""" |
| 175 | + # Setup mock |
| 176 | + mock_admin = MagicMock() |
| 177 | + expected_result = {"name": "test-exchange", "type": "fanout", "durable": True} |
| 178 | + mock_admin.get_exchange_info.return_value = expected_result |
| 179 | + |
| 180 | + # Call the function with default vhost |
| 181 | + result = handle_get_exchange_info(mock_admin, "test-exchange") |
| 182 | + |
| 183 | + # Verify the result |
| 184 | + assert result == expected_result |
| 185 | + mock_admin.get_exchange_info.assert_called_once_with("test-exchange", "/") |
| 186 | + |
| 187 | + # Reset mock and test with custom vhost |
| 188 | + mock_admin.reset_mock() |
| 189 | + result = handle_get_exchange_info(mock_admin, "test-exchange", "custom-vhost") |
| 190 | + |
| 191 | + # Verify the result |
| 192 | + assert result == expected_result |
| 193 | + mock_admin.get_exchange_info.assert_called_once_with("test-exchange", "custom-vhost") |
0 commit comments