Skip to content

Commit

Permalink
event tag should parse value from dictionary to int. (#55)
Browse files Browse the repository at this point in the history
* event tag should parse value from dictionary to int.

* add more test cases for eventtag_revenue.
  • Loading branch information
msohailhussain authored and kellyroach-optimizely committed Jan 24, 2018
1 parent 5df7cfb commit 25ff102
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions OptimizelySDK.Tests/EventTests/EventBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ public void TestCreateConversionEventNoAttributesWithInvalidValue()
{"timestamp", timeStamp },
{"uuid", guid },
{"key", "purchase" },
{"revenue", 42 },
{"tags",
new Dictionary<string, object>
{
Expand Down
10 changes: 10 additions & 0 deletions OptimizelySDK.Tests/UtilsTests/EventTagUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ public void TestGetRevenueValue()
{
var expectedValue = 42;
var expectedValue2 = 100;
var expectedValueString = 123;
var validTag = new Dictionary<string, object>() {
{ "revenue", 42 }
};
var validTag2 = new Dictionary<string, object>() {
{ "revenue", 100 }
};
var validTagStringValue = new Dictionary<string, object>() {
{ "revenue", "123" }
};

var invalidTag = new Dictionary<string, object>() {
{ "abc", 42 }
Expand All @@ -51,16 +55,22 @@ public void TestGetRevenueValue()
var invalidValue = new Dictionary<string, object>() {
{ "revenue", 42.5 }
};
var invalidTagNonRevenue = new Dictionary<string, object>()
{
{"non-revenue", 123 }
};

// Invalid data.
Assert.Null(EventTagUtils.GetRevenueValue(null));
Assert.Null(EventTagUtils.GetRevenueValue(invalidTag));
Assert.Null(EventTagUtils.GetRevenueValue(nullValue));
Assert.Null(EventTagUtils.GetRevenueValue(invalidValue));
Assert.Null(EventTagUtils.GetRevenueValue(invalidTagNonRevenue));

// Valid data.
Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag), expectedValue);
Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag2), expectedValue2);
Assert.AreEqual(EventTagUtils.GetRevenueValue(validTagStringValue), expectedValueString);
}

[Test]
Expand Down
5 changes: 3 additions & 2 deletions OptimizelySDK/Utils/EventTagUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ public class EventTagUtils

public static object GetRevenueValue(Dictionary<string, object> eventTags)
{
int result = 0;
if (eventTags == null
|| !eventTags.ContainsKey(REVENUE_EVENT_METRIC_NAME)
|| eventTags[REVENUE_EVENT_METRIC_NAME] == null
|| !(eventTags[REVENUE_EVENT_METRIC_NAME] is int))
|| !int.TryParse(eventTags[REVENUE_EVENT_METRIC_NAME].ToString(), out result))
return null;

return eventTags[REVENUE_EVENT_METRIC_NAME];
return result;
}

public static object GetNumericValue(Dictionary<string, object> eventTags, ILogger logger = null)
Expand Down

0 comments on commit 25ff102

Please sign in to comment.