Skip to content

Commit 93090fc

Browse files
wip: add create impression event tests
1 parent 015355c commit 93090fc

File tree

3 files changed

+148
-7
lines changed

3 files changed

+148
-7
lines changed

Tests/OptimizelyTests-Common/BatchEventBuilderTests_Events.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ class BatchEventBuilderTests_Events: XCTestCase {
2727
var project: Project!
2828
let datafile = OTUtils.loadJSONDatafile("api_datafile")!
2929

30+
var sampleHoldout: [String: Any] {
31+
return [
32+
"status": "Running",
33+
"id": "holdout_4444444",
34+
"key": "holdout_key",
35+
"layerId": "10420273888",
36+
"trafficAllocation": [
37+
["entityId": "holdout_variation_a11", "endOfRange": 10000] // 100% traffic allocation
38+
],
39+
"audienceIds": [],
40+
"variations": [
41+
[
42+
"variables": [],
43+
"id": "holdout_variation_a11",
44+
"key": "holdout_a"
45+
]
46+
]
47+
]
48+
}
49+
3050
override func setUp() {
3151
eventDispatcher = MockEventDispatcher()
3252
optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting",
@@ -459,6 +479,39 @@ extension BatchEventBuilderTests_Events {
459479
variation.featureEnabled = false
460480
fakeOptimizelyManager.config!.project!.sendFlagDecisions = nil
461481
}
482+
483+
func testImpressionEventWithUserInHoldout() {
484+
let eventDispatcher2 = MockEventDispatcher()
485+
let optimizely = OptimizelyClient(sdkKey: "12345", eventDispatcher: eventDispatcher2)
486+
487+
try! optimizely.start(datafile: datafile)
488+
489+
let holdout: Holdout = try! OTUtils.model(from: sampleHoldout)
490+
optimizely.config?.project.holdouts = [holdout]
491+
492+
let exp = expectation(description: "Wait for event to dispatch")
493+
optimizely.config!.project!.sendFlagDecisions = true
494+
495+
_ = optimizely.isFeatureEnabled(featureKey: featureKey, userId: userId)
496+
497+
let result = XCTWaiter.wait(for: [exp], timeout: 0.1)
498+
if result == XCTWaiter.Result.timedOut {
499+
let event = getFirstEventJSON(dispatcher: eventDispatcher2)!
500+
let visitor = (event["visitors"] as! Array<Dictionary<String, Any>>)[0]
501+
let snapshot = (visitor["snapshots"] as! Array<Dictionary<String, Any>>)[0]
502+
let decision = (snapshot["decisions"] as! Array<Dictionary<String, Any>>)[0]
503+
504+
let metaData = decision["metadata"] as! Dictionary<String, Any>
505+
XCTAssertEqual(metaData["rule_type"] as! String, Constants.DecisionSource.holdout.rawValue)
506+
XCTAssertEqual(metaData["rule_key"] as! String, "holdout_key")
507+
XCTAssertEqual(metaData["flag_key"] as! String, "feature_1")
508+
XCTAssertEqual(metaData["variation_key"] as! String, "holdout_a")
509+
XCTAssertFalse(metaData["enabled"] as! Bool)
510+
} else {
511+
XCTFail("No event found")
512+
}
513+
optimizely.config!.project!.sendFlagDecisions = nil
514+
}
462515
}
463516

464517
// MARK: - Utils

Tests/OptimizelyTests-Common/DecisionListenerTest_Holdouts.swift

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Tests/OptimizelyTests-DataModel/HoldoutTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import XCTest
2222
class HoldoutTests: XCTestCase {
2323
static var variationData: [String: Any] = ["id": "553339214",
2424
"key": "house",
25-
"featureEnabled": true]
25+
"featureEnabled": false]
2626

2727
static var trafficAllocationData: [String: Any] = ["entityId": "553339214", "endOfRange": 5000]
2828

0 commit comments

Comments
 (0)