@@ -26,6 +26,7 @@ class DecisionListenerTests_Holdouts: XCTestCase {
2626 var kAttributesCountryNotMatch : [ String : Any ] = [ " country " : " ca " ]
2727
2828 let kFeatureKey = " feature_1 "
29+ let kFeatureId = " 4482920077 "
2930
3031 let kVariableKeyString = " s_foo "
3132 let kVariableKeyInt = " i_42 "
@@ -115,6 +116,78 @@ class DecisionListenerTests_Holdouts: XCTestCase {
115116 wait ( for: [ exp] , timeout: 1 )
116117 }
117118
119+ func testDecisionListenerDecideWithIncludedFlags( ) {
120+ var holdout = try ! OTUtils . model ( from: sampleHoldout) as Holdout
121+ holdout. includedFlags = [ kFeatureId]
122+ optimizely. config!. project. holdouts = [ holdout]
123+
124+ let exp = expectation ( description: " x " )
125+ let user = optimizely. createUserContext ( userId: kUserId, attributes: kAttributesCountryMatch)
126+
127+ notificationCenter. clearAllNotificationListeners ( )
128+ _ = notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
129+ XCTAssertEqual ( type, Constants . DecisionType. flag. rawValue)
130+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. flagKey] as! String , self . kFeatureKey)
131+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. enabled] as! Bool , false )
132+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. variationKey] as! String , " key_holdout_variation " )
133+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. ruleKey] as! String , " key_holdout " )
134+ exp. fulfill ( )
135+ }
136+
137+ _ = user. decide ( key: kFeatureKey)
138+ wait ( for: [ exp] , timeout: 1 )
139+ }
140+
141+ func testDecisionListenerDecideWithExcludedFlags( ) {
142+ var holdout = try ! OTUtils . model ( from: sampleHoldout) as Holdout
143+ holdout. excludedFlags = [ kFeatureId]
144+ optimizely. config!. project. holdouts = [ holdout]
145+
146+ let exp = expectation ( description: " x " )
147+ let user = optimizely. createUserContext ( userId: kUserId, attributes: kAttributesCountryMatch)
148+
149+ notificationCenter. clearAllNotificationListeners ( )
150+ _ = notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
151+ XCTAssertEqual ( type, Constants . DecisionType. flag. rawValue)
152+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. flagKey] as! String , self . kFeatureKey)
153+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. enabled] as! Bool , true )
154+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. variationKey] as! String , " 3324490633 " )
155+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. ruleKey] as! String , " 3332020515 " )
156+ exp. fulfill ( )
157+ }
158+
159+ _ = user. decide ( key: kFeatureKey)
160+ wait ( for: [ exp] , timeout: 1 )
161+ }
162+
163+ func testDecisionListenerDecideWithMultipleHoldouts( ) {
164+ var holdout = try ! OTUtils . model ( from: sampleHoldout) as Holdout
165+ holdout. excludedFlags = [ kFeatureId]
166+
167+ var holdout_2 = holdout
168+ holdout_2. key = " holdout_key_2 "
169+ holdout_2. id = " holdout_id_2 "
170+ holdout_2. includedFlags = [ kFeatureId]
171+
172+ optimizely. config!. project. holdouts = [ holdout, holdout_2]
173+
174+ let exp = expectation ( description: " x " )
175+ let user = optimizely. createUserContext ( userId: kUserId, attributes: kAttributesCountryMatch)
176+
177+ notificationCenter. clearAllNotificationListeners ( )
178+ _ = notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
179+ XCTAssertEqual ( type, Constants . DecisionType. flag. rawValue)
180+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. flagKey] as! String , self . kFeatureKey)
181+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. enabled] as! Bool , false )
182+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. variationKey] as! String , " key_holdout_variation " )
183+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. ruleKey] as! String , " holdout_key_2 " )
184+ exp. fulfill ( )
185+ }
186+
187+ _ = user. decide ( key: kFeatureKey)
188+ wait ( for: [ exp] , timeout: 1 )
189+ }
190+
118191 func testDecisionListener_DecisionEventDispatched_withSendFlagDecisions( ) {
119192 let user = optimizely. createUserContext ( userId: kUserId, attributes: kAttributesCountryMatch)
120193
@@ -173,6 +246,27 @@ class DecisionListenerTests_Holdouts: XCTestCase {
173246 wait ( for: [ exp] , timeout: 1 )
174247 }
175248
249+ func testDecisionListenerDecideForKeys( ) {
250+ let user = optimizely. createUserContext ( userId: kUserId, attributes: [ " country " : " US " ] )
251+
252+ var count = 0
253+ notificationCenter. clearAllNotificationListeners ( )
254+ _ = notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
255+ XCTAssertEqual ( type, Constants . DecisionType. flag. rawValue)
256+ XCTAssertEqual ( userId, user. userId)
257+ XCTAssertEqual ( attributes![ " country " ] as! String , " US " )
258+
259+ XCTAssertNotNil ( decisionInfo [ Constants . DecisionInfoKeys. flagKey] )
260+ XCTAssertNotNil ( decisionInfo [ Constants . DecisionInfoKeys. enabled] )
261+ count += 1
262+ }
263+
264+ _ = user. decide ( keys: [ kFeatureKey, kFeatureKey, kFeatureKey, kFeatureKey] )
265+ sleep ( 1 )
266+
267+ XCTAssertEqual ( count, 4 )
268+ }
269+
176270 func testDecisionListenerDecideAll( ) {
177271 let user = optimizely. createUserContext ( userId: kUserId, attributes: [ " country " : " US " ] )
178272
@@ -193,10 +287,4 @@ class DecisionListenerTests_Holdouts: XCTestCase {
193287
194288 XCTAssertEqual ( count, 3 )
195289 }
196-
197- // func testFlagWithHoldout() {
198- // // global
199- // // include
200- // // exclude
201- // }
202290}
0 commit comments