-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathevent_utils_test.py
More file actions
101 lines (79 loc) · 5.1 KB
/
event_utils_test.py
File metadata and controls
101 lines (79 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Tests for event_utils module."""
import os
from unittest.mock import MagicMock, patch
from backend.api.event_utils import track_event_if_configured
class TestTrackEventIfConfigured:
"""Tests for track_event_if_configured function."""
def test_track_event_with_instrumentation_key(self):
"""Test tracking event when instrumentation key is set."""
connection_string = "InstrumentationKey=12345678-1234-5678-1234-567812345678;IngestionEndpoint=https://test.com"
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
mock_client = MagicMock()
mock_get_client.return_value = mock_client
track_event_if_configured("TestEvent", {"key": "value"})
mock_client.track_event.assert_called_once_with("TestEvent", properties={"key": "value"})
mock_client.flush.assert_called_once()
def test_track_event_without_instrumentation_key(self):
"""Test tracking event when instrumentation key is not set."""
with patch.dict(os.environ, {}, clear=True):
# Remove the key if it exists
os.environ.pop("APPLICATIONINSIGHTS_CONNECTION_STRING", None)
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
with patch("backend.api.event_utils.logging.warning") as mock_warning:
track_event_if_configured("TestEvent", {"key": "value"})
mock_get_client.assert_not_called()
mock_warning.assert_called_once()
def test_track_event_with_empty_data(self):
"""Test tracking event with empty data."""
connection_string = "InstrumentationKey=12345678-1234-5678-1234-567812345678;IngestionEndpoint=https://test.com"
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
mock_client = MagicMock()
mock_get_client.return_value = mock_client
track_event_if_configured("TestEvent", {})
mock_client.track_event.assert_called_once_with("TestEvent", properties={})
mock_client.flush.assert_called_once()
def test_track_event_with_complex_data(self):
"""Test tracking event with complex data."""
connection_string = "InstrumentationKey=12345678-1234-5678-1234-567812345678;IngestionEndpoint=https://test.com"
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
mock_client = MagicMock()
mock_get_client.return_value = mock_client
complex_data = {
"batch_id": "test-batch",
"file_count": 10,
"status": "completed",
"nested": {"key": "value"},
}
track_event_if_configured("ComplexEvent", complex_data)
# Values are converted to strings in the actual implementation
expected_properties = {
"batch_id": "test-batch",
"file_count": "10",
"status": "completed",
"nested": "{'key': 'value'}",
}
mock_client.track_event.assert_called_once_with("ComplexEvent", properties=expected_properties)
mock_client.flush.assert_called_once()
def test_track_event_client_returns_none(self):
"""Test tracking event when client initialization fails."""
connection_string = "InstrumentationKey=12345678-1234-5678-1234-567812345678;IngestionEndpoint=https://test.com"
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
mock_get_client.return_value = None
with patch("backend.api.event_utils.logging.warning") as mock_warning:
track_event_if_configured("TestEvent", {"key": "value"})
mock_warning.assert_called_once()
def test_track_event_with_exception(self):
"""Test tracking event when an exception occurs."""
connection_string = "InstrumentationKey=12345678-1234-5678-1234-567812345678;IngestionEndpoint=https://test.com"
with patch.dict(os.environ, {"APPLICATIONINSIGHTS_CONNECTION_STRING": connection_string}):
with patch("backend.api.event_utils._get_telemetry_client") as mock_get_client:
mock_client = MagicMock()
mock_client.track_event.side_effect = Exception("Test error")
mock_get_client.return_value = mock_client
with patch("backend.api.event_utils.logging.error") as mock_error:
track_event_if_configured("TestEvent", {"key": "value"})
mock_error.assert_called_once()