|
11 | 11 | from openfeature.event import ProviderEvent, EventDetails |
12 | 12 | from openfeature.exception import ErrorCode |
13 | 13 | from openfeature.flag_evaluation import Reason |
| 14 | +from openfeature.track import TrackingEventDetails |
14 | 15 | from openfeature import api |
15 | 16 |
|
16 | 17 | from ld_openfeature import LaunchDarklyProvider, Config |
@@ -150,6 +151,61 @@ def test_logger_changes_should_cascade_to_evaluation_converter(provider: LaunchD |
150 | 151 | assert caplog.records[0].message == "'kind' was set to a non-string value; defaulting to user" |
151 | 152 |
|
152 | 153 |
|
| 154 | +def test_track_without_context_does_not_send_an_event(provider: LaunchDarklyProvider): |
| 155 | + with patch.object(LDClient, 'track') as mock_track: |
| 156 | + provider.track("metric-key", None, None) |
| 157 | + |
| 158 | + mock_track.assert_not_called() |
| 159 | + |
| 160 | + |
| 161 | +def test_track_without_details_sends_event_without_data(provider: LaunchDarklyProvider, |
| 162 | + evaluation_context: EvaluationContext): |
| 163 | + with patch.object(LDClient, 'track') as mock_track: |
| 164 | + provider.track("metric-key", evaluation_context, None) |
| 165 | + |
| 166 | + mock_track.assert_called_once() |
| 167 | + name, context = mock_track.call_args.args |
| 168 | + assert name == "metric-key" |
| 169 | + assert context.key == 'user-key' |
| 170 | + |
| 171 | + |
| 172 | +def test_track_sends_attributes_as_data(provider: LaunchDarklyProvider, |
| 173 | + evaluation_context: EvaluationContext): |
| 174 | + with patch.object(LDClient, 'track') as mock_track: |
| 175 | + provider.track("metric-key", evaluation_context, TrackingEventDetails(attributes={'string': 'value'})) |
| 176 | + |
| 177 | + mock_track.assert_called_once() |
| 178 | + name, context, data = mock_track.call_args.args |
| 179 | + assert name == "metric-key" |
| 180 | + assert context.key == 'user-key' |
| 181 | + assert data == {'string': 'value'} |
| 182 | + |
| 183 | + |
| 184 | +def test_track_sends_value_as_metric_value(provider: LaunchDarklyProvider, |
| 185 | + evaluation_context: EvaluationContext): |
| 186 | + with patch.object(LDClient, 'track') as mock_track: |
| 187 | + provider.track("metric-key", evaluation_context, |
| 188 | + TrackingEventDetails(value=17, attributes={'string': 'value'})) |
| 189 | + |
| 190 | + mock_track.assert_called_once() |
| 191 | + name, context, data, metric_value = mock_track.call_args.args |
| 192 | + assert name == "metric-key" |
| 193 | + assert context.key == 'user-key' |
| 194 | + assert data == {'string': 'value'} |
| 195 | + assert metric_value == 17 |
| 196 | + |
| 197 | + |
| 198 | +def test_track_without_attributes_sends_metric_value_without_data(provider: LaunchDarklyProvider, |
| 199 | + evaluation_context: EvaluationContext): |
| 200 | + with patch.object(LDClient, 'track') as mock_track: |
| 201 | + provider.track("metric-key", evaluation_context, TrackingEventDetails(value=17)) |
| 202 | + |
| 203 | + mock_track.assert_called_once() |
| 204 | + name, context, data, metric_value = mock_track.call_args.args |
| 205 | + assert data is None |
| 206 | + assert metric_value == 17 |
| 207 | + |
| 208 | + |
153 | 209 | def test_provider_emits_ready_event_when_immediately_ready(): |
154 | 210 | emission_count = 0 |
155 | 211 | lock = threading.Lock() |
|
0 commit comments