|
| 1 | +/* |
| 2 | + * Copyright 2025, Optimizely |
| 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 | +using NUnit.Framework; |
| 18 | +using OptimizelySDK.Entity; |
| 19 | +using OptimizelySDK.Utils; |
| 20 | + |
| 21 | +namespace OptimizelySDK.Tests |
| 22 | +{ |
| 23 | + [TestFixture] |
| 24 | + public class HoldoutConfigBasicTests |
| 25 | + { |
| 26 | + [Test] |
| 27 | + public void TestEmptyHoldouts_ShouldHaveEmptyMaps() |
| 28 | + { |
| 29 | + var config = new HoldoutConfig(new Holdout[0]); |
| 30 | + |
| 31 | + Assert.IsNotNull(config.HoldoutIdMap); |
| 32 | + Assert.AreEqual(0, config.HoldoutIdMap.Count); |
| 33 | + Assert.AreEqual(0, config.HoldoutCount); |
| 34 | + } |
| 35 | + |
| 36 | + [Test] |
| 37 | + public void TestHoldoutIdMapping() |
| 38 | + { |
| 39 | + var holdout1 = CreateTestHoldout("holdout_1", "h1"); |
| 40 | + var holdout2 = CreateTestHoldout("holdout_2", "h2"); |
| 41 | + var allHoldouts = new[] { holdout1, holdout2 }; |
| 42 | + var config = new HoldoutConfig(allHoldouts); |
| 43 | + |
| 44 | + Assert.IsNotNull(config.HoldoutIdMap); |
| 45 | + Assert.AreEqual(2, config.HoldoutIdMap.Count); |
| 46 | + Assert.AreEqual(2, config.HoldoutCount); |
| 47 | + |
| 48 | + Assert.IsTrue(config.HoldoutIdMap.ContainsKey("holdout_1")); |
| 49 | + Assert.IsTrue(config.HoldoutIdMap.ContainsKey("holdout_2")); |
| 50 | + |
| 51 | + Assert.AreEqual(holdout1.Id, config.HoldoutIdMap["holdout_1"].Id); |
| 52 | + Assert.AreEqual(holdout2.Id, config.HoldoutIdMap["holdout_2"].Id); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void TestGetHoldoutById() |
| 57 | + { |
| 58 | + var holdout = CreateTestHoldout("holdout_1", "h1"); |
| 59 | + var config = new HoldoutConfig(new[] { holdout }); |
| 60 | + |
| 61 | + var retrieved = config.GetHoldout("holdout_1"); |
| 62 | + |
| 63 | + Assert.IsNotNull(retrieved); |
| 64 | + Assert.AreEqual("holdout_1", retrieved.Id); |
| 65 | + Assert.AreEqual("h1", retrieved.Key); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void TestGetHoldoutById_InvalidId() |
| 70 | + { |
| 71 | + var holdout = CreateTestHoldout("holdout_1", "h1"); |
| 72 | + var config = new HoldoutConfig(new[] { holdout }); |
| 73 | + |
| 74 | + var result = config.GetHoldout("invalid_id"); |
| 75 | + Assert.IsNull(result); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void TestGetHoldoutById_NullId() |
| 80 | + { |
| 81 | + var holdout = CreateTestHoldout("holdout_1", "h1"); |
| 82 | + var config = new HoldoutConfig(new[] { holdout }); |
| 83 | + |
| 84 | + var result = config.GetHoldout(null); |
| 85 | + Assert.IsNull(result); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void TestGetHoldoutById_EmptyId() |
| 90 | + { |
| 91 | + var holdout = CreateTestHoldout("holdout_1", "h1"); |
| 92 | + var config = new HoldoutConfig(new[] { holdout }); |
| 93 | + |
| 94 | + var result = config.GetHoldout(""); |
| 95 | + Assert.IsNull(result); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void TestUpdateHoldoutMapping() |
| 100 | + { |
| 101 | + var holdout1 = CreateTestHoldout("holdout_1", "h1"); |
| 102 | + var config = new HoldoutConfig(new[] { holdout1 }); |
| 103 | + |
| 104 | + // Initial state |
| 105 | + Assert.AreEqual(1, config.HoldoutIdMap.Count); |
| 106 | + Assert.AreEqual(1, config.HoldoutCount); |
| 107 | + |
| 108 | + // Update with new holdouts |
| 109 | + var holdout2 = CreateTestHoldout("holdout_2", "h2"); |
| 110 | + config.UpdateHoldoutMapping(new[] { holdout1, holdout2 }); |
| 111 | + |
| 112 | + Assert.AreEqual(2, config.HoldoutIdMap.Count); |
| 113 | + Assert.AreEqual(2, config.HoldoutCount); |
| 114 | + Assert.IsTrue(config.HoldoutIdMap.ContainsKey("holdout_1")); |
| 115 | + Assert.IsTrue(config.HoldoutIdMap.ContainsKey("holdout_2")); |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + public void TestNullHoldouts() |
| 120 | + { |
| 121 | + var config = new HoldoutConfig(null); |
| 122 | + |
| 123 | + Assert.IsNotNull(config.HoldoutIdMap); |
| 124 | + Assert.AreEqual(0, config.HoldoutIdMap.Count); |
| 125 | + Assert.AreEqual(0, config.HoldoutCount); |
| 126 | + } |
| 127 | + |
| 128 | + // Helper method to create test holdouts |
| 129 | + private Holdout CreateTestHoldout(string id, string key) |
| 130 | + { |
| 131 | + return new Holdout |
| 132 | + { |
| 133 | + Id = id, |
| 134 | + Key = key, |
| 135 | + Status = "Running", |
| 136 | + Variations = new Variation[0], |
| 137 | + TrafficAllocation = new TrafficAllocation[0], |
| 138 | + AudienceIds = new string[0], |
| 139 | + AudienceConditions = null |
| 140 | + }; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments