|
| 1 | +// |
| 2 | +// Copyright 2022, 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_BucketToEntity: XCTestCase { |
| 20 | + var optimizely: OptimizelyClient! |
| 21 | + var config: ProjectConfig! |
| 22 | + var bucketer: DefaultBucketer! |
| 23 | + |
| 24 | + var kUserId = "12345" |
| 25 | + var kGroupId = "333333" |
| 26 | + var kExperimentId = "444444" |
| 27 | + |
| 28 | + var kExperimentKey = "countryExperiment" |
| 29 | + |
| 30 | + var kVariationKeyA = "a" |
| 31 | + var kVariationKeyB = "b" |
| 32 | + var kVariationKeyC = "c" |
| 33 | + var kVariationKeyD = "d" |
| 34 | + |
| 35 | + var kVariationIdA = "a11" |
| 36 | + var kVariationIdB = "b11" |
| 37 | + var kVariationIdC = "c11" |
| 38 | + var kVariationIdD = "d11" |
| 39 | + |
| 40 | + var kAudienceIdCountry = "10" |
| 41 | + var kAudienceIdAge = "20" |
| 42 | + var kAudienceIdInvalid = "9999999" |
| 43 | + |
| 44 | + var kAttributesCountryMatch: [String: Any] = ["country": "us"] |
| 45 | + var kAttributesCountryNotMatch: [String: Any] = ["country": "ca"] |
| 46 | + var kAttributesAgeMatch: [String: Any] = ["age": 30] |
| 47 | + var kAttributesAgeNotMatch: [String: Any] = ["age": 10] |
| 48 | + var kAttributesEmpty: [String: Any] = [:] |
| 49 | + |
| 50 | + var experiment: Experiment! |
| 51 | + |
| 52 | + // MARK: - Sample datafile data |
| 53 | + |
| 54 | + var sampleExperimentData: [String: Any] { return |
| 55 | + [ |
| 56 | + "status": "Running", |
| 57 | + "id": kExperimentId, |
| 58 | + "key": kExperimentKey, |
| 59 | + "layerId": "10420273888", |
| 60 | + "trafficAllocation": [ |
| 61 | + ["entityId": kVariationIdA, "endOfRange": 2500], |
| 62 | + ["entityId": kVariationIdB, "endOfRange": 5000], |
| 63 | + ["entityId": kVariationIdC, "endOfRange": 7500], |
| 64 | + ["entityId": kVariationIdD, "endOfRange": 10000] |
| 65 | + ], |
| 66 | + "audienceIds": [kAudienceIdCountry], |
| 67 | + "variations": [ |
| 68 | + [ |
| 69 | + "variables": [], |
| 70 | + "id": kVariationIdA, |
| 71 | + "key": kVariationKeyA |
| 72 | + ], |
| 73 | + [ |
| 74 | + "variables": [], |
| 75 | + "id": kVariationIdB, |
| 76 | + "key": kVariationKeyB |
| 77 | + ], |
| 78 | + [ |
| 79 | + "variables": [], |
| 80 | + "id": kVariationIdC, |
| 81 | + "key": kVariationKeyC |
| 82 | + ], |
| 83 | + [ |
| 84 | + "variables": [], |
| 85 | + "id": kVariationIdD, |
| 86 | + "key": kVariationKeyD |
| 87 | + ] |
| 88 | + ], |
| 89 | + "forcedVariations": [:] |
| 90 | + ] |
| 91 | + } |
| 92 | + |
| 93 | + var sampleGroupData: [String: Any] { return |
| 94 | + ["id": kGroupId, |
| 95 | + "policy": "random", |
| 96 | + "trafficAllocation": [ |
| 97 | + ["entityId": kExperimentId, "endOfRange": 10000] |
| 98 | + ], |
| 99 | + "experiments": [sampleExperimentData] |
| 100 | + ] |
| 101 | + } |
| 102 | + |
| 103 | + // MARK: - Setup |
| 104 | + |
| 105 | + override func setUp() { |
| 106 | + super.setUp() |
| 107 | + |
| 108 | + self.optimizely = OTUtils.createOptimizely(datafileName: "empty_datafile", |
| 109 | + clearUserProfileService: true) |
| 110 | + self.config = self.optimizely.config |
| 111 | + self.bucketer = ((optimizely.decisionService as! DefaultDecisionService).bucketer as! DefaultBucketer) |
| 112 | + } |
| 113 | + |
| 114 | + func testBucketToEntityWithEmptyGroup() { |
| 115 | + experiment = try! OTUtils.model(from: sampleExperimentData) |
| 116 | + self.config.project.experiments = [experiment] |
| 117 | + |
| 118 | + self.config.project.groups = [] |
| 119 | + |
| 120 | + let fullAllocation = TrafficAllocation(entityId: "entity_123", endOfRange: 10000) |
| 121 | + let bucketedEntityId = bucketer.bucketToEntityId(config: config, experiment: experiment, bucketingId: "id_123", trafficAllocation: [fullAllocation]).result |
| 122 | + XCTAssertEqual(bucketedEntityId, "entity_123") |
| 123 | + |
| 124 | + let zeroAllocation = TrafficAllocation(entityId: "entity_123", endOfRange: 0) |
| 125 | + let nilEntityId = bucketer.bucketToEntityId(config: config, experiment: experiment, bucketingId: "id_123", trafficAllocation: [zeroAllocation]).result |
| 126 | + XCTAssertEqual(nilEntityId, nil) |
| 127 | + } |
| 128 | + |
| 129 | + func testBucketToEntityWithGroupMatched() { |
| 130 | + experiment = try! OTUtils.model(from: sampleExperimentData) |
| 131 | + self.config.project.experiments = [experiment] |
| 132 | + |
| 133 | + let group: Group = try! OTUtils.model(from: sampleGroupData) |
| 134 | + self.config.project.groups = [group] |
| 135 | + |
| 136 | + let tests = [["userId": "ppid1", "entityId": "entity1", "expect": "entity1"], |
| 137 | + ["userId": "ppid2", "entityId": "entity2", "expect": "entity2"], |
| 138 | + ["userId": "ppid3", "entityId": "entity3", "expect": "entity3"], |
| 139 | + ["userId": "a very very very very very very very very very very very very very very very long ppd string", "entityId": "entity4", "expect": "entity4"]] |
| 140 | + |
| 141 | + var entityId: String! |
| 142 | + |
| 143 | + for (idx, test) in tests.enumerated() { |
| 144 | + entityId = bucketer.bucketToEntityId(config: config, experiment: experiment, bucketingId: test["userId"]!, trafficAllocation: [TrafficAllocation(entityId: test["entityId"]!, endOfRange: 10000)]).result |
| 145 | + XCTAssertEqual(test["expect"], entityId, "test[\(idx)] failed") |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + func testBucketToEntityWithGroupNotMatched() { |
| 150 | + experiment = try! OTUtils.model(from: sampleExperimentData) |
| 151 | + self.config.project.experiments = [experiment] |
| 152 | + |
| 153 | + var group: Group = try! OTUtils.model(from: sampleGroupData) |
| 154 | + group.trafficAllocation[0].endOfRange = 0 |
| 155 | + self.config.project.groups = [group] |
| 156 | + |
| 157 | + let tests = [["userId": "ppid1", "entityId": "entity1", "expect": "entity1"], |
| 158 | + ["userId": "ppid2", "entityId": "entity2", "expect": "entity2"], |
| 159 | + ["userId": "ppid3", "entityId": "entity3", "expect": "entity3"], |
| 160 | + ["userId": "a very very very very very very very very very very very very very very very long ppd string", "entityId": "entity4", "expect": "entity4"]] |
| 161 | + |
| 162 | + for (_, test) in tests.enumerated() { |
| 163 | + let response: DecisionResponse<String> = bucketer.bucketToEntityId(config: config, experiment: experiment, bucketingId: test["userId"]!, trafficAllocation: [TrafficAllocation(entityId: test["entityId"]!, endOfRange: 10000)]) |
| 164 | + XCTAssertEqual(response.result, nil) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + func testBucketToEntityWithNoRandoomGroup() { |
| 169 | + experiment = try! OTUtils.model(from: sampleExperimentData) |
| 170 | + self.config.project.experiments = [experiment] |
| 171 | + |
| 172 | + var group: Group = try! OTUtils.model(from: sampleGroupData) |
| 173 | + group.policy = .overlapping |
| 174 | + self.config.project.groups = [group] |
| 175 | + |
| 176 | + let tests = [["userId": "ppid1", "entityId": "entity1", "expect": "entity1"], |
| 177 | + ["userId": "ppid2", "entityId": "entity2", "expect": "entity2"], |
| 178 | + ["userId": "ppid3", "entityId": "entity3", "expect": "entity3"], |
| 179 | + ["userId": "a very very very very very very very very very very very very very very very long ppd string", "entityId": "entity4", "expect": "entity4"]] |
| 180 | + |
| 181 | + var entityId: String! |
| 182 | + |
| 183 | + for (idx, test) in tests.enumerated() { |
| 184 | + entityId = bucketer.bucketToEntityId(config: config, experiment: experiment, bucketingId: test["userId"]!, trafficAllocation: [TrafficAllocation(entityId: test["entityId"]!, endOfRange: 10000)]).result |
| 185 | + XCTAssertEqual(test["expect"], entityId, "test[\(idx)] failed") |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments