Skip to content

Commit

Permalink
fixup! Add evaluation details to finally hook stage #403
Browse files Browse the repository at this point in the history
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
  • Loading branch information
chrfwow committed Jan 28, 2025
1 parent e803ae6 commit 6f71842
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion openfeature/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def evaluate_flag_details( # noqa: PLR0915
hook_context,
flag_evaluation,
reversed_merged_hooks,
hook_hints
hook_hints,
)

def _create_provider_evaluation(
Expand Down
2 changes: 1 addition & 1 deletion openfeature/hook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def finally_after(
self,
hook_context: HookContext,
details: FlagEvaluationDetails[typing.Any],
hints: HookHints
hints: HookHints,
) -> None:
"""
Run after flag evaluation, including any error processing.
Expand Down
28 changes: 14 additions & 14 deletions tests/features/steps/hooks_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from openfeature.hook import Hook


@when('a hook is added to the client')
@when("a hook is added to the client")
def step_impl_add_hook(context):
hook = MagicMock(spec=Hook)
hook.before = MagicMock()
Expand All @@ -17,54 +17,54 @@ def step_impl_add_hook(context):
context.client.add_hooks([hook])


@then('error hooks should be called')
@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('non-error hooks should be 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


def get_hook_from_name(context, hook_name):
if hook_name.lower() == 'before':
if hook_name.lower() == "before":
return context.hook.before
elif hook_name.lower() == 'after':
elif hook_name.lower() == "after":
return context.hook.after
elif hook_name.lower() == 'error':
elif hook_name.lower() == "error":
return context.hook.error
elif hook_name.lower() == 'finally':
elif hook_name.lower() == "finally":
return context.hook.finally_after
else:
raise ValueError(str(hook_name) + ' is not a valid 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':
if value == "None":
return None
if flag_type.lower() == 'boolean':
if flag_type.lower() == "boolean":
return bool(value)
elif flag_type.lower() == 'integer':
elif flag_type.lower() == "integer":
return int(value)
elif flag_type.lower() == 'float':
elif flag_type.lower() == "float":
return float(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(', '):
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]
actual = hook.call_args[1]["details"].__dict__[key]
if isinstance(actual, ErrorCode):
actual = str(actual)

Expand Down
2 changes: 1 addition & 1 deletion tests/hook/test_hook_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_after_hooks_run_after_method(mock_hook):

def test_finally_after_hooks_run_finally_after_method(mock_hook):
# Given
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, '')
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, "")
flag_evaluation_details = FlagEvaluationDetails(
hook_context.flag_key, "val", "unknown"
)
Expand Down

0 comments on commit 6f71842

Please sign in to comment.