1
1
from unittest .mock import MagicMock
2
2
3
- from behave import then , when
3
+ from behave import given , then
4
4
5
5
from openfeature .exception import ErrorCode
6
+ from openfeature .flag_evaluation import Reason
6
7
from openfeature .hook import Hook
7
8
8
9
9
- @when ("a hook is added to the client " )
10
+ @given ("a client with added hook " )
10
11
def step_impl_add_hook (context ):
11
12
hook = MagicMock (spec = Hook )
12
13
hook .before = MagicMock ()
@@ -17,18 +18,23 @@ def step_impl_add_hook(context):
17
18
context .client .add_hooks ([hook ])
18
19
19
20
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
25
25
26
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
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
32
38
33
39
34
40
def get_hook_from_name (context , hook_name ):
@@ -44,29 +50,17 @@ def get_hook_from_name(context, hook_name):
44
50
raise ValueError (str (hook_name ) + " is not a valid hook name" )
45
51
46
52
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" ) :
49
55
return None
50
56
if flag_type .lower () == "boolean" :
51
57
return bool (value )
52
58
elif flag_type .lower () == "integer" :
53
59
return int (value )
54
60
elif flag_type .lower () == "float" :
55
61
return float (value )
62
+ elif key == "reason" :
63
+ return Reason (value )
64
+ elif key == "error_code" :
65
+ return ErrorCode (value )
56
66
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