4
4
from unittest .mock import patch
5
5
import httpretty # type: ignore
6
6
import pytest
7
+ from eppo_client .assignment_logger import AssignmentLogger
7
8
from eppo_client .client import EppoClient
8
9
from eppo_client .config import Config
9
10
from eppo_client .configuration_requestor import (
@@ -43,7 +44,13 @@ def init_fixture():
43
44
MOCK_BASE_URL + "/randomized_assignment/config" ,
44
45
body = config_response_json ,
45
46
)
46
- client = init (Config (base_url = MOCK_BASE_URL , api_key = "dummy" ))
47
+ client = init (
48
+ Config (
49
+ base_url = MOCK_BASE_URL ,
50
+ api_key = "dummy" ,
51
+ assignment_logger = AssignmentLogger (),
52
+ )
53
+ )
47
54
sleep (0.1 ) # wait for initialization
48
55
yield
49
56
client ._shutdown ()
@@ -52,15 +59,19 @@ def init_fixture():
52
59
53
60
@patch ("eppo_client.configuration_requestor.ExperimentConfigurationRequestor" )
54
61
def test_assign_blank_experiment (mock_config_requestor ):
55
- client = EppoClient (config_requestor = mock_config_requestor )
62
+ client = EppoClient (
63
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
64
+ )
56
65
with pytest .raises (Exception ) as exc_info :
57
66
client .get_assignment ("subject-1" , "" )
58
67
assert exc_info .value .args [0 ] == "Invalid value for experiment_key: cannot be blank"
59
68
60
69
61
70
@patch ("eppo_client.configuration_requestor.ExperimentConfigurationRequestor" )
62
71
def test_assign_blank_subject (mock_config_requestor ):
63
- client = EppoClient (config_requestor = mock_config_requestor )
72
+ client = EppoClient (
73
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
74
+ )
64
75
with pytest .raises (Exception ) as exc_info :
65
76
client .get_assignment ("" , "experiment-1" )
66
77
assert exc_info .value .args [0 ] == "Invalid value for subject_key: cannot be blank"
@@ -78,7 +89,9 @@ def test_assign_subject_not_in_sample(mock_config_requestor):
78
89
name = "recommendation_algo" ,
79
90
overrides = dict (),
80
91
)
81
- client = EppoClient (config_requestor = mock_config_requestor )
92
+ client = EppoClient (
93
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
94
+ )
82
95
assert client .get_assignment ("user-1" , "experiment-key-1" ) is None
83
96
84
97
@@ -139,7 +152,9 @@ def test_assign_subject_with_with_attributes_and_rules(mock_config_requestor):
139
152
overrides = dict (),
140
153
rules = [text_rule ],
141
154
)
142
- client = EppoClient (config_requestor = mock_config_requestor )
155
+ client = EppoClient (
156
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
157
+ )
143
158
assert client .get_assignment ("user-1" , "experiment-key-1" ) is None
144
159
assert (
145
160
client .get_assignment (
@@ -165,10 +180,39 @@ def test_with_subject_in_overrides(mock_config_requestor):
165
180
name = "recommendation_algo" ,
166
181
overrides = {"d6d7705392bc7af633328bea8c4c6904" : "override-variation" },
167
182
)
168
- client = EppoClient (config_requestor = mock_config_requestor )
183
+ client = EppoClient (
184
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
185
+ )
169
186
assert client .get_assignment ("user-1" , "experiment-key-1" ) == "override-variation"
170
187
171
188
189
+ @patch ("eppo_client.configuration_requestor.ExperimentConfigurationRequestor" )
190
+ def test_with_subject_in_overrides_exp_disabled (mock_config_requestor ):
191
+ mock_config_requestor .get_configuration .return_value = ExperimentConfigurationDto (
192
+ subjectShards = 10000 ,
193
+ percentExposure = 0 ,
194
+ enabled = False ,
195
+ variations = [
196
+ VariationDto (name = "control" , shardRange = ShardRange (start = 0 , end = 100 ))
197
+ ],
198
+ name = "recommendation_algo" ,
199
+ overrides = {"d6d7705392bc7af633328bea8c4c6904" : "override-variation" },
200
+ )
201
+ client = EppoClient (
202
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
203
+ )
204
+ assert client .get_assignment ("user-1" , "experiment-key-1" ) == "override-variation"
205
+
206
+
207
+ @patch ("eppo_client.configuration_requestor.ExperimentConfigurationRequestor" )
208
+ def test_with_null_experiment_config (mock_config_requestor ):
209
+ mock_config_requestor .get_configuration .return_value = None
210
+ client = EppoClient (
211
+ config_requestor = mock_config_requestor , assignment_logger = AssignmentLogger ()
212
+ )
213
+ assert client .get_assignment ("user-1" , "experiment-key-1" ) is None
214
+
215
+
172
216
@pytest .mark .parametrize ("test_case" , test_data )
173
217
def test_assign_subject_in_sample (test_case ):
174
218
print ("---- Test case for {} Experiment" .format (test_case ["experiment" ]))
0 commit comments