1
+ import json
1
2
from unittest .mock import MagicMock , Mock , patch
2
3
4
+ import pytest
3
5
from azure .kusto .data import ClientRequestProperties
4
6
from azure .kusto .data .response import KustoResponseDataSet
5
7
@@ -21,6 +23,7 @@ def test_execute_basic_query(
21
23
mock_connection = MagicMock ()
22
24
mock_connection .query_client = mock_client
23
25
mock_connection .default_database = "default_db"
26
+ mock_connection .timeout_seconds = None # Add timeout_seconds attribute
24
27
mock_get_kusto_connection .return_value = mock_connection
25
28
26
29
query = " TestTable | take 10 " # Added whitespace to test stripping
@@ -197,3 +200,92 @@ def test_destructive_operation_with_custom_client_request_properties(
197
200
assert isinstance (result , list )
198
201
assert len (result ) == 1
199
202
assert result [0 ]["TestColumn" ] == "TestValue"
203
+
204
+
205
+ @patch ("fabric_rti_mcp.kusto.kusto_service.get_kusto_connection" )
206
+ def test_execute_error_includes_correlation_id (
207
+ mock_get_kusto_connection : Mock ,
208
+ sample_cluster_uri : str ,
209
+ ) -> None :
210
+ """Test that errors include correlation ID for easier debugging."""
211
+ # Arrange
212
+ mock_client = MagicMock ()
213
+ mock_client .execute .side_effect = Exception ("Kusto execution failed" )
214
+
215
+ mock_connection = MagicMock ()
216
+ mock_connection .query_client = mock_client
217
+ mock_connection .default_database = "default_db"
218
+ mock_get_kusto_connection .return_value = mock_connection
219
+
220
+ query = "TestTable | take 10"
221
+ database = "test_db"
222
+
223
+ # Act & Assert
224
+ with pytest .raises (RuntimeError ) as exc_info :
225
+ kusto_query (query , sample_cluster_uri , database = database )
226
+
227
+ error_message = str (exc_info .value )
228
+
229
+ # Verify the error message includes correlation ID and operation name
230
+ assert "correlation ID:" in error_message
231
+ assert "KFRTI_MCP.kusto_query:" in error_message
232
+ assert "kusto_query" in error_message
233
+ assert "Kusto execution failed" in error_message
234
+
235
+
236
+ @patch ("fabric_rti_mcp.kusto.kusto_service.get_kusto_connection" )
237
+ def test_execute_json_parse_error_includes_correlation_id (
238
+ mock_get_kusto_connection : Mock ,
239
+ sample_cluster_uri : str ,
240
+ ) -> None :
241
+ """Test that JSON parsing errors include correlation ID for easier debugging."""
242
+ # Arrange - simulate the kind of error that was happening in the issue
243
+ mock_client = MagicMock ()
244
+ mock_client .execute .side_effect = json .JSONDecodeError ("Expecting value" , "" , 0 )
245
+
246
+ mock_connection = MagicMock ()
247
+ mock_connection .query_client = mock_client
248
+ mock_connection .default_database = "default_db"
249
+ mock_get_kusto_connection .return_value = mock_connection
250
+
251
+ command = ".show version"
252
+
253
+ # Act & Assert
254
+ with pytest .raises (RuntimeError ) as exc_info :
255
+ kusto_command (command , sample_cluster_uri )
256
+
257
+ error_message = str (exc_info .value )
258
+
259
+ # Verify the error message includes correlation ID and operation name
260
+ assert "correlation ID:" in error_message
261
+ assert "KFRTI_MCP.kusto_command:" in error_message
262
+ assert "kusto_command" in error_message
263
+ assert "Expecting value" in error_message
264
+
265
+
266
+ @patch ("fabric_rti_mcp.kusto.kusto_service.logger" )
267
+ @patch ("fabric_rti_mcp.kusto.kusto_service.get_kusto_connection" )
268
+ def test_successful_operations_do_not_log_correlation_id (
269
+ mock_get_kusto_connection : Mock ,
270
+ mock_logger : Mock ,
271
+ sample_cluster_uri : str ,
272
+ mock_kusto_response : KustoResponseDataSet ,
273
+ ) -> None :
274
+ """Test that successful operations do not log correlation IDs (only errors do)."""
275
+ # Arrange
276
+ mock_client = MagicMock ()
277
+ mock_client .execute .return_value = mock_kusto_response
278
+
279
+ mock_connection = MagicMock ()
280
+ mock_connection .query_client = mock_client
281
+ mock_connection .default_database = "default_db"
282
+ mock_get_kusto_connection .return_value = mock_connection
283
+
284
+ query = "TestTable | take 10"
285
+
286
+ # Act
287
+ kusto_query (query , sample_cluster_uri )
288
+
289
+ # Assert - verify no info or debug logging occurs for successful operations
290
+ assert not mock_logger .info .called
291
+ assert not mock_logger .debug .called
0 commit comments