Skip to content

Commit d4f53b4

Browse files
chrfwowgruebel
andauthored
test: Implement gherkin hooks tests (#442)
* Implement gherkin hooks tests Signed-off-by: christian.lutnik <[email protected]> * fixup! Implement gherkin hooks tests Signed-off-by: christian.lutnik <[email protected]> * fix tests and lint Signed-off-by: gruebel <[email protected]> * lint Signed-off-by: gruebel <[email protected]> --------- Signed-off-by: christian.lutnik <[email protected]> Signed-off-by: gruebel <[email protected]> Co-authored-by: gruebel <[email protected]>
1 parent 37296dc commit d4f53b4

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

tests/features/steps/flag_steps.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import contextlib
2+
13
from behave import given, when
24

35

46
@given('a {flag_type}-flag with key "{flag_key}" and a default value "{default_value}"')
57
def step_impl_flag(context, flag_type: str, flag_key, default_value):
8+
if default_value.lower() == "true" or default_value.lower() == "false":
9+
default_value = bool(default_value)
10+
try:
11+
default_value = int(default_value)
12+
except ValueError:
13+
with contextlib.suppress(ValueError):
14+
default_value = float(default_value)
615
context.flag = (flag_type, flag_key, default_value)
716

817

tests/features/steps/hooks_steps.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from unittest.mock import MagicMock
22

3-
from behave import then, when
3+
from behave import given, then
44

55
from openfeature.exception import ErrorCode
6+
from openfeature.flag_evaluation import Reason
67
from openfeature.hook import Hook
78

89

9-
@when("a hook is added to the client")
10+
@given("a client with added hook")
1011
def step_impl_add_hook(context):
1112
hook = MagicMock(spec=Hook)
1213
hook.before = MagicMock()
@@ -17,18 +18,23 @@ def step_impl_add_hook(context):
1718
context.client.add_hooks([hook])
1819

1920

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
21+
@then('the "{hook_name}" hook should have been executed')
22+
def step_impl_should_called(context, hook_name):
23+
hook = get_hook_from_name(context, hook_name)
24+
assert hook.called
2525

2626

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
27+
@then('the "{hook_names}" hooks should be called with evaluation details')
28+
def step_impl_should_have_eval_details(context, hook_names):
29+
for hook_name in hook_names.split(", "):
30+
hook = get_hook_from_name(context, hook_name)
31+
for row in context.table:
32+
flag_type, key, value = row
33+
34+
value = convert_value_from_key_and_flag_type(value, key, flag_type)
35+
actual = hook.call_args[1]["details"].__dict__[key]
36+
37+
assert actual == value
3238

3339

3440
def get_hook_from_name(context, hook_name):
@@ -44,29 +50,17 @@ def get_hook_from_name(context, hook_name):
4450
raise ValueError(str(hook_name) + " is not a valid hook name")
4551

4652

47-
def convert_value_from_flag_type(value, flag_type):
48-
if value == "None":
53+
def convert_value_from_key_and_flag_type(value, key, flag_type):
54+
if value in ("None", "null"):
4955
return None
5056
if flag_type.lower() == "boolean":
5157
return bool(value)
5258
elif flag_type.lower() == "integer":
5359
return int(value)
5460
elif flag_type.lower() == "float":
5561
return float(value)
62+
elif key == "reason":
63+
return Reason(value)
64+
elif key == "error_code":
65+
return ErrorCode(value)
5666
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

Comments
 (0)