Skip to content

Commit 9ca271c

Browse files
test: finish coverage
1 parent 57c1ccd commit 9ca271c

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

OptimizelySDK.Tests/OptimizelyUserContextTest.cs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ public void SetUp()
6161
LoggerMock.Object, ErrorHandlerMock.Object);
6262
}
6363

64+
private Mock<UserProfileService> makeUserProfileServiceMock()
65+
{
66+
var projectConfig = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object,
67+
ErrorHandlerMock.Object);
68+
var experiment = projectConfig.Experiments[8];
69+
var variation = experiment.Variations[0];
70+
var decision = new Decision(variation.Id);
71+
var userProfile = new UserProfile(UserID, new Dictionary<string, Decision>
72+
{
73+
{ experiment.Id, decision },
74+
});
75+
var userProfileServiceMock = new Mock<UserProfileService>();
76+
userProfileServiceMock.Setup(up => up.Lookup(UserID)).Returns(userProfile.ToMap());
77+
return userProfileServiceMock;
78+
}
79+
6480
[Test]
6581
public void OptimizelyUserContextWithAttributes()
6682
{
@@ -407,18 +423,25 @@ public void DecideWhenConfigIsNull()
407423

408424
Assert.IsTrue(TestData.CompareObjects(decision, decisionExpected));
409425
}
410-
426+
411427
[Test]
412428
public void DecideWithUspShouldOnlyLookupSaveOnce()
413429
{
414430
var flagKeyFromTestDataJson = "double_single_variable_feature";
415-
var userProfileServiceMock = new Mock<UserProfileService>();
431+
var userProfileServiceMock = makeUserProfileServiceMock();
416432
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
417433
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
418434
var user = optimizely.CreateUserContext(UserID);
419-
435+
420436
_ = user.Decide(flagKeyFromTestDataJson);
421-
437+
438+
LoggerMock.Verify(
439+
l => l.Log(LogLevel.INFO,
440+
"We were unable to get a user profile map from the UserProfileService."),
441+
Times.Never);
442+
LoggerMock.Verify(
443+
l => l.Log(LogLevel.ERROR, "The UserProfileService returned an invalid map."),
444+
Times.Never);
422445
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
423446
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
424447
Times.Once);
@@ -431,19 +454,26 @@ public void DecideWithUspShouldOnlyLookupSaveOnce()
431454
[Test]
432455
public void DecideForKeysWithUspShouldOnlyLookupSaveOnceWithMultipleFlags()
433456
{
434-
var flagKeys = new [] { "double_single_variable_feature", "boolean_feature" };
435-
var userProfileServiceMock = new Mock<UserProfileService>();
457+
var flagKeys = new[] { "double_single_variable_feature", "boolean_feature" };
458+
var userProfileServiceMock = makeUserProfileServiceMock();
436459
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
437460
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
438461
var userContext = optimizely.CreateUserContext(UserID);
439-
462+
440463
_ = userContext.DecideForKeys(flagKeys);
441-
464+
465+
LoggerMock.Verify(
466+
l => l.Log(LogLevel.INFO,
467+
"We were unable to get a user profile map from the UserProfileService."),
468+
Times.Never);
469+
LoggerMock.Verify(
470+
l => l.Log(LogLevel.ERROR, "The UserProfileService returned an invalid map."),
471+
Times.Never);
442472
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
443473
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
444474
Times.Once);
445475
}
446-
476+
447477
[Test]
448478
public void DecideForKeysWithOneFlag()
449479
{
@@ -473,21 +503,28 @@ public void DecideForKeysWithOneFlag()
473503
new string[0]);
474504
Assert.IsTrue(TestData.CompareObjects(decision, expDecision));
475505
}
476-
506+
477507
#endregion DecideForKeys
478-
508+
479509
#region DecideAll
480-
510+
481511
[Test]
482512
public void DecideAllWithUspShouldOnlyLookupSaveOnce()
483513
{
484-
var userProfileServiceMock = new Mock<UserProfileService>();
514+
var userProfileServiceMock = makeUserProfileServiceMock();
485515
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
486516
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
487517
var user = optimizely.CreateUserContext(UserID);
488-
518+
489519
_ = user.DecideAll();
490-
520+
521+
LoggerMock.Verify(
522+
l => l.Log(LogLevel.INFO,
523+
"We were unable to get a user profile map from the UserProfileService."),
524+
Times.Never);
525+
LoggerMock.Verify(
526+
l => l.Log(LogLevel.ERROR, "The UserProfileService returned an invalid map."),
527+
Times.Never);
491528
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
492529
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
493530
Times.Once);

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class DecisionService
4545
private ILogger Logger;
4646
private UserProfile _userProfile;
4747

48-
private bool _decisionBatchInProgress = false;
48+
private bool _decisionBatchInProgress;
4949

5050
public bool DecisionBatchInProgress
5151
{

0 commit comments

Comments
 (0)