Skip to content

Commit 25ff102

Browse files
event tag should parse value from dictionary to int. (#55)
* event tag should parse value from dictionary to int. * add more test cases for eventtag_revenue.
1 parent 5df7cfb commit 25ff102

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Diff for: OptimizelySDK.Tests/EventTests/EventBuilderTest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ public void TestCreateConversionEventNoAttributesWithInvalidValue()
564564
{"timestamp", timeStamp },
565565
{"uuid", guid },
566566
{"key", "purchase" },
567+
{"revenue", 42 },
567568
{"tags",
568569
new Dictionary<string, object>
569570
{

Diff for: OptimizelySDK.Tests/UtilsTests/EventTagUtilsTest.cs

+10
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ public void TestGetRevenueValue()
3535
{
3636
var expectedValue = 42;
3737
var expectedValue2 = 100;
38+
var expectedValueString = 123;
3839
var validTag = new Dictionary<string, object>() {
3940
{ "revenue", 42 }
4041
};
4142
var validTag2 = new Dictionary<string, object>() {
4243
{ "revenue", 100 }
4344
};
45+
var validTagStringValue = new Dictionary<string, object>() {
46+
{ "revenue", "123" }
47+
};
4448

4549
var invalidTag = new Dictionary<string, object>() {
4650
{ "abc", 42 }
@@ -51,16 +55,22 @@ public void TestGetRevenueValue()
5155
var invalidValue = new Dictionary<string, object>() {
5256
{ "revenue", 42.5 }
5357
};
58+
var invalidTagNonRevenue = new Dictionary<string, object>()
59+
{
60+
{"non-revenue", 123 }
61+
};
5462

5563
// Invalid data.
5664
Assert.Null(EventTagUtils.GetRevenueValue(null));
5765
Assert.Null(EventTagUtils.GetRevenueValue(invalidTag));
5866
Assert.Null(EventTagUtils.GetRevenueValue(nullValue));
5967
Assert.Null(EventTagUtils.GetRevenueValue(invalidValue));
68+
Assert.Null(EventTagUtils.GetRevenueValue(invalidTagNonRevenue));
6069

6170
// Valid data.
6271
Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag), expectedValue);
6372
Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag2), expectedValue2);
73+
Assert.AreEqual(EventTagUtils.GetRevenueValue(validTagStringValue), expectedValueString);
6474
}
6575

6676
[Test]

Diff for: OptimizelySDK/Utils/EventTagUtils.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ public class EventTagUtils
1010

1111
public static object GetRevenueValue(Dictionary<string, object> eventTags)
1212
{
13+
int result = 0;
1314
if (eventTags == null
1415
|| !eventTags.ContainsKey(REVENUE_EVENT_METRIC_NAME)
1516
|| eventTags[REVENUE_EVENT_METRIC_NAME] == null
16-
|| !(eventTags[REVENUE_EVENT_METRIC_NAME] is int))
17+
|| !int.TryParse(eventTags[REVENUE_EVENT_METRIC_NAME].ToString(), out result))
1718
return null;
1819

19-
return eventTags[REVENUE_EVENT_METRIC_NAME];
20+
return result;
2021
}
2122

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

0 commit comments

Comments
 (0)