|
1 | 1 | //
|
2 |
| -// Copyright 2019-2021, Optimizely, Inc. and contributors |
| 2 | +// Copyright 2019-2021, 2023 Optimizely, Inc. and contributors |
3 | 3 | //
|
4 | 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | // you may not use this file except in compliance with the License.
|
@@ -35,6 +35,11 @@ class BatchEventBuilderTests_Events: XCTestCase {
|
35 | 35 | project = optimizely.config!.project!
|
36 | 36 | }
|
37 | 37 |
|
| 38 | + override func tearDown() { |
| 39 | + Utils.sdkVersion = OPTIMIZELYSDKVERSION |
| 40 | + Utils.swiftSdkClientName = "swift-sdk" |
| 41 | + } |
| 42 | + |
38 | 43 | func testCreateImpressionEvent() {
|
39 | 44 | let expVariationId = "10416523162"
|
40 | 45 | let expCampaignId = "10420273929"
|
@@ -99,6 +104,80 @@ class BatchEventBuilderTests_Events: XCTestCase {
|
99 | 104 | XCTAssertNil(de["value"])
|
100 | 105 | }
|
101 | 106 |
|
| 107 | + func testCreateImpressionEventCustomClientNameAndVersion() { |
| 108 | + // Needed custom instances to avoid breaking original tests |
| 109 | + let eventDispatcher = MockEventDispatcher() |
| 110 | + let optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting", |
| 111 | + clearUserProfileService: true, |
| 112 | + eventDispatcher: eventDispatcher, |
| 113 | + settings: OptimizelySdkSettings(sdkName: "flutter-sdk", sdkVersion: "1234"))! |
| 114 | + |
| 115 | + let expVariationId = "10416523162" |
| 116 | + let expCampaignId = "10420273929" |
| 117 | + let expExperimentId = "10390977714" |
| 118 | + |
| 119 | + let attributes: [String: Any] = [ |
| 120 | + "s_foo": "foo", |
| 121 | + "b_true": true, |
| 122 | + "i_42": 42, |
| 123 | + "d_4_2": 4.2 |
| 124 | + ] |
| 125 | + |
| 126 | + _ = try! optimizely.activate(experimentKey: experimentKey, |
| 127 | + userId: userId, |
| 128 | + attributes: attributes) |
| 129 | + |
| 130 | + // Skipped getFirstEventJSON as it uses the class level optimizely instance |
| 131 | + optimizely.eventLock.sync{} |
| 132 | + let rawEvent: EventForDispatch = eventDispatcher.events.first! |
| 133 | + let event = try! JSONSerialization.jsonObject(with: rawEvent.body, options: .allowFragments) as! [String: Any] |
| 134 | + |
| 135 | + XCTAssertEqual((event["revision"] as! String), project.revision) |
| 136 | + XCTAssertEqual((event["account_id"] as! String), project.accountId) |
| 137 | + XCTAssertEqual(event["client_version"] as! String, "1234") |
| 138 | + XCTAssertEqual(event["project_id"] as! String, project.projectId) |
| 139 | + XCTAssertEqual(event["client_name"] as! String, "flutter-sdk") |
| 140 | + XCTAssertEqual(event["anonymize_ip"] as! Bool, project.anonymizeIP) |
| 141 | + XCTAssertEqual(event["enrich_decisions"] as! Bool, true) |
| 142 | + |
| 143 | + let visitor = (event["visitors"] as! Array<Dictionary<String, Any>>)[0] |
| 144 | + |
| 145 | + XCTAssertEqual(visitor["visitor_id"] as! String, userId) |
| 146 | + |
| 147 | + let snapshot = (visitor["snapshots"] as! Array<Dictionary<String, Any>>)[0] |
| 148 | + |
| 149 | + // attributes contents are tested separately in "BatchEventBuilder_Attributes.swift" |
| 150 | + let eventAttributes = visitor["attributes"] as! Array<Dictionary<String, Any>> |
| 151 | + XCTAssertEqual(eventAttributes.count, attributes.count) |
| 152 | + |
| 153 | + let decision = (snapshot["decisions"] as! Array<Dictionary<String, Any>>)[0] |
| 154 | + |
| 155 | + XCTAssertEqual(decision["variation_id"] as! String, expVariationId) |
| 156 | + XCTAssertEqual(decision["campaign_id"] as! String, expCampaignId) |
| 157 | + XCTAssertEqual(decision["experiment_id"] as! String, expExperimentId) |
| 158 | + |
| 159 | + let metaData = decision["metadata"] as! Dictionary<String, Any> |
| 160 | + XCTAssertEqual(metaData["rule_type"] as! String, Constants.DecisionSource.experiment.rawValue) |
| 161 | + XCTAssertEqual(metaData["rule_key"] as! String, "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2") |
| 162 | + XCTAssertEqual(metaData["flag_key"] as! String, "") |
| 163 | + XCTAssertEqual(metaData["variation_key"] as! String, "all_traffic_variation") |
| 164 | + XCTAssertTrue(metaData["enabled"] as! Bool) |
| 165 | + |
| 166 | + let de = (snapshot["events"] as! Array<Dictionary<String, Any>>)[0] |
| 167 | + |
| 168 | + XCTAssertEqual(de["entity_id"] as! String, expCampaignId) |
| 169 | + XCTAssertEqual(de["key"] as! String, "campaign_activated") |
| 170 | + let expTimestamp = Int64((Date.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate) * 1000) |
| 171 | + // divide by 1000 to ignore small time difference |
| 172 | + XCTAssertEqual((de["timestamp"] as! Int64)/1000, expTimestamp / 1000) |
| 173 | + // cannot validate randomly-generated string. check if long enough. |
| 174 | + XCTAssert((de["uuid"] as! String).count > 20) |
| 175 | + // event tags are tested separately below |
| 176 | + XCTAssert((de["tags"] as! Dictionary<String, Any>).count==0) |
| 177 | + XCTAssertNil(de["revenue"]) |
| 178 | + XCTAssertNil(de["value"]) |
| 179 | + } |
| 180 | + |
102 | 181 | func testCreateImpressionEventWithoutVariation() {
|
103 | 182 | let attributes: [String: Any] = [
|
104 | 183 | "s_foo": "foo",
|
@@ -196,6 +275,68 @@ class BatchEventBuilderTests_Events: XCTestCase {
|
196 | 275 | XCTAssertNil(de["value"])
|
197 | 276 | }
|
198 | 277 |
|
| 278 | + func testCreateConversionEventCustomClientNameAndVersion() { |
| 279 | + // Needed custom instances to avoid breaking original tests |
| 280 | + let eventDispatcher = MockEventDispatcher() |
| 281 | + let optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting", |
| 282 | + clearUserProfileService: true, |
| 283 | + eventDispatcher: eventDispatcher, |
| 284 | + settings: OptimizelySdkSettings(sdkName: "flutter-sdk", sdkVersion: "1234"))! |
| 285 | + |
| 286 | + let eventKey = "event_single_targeted_exp" |
| 287 | + let eventId = "10404198135" |
| 288 | + |
| 289 | + let attributes: [String: Any] = ["s_foo": "bar"] |
| 290 | + let eventTags: [String: Any] = ["browser": "chrome"] |
| 291 | + |
| 292 | + try! optimizely.track(eventKey: eventKey, |
| 293 | + userId: userId, |
| 294 | + attributes: attributes, |
| 295 | + eventTags: eventTags) |
| 296 | + |
| 297 | + // Skipped getFirstEventJSON as it uses the class level optimizely instance |
| 298 | + optimizely.eventLock.sync{} |
| 299 | + let rawEvent: EventForDispatch = eventDispatcher.events.first! |
| 300 | + let event = try! JSONSerialization.jsonObject(with: rawEvent.body, options: .allowFragments) as! [String: Any] |
| 301 | + |
| 302 | + XCTAssertEqual(event["revision"] as! String, project.revision) |
| 303 | + XCTAssertEqual(event["account_id"] as! String, project.accountId) |
| 304 | + XCTAssertEqual(event["client_version"] as! String, "1234") |
| 305 | + XCTAssertEqual(event["project_id"] as! String, project.projectId) |
| 306 | + XCTAssertEqual(event["client_name"] as! String, "flutter-sdk") |
| 307 | + XCTAssertEqual(event["anonymize_ip"] as! Bool, project.anonymizeIP) |
| 308 | + XCTAssertEqual(event["enrich_decisions"] as! Bool, true) |
| 309 | + |
| 310 | + let visitor = (event["visitors"] as! Array<Dictionary<String, Any>>)[0] |
| 311 | + |
| 312 | + XCTAssertEqual(visitor["visitor_id"] as! String, userId) |
| 313 | + |
| 314 | + let snapshot = (visitor["snapshots"] as! Array<Dictionary<String, Any>>)[0] |
| 315 | + |
| 316 | + // attributes contents are tested separately in "BatchEventBuilder_Attributes.swift" |
| 317 | + let eventAttributes = visitor["attributes"] as! Array<Dictionary<String, Any>> |
| 318 | + XCTAssertEqual(eventAttributes[0]["key"] as! String, "s_foo") |
| 319 | + XCTAssertEqual(eventAttributes[0]["value"] as! String, "bar") |
| 320 | + |
| 321 | + let decisions = snapshot["decisions"] |
| 322 | + |
| 323 | + XCTAssertNil(decisions) |
| 324 | + |
| 325 | + let de = (snapshot["events"] as! Array<Dictionary<String, Any>>)[0] |
| 326 | + |
| 327 | + XCTAssertEqual(de["entity_id"] as! String, eventId) |
| 328 | + XCTAssertEqual(de["key"] as! String, eventKey) |
| 329 | + let expTimestamp = Int64((Date.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate) * 1000) |
| 330 | + // divide by 1000 to ignore small time difference |
| 331 | + XCTAssertEqual((de["timestamp"] as! Int64)/1000, expTimestamp / 1000) |
| 332 | + // cannot validate randomly-generated string. check if long enough. |
| 333 | + XCTAssert((de["uuid"] as!String).count > 20) |
| 334 | + // {tags, revenue, value} are tested separately below |
| 335 | + XCTAssertEqual((de["tags"] as! Dictionary<String, Any>)["browser"] as! String, "chrome") |
| 336 | + XCTAssertNil(de["revenue"]) |
| 337 | + XCTAssertNil(de["value"]) |
| 338 | + } |
| 339 | + |
199 | 340 | func testCreateConversionEventWhenEventKeyInvalid() {
|
200 | 341 | let eventKey = "not-existing-key"
|
201 | 342 |
|
|
0 commit comments