Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(feature-flags): bug handling multiple conditions #599

Merged
merged 8 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions aws_lambda_powertools/utilities/feature_flags/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def _evaluate_conditions(
rule_match_value = rule.get(schema.RULE_MATCH_VALUE)
conditions = cast(List[Dict], rule.get(schema.CONDITIONS_KEY))

if not conditions:
logger.debug(
f"rule did not match, no conditions to match, rule_name={rule_name}, rule_value={rule_match_value}, "
f"name={feature_name} "
)
return False

for condition in conditions:
context_value = context.get(str(condition.get(schema.CONDITION_KEY)))
cond_action = condition.get(schema.CONDITION_ACTION, "")
Expand All @@ -76,9 +83,8 @@ def _evaluate_conditions(
)
return False # context doesn't match condition

logger.debug(f"rule matched, rule_name={rule_name}, rule_value={rule_match_value}, name={feature_name}")
return True
return False
logger.debug(f"rule matched, rule_name={rule_name}, rule_value={rule_match_value}, name={feature_name}")
return True

def _evaluate_rules(
self, *, feature_name: str, context: Dict[str, Any], feat_default: bool, rules: Dict[str, Any]
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/feature_flags/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,44 @@ def test_flags_conditions_no_match(mocker, config):


# check that a rule can match when it has multiple conditions, see rule name for further explanation
def test_flags_conditions_rule_not_match_multiple_conditions_match_only_one_condition(mocker, config):
expected_value = False
tenant_id_val = "6"
username_val = "a"
mocked_app_config_schema = {
"my_feature": {
"default": expected_value,
"rules": {
"tenant id equals 6 and username is a": {
"when_match": True,
"conditions": [
{
"action": RuleAction.EQUALS.value, # this condition matches
"key": "tenant_id",
"value": tenant_id_val,
},
{
"action": RuleAction.EQUALS.value, # this condition does not
"key": "username",
"value": "bbb",
},
],
}
},
}
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
toggle = feature_flags.evaluate(
name="my_feature",
context={
"tenant_id": tenant_id_val,
"username": username_val,
},
default=True,
)
assert toggle == expected_value


def test_flags_conditions_rule_match_equal_multiple_conditions(mocker, config):
expected_value = False
tenant_id_val = "6"
Expand Down