|
| 1 | +// |
| 2 | +// Copyright 2019, 2021, Optimizely, Inc. and contributors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import XCTest |
| 18 | + |
| 19 | +class BucketTests_HoldoutToVariation: XCTestCase { |
| 20 | + var optimizely: OptimizelyClient! |
| 21 | + var config: ProjectConfig! |
| 22 | + var bucketer: DefaultBucketer! |
| 23 | + |
| 24 | + var kUserId = "123456" |
| 25 | + var kHoldoutId = "4444444" |
| 26 | + var kHoldoutKey = "holdout_key" |
| 27 | + |
| 28 | + var kVariationKeyA = "a" |
| 29 | + var kVariationIdA = "a11" |
| 30 | + |
| 31 | + var kAudienceIdCountry = "10" |
| 32 | + var kAudienceIdAge = "20" |
| 33 | + var kAudienceIdInvalid = "9999999" |
| 34 | + |
| 35 | + var kAttributesCountryMatch: [String: Any] = ["country": "us"] |
| 36 | + var kAttributesCountryNotMatch: [String: Any] = ["country": "ca"] |
| 37 | + var kAttributesAgeMatch: [String: Any] = ["age": 30] |
| 38 | + var kAttributesAgeNotMatch: [String: Any] = ["age": 10] |
| 39 | + var kAttributesEmpty: [String: Any] = [:] |
| 40 | + |
| 41 | + var holdout: Holdout! |
| 42 | + |
| 43 | + // MARK: - Sample datafile data |
| 44 | + |
| 45 | + var sampleHoldoutData: [String: Any] { |
| 46 | + return [ |
| 47 | + "status": "Running", |
| 48 | + "id": kHoldoutId, |
| 49 | + "key": kHoldoutKey, |
| 50 | + "layerId": "10420273888", |
| 51 | + "trafficAllocation": [ |
| 52 | + ["entityId": kVariationIdA, "endOfRange": 1000] // 10% traffic allocation (0-1000 out of 10000) |
| 53 | + ], |
| 54 | + "audienceIds": [kAudienceIdCountry], |
| 55 | + "variations": [ |
| 56 | + ["variables": [], "id": kVariationIdA, "key": kVariationKeyA] |
| 57 | + ], |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + // MARK: - Setup |
| 62 | + |
| 63 | + override func setUp() { |
| 64 | + super.setUp() |
| 65 | + |
| 66 | + self.optimizely = OTUtils.createOptimizely(datafileName: "empty_datafile", |
| 67 | + clearUserProfileService: true) |
| 68 | + self.config = self.optimizely.config! |
| 69 | + self.bucketer = ((optimizely.decisionService as! DefaultDecisionService).bucketer as! DefaultBucketer) |
| 70 | + |
| 71 | + // Initialize holdout |
| 72 | + holdout = try! OTUtils.model(from: sampleHoldoutData) |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: - Tests for bucketToVariation |
| 76 | + |
| 77 | + func testBucketToVariation_ValidBucketingWithinAllocation() { |
| 78 | + // Test users that should bucket into the single variation (within 0-1000 range) |
| 79 | + let testCases = [ |
| 80 | + ["userId": "user1", "expectedVariation": kVariationKeyA], // Buckets to variation A |
| 81 | + ["userId": "testuser", "expectedVariation": kVariationKeyA] // Buckets to variation A |
| 82 | + ] |
| 83 | + |
| 84 | + for (index, test) in testCases.enumerated() { |
| 85 | + // Mock bucket value to ensure it falls within 0-1000 |
| 86 | + let mockBucketValue = 500 // Within 10% allocation |
| 87 | + let mockBucketer = Mockbucketer(mockBucketValue: mockBucketValue) |
| 88 | + let response = mockBucketer.bucketToVariation(experiment: holdout, bucketingId: test["userId"]!) |
| 89 | + XCTAssertNotNil(response.result, "Variation should not be nil for test case \(index)") |
| 90 | + XCTAssertEqual(response.result?.key, test["expectedVariation"], "Wrong variation for test case \(index)") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + func testBucketToVariation_BucketValueOutsideAllocation() { |
| 95 | + // Test users that fall outside the 10% allocation (bucket value > 1000) |
| 96 | + let testCases = [ |
| 97 | + ["userId": "user2"], |
| 98 | + ["userId": "anotheruser"] |
| 99 | + ] |
| 100 | + |
| 101 | + for (index, test) in testCases.enumerated() { |
| 102 | + // Mock bucket value to ensure it falls outside 0-1000 |
| 103 | + let mockBucketValue = 1500 // Outside 10% allocation |
| 104 | + let mockBucketer = Mockbucketer(mockBucketValue: mockBucketValue) |
| 105 | + let response = mockBucketer.bucketToVariation(experiment: holdout, bucketingId: test["userId"]!) |
| 106 | + XCTAssertNil(response.result, "Variation should be nil for test case \(index) when outside allocation") |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + func testBucketToVariation_NoTrafficAllocation() { |
| 111 | + // Create a holdout with empty traffic allocation |
| 112 | + var modifiedHoldoutData = sampleHoldoutData |
| 113 | + modifiedHoldoutData["trafficAllocation"] = [] |
| 114 | + let modifiedHoldout = try! OTUtils.model(from: modifiedHoldoutData) as Holdout |
| 115 | + |
| 116 | + let response = bucketer.bucketToVariation(experiment: modifiedHoldout, bucketingId: kUserId) |
| 117 | + |
| 118 | + XCTAssertNil(response.result, "Variation should be nil when no traffic allocation") |
| 119 | + } |
| 120 | + |
| 121 | + func testBucketToVariation_InvalidVariationId() { |
| 122 | + // Create a holdout with invalid variation ID in traffic allocation |
| 123 | + var modifiedHoldoutData = sampleHoldoutData |
| 124 | + modifiedHoldoutData["trafficAllocation"] = [ |
| 125 | + ["entityId": "invalid_variation_id", "endOfRange": 1000] |
| 126 | + ] |
| 127 | + let modifiedHoldout = try! OTUtils.model(from: modifiedHoldoutData) as Holdout |
| 128 | + |
| 129 | + let response = bucketer.bucketToVariation(experiment: modifiedHoldout, bucketingId: kUserId) |
| 130 | + |
| 131 | + XCTAssertNil(response.result, "Variation should be nil for invalid variation ID") |
| 132 | + } |
| 133 | + |
| 134 | + func testBucketToVariation_EmptyBucketingId() { |
| 135 | + // Test with empty bucketing ID, still within allocation |
| 136 | + let mockBucketValue = 500 |
| 137 | + let mockBucketer = Mockbucketer(mockBucketValue: mockBucketValue) |
| 138 | + let response = mockBucketer.bucketToVariation(experiment: holdout, bucketingId: "") |
| 139 | + |
| 140 | + XCTAssertNotNil(response.result, "Should still bucket with empty bucketing ID") |
| 141 | + XCTAssertEqual(response.result?.key, kVariationKeyA, "Should bucket to variation A") |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// MARK: - Helper for mocking bucket value |
| 146 | + |
| 147 | +class Mockbucketer: DefaultBucketer { |
| 148 | + var mockBucketValue: Int |
| 149 | + |
| 150 | + init(mockBucketValue: Int) { |
| 151 | + self.mockBucketValue = mockBucketValue |
| 152 | + super.init() |
| 153 | + } |
| 154 | + |
| 155 | + override func generateBucketValue(bucketingId: String) -> Int { |
| 156 | + print(mockBucketValue) |
| 157 | + return mockBucketValue |
| 158 | + } |
| 159 | +} |
0 commit comments