Skip to content

Commit 6d60cf3

Browse files
authored
Feat: Added unit tests for reasons addInfo logs (#254)
1 parent 5754452 commit 6d60cf3

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

OptimizelySDK.Tests/BucketerTest.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017, 2019, Optimizely
2+
* Copyright 2017, 2019-2021, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,7 +61,7 @@ public override string ToString()
6161
public void Initialize()
6262
{
6363
LoggerMock = new Mock<ILogger>();
64-
DecisionReasons = DefaultDecisionReasons.NewInstance();
64+
DecisionReasons = DefaultDecisionReasons.NewInstance(new OptimizelyDecideOption[] { OptimizelyDecideOption.INCLUDE_REASONS });
6565
Config = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object, new ErrorHandler.NoOpErrorHandler());
6666
}
6767

@@ -105,13 +105,16 @@ public void TestBucketValidExperimentNotInGroup()
105105
LoggerMock.Verify(l => l.Log(It.IsAny<LogLevel>(), It.IsAny<string>()), Times.Exactly(2));
106106
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Assigned bucket [3000] to user [testUserId] with bucketing ID [testBucketingIdControl!]."));
107107
LoggerMock.Verify(l => l.Log(LogLevel.INFO, "User [testUserId] is in variation [control] of experiment [test_experiment]."));
108+
Assert.AreEqual(DecisionReasons.ToReport()[0], "User [testUserId] is in variation [control] of experiment [test_experiment].");
109+
108110
// variation
109111
Assert.AreEqual(new Variation { Id = "7721010009", Key = "variation" },
110112
bucketer.Bucket(Config, Config.GetExperimentFromKey("test_experiment"), TestBucketingIdControl, TestUserId, DecisionReasons));
111113

112114
LoggerMock.Verify(l => l.Log(It.IsAny<LogLevel>(), It.IsAny<string>()), Times.Exactly(4));
113115
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Assigned bucket [7000] to user [testUserId] with bucketing ID [testBucketingIdControl!]."));
114116
LoggerMock.Verify(l => l.Log(LogLevel.INFO, "User [testUserId] is in variation [variation] of experiment [test_experiment]."));
117+
Assert.AreEqual(DecisionReasons.ToReport()[1], "User [testUserId] is in variation [variation] of experiment [test_experiment].");
115118

116119
// no variation
117120
Assert.AreEqual(new Variation { },
@@ -120,7 +123,9 @@ public void TestBucketValidExperimentNotInGroup()
120123
LoggerMock.Verify(l => l.Log(It.IsAny<LogLevel>(), It.IsAny<string>()), Times.Exactly(6));
121124
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Assigned bucket [9000] to user [testUserId] with bucketing ID [testBucketingIdControl!]."));
122125
LoggerMock.Verify(l => l.Log(LogLevel.INFO, "User [testUserId] is in no variation."));
123-
Assert.AreEqual(DecisionReasons.ToReport().Count, 0);
126+
Assert.AreEqual(DecisionReasons.ToReport()[2], "User [testUserId] is in no variation.");
127+
128+
Assert.AreEqual(DecisionReasons.ToReport().Count, 3);
124129
}
125130

126131
[Test]
@@ -155,7 +160,12 @@ public void TestBucketValidExperimentInGroup()
155160
LoggerMock.Verify(l => l.Log(LogLevel.INFO, "User [testUserId] is not in experiment [group_experiment_1] of group [7722400015]."));
156161

157162
LoggerMock.Verify(l => l.Log(It.IsAny<LogLevel>(), It.IsAny<string>()), Times.Exactly(10));
158-
Assert.AreEqual(DecisionReasons.ToReport().Count, 0);
163+
Assert.AreEqual(DecisionReasons.ToReport().Count, 5);
164+
Assert.AreEqual(DecisionReasons.ToReport()[0], "User [testUserId] is in experiment [group_experiment_1] of group [7722400015].");
165+
Assert.AreEqual(DecisionReasons.ToReport()[1], "User [testUserId] is in variation [group_exp_1_var_1] of experiment [group_experiment_1].");
166+
Assert.AreEqual(DecisionReasons.ToReport()[2], "User [testUserId] is in experiment [group_experiment_1] of group [7722400015].");
167+
Assert.AreEqual(DecisionReasons.ToReport()[3], "User [testUserId] is in variation [group_exp_1_var_2] of experiment [group_experiment_1].");
168+
Assert.AreEqual(DecisionReasons.ToReport()[4], "User [testUserId] is not in experiment [group_experiment_1] of group [7722400015].");
159169
}
160170

161171
[Test]
@@ -180,7 +190,8 @@ public void TestBucketWithBucketingId()
180190
// make sure that the bucketing ID is used for the variation bucketing and not the user ID
181191
Assert.AreEqual(expectedVariation,
182192
bucketer.Bucket(Config, experiment, TestBucketingIdControl, TestUserIdBucketsToVariation, DecisionReasons));
183-
Assert.AreEqual(DecisionReasons.ToReport().Count, 0);
193+
Assert.AreEqual(DecisionReasons.ToReport().Count, 1);
194+
Assert.AreEqual(DecisionReasons.ToReport()[0], "User [bucketsToVariation!] is in variation [control] of experiment [test_experiment].");
184195
}
185196

186197
// Test for invalid experiment keys, null variation should be returned
@@ -206,7 +217,9 @@ public void TestBucketVariationGroupedExperimentsWithBucketingId()
206217
Assert.AreEqual(expectedGroupVariation,
207218
bucketer.Bucket(Config, Config.GetExperimentFromKey("group_experiment_2"),
208219
TestBucketingIdGroupExp2Var2, TestUserIdBucketsToNoGroup, DecisionReasons));
209-
Assert.AreEqual(DecisionReasons.ToReport().Count, 0);
220+
Assert.AreEqual(DecisionReasons.ToReport().Count, 2);
221+
Assert.AreEqual(DecisionReasons.ToReport()[0], "User [testUserId] is in experiment [group_experiment_2] of group [7722400015].");
222+
Assert.AreEqual(DecisionReasons.ToReport()[1], "User [testUserId] is in variation [group_exp_2_var_2] of experiment [group_experiment_2].");
210223
}
211224

212225
// Make sure that user gets bucketed into the rollout rule.
@@ -220,7 +233,8 @@ public void TestBucketRolloutRule()
220233

221234
Assert.True(TestData.CompareObjects(expectedVariation,
222235
bucketer.Bucket(Config, rolloutRule, "testBucketingId", TestUserId, DecisionReasons)));
223-
Assert.AreEqual(DecisionReasons.ToReport().Count, 0);
236+
Assert.AreEqual(DecisionReasons.ToReport().Count, 1);
237+
Assert.AreEqual(DecisionReasons.ToReport()[0], "User [testUserId] is in variation [177773] of experiment [177772].");
224238
}
225239
}
226240
}

OptimizelySDK.Tests/DecisionServiceTest.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2017-2020, Optimizely and contributors
3+
* Copyright 2017-2021, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -87,7 +87,25 @@ public void TestGetVariationForcedVariationPrecedesAudienceEval()
8787
Assert.IsTrue(TestData.CompareObjects(actualVariation, expectedVariation));
8888
BucketerMock.Verify(_ => _.Bucket(It.IsAny<ProjectConfig>(), It.IsAny<Experiment>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IDecisionReasons>()), Times.Never);
8989
}
90+
[Test]
91+
public void TestGetVariationLogsErrorWhenUserProfileMapItsNull()
92+
{
93+
Experiment experiment = ProjectConfig.Experiments[8];
94+
Variation variation = experiment.Variations[0];
95+
96+
Decision decision = new Decision(variation.Id);
97+
Dictionary<string, object> userProfile = null;
98+
99+
UserProfileServiceMock.Setup(up => up.Lookup(WhitelistedUserId)).Returns(userProfile);
90100

101+
DecisionService decisionService = new DecisionService(BucketerMock.Object, ErrorHandlerMock.Object, UserProfileServiceMock.Object, LoggerMock.Object);
102+
var options = new OptimizelyDecideOption[] { OptimizelyDecideOption.INCLUDE_REASONS };
103+
var reasons = DefaultDecisionReasons.NewInstance(new OptimizelyDecideOption[] { OptimizelyDecideOption.INCLUDE_REASONS }); ;
104+
decisionService.GetVariation(experiment, GenericUserId, ProjectConfig, new UserAttributes(), options, reasons);
105+
Assert.AreEqual(reasons.ToReport()[0], "We were unable to get a user profile map from the UserProfileService.");
106+
Assert.AreEqual(reasons.ToReport()[1], "User \"genericUserId\" does not meet conditions to be in experiment \"etag3\".");
107+
}
108+
91109
[Test]
92110
public void TestGetVariationEvaluatesUserProfileBeforeAudienceTargeting()
93111
{

OptimizelySDK.Tests/UtilsTests/ExperimentUtilsTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2020, Optimizely
2+
* Copyright 2019-2021, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
using OptimizelySDK.Config;
2020
using OptimizelySDK.Entity;
2121
using OptimizelySDK.Logger;
22+
using OptimizelySDK.OptimizelyDecisions;
2223
using OptimizelySDK.Utils;
2324

2425
namespace OptimizelySDK.Tests.UtilsTests
@@ -58,10 +59,11 @@ public void TestDoesUserMeetAudienceConditionsReturnsTrueWhenAudienceUsedInExper
5859
{
5960
var experiment = Config.GetExperimentFromKey("feat_with_var_test");
6061
experiment.AudienceIds = new string[] { "3468206648" };
61-
62+
var decisionReasons = DefaultDecisionReasons.NewInstance(new OptimizelyDecideOption[] { OptimizelyDecideOption.INCLUDE_REASONS });
6263
Assert.True(ExperimentUtils.DoesUserMeetAudienceConditions(Config, experiment, null , "experiment", experiment.Key, Logger));;
63-
Assert.True(ExperimentUtils.DoesUserMeetAudienceConditions(Config, experiment, new UserAttributes { }, "experiment", experiment.Key, Logger));
64+
Assert.True(ExperimentUtils.DoesUserMeetAudienceConditions(Config, experiment, new UserAttributes { }, "experiment", experiment.Key, decisionReasons, Logger));
6465

66+
Assert.AreEqual(decisionReasons.ToReport()[0], "Audiences for experiment \"feat_with_var_test\" collectively evaluated to TRUE");
6567
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, @"Evaluating audiences for experiment ""feat_with_var_test"": [""3468206648""]."), Times.Exactly(2));
6668
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, @"Starting to evaluate audience ""3468206648"" with conditions: [""not"",{""name"":""input_value"",""type"":""custom_attribute"",""match"":""exists""}]"), Times.Exactly(2));
6769
LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, $@"Audience ""3468206648"" evaluated to TRUE"), Times.Exactly(2));

0 commit comments

Comments
 (0)