Skip to content

Commit c6889e8

Browse files
test: add content verification of UPS Save()
1 parent 5b7e5ae commit c6889e8

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

OptimizelySDK.Tests/OptimizelyUserContextTest.cs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using System.Linq;
1920
using Castle.Core.Internal;
2021
using Moq;
2122
using NUnit.Framework;
@@ -61,7 +62,7 @@ public void SetUp()
6162
LoggerMock.Object, ErrorHandlerMock.Object);
6263
}
6364

64-
private Mock<UserProfileService> makeUserProfileServiceMock()
65+
private Mock<UserProfileService> MakeUserProfileServiceMock()
6566
{
6667
var projectConfig = DatafileProjectConfig.Create(TestData.Datafile, LoggerMock.Object,
6768
ErrorHandlerMock.Object);
@@ -425,13 +426,20 @@ public void DecideWhenConfigIsNull()
425426
}
426427

427428
[Test]
428-
public void DecideWithUspShouldOnlyLookupSaveOnce()
429+
public void DecideWithUpsShouldOnlyLookupSaveOnce()
429430
{
430431
var flagKeyFromTestDataJson = "double_single_variable_feature";
431-
var userProfileServiceMock = makeUserProfileServiceMock();
432+
var userProfileServiceMock = MakeUserProfileServiceMock();
433+
var saveArgsCollector = new List<Dictionary<string, object>>();
434+
userProfileServiceMock.Setup(up => up.Save(Capture.In(saveArgsCollector)));
432435
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
433436
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
434437
var user = optimizely.CreateUserContext(UserID);
438+
var expectedUserProfile = new UserProfile(UserID, new Dictionary<string, Decision>
439+
{
440+
{ "224", new Decision("280") },
441+
{ "122238", new Decision("122240") },
442+
});
435443

436444
_ = user.Decide(flagKeyFromTestDataJson);
437445

@@ -445,20 +453,28 @@ public void DecideWithUspShouldOnlyLookupSaveOnce()
445453
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
446454
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
447455
Times.Once);
456+
Assert.AreEqual(saveArgsCollector.First(), expectedUserProfile.ToMap());
448457
}
449458

450459
#endregion Decide
451460

452461
#region DecideForKeys
453462

454463
[Test]
455-
public void DecideForKeysWithUspShouldOnlyLookupSaveOnceWithMultipleFlags()
464+
public void DecideForKeysWithUpsShouldOnlyLookupSaveOnceWithMultipleFlags()
456465
{
457466
var flagKeys = new[] { "double_single_variable_feature", "boolean_feature" };
458-
var userProfileServiceMock = makeUserProfileServiceMock();
467+
var userProfileServiceMock = MakeUserProfileServiceMock();
468+
var saveArgsCollector = new List<Dictionary<string, object>>();
469+
userProfileServiceMock.Setup(up => up.Save(Capture.In(saveArgsCollector)));
459470
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
460471
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
461472
var userContext = optimizely.CreateUserContext(UserID);
473+
var expectedUserProfile = new UserProfile(UserID, new Dictionary<string, Decision>
474+
{
475+
{ "224", new Decision("280") },
476+
{ "122238", new Decision("122240") },
477+
});
462478

463479
_ = userContext.DecideForKeys(flagKeys);
464480

@@ -472,6 +488,7 @@ public void DecideForKeysWithUspShouldOnlyLookupSaveOnceWithMultipleFlags()
472488
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
473489
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
474490
Times.Once);
491+
Assert.AreEqual(saveArgsCollector.First(), expectedUserProfile.ToMap());
475492
}
476493

477494
[Test]
@@ -507,14 +524,23 @@ public void DecideForKeysWithOneFlag()
507524
#endregion DecideForKeys
508525

509526
#region DecideAll
510-
527+
511528
[Test]
512-
public void DecideAllWithUspShouldOnlyLookupSaveOnce()
529+
public void DecideAllWithUpsShouldOnlyLookupSaveOnce()
513530
{
514-
var userProfileServiceMock = makeUserProfileServiceMock();
531+
var userProfileServiceMock = MakeUserProfileServiceMock();
532+
var saveArgsCollector = new List<Dictionary<string, object>>();
533+
userProfileServiceMock.Setup(up => up.Save(Capture.In(saveArgsCollector)));
515534
var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,
516535
LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);
517536
var user = optimizely.CreateUserContext(UserID);
537+
var expectedUserProfile = new UserProfile(UserID, new Dictionary<string, Decision>
538+
{
539+
{ "224", new Decision("280") },
540+
{ "122238", new Decision("122240") },
541+
{ "122241", new Decision("122242") },
542+
{ "122235", new Decision("122236") },
543+
});
518544

519545
_ = user.DecideAll();
520546

@@ -526,8 +552,8 @@ public void DecideAllWithUspShouldOnlyLookupSaveOnce()
526552
l => l.Log(LogLevel.ERROR, "The UserProfileService returned an invalid map."),
527553
Times.Never);
528554
userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);
529-
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),
530-
Times.Once);
555+
userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string,object>>()), Times.Once);
556+
Assert.AreEqual(saveArgsCollector.First(), expectedUserProfile.ToMap());
531557
}
532558

533559
[Test]

0 commit comments

Comments
 (0)