Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/features/steps/flag_steps.py
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
54 changes: 24 additions & 30 deletions tests/features/steps/hooks_steps.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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):
Expand All @@ -44,29 +50,17 @@ 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)
elif flag_type.lower() == "integer":
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