@@ -38,6 +38,11 @@ func (c *MockProjectConfig) GetFeatureByKey(featureKey string) (entities.Feature
38
38
return args .Get (0 ).(entities.Feature ), args .Error (1 )
39
39
}
40
40
41
+ func (c * MockProjectConfig ) GetFeatureList () []entities.Feature {
42
+ args := c .Called ()
43
+ return args .Get (0 ).([]entities.Feature )
44
+ }
45
+
41
46
type MockProjectConfigManager struct {
42
47
mock.Mock
43
48
}
@@ -169,3 +174,126 @@ func TestIsFeatureEnabledPanic(t *testing.T) {
169
174
assert .False (t , result )
170
175
assert .True (t , assert .Error (t , err ))
171
176
}
177
+
178
+ func TestGetEnabledFeatures (t * testing.T ) {
179
+ testUserContext := entities.UserContext {ID : "test_user_1" }
180
+ testVariationEnabled := entities.Variation {
181
+ ID : "22222" ,
182
+ Key : "22222" ,
183
+ FeatureEnabled : true ,
184
+ }
185
+ testVariationDisabled := entities.Variation {
186
+ ID : "22222" ,
187
+ Key : "22222" ,
188
+ FeatureEnabled : false ,
189
+ }
190
+ testExperimentEnabled := entities.Experiment {
191
+ ID : "111111" ,
192
+ Variations : map [string ]entities.Variation {"22222" : testVariationEnabled },
193
+ }
194
+ testExperimentDisabled := entities.Experiment {
195
+ ID : "111111" ,
196
+ Variations : map [string ]entities.Variation {"22222" : testVariationDisabled },
197
+ }
198
+ testFeatureEnabledKey := "test_feature_enabled_key"
199
+ testFeatureEnabled := entities.Feature {
200
+ ID : "22222" ,
201
+ Key : testFeatureEnabledKey ,
202
+ FeatureExperiments : []entities.Experiment {testExperimentEnabled },
203
+ }
204
+ testFeatureDisabledKey := "test_feature_disabled_key"
205
+ testFeatureDisabled := entities.Feature {
206
+ ID : "22222" ,
207
+ Key : testFeatureDisabledKey ,
208
+ FeatureExperiments : []entities.Experiment {testExperimentDisabled },
209
+ }
210
+ featureList := []entities.Feature {testFeatureEnabled , testFeatureDisabled }
211
+ // Test happy path
212
+ mockConfig := new (MockProjectConfig )
213
+ mockConfig .On ("GetFeatureByKey" , testFeatureEnabledKey ).Return (testFeatureEnabled , nil )
214
+ mockConfig .On ("GetFeatureByKey" , testFeatureDisabledKey ).Return (testFeatureDisabled , nil )
215
+ mockConfig .On ("GetFeatureList" ).Return (featureList )
216
+ mockConfigManager := new (MockProjectConfigManager )
217
+ mockConfigManager .On ("GetConfig" ).Return (mockConfig )
218
+ // Set up the mock decision service and its return value
219
+ testDecisionContextEnabled := decision.FeatureDecisionContext {
220
+ Feature : & testFeatureEnabled ,
221
+ ProjectConfig : mockConfig ,
222
+ }
223
+ testDecisionContextDisabled := decision.FeatureDecisionContext {
224
+ Feature : & testFeatureDisabled ,
225
+ ProjectConfig : mockConfig ,
226
+ }
227
+
228
+ expectedFeatureDecisionEnabled := decision.FeatureDecision {
229
+ Experiment : testExperimentEnabled ,
230
+ Variation : testVariationEnabled ,
231
+ Decision : decision.Decision {
232
+ DecisionMade : true ,
233
+ },
234
+ }
235
+ expectedFeatureDecisionDisabled := decision.FeatureDecision {
236
+ Experiment : testExperimentDisabled ,
237
+ Variation : testVariationDisabled ,
238
+ Decision : decision.Decision {
239
+ DecisionMade : true ,
240
+ },
241
+ }
242
+
243
+ mockDecisionService := new (MockDecisionService )
244
+ mockDecisionService .On ("GetFeatureDecision" , testDecisionContextEnabled , testUserContext ).Return (expectedFeatureDecisionEnabled , nil )
245
+ mockDecisionService .On ("GetFeatureDecision" , testDecisionContextDisabled , testUserContext ).Return (expectedFeatureDecisionDisabled , nil )
246
+
247
+ client := OptimizelyClient {
248
+ configManager : mockConfigManager ,
249
+ decisionService : mockDecisionService ,
250
+ isValid : true ,
251
+ }
252
+ result , err := client .GetEnabledFeatures (testUserContext )
253
+ assert .NoError (t , err )
254
+ assert .ElementsMatch (t , result , []string {testFeatureEnabledKey })
255
+ mockConfig .AssertExpectations (t )
256
+ mockConfigManager .AssertExpectations (t )
257
+ mockDecisionService .AssertExpectations (t )
258
+ }
259
+
260
+ func TestGetEnabledFeaturesErrorCases (t * testing.T ) {
261
+ testUserContext := entities.UserContext {ID : "test_user_1" }
262
+
263
+ // Test instance invalid
264
+ mockConfigManager := new (MockProjectConfigManager )
265
+ mockDecisionService := new (MockDecisionService )
266
+
267
+ client := OptimizelyClient {
268
+ configManager : mockConfigManager ,
269
+ decisionService : mockDecisionService ,
270
+ isValid : false ,
271
+ }
272
+ result , err := client .GetEnabledFeatures (testUserContext )
273
+ assert .Error (t , err )
274
+ assert .Empty (t , result )
275
+ mockConfigManager .AssertNotCalled (t , "GetFeatureByKey" )
276
+ mockDecisionService .AssertNotCalled (t , "GetFeatureDecision" )
277
+ }
278
+
279
+ func TestGetEnabledFeaturesPanic (t * testing.T ) {
280
+ testUserContext := entities.UserContext {ID : "test_user_1" }
281
+ testFeatureKey := "test_feature_key"
282
+
283
+ mockConfigManager := new (MockProjectConfigManager )
284
+ mockDecisionService := new (MockDecisionService )
285
+
286
+ client := OptimizelyClient {
287
+ configManager : mockConfigManager ,
288
+ decisionService : mockDecisionService ,
289
+ isValid : true ,
290
+ }
291
+
292
+ // returning an error object will cause the Client to panic
293
+ mockConfigManager .On ("GetFeatureByKey" , testFeatureKey , testUserContext ).Return (errors .New ("failure" ))
294
+
295
+ // ensure that the client calms back down and recovers
296
+ result , err := client .GetEnabledFeatures (testUserContext )
297
+ assert .Empty (t , result )
298
+ assert .True (t , assert .Error (t , err ))
299
+ }
0 commit comments