diff --git a/tests/features/steps/flag_steps.py b/tests/features/steps/flag_steps.py index 574d6791..7f6cd153 100644 --- a/tests/features/steps/flag_steps.py +++ b/tests/features/steps/flag_steps.py @@ -1,8 +1,17 @@ +import contextlib + from behave import given, when @given('a {flag_type}-flag with key "{flag_key}" and a default value "{default_value}"') def step_impl_flag(context, flag_type: str, flag_key, default_value): + if default_value.lower() == "true" or default_value.lower() == "false": + default_value = bool(default_value) + try: + default_value = int(default_value) + except ValueError: + with contextlib.suppress(ValueError): + default_value = float(default_value) context.flag = (flag_type, flag_key, default_value) diff --git a/tests/features/steps/hooks_steps.py b/tests/features/steps/hooks_steps.py index bc7e156b..d93ac643 100644 --- a/tests/features/steps/hooks_steps.py +++ b/tests/features/steps/hooks_steps.py @@ -1,12 +1,13 @@ from unittest.mock import MagicMock -from behave import then, when +from behave import given, then from openfeature.exception import ErrorCode +from openfeature.flag_evaluation import Reason from openfeature.hook import Hook -@when("a hook is added to the client") +@given("a client with added hook") def step_impl_add_hook(context): hook = MagicMock(spec=Hook) hook.before = MagicMock() @@ -17,18 +18,23 @@ def step_impl_add_hook(context): context.client.add_hooks([hook]) -@then("error hooks should be called") -def step_impl_call_error(context): - assert context.hook.before.called - assert context.hook.error.called - assert context.hook.finally_after.called +@then('the "{hook_name}" hook should have been executed') +def step_impl_should_called(context, hook_name): + hook = get_hook_from_name(context, hook_name) + assert hook.called -@then("non-error hooks should be called") -def step_impl_call_non_error(context): - assert context.hook.before.called - assert context.hook.after.called - assert context.hook.finally_after.called +@then('the "{hook_names}" hooks should be called with evaluation details') +def step_impl_should_have_eval_details(context, hook_names): + for hook_name in hook_names.split(", "): + hook = get_hook_from_name(context, hook_name) + for row in context.table: + flag_type, key, value = row + + value = convert_value_from_key_and_flag_type(value, key, flag_type) + actual = hook.call_args[1]["details"].__dict__[key] + + assert actual == value def get_hook_from_name(context, hook_name): @@ -44,8 +50,8 @@ def get_hook_from_name(context, hook_name): raise ValueError(str(hook_name) + " is not a valid hook name") -def convert_value_from_flag_type(value, flag_type): - if value == "None": +def convert_value_from_key_and_flag_type(value, key, flag_type): + if value in ("None", "null"): return None if flag_type.lower() == "boolean": return bool(value) @@ -53,20 +59,8 @@ def convert_value_from_flag_type(value, flag_type): return int(value) elif flag_type.lower() == "float": return float(value) + elif key == "reason": + return Reason(value) + elif key == "error_code": + return ErrorCode(value) return value - - -@then('"{hook_names}" hooks should have evaluation details') -def step_impl_should_have_eval_details(context, hook_names): - for hook_name in hook_names.split(", "): - hook = get_hook_from_name(context, hook_name) - for row in context.table: - flag_type, key, value = row - - value = convert_value_from_flag_type(value, flag_type) - - actual = hook.call_args[1]["details"].__dict__[key] - if isinstance(actual, ErrorCode): - actual = str(actual) - - assert actual == value