-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(feature flags): copy cohorts linked to a flag (#18642)
- Loading branch information
1 parent
b8c188c
commit 93e2011
Showing
6 changed files
with
433 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from typing import Dict, Set | ||
from posthog.test.base import ( | ||
APIBaseTest, | ||
) | ||
from posthog.models.cohort import Cohort | ||
from posthog.models.cohort.util import sort_cohorts_topologically | ||
|
||
|
||
class TestFeatureFlagUtils(APIBaseTest): | ||
def setUp(self): | ||
super().setUp() | ||
|
||
def test_cohorts_sorted_topologically(self): | ||
cohorts = {} | ||
|
||
def create_cohort(name): | ||
cohorts[name] = Cohort.objects.create( | ||
team=self.team, | ||
name=name, | ||
filters={ | ||
"properties": { | ||
"type": "AND", | ||
"values": [ | ||
{"key": "name", "value": "test", "type": "person"}, | ||
], | ||
} | ||
}, | ||
) | ||
|
||
create_cohort("a") | ||
create_cohort("b") | ||
create_cohort("c") | ||
|
||
# (c)-->(b) | ||
cohorts["c"].filters["properties"]["values"][0] = { | ||
"key": "id", | ||
"value": cohorts["b"].pk, | ||
"type": "cohort", | ||
"negation": True, | ||
} | ||
cohorts["c"].save() | ||
|
||
# (a)-->(c) | ||
cohorts["a"].filters["properties"]["values"][0] = { | ||
"key": "id", | ||
"value": cohorts["c"].pk, | ||
"type": "cohort", | ||
"negation": True, | ||
} | ||
cohorts["a"].save() | ||
|
||
cohort_ids = {cohorts["a"].pk, cohorts["b"].pk, cohorts["c"].pk} | ||
seen_cohorts_cache = { | ||
str(cohorts["a"].pk): cohorts["a"], | ||
str(cohorts["b"].pk): cohorts["b"], | ||
str(cohorts["c"].pk): cohorts["c"], | ||
} | ||
|
||
# (a)-->(c)-->(b) | ||
# create b first, since it doesn't depend on any other cohorts | ||
# then c, because it depends on b | ||
# then a, because it depends on c | ||
|
||
# thus destination creation order: b, c, a | ||
destination_creation_order = [cohorts["b"].pk, cohorts["c"].pk, cohorts["a"].pk] | ||
topologically_sorted_cohort_ids = sort_cohorts_topologically(cohort_ids, seen_cohorts_cache) | ||
self.assertEqual(topologically_sorted_cohort_ids, destination_creation_order) | ||
|
||
def test_empty_cohorts_set(self): | ||
cohort_ids: Set[int] = set() | ||
seen_cohorts_cache: Dict[str, Cohort] = {} | ||
topologically_sorted_cohort_ids = sort_cohorts_topologically(cohort_ids, seen_cohorts_cache) | ||
self.assertEqual(topologically_sorted_cohort_ids, []) |
Oops, something went wrong.