|
| 1 | +import json |
| 2 | +from concurrent.futures import Future |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from flagsmith.analytics import ( |
| 8 | + PipelineAnalyticsConfig, |
| 9 | + PipelineAnalyticsProcessor, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def test_record_evaluation_event_buffers_event( |
| 14 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 15 | +) -> None: |
| 16 | + pipeline_analytics_processor.record_evaluation_event( |
| 17 | + flag_key="my_flag", |
| 18 | + enabled=True, |
| 19 | + value="variant_a", |
| 20 | + identity_identifier="user123", |
| 21 | + traits={"plan": "premium"}, |
| 22 | + ) |
| 23 | + |
| 24 | + assert len(pipeline_analytics_processor._buffer) == 1 |
| 25 | + event = pipeline_analytics_processor._buffer[0] |
| 26 | + assert event["event_id"] == "my_flag" |
| 27 | + assert event["event_type"] == "flag_evaluation" |
| 28 | + assert event["identity_identifier"] == "user123" |
| 29 | + assert event["enabled"] is True |
| 30 | + assert event["value"] == "variant_a" |
| 31 | + assert event["traits"] == {"plan": "premium"} |
| 32 | + assert "sdk_version" in event["metadata"] |
| 33 | + assert isinstance(event["evaluated_at"], int) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "second_enabled, expected_count", |
| 38 | + [ |
| 39 | + (True, 1), # same fingerprint -> deduplicated |
| 40 | + (False, 2), # different fingerprint -> both kept |
| 41 | + ], |
| 42 | +) |
| 43 | +def test_evaluation_event_deduplication( |
| 44 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 45 | + second_enabled: bool, |
| 46 | + expected_count: int, |
| 47 | +) -> None: |
| 48 | + pipeline_analytics_processor.record_evaluation_event( |
| 49 | + flag_key="my_flag", enabled=True, value="v1", identity_identifier="user1" |
| 50 | + ) |
| 51 | + pipeline_analytics_processor.record_evaluation_event( |
| 52 | + flag_key="my_flag", enabled=second_enabled, value="v1", identity_identifier="user1" |
| 53 | + ) |
| 54 | + |
| 55 | + assert len(pipeline_analytics_processor._buffer) == expected_count |
| 56 | + |
| 57 | + |
| 58 | +def test_dedup_keys_cleared_after_flush( |
| 59 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 60 | +) -> None: |
| 61 | + with mock.patch("flagsmith.analytics.session"): |
| 62 | + pipeline_analytics_processor.record_evaluation_event( |
| 63 | + flag_key="my_flag", enabled=True, value="v1", identity_identifier="user1" |
| 64 | + ) |
| 65 | + pipeline_analytics_processor.flush() |
| 66 | + |
| 67 | + pipeline_analytics_processor.record_evaluation_event( |
| 68 | + flag_key="my_flag", enabled=True, value="v1", identity_identifier="user1" |
| 69 | + ) |
| 70 | + |
| 71 | + assert len(pipeline_analytics_processor._buffer) == 1 |
| 72 | + |
| 73 | + |
| 74 | +def test_auto_flush_on_buffer_full() -> None: |
| 75 | + config = PipelineAnalyticsConfig( |
| 76 | + analytics_server_url="http://test/", max_buffer=5 |
| 77 | + ) |
| 78 | + processor = PipelineAnalyticsProcessor(config=config, environment_key="key") |
| 79 | + |
| 80 | + with mock.patch("flagsmith.analytics.session"): |
| 81 | + for i in range(5): |
| 82 | + processor.record_evaluation_event( |
| 83 | + flag_key=f"flag_{i}", enabled=True, value=str(i) |
| 84 | + ) |
| 85 | + |
| 86 | + assert len(processor._buffer) == 0 |
| 87 | + |
| 88 | + |
| 89 | +def test_flush_sends_correct_http_request( |
| 90 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 91 | +) -> None: |
| 92 | + with mock.patch("flagsmith.analytics.session") as mock_session: |
| 93 | + pipeline_analytics_processor.record_evaluation_event( |
| 94 | + flag_key="my_flag", enabled=True, value="v1", identity_identifier="user1" |
| 95 | + ) |
| 96 | + pipeline_analytics_processor.flush() |
| 97 | + |
| 98 | + mock_session.post.assert_called_once() |
| 99 | + call_kwargs = mock_session.post.call_args |
| 100 | + assert call_kwargs[0][0] == "http://test_analytics/v1/analytics/batch" |
| 101 | + |
| 102 | + headers = call_kwargs[1]["headers"] |
| 103 | + assert headers["X-Environment-Key"] == "test_key" |
| 104 | + assert headers["Content-Type"] == "application/json; charset=utf-8" |
| 105 | + assert "flagsmith-python-client/" in headers["Flagsmith-SDK-User-Agent"] |
| 106 | + |
| 107 | + body = json.loads(call_kwargs[1]["data"]) |
| 108 | + assert body["environment_key"] == "test_key" |
| 109 | + assert len(body["events"]) == 1 |
| 110 | + assert body["events"][0]["event_id"] == "my_flag" |
| 111 | + |
| 112 | + |
| 113 | +def test_flush_noop_when_empty( |
| 114 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 115 | +) -> None: |
| 116 | + with mock.patch("flagsmith.analytics.session") as mock_session: |
| 117 | + pipeline_analytics_processor.flush() |
| 118 | + |
| 119 | + mock_session.post.assert_not_called() |
| 120 | + |
| 121 | + |
| 122 | +def test_failed_flush_requeues_events( |
| 123 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 124 | +) -> None: |
| 125 | + future: Future[None] = Future() |
| 126 | + future.set_exception(Exception("connection error")) |
| 127 | + |
| 128 | + with mock.patch("flagsmith.analytics.session") as mock_session: |
| 129 | + mock_session.post.return_value = future |
| 130 | + pipeline_analytics_processor.record_evaluation_event( |
| 131 | + flag_key="my_flag", enabled=True, value="v1" |
| 132 | + ) |
| 133 | + pipeline_analytics_processor.flush() |
| 134 | + |
| 135 | + assert len(pipeline_analytics_processor._buffer) == 1 |
| 136 | + assert pipeline_analytics_processor._buffer[0]["event_id"] == "my_flag" |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.parametrize( |
| 140 | + "url, expected_endpoint", |
| 141 | + [ |
| 142 | + ("http://example.com", "http://example.com/v1/analytics/batch"), |
| 143 | + ("http://example.com/", "http://example.com/v1/analytics/batch"), |
| 144 | + ], |
| 145 | +) |
| 146 | +def test_url_trailing_slash_handling(url: str, expected_endpoint: str) -> None: |
| 147 | + config = PipelineAnalyticsConfig(analytics_server_url=url) |
| 148 | + processor = PipelineAnalyticsProcessor(config=config, environment_key="key") |
| 149 | + assert processor._batch_endpoint == expected_endpoint |
| 150 | + |
| 151 | + |
| 152 | +def test_record_custom_event( |
| 153 | + pipeline_analytics_processor: PipelineAnalyticsProcessor, |
| 154 | +) -> None: |
| 155 | + pipeline_analytics_processor.record_custom_event( |
| 156 | + event_name="purchase", |
| 157 | + identity_identifier="user1", |
| 158 | + traits={"plan": "premium"}, |
| 159 | + metadata={"amount": 99}, |
| 160 | + ) |
| 161 | + # Custom events are never deduplicated |
| 162 | + pipeline_analytics_processor.record_custom_event( |
| 163 | + event_name="purchase", |
| 164 | + identity_identifier="user1", |
| 165 | + ) |
| 166 | + |
| 167 | + assert len(pipeline_analytics_processor._buffer) == 2 |
| 168 | + event = pipeline_analytics_processor._buffer[0] |
| 169 | + assert event["event_id"] == "purchase" |
| 170 | + assert event["event_type"] == "custom_event" |
| 171 | + assert event["enabled"] is None |
| 172 | + assert event["value"] is None |
| 173 | + assert event["traits"] == {"plan": "premium"} |
| 174 | + assert event["metadata"]["amount"] == 99 |
| 175 | + assert "sdk_version" in event["metadata"] |
| 176 | + |
| 177 | + |
| 178 | +def test_start_stop_lifecycle() -> None: |
| 179 | + config = PipelineAnalyticsConfig( |
| 180 | + analytics_server_url="http://test/", flush_interval_seconds=100 |
| 181 | + ) |
| 182 | + processor = PipelineAnalyticsProcessor(config=config, environment_key="key") |
| 183 | + |
| 184 | + processor.start() |
| 185 | + assert processor._timer is not None |
| 186 | + assert processor._timer.is_alive() |
| 187 | + |
| 188 | + with mock.patch("flagsmith.analytics.session"): |
| 189 | + processor.record_evaluation_event( |
| 190 | + flag_key="my_flag", enabled=True, value="v1" |
| 191 | + ) |
| 192 | + processor.stop() |
| 193 | + |
| 194 | + assert len(processor._buffer) == 0 |
0 commit comments