Skip to content

Commit

Permalink
test: WIP add coverage for single Lookup & Save via USP
Browse files Browse the repository at this point in the history
I'm hitting `Lookup` thrice for some reason in DecideAllWithUspShouldOnlyLookupSaveOnce
  • Loading branch information
mikechu-optimizely committed Oct 7, 2024
1 parent 2b17438 commit c9047a0
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions OptimizelySDK.Tests/OptimizelyUserContextTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021, 2022-2023 Optimizely and contributors
* Copyright 2020-2024 Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@

using System;
using System.Collections.Generic;
using System.Threading;
using Castle.Core.Internal;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -193,7 +192,7 @@ public void SetAttributeToOverrideAttribute()
Assert.AreEqual(user.GetAttributes()["k1"], true);
}

#region decide
#region Decide

[Test]
public void TestDecide()
Expand Down Expand Up @@ -408,11 +407,43 @@ public void DecideWhenConfigIsNull()

Assert.IsTrue(TestData.CompareObjects(decision, decisionExpected));
}

[Test]
public void DecideWithUspShouldOnlyLookupSaveOnce()
{
var flagKeyFromTestDataJson = "double_single_variable_feature";
var userProfileServiceMock = new Mock<UserProfileService>();
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
var user = optimizely.CreateUserContext(UserID);

_ = user.Decide(flagKeyFromTestDataJson);

userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
Times.Once);
}

#endregion decide
#endregion Decide

#region decideAll
#region DecideForKeys

[Test]
public void DecideForKeysWithUspShouldOnlyLookupSaveOnceWithMultipleFlags()
{
var flagKeys = new [] { "double_single_variable_feature", "boolean_feature" };
var userProfileServiceMock = new Mock<UserProfileService>();
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
var userContext = optimizely.CreateUserContext(UserID);

_ = userContext.DecideForKeys(flagKeys);

userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
Times.Once);
}

[Test]
public void DecideForKeysWithOneFlag()
{
Expand Down Expand Up @@ -442,6 +473,25 @@ public void DecideForKeysWithOneFlag()
new string[0]);
Assert.IsTrue(TestData.CompareObjects(decision, expDecision));
}

#endregion DecideForKeys

#region DecideAll

[Test]
public void DecideAllWithUspShouldOnlyLookupSaveOnce()
{
var userProfileServiceMock = new Mock<UserProfileService>();
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
var user = optimizely.CreateUserContext(UserID);

_ = user.DecideAll();

userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
Times.Once);
}

[Test]
public void DecideAllTwoFlag()
Expand Down

0 comments on commit c9047a0

Please sign in to comment.