|
1 | 1 | from unittest.mock import MagicMock |
2 | 2 |
|
| 3 | +from django.utils import timezone |
3 | 4 | from pytest_django.fixtures import SettingsWrapper |
4 | 5 | from pytest_mock import MockerFixture |
5 | 6 | from pytest_structlog import StructuredLogCapture |
@@ -295,3 +296,63 @@ def test_refresh_project_segment_counts__counts_returned__upserts_per_env_rows( |
295 | 296 | f":project_{project.id}" |
296 | 297 | ) |
297 | 298 | ) |
| 299 | + |
| 300 | + |
| 301 | +def test_refresh_project_segment_counts__previously_matching_pair_drops_to_zero__row_deleted( |
| 302 | + mocker: MockerFixture, |
| 303 | + settings: SettingsWrapper, |
| 304 | + project: Project, |
| 305 | + environment: Environment, |
| 306 | + segment: Segment, |
| 307 | + enable_features: EnableFeaturesFixture, |
| 308 | +) -> None: |
| 309 | + # Given a prior refresh that landed a non-zero count for (segment, env) |
| 310 | + enable_features("segment_membership_inspection") |
| 311 | + settings.CLICKHOUSE_ENABLED = True |
| 312 | + SegmentMembershipCount.objects.create( |
| 313 | + segment=segment, |
| 314 | + environment=environment, |
| 315 | + count=15, |
| 316 | + last_synced_at=timezone.now(), |
| 317 | + ) |
| 318 | + cursor = MagicMock() |
| 319 | + open_cursor = mocker.patch.object(tasks, "open_clickhouse_cursor") |
| 320 | + open_cursor.return_value.__enter__.return_value = cursor |
| 321 | + # ... and a new compute that returns no matches for the same pair (the |
| 322 | + # rule was edited, the identity set drifted, etc.). |
| 323 | + mocker.patch.object(tasks, "compute_segment_counts_for_project", return_value=[]) |
| 324 | + |
| 325 | + # When |
| 326 | + refresh_project_segment_counts(project.id) |
| 327 | + |
| 328 | + # Then the stale row is gone -- pairs that no longer match drop out of |
| 329 | + # the table entirely rather than lingering at the previous count. |
| 330 | + assert not SegmentMembershipCount.objects.filter( |
| 331 | + segment=segment, environment=environment |
| 332 | + ).exists() |
| 333 | + |
| 334 | + |
| 335 | +def test_refresh_project_segment_counts__never_matched_pair__no_row_written( |
| 336 | + mocker: MockerFixture, |
| 337 | + settings: SettingsWrapper, |
| 338 | + project: Project, |
| 339 | + environment: Environment, |
| 340 | + segment: Segment, |
| 341 | + enable_features: EnableFeaturesFixture, |
| 342 | +) -> None: |
| 343 | + # Given a project with no prior membership rows |
| 344 | + enable_features("segment_membership_inspection") |
| 345 | + settings.CLICKHOUSE_ENABLED = True |
| 346 | + cursor = MagicMock() |
| 347 | + open_cursor = mocker.patch.object(tasks, "open_clickhouse_cursor") |
| 348 | + open_cursor.return_value.__enter__.return_value = cursor |
| 349 | + mocker.patch.object(tasks, "compute_segment_counts_for_project", return_value=[]) |
| 350 | + |
| 351 | + # When |
| 352 | + refresh_project_segment_counts(project.id) |
| 353 | + |
| 354 | + # Then no row is written: refresh upserts matches, drops misses, and |
| 355 | + # leaves never-matched pairs untouched. |
| 356 | + assert not SegmentMembershipCount.objects.filter( |
| 357 | + segment=segment, environment=environment |
| 358 | + ).exists() |
0 commit comments