|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +from behave import then, when |
| 4 | + |
| 5 | +from openfeature.exception import ErrorCode |
| 6 | +from openfeature.hook import Hook |
| 7 | + |
| 8 | + |
| 9 | +@when("a hook is added to the client") |
| 10 | +def step_impl_add_hook(context): |
| 11 | + hook = MagicMock(spec=Hook) |
| 12 | + hook.before = MagicMock() |
| 13 | + hook.after = MagicMock() |
| 14 | + hook.error = MagicMock() |
| 15 | + hook.finally_after = MagicMock() |
| 16 | + context.hook = hook |
| 17 | + context.client.add_hooks([hook]) |
| 18 | + |
| 19 | + |
| 20 | +@then("error hooks should be called") |
| 21 | +def step_impl_call_error(context): |
| 22 | + assert context.hook.before.called |
| 23 | + assert context.hook.error.called |
| 24 | + assert context.hook.finally_after.called |
| 25 | + |
| 26 | + |
| 27 | +@then("non-error hooks should be called") |
| 28 | +def step_impl_call_non_error(context): |
| 29 | + assert context.hook.before.called |
| 30 | + assert context.hook.after.called |
| 31 | + assert context.hook.finally_after.called |
| 32 | + |
| 33 | + |
| 34 | +def get_hook_from_name(context, hook_name): |
| 35 | + if hook_name.lower() == "before": |
| 36 | + return context.hook.before |
| 37 | + elif hook_name.lower() == "after": |
| 38 | + return context.hook.after |
| 39 | + elif hook_name.lower() == "error": |
| 40 | + return context.hook.error |
| 41 | + elif hook_name.lower() == "finally": |
| 42 | + return context.hook.finally_after |
| 43 | + else: |
| 44 | + raise ValueError(str(hook_name) + " is not a valid hook name") |
| 45 | + |
| 46 | + |
| 47 | +def convert_value_from_flag_type(value, flag_type): |
| 48 | + if value == "None": |
| 49 | + return None |
| 50 | + if flag_type.lower() == "boolean": |
| 51 | + return bool(value) |
| 52 | + elif flag_type.lower() == "integer": |
| 53 | + return int(value) |
| 54 | + elif flag_type.lower() == "float": |
| 55 | + return float(value) |
| 56 | + return value |
| 57 | + |
| 58 | + |
| 59 | +@then('"{hook_names}" hooks should have evaluation details') |
| 60 | +def step_impl_should_have_eval_details(context, hook_names): |
| 61 | + for hook_name in hook_names.split(", "): |
| 62 | + hook = get_hook_from_name(context, hook_name) |
| 63 | + for row in context.table: |
| 64 | + flag_type, key, value = row |
| 65 | + |
| 66 | + value = convert_value_from_flag_type(value, flag_type) |
| 67 | + |
| 68 | + actual = hook.call_args[1]["details"].__dict__[key] |
| 69 | + if isinstance(actual, ErrorCode): |
| 70 | + actual = str(actual) |
| 71 | + |
| 72 | + assert actual == value |
0 commit comments