11from unittest .mock import MagicMock
22
3- from behave import then , when
3+ from behave import given , then
44
55from openfeature .exception import ErrorCode
6+ from openfeature .flag_evaluation import Reason
67from openfeature .hook import Hook
78
89
9- @when ("a hook is added to the client " )
10+ @given ("a client with added hook " )
1011def 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
3440def 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