forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_complex_rule_values.py
155 lines (136 loc) · 5.33 KB
/
test_complex_rule_values.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from typing import Dict, List, Optional
import pytest
from botocore.config import Config
from aws_lambda_powertools.utilities.feature_flags.appconfig import AppConfigStore
from aws_lambda_powertools.utilities.feature_flags.feature_flags import FeatureFlags
from aws_lambda_powertools.utilities.feature_flags.schema import (
CONDITION_ACTION,
CONDITION_KEY,
CONDITION_VALUE,
CONDITIONS_KEY,
FEATURE_DEFAULT_VAL_KEY,
FEATURE_DEFAULT_VAL_TYPE_KEY,
RULE_MATCH_VALUE,
RULES_KEY,
RuleAction,
)
@pytest.fixture(scope="module")
def config():
return Config(region_name="us-east-1")
def init_feature_flags(
mocker, mock_schema: Dict, config: Config, envelope: str = "", jmespath_options: Optional[Dict] = None
) -> FeatureFlags:
mocked_get_conf = mocker.patch("aws_lambda_powertools.utilities.parameters.AppConfigProvider.get")
mocked_get_conf.return_value = mock_schema
app_conf_fetcher = AppConfigStore(
environment="test_env",
application="test_app",
name="test_conf_name",
max_age=600,
sdk_config=config,
envelope=envelope,
jmespath_options=jmespath_options,
)
feature_flags: FeatureFlags = FeatureFlags(store=app_conf_fetcher)
return feature_flags
# default return value is an empty list, when rule matches return a non empty list
def test_feature_rule_match(mocker, config):
expected_value = ["value1"]
mocked_app_config_schema = {
"my_feature": {
FEATURE_DEFAULT_VAL_KEY: [],
FEATURE_DEFAULT_VAL_TYPE_KEY: False,
RULES_KEY: {
"tenant id equals 345345435": {
RULE_MATCH_VALUE: expected_value,
CONDITIONS_KEY: [
{
CONDITION_ACTION: RuleAction.EQUALS.value,
CONDITION_KEY: "tenant_id",
CONDITION_VALUE: "345345435",
}
],
}
},
}
}
features = init_feature_flags(mocker, mocked_app_config_schema, config)
feature_value = features.evaluate(name="my_feature", context={"tenant_id": "345345435"}, default=[])
assert feature_value == expected_value
def test_complex_feature_no_rules(mocker, config):
expected_value = ["value1"]
mocked_app_config_schema = {
"my_feature": {FEATURE_DEFAULT_VAL_KEY: expected_value, FEATURE_DEFAULT_VAL_TYPE_KEY: False}
}
features = init_feature_flags(mocker, mocked_app_config_schema, config)
feature_value = features.evaluate(name="my_feature", context={"tenant_id": "345345435"}, default=[])
assert feature_value == expected_value
def test_feature_no_rule_match(mocker, config):
expected_value = []
mocked_app_config_schema = {
"my_feature": {
FEATURE_DEFAULT_VAL_KEY: expected_value,
FEATURE_DEFAULT_VAL_TYPE_KEY: False,
RULES_KEY: {
"tenant id equals 345345435": {
RULE_MATCH_VALUE: ["value1"],
CONDITIONS_KEY: [
{
CONDITION_ACTION: RuleAction.EQUALS.value,
CONDITION_KEY: "tenant_id",
CONDITION_VALUE: "345345435",
}
],
}
},
}
}
features = init_feature_flags(mocker, mocked_app_config_schema, config)
feature_value = features.evaluate(name="my_feature", context={}, default=[])
assert feature_value == expected_value
# Check multiple features
def test_multiple_features_enabled_with_complex_toggles_and_boolean_toggles(mocker, config):
expected_value = ["my_feature", "my_feature2"]
mocked_app_config_schema = {
"my_feature": {
FEATURE_DEFAULT_VAL_KEY: False,
RULES_KEY: {
"tenant id is contained in [6, 2]": {
RULE_MATCH_VALUE: True,
CONDITIONS_KEY: [
{
CONDITION_ACTION: RuleAction.IN.value,
CONDITION_KEY: "tenant_id",
CONDITION_VALUE: ["6", "2"],
}
],
}
},
},
"my_complex_feature": {
FEATURE_DEFAULT_VAL_KEY: {},
FEATURE_DEFAULT_VAL_TYPE_KEY: False,
RULES_KEY: {
"tenant id equals 345345435": {
RULE_MATCH_VALUE: {"b": 4},
CONDITIONS_KEY: [
{
CONDITION_ACTION: RuleAction.EQUALS.value,
CONDITION_KEY: "tenant_id",
CONDITION_VALUE: "345345435",
}
],
},
},
},
"my_feature2": {
FEATURE_DEFAULT_VAL_KEY: True,
},
"my_feature3": {
FEATURE_DEFAULT_VAL_KEY: False,
},
"my_feature4": {FEATURE_DEFAULT_VAL_KEY: {"a": "b"}, FEATURE_DEFAULT_VAL_TYPE_KEY: False},
}
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
enabled_list: List[str] = feature_flags.get_enabled_features(context={"tenant_id": "6", "username": "a"})
assert enabled_list == expected_value