Skip to content

Commit 6b768ae

Browse files
refactor: Update import statements in event_utils and event_utils_test for clarity
1 parent b4cc949 commit 6b768ae

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

src/backend/api/event_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Third-party
66
from applicationinsights import TelemetryClient
7-
from applicationinsights.channel import SynchronousSender, SynchronousQueue, TelemetryChannel
7+
from applicationinsights.channel import SynchronousQueue, SynchronousSender, TelemetryChannel
88

99
from dotenv import load_dotenv
1010

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for event_utils module."""
22

33
import os
4-
from unittest.mock import patch
4+
from unittest.mock import MagicMock, patch
55

66
from backend.api.event_utils import track_event_if_configured
77

@@ -11,36 +11,50 @@ class TestTrackEventIfConfigured:
1111

1212
def test_track_event_with_instrumentation_key(self):
1313
"""Test tracking event when instrumentation key is set."""
14-
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": "test-key"}):
15-
with patch("backend.api.event_utils.track_event") as mock_track:
14+
connection_string = "InstrumentationKey=test-key;IngestionEndpoint=https://test.com"
15+
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
16+
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
17+
mock_client = MagicMock()
18+
mock_get_client.return_value = mock_client
19+
1620
track_event_if_configured("TestEvent", {"key": "value"})
1721

18-
mock_track.assert_called_once_with("TestEvent", {"key": "value"})
22+
mock_client.track_event.assert_called_once_with("TestEvent", properties={"key": "value"})
23+
mock_client.flush.assert_called_once()
1924

2025
def test_track_event_without_instrumentation_key(self):
2126
"""Test tracking event when instrumentation key is not set."""
2227
with patch.dict(os.environ, {}, clear=True):
2328
# Remove the key if it exists
2429
os.environ.pop("APPLICATIONINSIGHTS_CONNECTION_STRING", None)
25-
with patch("backend.api.event_utils.track_event") as mock_track:
30+
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
2631
with patch("backend.api.event_utils.logging.warning") as mock_warning:
2732
track_event_if_configured("TestEvent", {"key": "value"})
2833

29-
mock_track.assert_not_called()
34+
mock_get_client.assert_not_called()
3035
mock_warning.assert_called_once()
3136

3237
def test_track_event_with_empty_data(self):
3338
"""Test tracking event with empty data."""
34-
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": "test-key"}):
35-
with patch("backend.api.event_utils.track_event") as mock_track:
39+
connection_string = "InstrumentationKey=test-key;IngestionEndpoint=https://test.com"
40+
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
41+
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
42+
mock_client = MagicMock()
43+
mock_get_client.return_value = mock_client
44+
3645
track_event_if_configured("TestEvent", {})
3746

38-
mock_track.assert_called_once_with("TestEvent", {})
47+
mock_client.track_event.assert_called_once_with("TestEvent", properties={})
48+
mock_client.flush.assert_called_once()
3949

4050
def test_track_event_with_complex_data(self):
4151
"""Test tracking event with complex data."""
42-
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": "test-key"}):
43-
with patch("backend.api.event_utils.track_event") as mock_track:
52+
connection_string = "InstrumentationKey=test-key;IngestionEndpoint=https://test.com"
53+
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
54+
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
55+
mock_client = MagicMock()
56+
mock_get_client.return_value = mock_client
57+
4458
complex_data = {
4559
"batch_id": "test-batch",
4660
"file_count": 10,
@@ -50,4 +64,38 @@ def test_track_event_with_complex_data(self):
5064

5165
track_event_if_configured("ComplexEvent", complex_data)
5266

53-
mock_track.assert_called_once_with("ComplexEvent", complex_data)
67+
# Values are converted to strings in the actual implementation
68+
expected_properties = {
69+
"batch_id": "test-batch",
70+
"file_count": "10",
71+
"status": "completed",
72+
"nested": "{'key': 'value'}",
73+
}
74+
75+
mock_client.track_event.assert_called_once_with("ComplexEvent", properties=expected_properties)
76+
mock_client.flush.assert_called_once()
77+
78+
def test_track_event_client_returns_none(self):
79+
"""Test tracking event when client initialization fails."""
80+
connection_string = "InstrumentationKey=test-key;IngestionEndpoint=https://test.com"
81+
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
82+
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
83+
mock_get_client.return_value = None
84+
with patch("backend.api.event_utils.logging.warning") as mock_warning:
85+
track_event_if_configured("TestEvent", {"key": "value"})
86+
87+
mock_warning.assert_called_once()
88+
89+
def test_track_event_with_exception(self):
90+
"""Test tracking event when an exception occurs."""
91+
connection_string = "InstrumentationKey=test-key;IngestionEndpoint=https://test.com"
92+
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
93+
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
94+
mock_client = MagicMock()
95+
mock_client.track_event.side_effect = Exception("Test error")
96+
mock_get_client.return_value = mock_client
97+
98+
with patch("backend.api.event_utils.logging.error") as mock_error:
99+
track_event_if_configured("TestEvent", {"key": "value"})
100+
101+
mock_error.assert_called_once()

0 commit comments

Comments
 (0)