|
1 | | -from django.db import IntegrityError |
2 | | -from django.utils import timezone |
3 | 1 | from flag_engine.segments.constants import IS_SET |
4 | 2 | from pytest_mock import MockerFixture |
5 | 3 | from pytest_structlog import StructuredLogCapture |
|
8 | 6 | Cohort, |
9 | 7 | CohortMembership, |
10 | 8 | CohortMembershipState, |
11 | | - CohortSourceType, |
12 | 9 | ) |
13 | 10 | from cohorts.services import ( |
14 | 11 | apply_pending_memberships, |
15 | 12 | create_cohort, |
16 | 13 | delete_cohort, |
17 | | - get_or_create_cohort_for_source, |
18 | 14 | ) |
19 | 15 | from environments.dynamodb import DynamoIdentityWrapper |
20 | 16 | from environments.identities.models import Identity |
21 | 17 | from environments.models import Environment |
22 | | -from segments.models import Segment, SegmentManagedBy, SegmentRule |
| 18 | +from segments.models import SegmentManagedBy, SegmentRule |
23 | 19 |
|
24 | 20 |
|
25 | 21 | def test_apply_pending_memberships__no_pending_rows__returns_false( |
@@ -289,64 +285,3 @@ def test_apply_pending_memberships__system_trait_already_unset__deletes_membersh |
289 | 285 | identity.refresh_from_db() |
290 | 286 | assert identity.system_traits == {"flagsmith_cohort_other": True} |
291 | 287 | assert not CohortMembership.objects.filter(cohort=cohort).exists() |
292 | | - |
293 | | - |
294 | | -def test_get_or_create_cohort_for_source__simultaneous_creation__returns_other_requests_cohort( |
295 | | - environment: Environment, |
296 | | - mocker: MockerFixture, |
297 | | -) -> None: |
298 | | - # Given - creating the cohort fails because another request created its |
299 | | - # own cohort between our lookup and our insert |
300 | | - def create_winning_cohort_and_conflict(**kwargs: object) -> Cohort: |
301 | | - segment = Segment.objects.create( |
302 | | - name="Power users", project=environment.project |
303 | | - ) |
304 | | - Cohort.objects.create( |
305 | | - environment=environment, |
306 | | - segment=segment, |
307 | | - source_type=CohortSourceType.MIXPANEL, |
308 | | - external_id="mp-42", |
309 | | - ) |
310 | | - raise IntegrityError("unique_active_cohort_per_source_external_id") |
311 | | - |
312 | | - mocker.patch( |
313 | | - "cohorts.services.create_cohort_for_source", |
314 | | - side_effect=create_winning_cohort_and_conflict, |
315 | | - ) |
316 | | - |
317 | | - # When |
318 | | - cohort = get_or_create_cohort_for_source( |
319 | | - environment=environment, |
320 | | - name="Power users", |
321 | | - source_type=CohortSourceType.MIXPANEL, |
322 | | - external_id="mp-42", |
323 | | - ) |
324 | | - |
325 | | - # Then |
326 | | - assert cohort == Cohort.objects.get(external_id="mp-42") |
327 | | - |
328 | | - |
329 | | -def test_get_or_create_cohort_for_source__deletion_requested_same_external_id__returns_none( |
330 | | - environment: Environment, |
331 | | -) -> None: |
332 | | - # Given - a cohort with the same external ID is awaiting deletion, so it |
333 | | - # is invisible to the lookup but still occupies the external ID |
334 | | - segment = Segment.objects.create(name="Power users", project=environment.project) |
335 | | - Cohort.objects.create( |
336 | | - environment=environment, |
337 | | - segment=segment, |
338 | | - source_type=CohortSourceType.MIXPANEL, |
339 | | - external_id="mp-42", |
340 | | - deletion_requested_at=timezone.now(), |
341 | | - ) |
342 | | - |
343 | | - # When |
344 | | - cohort = get_or_create_cohort_for_source( |
345 | | - environment=environment, |
346 | | - name="Power users", |
347 | | - source_type=CohortSourceType.MIXPANEL, |
348 | | - external_id="mp-42", |
349 | | - ) |
350 | | - |
351 | | - # Then |
352 | | - assert cohort is None |
0 commit comments