Skip to content

Commit 387da38

Browse files
committed
refactor(tests): conf_store to feature flags
1 parent 4aba507 commit 387da38

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

tests/functional/feature_toggles/test_feature_toggles.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def config():
1515
return Config(region_name="us-east-1")
1616

1717

18-
def init_configuration_store(mocker, mock_schema: Dict, config: Config) -> FeatureFlags:
18+
def init_feature_flags(mocker, mock_schema: Dict, config: Config) -> FeatureFlags:
1919
mocked_get_conf = mocker.patch("aws_lambda_powertools.utilities.parameters.AppConfigProvider.get")
2020
mocked_get_conf.return_value = mock_schema
2121

@@ -26,8 +26,8 @@ def init_configuration_store(mocker, mock_schema: Dict, config: Config) -> Featu
2626
cache_seconds=600,
2727
config=config,
2828
)
29-
conf_store: FeatureFlags = FeatureFlags(store=app_conf_fetcher)
30-
return conf_store
29+
feature_flags: FeatureFlags = FeatureFlags(store=app_conf_fetcher)
30+
return feature_flags
3131

3232

3333
def init_fetcher_side_effect(mocker, config: Config, side_effect) -> AppConfigStore:
@@ -66,8 +66,8 @@ def test_toggles_rule_does_not_match(mocker, config):
6666
}
6767
}
6868

69-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
70-
toggle = conf_store.evaluate(name="my_feature", context={}, default=False)
69+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
70+
toggle = feature_flags.evaluate(name="my_feature", context={}, default=False)
7171
assert toggle == expected_value
7272

7373

@@ -77,8 +77,8 @@ def test_toggles_no_conditions_feature_does_not_exist(mocker, config):
7777
expected_value = False
7878
mocked_app_config_schema = {"features": {"my_fake_feature": {"default": True}}}
7979

80-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
81-
toggle = conf_store.evaluate(name="my_feature", context={}, default=expected_value)
80+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
81+
toggle = feature_flags.evaluate(name="my_feature", context={}, default=expected_value)
8282
assert toggle == expected_value
8383

8484

@@ -87,8 +87,8 @@ def test_toggles_no_conditions_feature_does_not_exist(mocker, config):
8787
def test_toggles_no_rules(mocker, config):
8888
expected_value = True
8989
mocked_app_config_schema = {"features": {"my_feature": {"default": expected_value}}}
90-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
91-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
90+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
91+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
9292
assert toggle == expected_value
9393

9494

@@ -114,8 +114,8 @@ def test_toggles_conditions_no_match(mocker, config):
114114
}
115115
}
116116
}
117-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
118-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
117+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
118+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
119119
assert toggle == expected_value
120120

121121

@@ -148,8 +148,8 @@ def test_toggles_conditions_rule_match_equal_multiple_conditions(mocker, config)
148148
}
149149
},
150150
}
151-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
152-
toggle = conf_store.evaluate(
151+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
152+
toggle = feature_flags.evaluate(
153153
name="my_feature",
154154
context={
155155
"tenant_id": tenant_id_val,
@@ -190,8 +190,8 @@ def test_toggles_conditions_no_rule_match_equal_multiple_conditions(mocker, conf
190190
}
191191
}
192192
}
193-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
194-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
193+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
194+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
195195
assert toggle == expected_val
196196

197197

@@ -246,18 +246,20 @@ def test_toggles_conditions_rule_match_multiple_actions_multiple_rules_multiple_
246246
}
247247
}
248248

249-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
249+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
250250
# match first rule
251-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "6", "username": "abcd"}, default=False)
251+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "abcd"}, default=False)
252252
assert toggle == expected_value_first_check
253253
# match second rule
254-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "4446", "username": "az"}, default=False)
254+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "4446", "username": "az"}, default=False)
255255
assert toggle == expected_value_second_check
256256
# match no rule
257-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "11114446", "username": "ab"}, default=False)
257+
toggle = feature_flags.evaluate(
258+
name="my_feature", context={"tenant_id": "11114446", "username": "ab"}, default=False
259+
)
258260
assert toggle == expected_value_third_check
259261
# feature doesn't exist
260-
toggle = conf_store.evaluate(
262+
toggle = feature_flags.evaluate(
261263
name="my_fake_feature",
262264
context={"tenant_id": "11114446", "username": "ab"},
263265
default=expected_value_fourth_case,
@@ -287,8 +289,8 @@ def test_toggles_match_rule_with_contains_action(mocker, config):
287289
}
288290
}
289291
}
290-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
291-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
292+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
293+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
292294
assert toggle == expected_value
293295

294296

@@ -314,8 +316,8 @@ def test_toggles_no_match_rule_with_contains_action(mocker, config):
314316
}
315317
},
316318
}
317-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
318-
toggle = conf_store.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
319+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
320+
toggle = feature_flags.evaluate(name="my_feature", context={"tenant_id": "6", "username": "a"}, default=False)
319321
assert toggle == expected_value
320322

321323

@@ -346,8 +348,8 @@ def test_multiple_features_enabled(mocker, config):
346348
},
347349
}
348350
}
349-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
350-
enabled_list: List[str] = conf_store.get_enabled_features(context={"tenant_id": "6", "username": "a"})
351+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
352+
enabled_list: List[str] = feature_flags.get_enabled_features(context={"tenant_id": "6", "username": "a"})
351353
assert enabled_list == expected_value
352354

353355

@@ -394,18 +396,18 @@ def test_multiple_features_only_some_enabled(mocker, config):
394396
},
395397
}
396398
}
397-
conf_store = init_configuration_store(mocker, mocked_app_config_schema, config)
398-
enabled_list: List[str] = conf_store.get_enabled_features(context={"tenant_id": "6", "username": "a"})
399+
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
400+
enabled_list: List[str] = feature_flags.get_enabled_features(context={"tenant_id": "6", "username": "a"})
399401
assert enabled_list == expected_value
400402

401403

402404
def test_get_feature_toggle_handles_error(mocker, config):
403405
# GIVEN a schema fetch that raises a ConfigurationError
404406
schema_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError())
405-
conf_store = FeatureFlags(schema_fetcher)
407+
feature_flags = FeatureFlags(schema_fetcher)
406408

407409
# WHEN calling evaluate
408-
toggle = conf_store.evaluate(name="Foo", default=False)
410+
toggle = feature_flags.evaluate(name="Foo", default=False)
409411

410412
# THEN handle the error and return the default
411413
assert toggle is False
@@ -414,10 +416,10 @@ def test_get_feature_toggle_handles_error(mocker, config):
414416
def test_get_all_enabled_feature_toggles_handles_error(mocker, config):
415417
# GIVEN a schema fetch that raises a ConfigurationError
416418
schema_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError())
417-
conf_store = FeatureFlags(schema_fetcher)
419+
feature_flags = FeatureFlags(schema_fetcher)
418420

419421
# WHEN calling get_enabled_features
420-
toggles = conf_store.get_enabled_features(context=None)
422+
toggles = feature_flags.get_enabled_features(context=None)
421423

422424
# THEN handle the error and return an empty list
423425
assert toggles == []
@@ -437,18 +439,18 @@ def test_app_config_get_parameter_err(mocker, config):
437439

438440
def test_match_by_action_no_matching_action(mocker, config):
439441
# GIVEN an unsupported action
440-
conf_store = init_configuration_store(mocker, {}, config)
442+
feature_flags = init_feature_flags(mocker, {}, config)
441443
# WHEN calling _match_by_action
442-
result = conf_store._match_by_action("Foo", None, "foo")
444+
result = feature_flags._match_by_action("Foo", None, "foo")
443445
# THEN default to False
444446
assert result is False
445447

446448

447449
def test_match_by_action_attribute_error(mocker, config):
448450
# GIVEN a startswith action and 2 integer
449-
conf_store = init_configuration_store(mocker, {}, config)
451+
feature_flags = init_feature_flags(mocker, {}, config)
450452
# WHEN calling _match_by_action
451-
result = conf_store._match_by_action(RuleAction.STARTSWITH.value, 1, 100)
453+
result = feature_flags._match_by_action(RuleAction.STARTSWITH.value, 1, 100)
452454
# THEN swallow the AttributeError and return False
453455
assert result is False
454456

@@ -457,10 +459,10 @@ def test_is_rule_matched_no_matches(mocker, config):
457459
# GIVEN an empty list of conditions
458460
rule = {schema.CONDITIONS_KEY: []}
459461
rules_context = {}
460-
conf_store = init_configuration_store(mocker, {}, config)
462+
feature_flags = init_feature_flags(mocker, {}, config)
461463

462464
# WHEN calling _is_rule_matched
463-
result = conf_store._is_rule_matched(rule_name="dummy", feature_name="dummy", rule=rule, context=rules_context)
465+
result = feature_flags._is_rule_matched(rule_name="dummy", feature_name="dummy", rule=rule, context=rules_context)
464466

465467
# THEN return False
466468
assert result is False

0 commit comments

Comments
 (0)