Skip to content

Commit

Permalink
test: add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mikechu-optimizely committed Oct 17, 2024
1 parent 73658f6 commit 80e73d4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
52 changes: 49 additions & 3 deletions OptimizelySDK.Tests/OptimizelyUserContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,52 @@ public void DecideWhenConfigIsNull()
Assert.IsTrue(TestData.CompareObjects(decision, decisionExpected));
}

[Test]
public void SeparateDecideShouldHaveSameNumberOfUpsSaveOnlyOneLookup()
{
var experimentFlagKeys = new[] { "double_single_variable_feature", "integer_single_variable_feature" };
// var rolloutFlagKey = "boolean_single_variable_feature";
var userProfileServiceMock = MakeUserProfileServiceMock();
var saveArgsCollector = new List<Dictionary<string, object>>();
userProfileServiceMock.Setup(up => up.Save(Capture.In(saveArgsCollector)));
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
var user = optimizely.CreateUserContext(UserID);
var expectedUserProfileExperiment1 = new UserProfile(UserID, new Dictionary<string, Decision>
{
{ "224", new Decision("280") },
{ "122238", new Decision("122240") },
});
var expectedUserProfileExperiment2 = new UserProfile(UserID, new Dictionary<string, Decision>
{
{ "224", new Decision("280") },
{ "122238", new Decision("122240") },
{ "122241", new Decision("122242") },
});
// var expectedUserProfileRollout = new UserProfile(UserID, new Dictionary<string, Decision>
// {
// // expected decisions for rollout
// });

user.Decide(experimentFlagKeys[0]);
user.Decide(experimentFlagKeys[1]);
// user.Decide(rolloutFlagKey); // TODO: Test UPS with rollout?

LoggerMock.Verify(
l => l.Log(LogLevel.INFO,
"We were unable to get a user profile map from the UserProfileService."),
Times.Never);
LoggerMock.Verify(
l => l.Log(LogLevel.ERROR, "The UserProfileService returned an invalid map."),
Times.Never);
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
Times.Exactly(2));
Assert.AreEqual(saveArgsCollector[0], expectedUserProfileExperiment1.ToMap());
Assert.AreEqual(saveArgsCollector[1], expectedUserProfileExperiment2.ToMap());
// Assert.AreEqual(saveArgsCollector[2], expectedUserProfileRollout.ToMap());
}

[Test]
public void DecideWithUpsShouldOnlyLookupSaveOnce()
{
Expand All @@ -441,7 +487,7 @@ public void DecideWithUpsShouldOnlyLookupSaveOnce()
{ "122238", new Decision("122240") },
});

_ = user.Decide(flagKeyFromTestDataJson);
user.Decide(flagKeyFromTestDataJson);

LoggerMock.Verify(
l => l.Log(LogLevel.INFO,
Expand Down Expand Up @@ -476,7 +522,7 @@ public void DecideForKeysWithUpsShouldOnlyLookupSaveOnceWithMultipleFlags()
{ "122238", new Decision("122240") },
});

_ = userContext.DecideForKeys(flagKeys);
userContext.DecideForKeys(flagKeys);

LoggerMock.Verify(
l => l.Log(LogLevel.INFO,
Expand Down Expand Up @@ -543,7 +589,7 @@ public void DecideAllWithUpsShouldOnlyLookupSaveOnce()
{ "188880", new Decision("188881") },
});

_ = user.DecideAll();
user.DecideAll();

LoggerMock.Verify(
l => l.Log(LogLevel.INFO,
Expand Down
8 changes: 5 additions & 3 deletions OptimizelySDK/Optimizely.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,13 +927,15 @@ internal OptimizelyDecision Decide(OptimizelyUserContext user,
decision = flagDecisionResult.ResultObject;
}

// TODO: Fix when the flag is a rollout instead of an experiment
DecisionService.AddDecisionToUnitOfWork(userId, decision.Experiment?.Id,
new Decision(decision.Variation?.Id ?? ""));

var featureEnabled = false;

if (decision?.Variation != null)
{
featureEnabled = decision.Variation.FeatureEnabled.GetValueOrDefault();
DecisionService.AddDecisionToUnitOfWork(userId, decision.Experiment?.Id,
new Decision(decision.Variation.Id));
}

if (featureEnabled)
Expand Down Expand Up @@ -1077,7 +1079,7 @@ OptimizelyDecideOption[] options
decisionDictionary.Add(key, decision);
}
}

DecisionService.CommitDecisionsToUserProfileService();

return decisionDictionary;
Expand Down

0 comments on commit 80e73d4

Please sign in to comment.