Skip to content

Commit

Permalink
1. Updated flag_type to rule_type.
Browse files Browse the repository at this point in the history
2. Added tests for rule_key in metadata.
  • Loading branch information
yasirfolio3 committed Oct 8, 2020
1 parent 69d6e22 commit 9712a60
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 51 deletions.
38 changes: 20 additions & 18 deletions Sources/Data Model/DispatchEvents/BatchEvent.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/****************************************************************************
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

import Foundation

Expand Down Expand Up @@ -80,12 +80,14 @@ struct Snapshot: Codable, Equatable {
}

struct DecisionMetadata: Codable, Equatable {
let flagType: String
let ruleType: String
let ruleKey: String
let flagKey: String
let variationKey: String

enum CodingKeys: String, CodingKey {
case flagType = "flag_type"
case ruleType = "rule_type"
case ruleKey = "rule_key"
case flagKey = "flag_key"
case variationKey = "variation_key"
}
Expand Down Expand Up @@ -118,7 +120,7 @@ struct DispatchEvent: Codable, Equatable {
var tags: [String: AttributeValue]?
var revenue: AttributeValue?
var value: AttributeValue?

enum CodingKeys: String, CodingKey {
case entityID = "entity_id"
case key
Expand All @@ -138,7 +140,7 @@ struct DispatchEvent: Codable, Equatable {
revenue: AttributeValue? = nil) {

// TODO: add validation and throw here for invalid value (int, double) and revenue (int) types

self.timestamp = timestamp
self.key = key
self.entityID = entityID
Expand Down
38 changes: 19 additions & 19 deletions Sources/Implementation/Events/BatchEventBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/****************************************************************************
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
* Copyright 2019-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

import Foundation

Expand All @@ -29,20 +29,20 @@ class BatchEventBuilder {
userId: String,
attributes: OptimizelyAttributes?,
flagKey: String,
flagType: String) -> Data? {
ruleType: String) -> Data? {

if (flagType == Constants.DecisionSource.rollout.rawValue || variation == nil) && !config.sendFlagDecisions() {
if (ruleType == Constants.DecisionSource.rollout.rawValue || variation == nil) && !config.sendFlagDecisions() {
return nil
}

var variationId = ""
var variationKey = ""
if let tmpVariation = variation {
variationKey = tmpVariation.key
variationId = tmpVariation.id
}

let metaData = DecisionMetadata(flagType: flagType, flagKey: flagKey, variationKey: variationKey)
let metaData = DecisionMetadata(ruleType: ruleType,ruleKey: experiment.key, flagKey: flagKey, variationKey: variationKey)

let decision = Decision(variationID: variationId,
campaignID: experiment.layerId,
Expand Down Expand Up @@ -115,7 +115,7 @@ class BatchEventBuilder {

return try? JSONEncoder().encode(batchEvent)
}

// MARK: - Event Tags

static func filterEventTags(_ eventTags: [String: Any]?) -> ([String: AttributeValue], AttributeValue?, AttributeValue?) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ open class OptimizelyClient: NSObject {
userId: userId,
attributes: attributes,
flagKey: experimentKey,
flagType: "experiment")
ruleType: "experiment")

return variation.key
}
Expand Down Expand Up @@ -393,7 +393,7 @@ open class OptimizelyClient: NSObject {
logger.i(.featureNotEnabledForUser(featureKey, userId))
}

sendImpressionEvent(experiment: pair.experiment, variation: pair.variation, userId: userId, attributes: attributes, flagKey: featureKey, flagType: pair.source)
sendImpressionEvent(experiment: pair.experiment, variation: pair.variation, userId: userId, attributes: attributes, flagKey: featureKey, ruleType: pair.source)

sendDecisionNotification(decisionType: .feature,
userId: userId,
Expand Down Expand Up @@ -751,7 +751,7 @@ extension OptimizelyClient {
userId: String,
attributes: OptimizelyAttributes? = nil,
flagKey: String,
flagType: String) {
ruleType: String) {

// non-blocking (event data serialization takes time)
eventLock.async {
Expand All @@ -763,7 +763,7 @@ extension OptimizelyClient {
userId: userId,
attributes: attributes,
flagKey: flagKey,
flagType: flagType) else {
ruleType: ruleType) else {
self.logger.e(OptimizelyError.eventBuildFailure(DispatchEvent.activateEventKey))
return
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/OptimizelyTests-APIs/OptimizelyClientTests_Others.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class OptimizelyClientTests_Others: XCTestCase {
// set invalid (infinity) to attribute values, which will cause JSONEncoder.encode exception
let attributes = ["testvar": Double.infinity]

optimizely.sendImpressionEvent(experiment: experiment, variation: variation, userId: kUserId, attributes: attributes, flagKey: experiment.key, flagType: Constants.DecisionSource.rollout.rawValue)
optimizely.sendImpressionEvent(experiment: experiment, variation: variation, userId: kUserId, attributes: attributes, flagKey: experiment.key, ruleType: Constants.DecisionSource.rollout.rawValue)
XCTAssert(eventDispatcher.events.count == 0)
}

Expand Down Expand Up @@ -322,7 +322,7 @@ class OptimizelyClientTests_Others: XCTestCase {
// force condition for sdk-not-ready
optimizely.config = nil

optimizely.sendImpressionEvent(experiment: experiment, variation: variation, userId: kUserId, flagKey: experiment.key, flagType: Constants.DecisionSource.rollout.rawValue)
optimizely.sendImpressionEvent(experiment: experiment, variation: variation, userId: kUserId, flagKey: experiment.key, ruleType: Constants.DecisionSource.rollout.rawValue)
XCTAssert(eventDispatcher.events.isEmpty, "event should not be sent out sdk is not configured properly")

optimizely.sendConversionEvent(eventKey: kEventKey, userId: kUserId)
Expand Down
14 changes: 8 additions & 6 deletions Tests/OptimizelyTests-Common/BatchEventBuilderTests_Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class BatchEventBuilderTests_Events: XCTestCase {
XCTAssertEqual(decision["experiment_id"] as! String, expExperimentId)

let metaData = decision["metadata"] as! Dictionary<String, Any>
XCTAssertEqual(metaData["flag_type"] as! String, "experiment")
XCTAssertEqual(metaData["rule_type"] as! String, "experiment")
XCTAssertEqual(metaData["rule_key"] as! String, "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2")
XCTAssertEqual(metaData["flag_key"] as! String, "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2")
XCTAssertEqual(metaData["variation_key"] as! String, "all_traffic_variation")

Expand Down Expand Up @@ -113,20 +114,20 @@ class BatchEventBuilderTests_Events: XCTestCase {
let variation = experiment?.getVariation(id: "10416523162")

for scenario in scenarios {
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: variation, userId: userId, attributes: attributes, flagKey: experiment!.key, flagType: scenario.key)
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: variation, userId: userId, attributes: attributes, flagKey: experiment!.key, ruleType: scenario.key)
scenario.value ? XCTAssertNotNil(event): XCTAssertNil(event)
}

// nil variation should always return nil
for scenario in scenarios {
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: nil, userId: userId, attributes: attributes, flagKey: experiment!.key, flagType: scenario.key)
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: nil, userId: userId, attributes: attributes, flagKey: experiment!.key, ruleType: scenario.key)
XCTAssertNil(event)
}

// should always return a event if sendFlagDecisions is set
optimizely.config?.project.sendFlagDecisions = true
for scenario in scenarios {
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: nil, userId: userId, attributes: attributes, flagKey: experiment!.key, flagType: scenario.key)
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: nil, userId: userId, attributes: attributes, flagKey: experiment!.key, ruleType: scenario.key)
XCTAssertNotNil(event)
}
optimizely.config?.project.sendFlagDecisions = nil
Expand All @@ -142,15 +143,16 @@ class BatchEventBuilderTests_Events: XCTestCase {
let experiment = optimizely.config?.getExperiment(id: "10390977714")

optimizely.config?.project.sendFlagDecisions = true
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: nil, userId: userId, attributes: attributes, flagKey: experiment!.key, flagType: Constants.DecisionSource.featureTest.rawValue)
let event = BatchEventBuilder.createImpressionEvent(config: optimizely.config!, experiment: experiment!, variation: nil, userId: userId, attributes: attributes, flagKey: experiment!.key, ruleType: Constants.DecisionSource.featureTest.rawValue)
XCTAssertNotNil(event)

let visitor = (getEventJSON(data: event!)!["visitors"] as! Array<Dictionary<String, Any>>)[0]
let snapshot = (visitor["snapshots"] as! Array<Dictionary<String, Any>>)[0]
let decision = (snapshot["decisions"] as! Array<Dictionary<String, Any>>)[0]

let metaData = decision["metadata"] as! Dictionary<String, Any>
XCTAssertEqual(metaData["flag_type"] as! String, Constants.DecisionSource.featureTest.rawValue)
XCTAssertEqual(metaData["rule_type"] as! String, Constants.DecisionSource.featureTest.rawValue)
XCTAssertEqual(metaData["rule_key"] as! String, "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2")
XCTAssertEqual(metaData["flag_key"] as! String, "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2")
XCTAssertEqual(metaData["variation_key"] as! String, "")
optimizely.config?.project.sendFlagDecisions = nil
Expand Down
6 changes: 4 additions & 2 deletions Tests/OptimizelyTests-Common/DecisionListenerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ class DecisionListenerTests: XCTestCase {
let decision = (snapshot["decisions"] as! Array<Dictionary<String, Any>>)[0]

let metaData = decision["metadata"] as! Dictionary<String, Any>
XCTAssertEqual(metaData["flag_type"] as! String, Constants.DecisionSource.rollout.rawValue)
XCTAssertEqual(metaData["rule_type"] as! String, Constants.DecisionSource.rollout.rawValue)
XCTAssertEqual(metaData["rule_key"] as! String, "exp_with_audience")
XCTAssertEqual(metaData["flag_key"] as! String, "feature_1")
XCTAssertEqual(metaData["variation_key"] as! String, "a")

Expand Down Expand Up @@ -854,7 +855,8 @@ class DecisionListenerTests: XCTestCase {
let decision = (snapshot["decisions"] as! Array<Dictionary<String, Any>>)[0]

let metaData = decision["metadata"] as! Dictionary<String, Any>
XCTAssertEqual(metaData["flag_type"] as! String, Constants.DecisionSource.featureTest.rawValue)
XCTAssertEqual(metaData["rule_type"] as! String, Constants.DecisionSource.featureTest.rawValue)
XCTAssertEqual(metaData["rule_key"] as! String, "exp_with_audience")
XCTAssertEqual(metaData["flag_key"] as! String, "feature_1")
XCTAssertEqual(metaData["variation_key"] as! String, "a")

Expand Down

0 comments on commit 9712a60

Please sign in to comment.