Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions OptimizelySDK.Tests/CmabTests/DecisionServiceCmabTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ public void TestGetVariationWithCmabExperimentCacheHit()
TEST_EXPERIMENT_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 1 && attrs.ContainsKey(AGE_ATTRIBUTE_KEY) &&
(int)attrs[AGE_ATTRIBUTE_KEY] == 25),
attrs.Count == 1 && attrs.ContainsKey("age_attr_id") &&
(int)attrs["age_attr_id"] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()))
.Returns(VARIATION_A_ID);
Expand Down Expand Up @@ -300,7 +300,7 @@ public void TestGetVariationWithCmabExperimentCacheHit()
TEST_EXPERIMENT_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 1 && (int)attrs[AGE_ATTRIBUTE_KEY] == 25),
attrs.Count == 1 && (int)attrs["age_attr_id"] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()),
Times.Once);
Expand Down Expand Up @@ -334,7 +334,7 @@ public void TestGetVariationWithCmabExperimentCacheMissAttributesChanged()
cmabClientMock.Setup(c => c.FetchDecision(
TEST_EXPERIMENT_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs => attrs.ContainsKey(AGE_ATTRIBUTE_KEY)),
It.Is<IDictionary<string, object>>(attrs => attrs.ContainsKey("age_attr_id")),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()))
.Returns(VARIATION_A_ID);
Expand Down Expand Up @@ -364,15 +364,15 @@ public void TestGetVariationWithCmabExperimentCacheMissAttributesChanged()
TEST_EXPERIMENT_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.ContainsKey(AGE_ATTRIBUTE_KEY) && (int)attrs[AGE_ATTRIBUTE_KEY] == 25),
attrs.ContainsKey("age_attr_id") && (int)attrs["age_attr_id"] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()),
Times.Once);
cmabClientMock.Verify(c => c.FetchDecision(
TEST_EXPERIMENT_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.ContainsKey(AGE_ATTRIBUTE_KEY) && (int)attrs[AGE_ATTRIBUTE_KEY] == 30),
attrs.ContainsKey("age_attr_id") && (int)attrs["age_attr_id"] == 30),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()),
Times.Once);
Expand Down Expand Up @@ -489,8 +489,8 @@ public void TestGetDecisionForCmabExperimentAttributeFiltering()
TEST_EXPERIMENT_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 2 && (int)attrs["age"] == 25 &&
(string)attrs["location"] == "USA" &&
attrs.Count == 2 && (int)attrs["age_attr_id"] == 25 &&
(string)attrs["location_attr_id"] == "USA" &&
!attrs.ContainsKey("extra")),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()))
Expand Down
59 changes: 49 additions & 10 deletions OptimizelySDK.Tests/CmabTests/DefaultCmabServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void ReturnsDecisionFromCacheWhenHashMatches()
var userContext = CreateUserContext(TEST_USER_ID,
new Dictionary<string, object> { { "age", 25 } });
var filteredAttributes =
new UserAttributes(new Dictionary<string, object> { { "age", 25 } });
new UserAttributes(new Dictionary<string, object> { { AGE_ATTRIBUTE_ID, 25 } });
var cacheKey = DefaultCmabService.GetCacheKey(TEST_USER_ID, TEST_RULE_ID);

_cmabCache.Save(cacheKey, new CmabCacheEntry
Expand Down Expand Up @@ -107,8 +107,8 @@ public void IgnoresCacheWhenOptionSpecified()

_mockCmabClient.Setup(c => c.FetchDecision(TEST_RULE_ID, TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs != null && attrs.Count == 1 && attrs.ContainsKey("age") &&
(int)attrs["age"] == 25),
attrs != null && attrs.Count == 1 && attrs.ContainsKey(AGE_ATTRIBUTE_ID) &&
(int)attrs[AGE_ATTRIBUTE_ID] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>())).Returns("varB");

Expand Down Expand Up @@ -143,7 +143,7 @@ public void ResetsCacheWhenOptionSpecified()

_mockCmabClient.Setup(c => c.FetchDecision(TEST_RULE_ID, TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 1 && (int)attrs["age"] == 25),
attrs.Count == 1 && (int)attrs[AGE_ATTRIBUTE_ID] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>())).Returns("varNew");

Expand Down Expand Up @@ -190,7 +190,7 @@ public void InvalidatesUserEntryWhenOptionSpecified()

_mockCmabClient.Setup(c => c.FetchDecision(TEST_RULE_ID, TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 1 && (int)attrs["age"] == 25),
attrs.Count == 1 && (int)attrs[AGE_ATTRIBUTE_ID] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>())).Returns("varNew");

Expand Down Expand Up @@ -232,7 +232,7 @@ public void FetchesNewDecisionWhenHashDiffers()

_mockCmabClient.Setup(c => c.FetchDecision(TEST_RULE_ID, TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 1 && (int)attrs["age"] == 25),
attrs.Count == 1 && (int)attrs[AGE_ATTRIBUTE_ID] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>())).Returns("varUpdated");

Expand Down Expand Up @@ -270,8 +270,8 @@ public void FiltersAttributesBeforeCallingClient()

_mockCmabClient.Setup(c => c.FetchDecision(TEST_RULE_ID, TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs => attrs.Count == 2 &&
(int)attrs["age"] == 25 &&
(string)attrs["location"] == "USA" &&
(int)attrs[AGE_ATTRIBUTE_ID] == 25 &&
(string)attrs[LOCATION_ATTRIBUTE_ID] == "USA" &&
!attrs.ContainsKey("extra")),
It.IsAny<string>(),
It.IsAny<TimeSpan?>())).Returns("varFiltered");
Expand All @@ -283,6 +283,45 @@ public void FiltersAttributesBeforeCallingClient()
_mockCmabClient.VerifyAll();
}

[Test]
public void FilteredAttributesUseAttributeIdNotKey()
{
var experiment = CreateExperiment(TEST_RULE_ID,
new List<string> { AGE_ATTRIBUTE_ID, LOCATION_ATTRIBUTE_ID });
var attributeMap = new Dictionary<string, AttributeEntity>
{
{ AGE_ATTRIBUTE_ID, new AttributeEntity { Id = AGE_ATTRIBUTE_ID, Key = "age" } },
{
LOCATION_ATTRIBUTE_ID,
new AttributeEntity { Id = LOCATION_ATTRIBUTE_ID, Key = "location" }
},
};
var projectConfig = CreateProjectConfig(TEST_RULE_ID, experiment, attributeMap);
var userContext = CreateUserContext(TEST_USER_ID, new Dictionary<string, object>
{
{ "age", 25 },
{ "location", "USA" },
});

_mockCmabClient.Setup(c => c.FetchDecision(TEST_RULE_ID, TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs.Count == 2 &&
attrs.ContainsKey(AGE_ATTRIBUTE_ID) &&
!attrs.ContainsKey("age") &&
attrs.ContainsKey(LOCATION_ATTRIBUTE_ID) &&
!attrs.ContainsKey("location") &&
(int)attrs[AGE_ATTRIBUTE_ID] == 25 &&
(string)attrs[LOCATION_ATTRIBUTE_ID] == "USA"),
It.IsAny<string>(),
It.IsAny<TimeSpan?>())).Returns("varIdBased");

var decision = _cmabService.GetDecision(projectConfig, userContext, TEST_RULE_ID);

Assert.IsNotNull(decision);
Assert.AreEqual("varIdBased", decision.VariationId);
_mockCmabClient.VerifyAll();
}

[Test]
public void HandlesMissingCmabConfiguration()
{
Expand Down Expand Up @@ -509,8 +548,8 @@ public void ConcurrentRequestsForSameUserUseCacheAfterFirstNetworkCall()
TEST_RULE_ID,
TEST_USER_ID,
It.Is<IDictionary<string, object>>(attrs =>
attrs != null && attrs.Count == 1 && attrs.ContainsKey("age") &&
(int)attrs["age"] == 25),
attrs != null && attrs.Count == 1 && attrs.ContainsKey(AGE_ATTRIBUTE_ID) &&
(int)attrs[AGE_ATTRIBUTE_ID] == 25),
It.IsAny<string>(),
It.IsAny<TimeSpan?>()))
.Returns(() =>
Expand Down
2 changes: 1 addition & 1 deletion OptimizelySDK/Cmab/DefaultCmabService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private UserAttributes FilterAttributes(ProjectConfig projectConfig,
if (attributeIdMap.TryGetValue(attributeId, out var attribute) &&
userAttributes.TryGetValue(attribute.Key, out var value))
{
filtered[attribute.Key] = value;
filtered[attribute.Id] = value;
}
}

Expand Down
Loading