Skip to content

Commit e132c56

Browse files
WIP: Batch load and save test case added
1 parent 3e0f560 commit e132c56

File tree

2 files changed

+85
-31
lines changed

2 files changed

+85
-31
lines changed

Sources/Implementation/DefaultDecisionService.swift

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class DefaultDecisionService: OPTDecisionService {
111111
}
112112

113113
/// Load variation from tracker
114-
if let _userId = userProfileTracker?.userId,
115-
let variationId = getVariationIdFromProfile(userId: _userId, experimentId: experimentId),
114+
if let profile = userProfileTracker?.userProfile,
115+
let variationId = getVariationIdFromProfile(profile: profile, experimentId: experimentId),
116116
let variation = experiment.getVariation(id: variationId) {
117117

118118
let info = LogMessage.gotVariationFromUserProfile(variation.key, experiment.key, userId)
@@ -226,35 +226,6 @@ class DefaultDecisionService: OPTDecisionService {
226226
}
227227

228228
return response!
229-
230-
231-
232-
// // Evaluate in this order:
233-
//
234-
// // 1. Attempt to bucket user into experiment using feature flag.
235-
// // Check if the feature flag is under an experiment and the the user is bucketed into one of these experiments
236-
// var decisionResponse = getVariationForFeatureExperiment(config: config,
237-
// featureFlag: featureFlag,
238-
// user: user,
239-
// userProfileTracker: nil,
240-
// options: options)
241-
// reasons.merge(decisionResponse.reasons)
242-
// if let decision = decisionResponse.result {
243-
// return DecisionResponse(result: decision, reasons: reasons)
244-
// }
245-
//
246-
// // 2. Attempt to bucket user into rollout using the feature flag.
247-
// // Check if the feature flag has rollout and the user is bucketed into one of it's rules
248-
// decisionResponse = getVariationForFeatureRollout(config: config,
249-
// featureFlag: featureFlag,
250-
// user: user,
251-
// options: options)
252-
// reasons.merge(decisionResponse.reasons)
253-
// if let decision = decisionResponse.result {
254-
// return DecisionResponse(result: decision, reasons: reasons)
255-
// }
256-
//
257-
// return DecisionResponse(result: nil, reasons: reasons)
258229
}
259230

260231
func getVariationForFeatureList(config: ProjectConfig,
@@ -544,6 +515,18 @@ extension DefaultDecisionService {
544515
}
545516
}
546517

518+
func getVariationIdFromProfile(profile: UserProfile?,
519+
experimentId: String) -> String? {
520+
if let _profile = profile,
521+
let bucketMap = _profile[UserProfileKeys.kBucketMap] as? OPTUserProfileService.UPBucketMap,
522+
let experimentMap = bucketMap[experimentId],
523+
let variationId = experimentMap[UserProfileKeys.kVariationId] {
524+
return variationId
525+
} else {
526+
return nil
527+
}
528+
}
529+
547530
func saveProfile(userId: String,
548531
experimentId: String,
549532
variationId: String) {

Tests/OptimizelyTests-Common/DecisionServiceTests_Features.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,61 @@ extension DecisionServiceTests_Features {
289289

290290
}
291291

292+
// MARK: - Test getVariationForFeatureList()
293+
294+
extension DecisionServiceTests_Features {
295+
func testGetVariationForFeatureListBatchUPSLoadAndSave() {
296+
let mockProfileService = MocProfileService()
297+
298+
let ups_service = DefaultDecisionService(userProfileService: mockProfileService)
299+
300+
let flag1: FeatureFlag = try! OTUtils.model(
301+
from:[
302+
"id": "553339214",
303+
"key": "house",
304+
"experimentIds": [kExperimentId],
305+
"rolloutId": "",
306+
"variables": []
307+
]
308+
)
309+
310+
let flag2: FeatureFlag = try! OTUtils.model(
311+
from:[
312+
"id": "553339215",
313+
"key": "house",
314+
"experimentIds": [kExperimentId],
315+
"rolloutId": "",
316+
"variables": []
317+
]
318+
)
319+
320+
let flag3: FeatureFlag = try! OTUtils.model(
321+
from:[
322+
"id": "553339216",
323+
"key": "house",
324+
"experimentIds": [kExperimentId],
325+
"rolloutId": "",
326+
"variables": []
327+
]
328+
)
329+
330+
let pair = ups_service.getVariationForFeatureList(
331+
config: config,
332+
featureFlags: [flag1, flag2, flag3],
333+
user: optimizely.createUserContext(userId: kUserId,
334+
attributes: kAttributesCountryMatch)
335+
)
336+
337+
338+
XCTAssertEqual(mockProfileService.lookupCount, 1)
339+
XCTAssertEqual(mockProfileService.saveCount, 1)
340+
XCTAssertEqual(pair.count, 3)
341+
XCTAssert(pair[0].result?.experiment?.key == kExperimentKey)
342+
XCTAssert(pair[0].result?.variation.key == kVariationKeyD)
343+
XCTAssert(pair[0].result?.source == Constants.DecisionSource.featureTest.rawValue)
344+
}
345+
}
346+
292347
// MARK: - Test getVariationForFeatureRollout()
293348

294349
extension DecisionServiceTests_Features {
@@ -466,3 +521,19 @@ extension DecisionServiceTests_Features {
466521
}
467522

468523
}
524+
525+
class MocProfileService: DefaultUserProfileService {
526+
var lookupCount = 0
527+
var saveCount = 0
528+
529+
override func lookup(userId: String) -> DefaultUserProfileService.UPProfile? {
530+
lookupCount += 1
531+
return super.lookup(userId: userId)
532+
}
533+
534+
override func save(userProfile: DefaultUserProfileService.UPProfile) {
535+
super.save(userProfile: userProfile)
536+
saveCount += 1
537+
}
538+
539+
}

0 commit comments

Comments
 (0)