Skip to content

Commit 9447877

Browse files
committed
add testing for the use case where the optimizely variation is removed from the datafile
1 parent cbe6d01 commit 9447877

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

OptimizelySDKUserProfile/OptimizelySDKUserProfile.xcodeproj/project.pbxproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
2B2097A5E4C881CEECF8B701 /* Pods_OptimizelySDKUserProfileTVOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B450AF98AC0AE97B588B419 /* Pods_OptimizelySDKUserProfileTVOSTests.framework */; };
11+
2D3EC7981DEFB2CD0014D9A7 /* RemovedVariationDatafile.json in Resources */ = {isa = PBXBuildFile; fileRef = 2D3EC7971DEFB2CD0014D9A7 /* RemovedVariationDatafile.json */; };
12+
2D3EC7991DEFB2CD0014D9A7 /* RemovedVariationDatafile.json in Resources */ = {isa = PBXBuildFile; fileRef = 2D3EC7971DEFB2CD0014D9A7 /* RemovedVariationDatafile.json */; };
1113
2D56A9971DEE07670052AD54 /* OPTLYTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D56A9921DEE07560052AD54 /* OPTLYTestHelper.m */; };
1214
2D56A9981DEE07670052AD54 /* OPTLYTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D56A9921DEE07560052AD54 /* OPTLYTestHelper.m */; };
1315
2D56A99B1DEE077E0052AD54 /* OPTLYTestHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D56A9911DEE07560052AD54 /* OPTLYTestHelper.h */; };
@@ -102,10 +104,11 @@
102104
0E98DBE48E244F260EA2A9F4 /* Pods-OptimizelySDKUserProfileTVOSTests.rc.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKUserProfileTVOSTests.rc.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKUserProfileTVOSTests/Pods-OptimizelySDKUserProfileTVOSTests.rc.xcconfig"; sourceTree = "<group>"; };
103105
1C62B7FF941C777EC3DEB0F8 /* Pods-OptimizelySDKUserProfileTVOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKUserProfileTVOS.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKUserProfileTVOS/Pods-OptimizelySDKUserProfileTVOS.debug.xcconfig"; sourceTree = "<group>"; };
104106
234097C09F518FB25E8F1B9E /* Pods-OptimizelySDKUserProfileiOSTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKUserProfileiOSTests.release.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKUserProfileiOSTests/Pods-OptimizelySDKUserProfileiOSTests.release.xcconfig"; sourceTree = "<group>"; };
107+
2D3EC7971DEFB2CD0014D9A7 /* RemovedVariationDatafile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = RemovedVariationDatafile.json; path = TestData/RemovedVariationDatafile.json; sourceTree = "<group>"; };
105108
2D56A9911DEE07560052AD54 /* OPTLYTestHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OPTLYTestHelper.h; sourceTree = "<group>"; };
106109
2D56A9921DEE07560052AD54 /* OPTLYTestHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OPTLYTestHelper.m; sourceTree = "<group>"; };
107110
2D56A99E1DEE2DE10052AD54 /* InitialDatafile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = InitialDatafile.json; path = TestData/InitialDatafile.json; sourceTree = "<group>"; };
108-
2D56A9A11DEE2DFA0052AD54 /* UpdatedDatafile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = UpdatedDatafile.json; sourceTree = "<group>"; };
111+
2D56A9A11DEE2DFA0052AD54 /* UpdatedDatafile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = UpdatedDatafile.json; path = TestData/UpdatedDatafile.json; sourceTree = "<group>"; };
109112
2DA65807C8109484A1CFD535 /* Pods-OptimizelySDKUserProfileTVOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKUserProfileTVOS.release.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKUserProfileTVOS/Pods-OptimizelySDKUserProfileTVOS.release.xcconfig"; sourceTree = "<group>"; };
110113
30FCB99F9980AFF868A10EF5 /* Pods-OptimizelySDKUserProfileTVOS.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKUserProfileTVOS.beta.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKUserProfileTVOS/Pods-OptimizelySDKUserProfileTVOS.beta.xcconfig"; sourceTree = "<group>"; };
111114
340CAAE9EFC13D4CEC55A37D /* Pods-OptimizelySDKUserProfileTVOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKUserProfileTVOSTests.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKUserProfileTVOSTests/Pods-OptimizelySDKUserProfileTVOSTests.debug.xcconfig"; sourceTree = "<group>"; };
@@ -182,6 +185,7 @@
182185
children = (
183186
2D56A99E1DEE2DE10052AD54 /* InitialDatafile.json */,
184187
2D56A9A11DEE2DFA0052AD54 /* UpdatedDatafile.json */,
188+
2D3EC7971DEFB2CD0014D9A7 /* RemovedVariationDatafile.json */,
185189
);
186190
name = TestData;
187191
sourceTree = "<group>";
@@ -504,6 +508,7 @@
504508
isa = PBXResourcesBuildPhase;
505509
buildActionMask = 2147483647;
506510
files = (
511+
2D3EC7981DEFB2CD0014D9A7 /* RemovedVariationDatafile.json in Resources */,
507512
2D56A99F1DEE2DE10052AD54 /* InitialDatafile.json in Resources */,
508513
2D56A9A21DEE2DFA0052AD54 /* UpdatedDatafile.json in Resources */,
509514
);
@@ -520,6 +525,7 @@
520525
isa = PBXResourcesBuildPhase;
521526
buildActionMask = 2147483647;
522527
files = (
528+
2D3EC7991DEFB2CD0014D9A7 /* RemovedVariationDatafile.json in Resources */,
523529
2D56A9A01DEE2DE10052AD54 /* InitialDatafile.json in Resources */,
524530
2D56A9A31DEE2DFA0052AD54 /* UpdatedDatafile.json in Resources */,
525531
);

OptimizelySDKUserProfile/OptimizelySDKUserProfileTests/OptimizelySDKUserProfileTests.m

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040

4141
static NSString * const kOriginalDatafileName = @"InitialDatafile";
4242
static NSString * const kUpdatedDatafileName = @"UpdatedDatafile";
43+
static NSString * const kRemovedVariationDatafileName = @"RemovedVariationDatafile";
4344
static NSString * const kUserProfileExperimentKey = @"User_Profile_Experiment";
4445
static NSString * const kUserProfileExperimentOriginalVariationKey = @"original";
4546
static NSString * const kUserProfileExperimentTreatmentVariationKey = @"treatment";
4647
static NSData *originalDatafile;
4748
static NSData *updatedDatafile;
49+
static NSData *removedVariationDatafile;
4850

4951
@interface OPTLYUserProfile(test)
5052
@property (nonatomic, strong) OPTLYDataStore *dataStore;
@@ -62,6 +64,7 @@ + (void)setUp {
6264
// load the datafiles
6365
originalDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kOriginalDatafileName];
6466
updatedDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kUpdatedDatafileName];
67+
removedVariationDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kRemovedVariationDatafileName];
6568

6669
// stub all requests
6770
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
@@ -237,7 +240,7 @@ - (void)testBucketingPersistsWhenDatafileIsUpdated {
237240
OPTLYVariation *originalVariation = [originalClient activateExperiment:kUserProfileExperimentKey userId:kUserId1];
238241
XCTAssertNotNil(originalVariation);
239242
XCTAssertEqualObjects(originalVariation.variationKey, kUserProfileExperimentOriginalVariationKey);
240-
243+
XCTAssertNotNil([originalClient.optimizely.userProfile getVariationForUser:kUserId1 experiment:kUserProfileExperimentKey], @"User experiment should be stored");
241244

242245
OPTLYClient *updatedClient = [manager initializeClientWithDatafile:updatedDatafile];
243246
XCTAssertNotNil(updatedClient);
@@ -250,4 +253,38 @@ - (void)testBucketingPersistsWhenDatafileIsUpdated {
250253
XCTAssertEqualObjects(originalVariation.variationId, variationForUser1.variationId);
251254
}
252255

256+
- (void)testStickyBucketingRevertsWhenVariationIsRemoved {
257+
// make sure we have 2 different datafiles
258+
XCTAssertNotNil(originalDatafile);
259+
XCTAssertNotNil(removedVariationDatafile);
260+
XCTAssertNotEqualObjects(originalDatafile, removedVariationDatafile);
261+
262+
// instantiate the manager
263+
OPTLYManager *manager = [OPTLYManager initWithBuilderBlock:^(OPTLYManagerBuilder * _Nullable builder) {
264+
builder.projectId = @"projectId";
265+
__block id<OPTLYLogger> logger = builder.logger;
266+
builder.userProfile = [OPTLYUserProfile initWithBuilderBlock:^(OPTLYUserProfileBuilder * _Nullable builder) {
267+
builder.logger = logger;
268+
}];
269+
}];
270+
XCTAssertNotNil(manager);
271+
272+
OPTLYClient *originalClient = [manager initializeClientWithDatafile:originalDatafile];
273+
XCTAssertNotNil(originalClient);
274+
OPTLYVariation *originalVariation = [originalClient activateExperiment:kUserProfileExperimentKey userId:kUserId1];
275+
XCTAssertNotNil(originalVariation);
276+
XCTAssertEqualObjects(originalVariation.variationKey, kUserProfileExperimentOriginalVariationKey);
277+
XCTAssertNotNil([originalClient.optimizely.userProfile getVariationForUser:kUserId1 experiment:kUserProfileExperimentKey], @"User experiment should be stored");
278+
279+
// update client with a new datafile
280+
OPTLYClient *updatedClient = [manager initializeClientWithDatafile:removedVariationDatafile];
281+
XCTAssertNotNil(updatedClient);
282+
XCTAssertNotNil([updatedClient.optimizely.userProfile getVariationForUser:kUserId1 experiment:kUserProfileExperimentKey], @"User experiment should be same as original client");
283+
OPTLYVariation *variationForUser1 = [updatedClient activateExperiment:kUserProfileExperimentKey userId:kUserId1];
284+
XCTAssertNotNil(variationForUser1);
285+
XCTAssertNotEqualObjects(originalVariation.variationKey, variationForUser1.variationKey);
286+
XCTAssertNotEqualObjects(originalVariation.variationId, variationForUser1.variationId);
287+
XCTAssertEqualObjects(variationForUser1.variationKey, kUserProfileExperimentTreatmentVariationKey, @"treatment should be the new variation since original was removed");
288+
}
289+
253290
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"experiments": [
3+
{
4+
"status": "Running",
5+
"key": "User_Profile_Experiment",
6+
"layerId": "7939966803",
7+
"trafficAllocation": [
8+
{
9+
"entityId": "7954100907",
10+
"endOfRange": 10000
11+
}
12+
],
13+
"audienceIds": [],
14+
"variations": [
15+
{
16+
"variables": [],
17+
"id": "7954100907",
18+
"key": "treatment"
19+
}
20+
],
21+
"forcedVariations": {},
22+
"id": "7926463378"
23+
}
24+
],
25+
"version": "3",
26+
"audiences": [],
27+
"groups": [],
28+
"attributes": [],
29+
"projectId": "7952901082",
30+
"variables": [],
31+
"accountId": "6384711706",
32+
"events": [],
33+
"revision": "4"
34+
}

0 commit comments

Comments
 (0)