Skip to content

Commit c9047a0

Browse files
test: WIP add coverage for single Lookup & Save via USP
I'm hitting `Lookup` thrice for some reason in DecideAllWithUspShouldOnlyLookupSaveOnce
1 parent 2b17438 commit c9047a0

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

OptimizelySDK.Tests/OptimizelyUserContextTest.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021, 2022-2023 Optimizely and contributors
2+
* Copyright 2020-2024 Optimizely and contributors
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.
@@ -16,7 +16,6 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Threading;
2019
using Castle.Core.Internal;
2120
using Moq;
2221
using NUnit.Framework;
@@ -193,7 +192,7 @@ public void SetAttributeToOverrideAttribute()
193192
Assert.AreEqual(user.GetAttributes()["k1"], true);
194193
}
195194

196-
#region decide
195+
#region Decide
197196

198197
[Test]
199198
public void TestDecide()
@@ -408,11 +407,43 @@ public void DecideWhenConfigIsNull()
408407

409408
Assert.IsTrue(TestData.CompareObjects(decision, decisionExpected));
410409
}
410+
411+
[Test]
412+
public void DecideWithUspShouldOnlyLookupSaveOnce()
413+
{
414+
var flagKeyFromTestDataJson = "double_single_variable_feature";
415+
var userProfileServiceMock = new Mock<UserProfileService>();
416+
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
417+
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
418+
var user = optimizely.CreateUserContext(UserID);
419+
420+
_ = user.Decide(flagKeyFromTestDataJson);
421+
422+
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
423+
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
424+
Times.Once);
425+
}
411426

412-
#endregion decide
427+
#endregion Decide
413428

414-
#region decideAll
429+
#region DecideForKeys
415430

431+
[Test]
432+
public void DecideForKeysWithUspShouldOnlyLookupSaveOnceWithMultipleFlags()
433+
{
434+
var flagKeys = new [] { "double_single_variable_feature", "boolean_feature" };
435+
var userProfileServiceMock = new Mock<UserProfileService>();
436+
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
437+
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
438+
var userContext = optimizely.CreateUserContext(UserID);
439+
440+
_ = userContext.DecideForKeys(flagKeys);
441+
442+
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
443+
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
444+
Times.Once);
445+
}
446+
416447
[Test]
417448
public void DecideForKeysWithOneFlag()
418449
{
@@ -442,6 +473,25 @@ public void DecideForKeysWithOneFlag()
442473
new string[0]);
443474
Assert.IsTrue(TestData.CompareObjects(decision, expDecision));
444475
}
476+
477+
#endregion DecideForKeys
478+
479+
#region DecideAll
480+
481+
[Test]
482+
public void DecideAllWithUspShouldOnlyLookupSaveOnce()
483+
{
484+
var userProfileServiceMock = new Mock<UserProfileService>();
485+
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
486+
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
487+
var user = optimizely.CreateUserContext(UserID);
488+
489+
_ = user.DecideAll();
490+
491+
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
492+
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
493+
Times.Once);
494+
}
445495

446496
[Test]
447497
public void DecideAllTwoFlag()

0 commit comments

Comments
 (0)