Skip to content

Commit

Permalink
Create virtual methods on user context (#280)
Browse files Browse the repository at this point in the history
* feat(common): Changed public members on OptimizelyUserContext to be virtual
Make OptimizelyUserContext members virtual, making them overridable by testing frameworks

* fix(tests): Run tests on English culture Set the current thread culture to english, as for systems on others cultures, some tests were failing.
  • Loading branch information
daniel-pons authored Sep 13, 2021
1 parent 34a74cb commit c1233ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions OptimizelySDK.Tests/OptimizelySDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="TestBucketer.cs" />
<Compile Include="BucketerTest.cs" />
<Compile Include="ProjectConfigTest.cs" />
<Compile Include="TestSetup.cs" />
<Compile Include="UtilsTests\ConditionParserTest.cs" />
<Compile Include="UtilsTests\EventTagUtilsTest.cs" />
<Compile Include="UtilsTests\ExceptionExtensionsTest.cs" />
Expand Down
25 changes: 25 additions & 0 deletions OptimizelySDK.Tests/TestSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using System.Globalization;
using System.Threading;

namespace OptimizelySDK.Tests
{
[SetUpFixture]
public class TestSetup
{
[SetUp]
public void Init()
{
/* There are some issues doing assertions on tests with floating point numbers using the .ToString()
* method, as it's culture dependent. EG: TestGetFeatureVariableValueForTypeGivenFeatureFlagIsNotEnabledForUser,
* assigning the culture to English will make this kind of tests to work on others culture based systems. */
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
}

[TearDown]
public void Cleanup()
{
// Empty, but required: https://nunit.org/nunitv2/docs/2.6.4/setupFixture.html
}
}
}
20 changes: 10 additions & 10 deletions OptimizelySDK/OptimizelyUserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public OptimizelyUserContext(Optimizely optimizely, string userId, UserAttribute
/// Returns Optimizely instance associated with the UserContext.
/// </summary>
/// <returns> Optimizely instance.</returns>
public Optimizely GetOptimizely()
public virtual Optimizely GetOptimizely()
{
return Optimizely;
}
Expand All @@ -61,7 +61,7 @@ public Optimizely GetOptimizely()
/// Returns UserId associated with the UserContext
/// </summary>
/// <returns>UserId of this instance.</returns>
public string GetUserId()
public virtual string GetUserId()
{
return UserId;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public void SetAttribute(string key, object value)
/// </summary>
/// <param name="key">A flag key for which a decision will be made.</param>
/// <returns>A decision result.</returns>
public OptimizelyDecision Decide(string key)
public virtual OptimizelyDecision Decide(string key)
{
return Decide(key, new OptimizelyDecideOption[] { });
}
Expand All @@ -122,7 +122,7 @@ public OptimizelyDecision Decide(string key)
/// <param name="key">A flag key for which a decision will be made.</param>
/// <param name="options">A list of options for decision-making.</param>
/// <returns>A decision result.</returns>
public OptimizelyDecision Decide(string key,
public virtual OptimizelyDecision Decide(string key,
OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
Expand All @@ -134,7 +134,7 @@ public OptimizelyDecision Decide(string key,
/// </summary>
/// <param name="keys">list of flag keys for which a decision will be made.</param>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, OptimizelyDecideOption[] options)
public virtual Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.DecideForKeys(optimizelyUserContext, keys, options);
Expand All @@ -145,7 +145,7 @@ public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys, Optim
/// </summary>
/// <param name="keys">list of flag keys for which a decision will be made.</param>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys)
public virtual Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys)
{
return DecideForKeys(keys, new OptimizelyDecideOption[] { });
}
Expand All @@ -154,7 +154,7 @@ public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys)
/// Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys.
/// </summary>
/// <returns>A dictionary of all decision results, mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideAll()
public virtual Dictionary<string, OptimizelyDecision> DecideAll()
{
return DecideAll(new OptimizelyDecideOption[] { });
}
Expand All @@ -164,7 +164,7 @@ public Dictionary<string, OptimizelyDecision> DecideAll()
/// </summary>
/// <param name="options">A list of options for decision-making.</param>
/// <returns>All decision results mapped by flag keys.</returns>
public Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options)
public virtual Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options)
{
var optimizelyUserContext = Copy();
return Optimizely.DecideAll(optimizelyUserContext, options);
Expand All @@ -174,7 +174,7 @@ public Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[]
/// Track an event.
/// </summary>
/// <param name="eventName">The event name.</param>
public void TrackEvent(string eventName)
public virtual void TrackEvent(string eventName)
{
TrackEvent(eventName, new EventTags());
}
Expand All @@ -184,7 +184,7 @@ public void TrackEvent(string eventName)
/// </summary>
/// <param name="eventName">The event name.</param>
/// <param name="eventTags">A map of event tag names to event tag values.</param>
public void TrackEvent(string eventName,
public virtual void TrackEvent(string eventName,
EventTags eventTags)
{
Optimizely.Track(eventName, UserId, Attributes, eventTags);
Expand Down

0 comments on commit c1233ce

Please sign in to comment.