Skip to content

Commit 577987c

Browse files
authored
Assignment Fix (#42)
* Fixing bug where assignment is worng * Update test_feature_variants.py * Update test_feature_variants.py
1 parent d26463d commit 577987c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

featuremanagement/_featuremanagerbase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ def _assign_variant(
168168
if targeting_context.user_id in user_allocation.users:
169169
evaluation_event.reason = VariantAssignmentReason.USER
170170
variant_name = user_allocation.variant
171-
elif feature.allocation.group and len(targeting_context.groups) > 0:
171+
if not variant_name and feature.allocation.group and len(targeting_context.groups) > 0:
172172
for group_allocation in feature.allocation.group:
173173
for group in targeting_context.groups:
174174
if group in group_allocation.groups:
175175
evaluation_event.reason = VariantAssignmentReason.GROUP
176176
variant_name = group_allocation.variant
177-
elif feature.allocation.percentile:
177+
if not variant_name and feature.allocation.percentile:
178178
context_id = targeting_context.user_id + "\n" + feature.allocation.seed
179179
box: float = self._is_targeted(context_id)
180180
for percentile_allocation in feature.allocation.percentile:

tests/test_feature_variants.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,46 @@ def test_basic_feature_variant_allocation_users(self):
112112
assert feature_manager.is_enabled("Alpha", "Charlie")
113113
assert feature_manager.get_variant("Alpha", "Charlie") is None
114114

115+
def test_user_and_group_allocation(self):
116+
feature_flags = {
117+
"feature_management": {
118+
"feature_flags": [
119+
{
120+
"id": "Alpha",
121+
"enabled": True,
122+
"variants": [
123+
{"name": "Off", "status_override": "Enabled"},
124+
{"name": "On", "status_override": "Disabled"},
125+
],
126+
"allocation": {
127+
"user": [{"variant": "On", "users": ["Adam"]}, {"variant": "Off", "users": ["Brittney"]}],
128+
"group": [
129+
{"variant": "On", "groups": ["Group1"]},
130+
{"variant": "Off", "groups": ["Group2"]},
131+
],
132+
},
133+
"conditions": {
134+
"client_filters": [
135+
{
136+
"name": "AlwaysOnFilter",
137+
"parameters": {},
138+
}
139+
]
140+
},
141+
}
142+
]
143+
}
144+
}
145+
feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOnFilter()])
146+
assert feature_manager.is_enabled("Alpha")
147+
assert feature_manager.get_variant("Alpha") is None
148+
assert not feature_manager.is_enabled("Alpha", TargetingContext(user_id="NotAdam", groups=["Group1"]))
149+
assert feature_manager.get_variant("Alpha", TargetingContext(user_id="NotAdam", groups=["Group1"])).name == "On"
150+
assert not feature_manager.is_enabled("Alpha", TargetingContext(user_id="NotAdam", groups=["Group1"]))
151+
assert feature_manager.get_variant("Alpha", TargetingContext(groups=["Group2"])).name == "Off"
152+
assert feature_manager.is_enabled("Alpha", TargetingContext(user_id="NotCharlie", groups=["Group3"]))
153+
assert feature_manager.get_variant("Alpha", TargetingContext(user_id="NotCharlie", groups=["Group3"])) is None
154+
115155
# method: is_enabled
116156
def test_basic_feature_variant_allocation_groups(self):
117157
feature_flags = {

0 commit comments

Comments
 (0)