|
| 1 | +from datetime import datetime |
1 | 2 | from unittest.mock import MagicMock |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 | from common.test_tools import RunTasksFixture |
| 6 | +from django.db import connections |
5 | 7 | from flag_engine.segments.constants import EQUAL |
6 | 8 | from pytest_django.fixtures import SettingsWrapper |
7 | 9 | from pytest_mock import MockerFixture |
@@ -186,6 +188,96 @@ def matching_segment(segment: Segment) -> Segment: |
186 | 188 | return segment |
187 | 189 |
|
188 | 190 |
|
| 191 | +@pytest.mark.clickhouse |
| 192 | +def test_get_segment_members_page__deleted_identity__excluded( |
| 193 | + segment_membership_identities: None, |
| 194 | + matching_segment: Segment, |
| 195 | + environment: Environment, |
| 196 | + environment_api_key_str: str, |
| 197 | +) -> None: |
| 198 | + # Given |
| 199 | + with connections["clickhouse"].cursor() as cursor: |
| 200 | + cursor.executemany( |
| 201 | + "INSERT INTO IDENTITIES " |
| 202 | + "(environment_id, identifier, identity_key, traits, is_deleted) VALUES", |
| 203 | + [(environment_api_key_str, "aaron", "aaron_key", {"foo": "bar"}, True)], # type: ignore[list-item] |
| 204 | + ) |
| 205 | + |
| 206 | + # When |
| 207 | + members = get_segment_members_page( |
| 208 | + matching_segment, environment, cursor=None, limit=100 |
| 209 | + ) |
| 210 | + |
| 211 | + # Then |
| 212 | + assert [member["identifier"] for member in members] == ["alice", "bob"] |
| 213 | + |
| 214 | + |
| 215 | +@pytest.mark.clickhouse |
| 216 | +def test_get_segment_members_page__duplicate_versions__returns_latest_once( |
| 217 | + segment_membership_identities: None, |
| 218 | + matching_segment: Segment, |
| 219 | + environment: Environment, |
| 220 | + environment_api_key_str: str, |
| 221 | +) -> None: |
| 222 | + # Given |
| 223 | + # a newer version of alice that still matches the segment |
| 224 | + with connections["clickhouse"].cursor() as cursor: |
| 225 | + cursor.executemany( |
| 226 | + "INSERT INTO IDENTITIES " |
| 227 | + "(environment_id, identifier, identity_key, traits, inserted_at) VALUES", |
| 228 | + [ |
| 229 | + ( # type: ignore[list-item] |
| 230 | + environment_api_key_str, |
| 231 | + "alice", |
| 232 | + "alice_key", |
| 233 | + {"foo": "bar", "version": "new"}, |
| 234 | + datetime(2099, 1, 1), |
| 235 | + ) |
| 236 | + ], |
| 237 | + ) |
| 238 | + |
| 239 | + # When |
| 240 | + members = get_segment_members_page( |
| 241 | + matching_segment, environment, cursor=None, limit=100 |
| 242 | + ) |
| 243 | + |
| 244 | + # Then |
| 245 | + # alice appears once, with the latest version's traits |
| 246 | + assert members == [ |
| 247 | + SegmentMember( |
| 248 | + identifier="alice", |
| 249 | + identity_key="alice_key", |
| 250 | + traits={"foo": "bar", "version": "new"}, |
| 251 | + ), |
| 252 | + SegmentMember(identifier="bob", identity_key="bob_key", traits={"foo": "bar"}), |
| 253 | + ] |
| 254 | + |
| 255 | + |
| 256 | +@pytest.mark.clickhouse |
| 257 | +def test_compute_segment_counts_for_project__deleted_identity__excluded_from_count( |
| 258 | + segment_membership_identities: None, |
| 259 | + matching_segment: Segment, |
| 260 | + project: Project, |
| 261 | + environment_api_key_str: str, |
| 262 | +) -> None: |
| 263 | + # Given |
| 264 | + with connections["clickhouse"].cursor() as cursor: |
| 265 | + cursor.executemany( |
| 266 | + "INSERT INTO IDENTITIES " |
| 267 | + "(environment_id, identifier, identity_key, traits, is_deleted) VALUES", |
| 268 | + [(environment_api_key_str, "aaron", "aaron_key", {"foo": "bar"}, True)], # type: ignore[list-item] |
| 269 | + ) |
| 270 | + |
| 271 | + # When |
| 272 | + with connections["clickhouse"].cursor() as cursor: |
| 273 | + counts = compute_segment_counts_for_project(project, cursor) |
| 274 | + |
| 275 | + # Then |
| 276 | + assert len(counts) == 1 |
| 277 | + assert counts[0].segment_id == matching_segment.id |
| 278 | + assert counts[0].count == 2 |
| 279 | + |
| 280 | + |
189 | 281 | @pytest.mark.clickhouse |
190 | 282 | def test_get_segment_members_page__matching_identities__returns_members_ordered_by_identifier( |
191 | 283 | segment_membership_identities: None, |
|
0 commit comments