Skip to content

Commit 1f38e54

Browse files
jaeoptthomaszurkan-optimizely
authored andcommitted
fix feature variable when feature disabled (#3707) (#123)
1 parent a5eb95b commit 1f38e54

File tree

3 files changed

+124
-8
lines changed

3 files changed

+124
-8
lines changed

OptimizelySDK/Optimizely/OptimizelyManager.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,21 @@ open class OptimizelyManager: NSObject {
532532
throw OptimizelyError.variableUnknown
533533
}
534534

535-
// TODO: [Jae] optional? fallback to empty string is OK?
536-
var defaultValue = variable.defaultValue ?? ""
535+
var featureValue = variable.defaultValue ?? ""
537536

538537
var _attributes = OptimizelyAttributes()
539538
if attributes != nil {
540539
_attributes = attributes!
541540
}
542541
if let decision = self.decisionService.getVariationForFeature(config: config, featureFlag: featureFlag, userId: userId, attributes: _attributes) {
543-
if let featureVariableUsage = decision.variation?.variables?.filter({$0.id == variable.id}).first {
544-
defaultValue = featureVariableUsage.value
542+
if let variation = decision.variation,
543+
let featureVariableUsage = variation.variables?.filter({$0.id == variable.id}).first
544+
{
545+
if let featureEnabled = variation.featureEnabled, featureEnabled {
546+
featureValue = featureVariableUsage.value
547+
} else {
548+
// add standard log message here
549+
}
545550
}
546551
}
547552

@@ -551,16 +556,16 @@ open class OptimizelyManager: NSObject {
551556
switch T.self {
552557
case is String.Type:
553558
typeName = "string"
554-
valueParsed = defaultValue as? T
559+
valueParsed = featureValue as? T
555560
case is Int.Type:
556561
typeName = "integer"
557-
valueParsed = Int(defaultValue) as? T
562+
valueParsed = Int(featureValue) as? T
558563
case is Double.Type:
559564
typeName = "double"
560-
valueParsed = Double(defaultValue) as? T
565+
valueParsed = Double(featureValue) as? T
561566
case is Bool.Type:
562567
typeName = "boolean"
563-
valueParsed = Bool(defaultValue) as? T
568+
valueParsed = Bool(featureValue) as? T
564569
default:
565570
break
566571
}

OptimizelySDK/OptimizelySwiftSDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@
543543
6E8B6A49221CBBC5006EA8A6 /* GroupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E8B6A47221CBBC5006EA8A6 /* GroupTests.swift */; };
544544
6E8B6A4B221CC2D6006EA8A6 /* ProjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E8B6A4A221CC2D6006EA8A6 /* ProjectTests.swift */; };
545545
6E8B6A4C221CC2D6006EA8A6 /* ProjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E8B6A4A221CC2D6006EA8A6 /* ProjectTests.swift */; };
546+
6E8CB5C9224C461A00B8CB7A /* OptimizelyManagerTests_Variables.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E8CB5C8224C461A00B8CB7A /* OptimizelyManagerTests_Variables.swift */; };
546547
6E95C8B62217F10800B6EE06 /* ConditionLeaf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E95C8B52217F10800B6EE06 /* ConditionLeaf.swift */; };
547548
6E95C8B92217F9B100B6EE06 /* ConditionLeaf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E95C8B52217F10800B6EE06 /* ConditionLeaf.swift */; };
548549
6E95C8BA2217F9B200B6EE06 /* ConditionLeaf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E95C8B52217F10800B6EE06 /* ConditionLeaf.swift */; };
@@ -1270,6 +1271,7 @@
12701271
6E852C69221A7E3300C41F94 /* ExperimentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExperimentTests.swift; sourceTree = "<group>"; };
12711272
6E8B6A47221CBBC5006EA8A6 /* GroupTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupTests.swift; sourceTree = "<group>"; };
12721273
6E8B6A4A221CC2D6006EA8A6 /* ProjectTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectTests.swift; sourceTree = "<group>"; };
1274+
6E8CB5C8224C461A00B8CB7A /* OptimizelyManagerTests_Variables.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptimizelyManagerTests_Variables.swift; sourceTree = "<group>"; };
12731275
6E95C8A822176EA500B6EE06 /* AudienceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AudienceTests.swift; sourceTree = "<group>"; };
12741276
6E95C8AA2217736000B6EE06 /* ConditionHolderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConditionHolderTests.swift; sourceTree = "<group>"; };
12751277
6E95C8B12217C92E00B6EE06 /* ConditionHolderTests_Evaluate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConditionHolderTests_Evaluate.swift; sourceTree = "<group>"; };
@@ -1678,6 +1680,7 @@
16781680
6E636C162236CB6D00AF3CEF /* OptimizelyManagerTests_Valid.swift */,
16791681
6EC5D3612231C19200BF9D77 /* OptimizelyManagerTests_Invalid.swift */,
16801682
6E636C5D223708B000AF3CEF /* OptimizelyManagerTests_ForcedVariation.swift */,
1683+
6E8CB5C8224C461A00B8CB7A /* OptimizelyManagerTests_Variables.swift */,
16811684
0B8F537B223FFB0500C56082 /* OptimizelyManagerTests_Group.swift */,
16821685
0B2D48FE2245670C0024A661 /* OptimizelyManagerTests_Threading.swift */,
16831686
);
@@ -2705,6 +2708,7 @@
27052708
6E636BF32236C9BC00AF3CEF /* Variable.swift in Sources */,
27062709
6E636BBC2236C99300AF3CEF /* DataStoreFile.swift in Sources */,
27072710
6E636BAF2236C98300AF3CEF /* DefaultEventDispatcher.swift in Sources */,
2711+
6E8CB5C9224C461A00B8CB7A /* OptimizelyManagerTests_Variables.swift in Sources */,
27082712
6E636BA92236C97E00AF3CEF /* OptimizelyLogLevel.swift in Sources */,
27092713
6E636BE02236C9B700AF3CEF /* Audience.swift in Sources */,
27102714
6E636C022236C9C500AF3CEF /* EventForDispatch+Extension.swift in Sources */,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// OptimizelyManagerTests_Variables.swift
3+
// OptimizelyTests-APIs-iOS
4+
//
5+
// Created by Jae Kim on 3/11/19.
6+
// Copyright © 2019 Optimizely. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
class OptimizelyManagerTests_Variables: XCTestCase {
12+
13+
let kVariableId = "1111"
14+
let kVariableDefaultValue = 20
15+
let kVariableValueA = 30
16+
17+
let kUserId = "12345"
18+
let kExperimentId = "11111"
19+
20+
var sampleExperimentData: [String: Any] { return
21+
[
22+
"status": "Running",
23+
"id": kExperimentId,
24+
"key": "43432134",
25+
"layerId": "10420273888",
26+
"trafficAllocation": [
27+
[
28+
"entityId": "10389729780",
29+
"endOfRange": 10000
30+
]
31+
],
32+
"audienceIds": [],
33+
"variations": [
34+
[
35+
"variables": [
36+
["id": kVariableId, "value": String(kVariableValueA)]
37+
],
38+
"id": "10389729780",
39+
"key": "16456523121"
40+
]
41+
],
42+
"forcedVariations":[:]
43+
]
44+
}
45+
var sampleFeatureFlagData: [String: Any] { return
46+
[
47+
"id": "553339214",
48+
"key": "house",
49+
"experimentIds":[kExperimentId],
50+
"rolloutId": "",
51+
"variables": [
52+
[
53+
"defaultValue": String(kVariableDefaultValue),
54+
"type": "integer",
55+
"id": kVariableId,
56+
"key": "window"
57+
]
58+
]
59+
]
60+
}
61+
62+
var optimizely: OptimizelyManager!
63+
var experiment: Experiment!
64+
var featureFlag: FeatureFlag!
65+
66+
override func setUp() {
67+
super.setUp()
68+
69+
optimizely = OTUtils.createOptimizely(datafileName: "empty_datafile",
70+
clearUserProfileService: true)!
71+
72+
let featureFlag: FeatureFlag = try! OTUtils.model(from: sampleFeatureFlagData)
73+
optimizely.config!.project.featureFlags = [featureFlag]
74+
}
75+
76+
77+
func testFeatureVariableWhenFeatureEnabled() {
78+
var experiment: Experiment = try! OTUtils.model(from: sampleExperimentData)
79+
experiment.variations[0].featureEnabled = true
80+
optimizely.config!.project.experiments = [experiment]
81+
82+
let value = try! optimizely.getFeatureVariableInteger(featureKey: "house", variableKey: "window", userId: kUserId)
83+
84+
XCTAssertEqual(value, kVariableValueA)
85+
}
86+
87+
func testFeatureVariableWhenFeatureDisabled() {
88+
var experiment: Experiment = try! OTUtils.model(from: sampleExperimentData)
89+
experiment.variations[0].featureEnabled = false
90+
optimizely.config!.project.experiments = [experiment]
91+
92+
let value = try! optimizely.getFeatureVariableInteger(featureKey: "house", variableKey: "window", userId: kUserId)
93+
94+
XCTAssertEqual(value, kVariableDefaultValue)
95+
}
96+
97+
func testFeatureVariableWhenFeatureEnabledNil() {
98+
var experiment: Experiment = try! OTUtils.model(from: sampleExperimentData)
99+
experiment.variations[0].featureEnabled = nil
100+
optimizely.config!.project.experiments = [experiment]
101+
102+
let value = try! optimizely.getFeatureVariableInteger(featureKey: "house", variableKey: "window", userId: kUserId)
103+
104+
XCTAssertEqual(value, kVariableDefaultValue)
105+
}
106+
107+
}

0 commit comments

Comments
 (0)