1
- using NUnit . Framework ;
2
- using OptimizelySDK . Bucketing ;
3
- using OptimizelySDK . Entity ;
4
- using OptimizelySDK . Event ;
5
- using OptimizelySDK . Event . Builder ;
1
+ /*
2
+ * Copyright 2017-2018, Optimizely
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ using Moq ;
18
+ using NUnit . Framework ;
6
19
using OptimizelySDK . Logger ;
7
- using OptimizelySDK . Tests . EventTests ;
8
20
using OptimizelySDK . Utils ;
9
- using System ;
10
21
using System . Collections . Generic ;
11
- using System . Linq ;
12
- using System . Text ;
13
- using System . Threading . Tasks ;
14
22
15
23
namespace OptimizelySDK . Tests . UtilsTests
16
24
{
17
25
[ TestFixture ]
18
26
class EventTagUtilsTest
19
27
{
20
- private string TestUserId = string . Empty ;
21
- private ProjectConfig Config ;
22
- private EventBuilder EventBuilder ;
28
+ private Mock < ILogger > LoggerMock ;
29
+ private ILogger Logger ;
23
30
24
- [ TestFixtureSetUp ]
31
+ [ SetUp ]
25
32
public void Setup ( )
26
33
{
27
- TestUserId = "testUserId" ;
28
- var logger = new NoOpLogger ( ) ;
29
- Config = ProjectConfig . Create ( TestData . Datafile , logger , new ErrorHandler . NoOpErrorHandler ( ) ) ;
30
- EventBuilder = new EventBuilder ( new Bucketer ( logger ) ) ;
34
+ LoggerMock = new Mock < ILogger > ( ) ;
35
+ LoggerMock . Setup ( i => i . Log ( It . IsAny < LogLevel > ( ) , It . IsAny < string > ( ) ) ) ;
36
+
37
+ Logger = LoggerMock . Object ;
31
38
}
32
39
33
40
[ Test ]
@@ -61,16 +68,25 @@ public void TestGetRevenueValue()
61
68
} ;
62
69
63
70
// Invalid data.
64
- Assert . Null ( EventTagUtils . GetRevenueValue ( null ) ) ;
65
- Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTag ) ) ;
66
- Assert . Null ( EventTagUtils . GetRevenueValue ( nullValue ) ) ;
67
- Assert . Null ( EventTagUtils . GetRevenueValue ( invalidValue ) ) ;
68
- Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTagNonRevenue ) ) ;
71
+ Assert . Null ( EventTagUtils . GetRevenueValue ( null , Logger ) ) ;
72
+ Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTag , Logger ) ) ;
73
+ Assert . Null ( EventTagUtils . GetRevenueValue ( nullValue , Logger ) ) ;
74
+ Assert . Null ( EventTagUtils . GetRevenueValue ( invalidValue , Logger ) ) ;
75
+ Assert . Null ( EventTagUtils . GetRevenueValue ( invalidTagNonRevenue , Logger ) ) ;
76
+
77
+ LoggerMock . Verify ( l => l . Log ( LogLevel . DEBUG , "Event tags is undefined." ) , Times . Once ) ;
78
+ LoggerMock . Verify ( l => l . Log ( LogLevel . DEBUG , "The revenue key is not defined in the event tags." ) , Times . Exactly ( 2 ) ) ;
79
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The revenue key value is not defined in event tags." ) , Times . Once ) ;
80
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Revenue value is not an integer or couldn't be parsed as an integer." ) , Times . Once ) ;
69
81
70
82
// Valid data.
71
- Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag ) , expectedValue ) ;
72
- Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag2 ) , expectedValue2 ) ;
73
- Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTagStringValue ) , expectedValueString ) ;
83
+ Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag , Logger ) , expectedValue ) ;
84
+ Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTag2 , Logger ) , expectedValue2 ) ;
85
+ Assert . AreEqual ( EventTagUtils . GetRevenueValue ( validTagStringValue , Logger ) , expectedValueString ) ;
86
+
87
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The revenue value { expectedValue } will be sent to results.") , Times . Once ) ;
88
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The revenue value { expectedValue2 } will be sent to results.") , Times . Once ) ;
89
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The revenue value { expectedValueString } will be sent to results.") , Times . Once ) ;
74
90
}
75
91
76
92
[ Test ]
@@ -106,23 +122,32 @@ public void TestGetEventValue()
106
122
} ;
107
123
108
124
// Invalid data.
109
- Assert . Null ( EventTagUtils . GetNumericValue ( null , new Logger . DefaultLogger ( ) ) ) ;
110
- Assert . Null ( EventTagUtils . GetNumericValue ( invalidTag , new Logger . DefaultLogger ( ) ) ) ;
111
- Assert . Null ( EventTagUtils . GetNumericValue ( nullValue , new Logger . DefaultLogger ( ) ) ) ;
125
+ Assert . Null ( EventTagUtils . GetNumericValue ( null , Logger ) ) ;
126
+ Assert . Null ( EventTagUtils . GetNumericValue ( invalidTag , Logger ) ) ;
127
+ Assert . Null ( EventTagUtils . GetNumericValue ( nullValue , Logger ) ) ;
112
128
129
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Event tags is undefined." ) , Times . Once ) ;
130
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key is not in event tags." ) , Times . Once ) ;
131
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key value is not defined in event tags." ) , Times . Once ) ;
113
132
114
133
// Valid data.
115
- Assert . AreEqual ( 42 , EventTagUtils . GetNumericValue ( validTagStr , new Logger . DefaultLogger ( ) ) ) ;
116
- Assert . AreEqual ( "42.3" , EventTagUtils . GetNumericValue ( validTagStr1 , new Logger . DefaultLogger ( ) ) . ToString ( ) ) ;
117
- Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag , new Logger . DefaultLogger ( ) ) , expectedValue ) ;
118
- Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag2 , new Logger . DefaultLogger ( ) ) , expectedValue2 ) ;
119
- Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag3 , new Logger . DefaultLogger ( ) ) . ToString ( ) , expectedValue3 . ToString ( ) ) ;
134
+ Assert . AreEqual ( 42 , EventTagUtils . GetNumericValue ( validTagStr , Logger ) ) ;
135
+ Assert . AreEqual ( "42.3" , EventTagUtils . GetNumericValue ( validTagStr1 , Logger ) . ToString ( ) ) ;
136
+ Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag , Logger ) , expectedValue ) ;
137
+ Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag2 , Logger ) , expectedValue2 ) ;
138
+ Assert . AreEqual ( EventTagUtils . GetNumericValue ( validTag3 , Logger ) . ToString ( ) , expectedValue3 . ToString ( ) ) ;
139
+
140
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The numeric metric value 42.3 will be sent to results." ) , Times . Once ) ;
141
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { expectedValue } will be sent to results.") , Times . Exactly ( 2 ) ) ;
142
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { expectedValue2 } will be sent to results.") , Times . Once ) ;
143
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { expectedValue3 } will be sent to results.") , Times . Once ) ;
120
144
}
121
145
122
146
[ Test ]
123
147
public void TestGetNumericMetricInvalidArgs ( )
124
148
{
125
- Assert . IsNull ( EventTagUtils . GetNumericValue ( null , new Logger . DefaultLogger ( ) ) ) ;
149
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( null , Logger ) ) ;
150
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Event tags is undefined." ) , Times . Once ) ;
126
151
127
152
//Errors for all, because it accepts only dictionary//
128
153
// Not valid test cases in C#
@@ -139,8 +164,10 @@ public void TestGetNumericMetricInvalidArgs()
139
164
public void TestGetNumericMetricNoValueTag ( )
140
165
{
141
166
// Test that numeric value is not returned when there's no numeric event tag.
142
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { } , new Logger . DefaultLogger ( ) ) ) ;
143
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 42 } } , new Logger . DefaultLogger ( ) ) ) ;
167
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { } , Logger ) ) ;
168
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 42 } } , Logger ) ) ;
169
+
170
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key is not in event tags." ) , Times . Exactly ( 2 ) ) ;
144
171
145
172
//Errors for all, because it accepts only dictionary//
146
173
//Assert.IsNull(EventTagUtils.GetEventValue(new object[] { }));
@@ -152,14 +179,16 @@ public void TestGetNumericMetricInvalidValueTag()
152
179
153
180
// Test that numeric value is not returned when revenue event tag has invalid data type.
154
181
155
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , null } } , new Logger . DefaultLogger ( ) ) ) ;
156
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 0.5 } } , new Logger . DefaultLogger ( ) ) ) ;
157
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 12345 } } , new Logger . DefaultLogger ( ) ) ) ;
158
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , "65536" } } , new Logger . DefaultLogger ( ) ) ) ;
159
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , true } } , new Logger . DefaultLogger ( ) ) ) ;
160
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , false } } , new Logger . DefaultLogger ( ) ) ) ;
161
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 1 , 2 , 3 } } } , new Logger . DefaultLogger ( ) ) ) ;
162
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 'a' , 'b' , 'c' } } } , new Logger . DefaultLogger ( ) ) ) ;
182
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , null } } , Logger ) ) ;
183
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 0.5 } } , Logger ) ) ;
184
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , 12345 } } , Logger ) ) ;
185
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , "65536" } } , Logger ) ) ;
186
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , true } } , Logger ) ) ;
187
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , false } } , Logger ) ) ;
188
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 1 , 2 , 3 } } } , Logger ) ) ;
189
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "non-value" , new object [ ] { 'a' , 'b' , 'c' } } } , Logger ) ) ;
190
+
191
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "The numeric metric key is not in event tags." ) , Times . Exactly ( 8 ) ) ;
163
192
}
164
193
165
194
[ Test ]
@@ -168,43 +197,50 @@ public void TestGetNumericMetricValueTag()
168
197
{
169
198
170
199
// An integer should be cast to a float
171
- Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 12345 } } , new Logger . DefaultLogger ( ) ) ) ;
200
+ Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 12345 } } , Logger ) ) ;
172
201
173
202
// A string should be cast to a float
174
- Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , "12345" } } , new Logger . DefaultLogger ( ) ) ) ;
203
+ Assert . AreEqual ( 12345.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , "12345" } } , Logger ) ) ;
175
204
176
205
// Valid float values
177
206
float someFloat = 1.2345F ;
178
207
179
- Assert . AreEqual ( someFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , someFloat } } , new Logger . DefaultLogger ( ) ) ) ;
208
+ Assert . AreEqual ( someFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , someFloat } } , Logger ) ) ;
180
209
181
210
float maxFloat = float . MaxValue ;
182
- Assert . AreEqual ( maxFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , maxFloat } } , new Logger . DefaultLogger ( ) ) ) ;
211
+ Assert . AreEqual ( maxFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , maxFloat } } , Logger ) ) ;
183
212
184
213
185
214
float minFloat = float . MinValue ;
186
- Assert . AreEqual ( minFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , minFloat } } , new Logger . DefaultLogger ( ) ) ) ;
215
+ Assert . AreEqual ( minFloat , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , minFloat } } , Logger ) ) ;
187
216
188
217
// Invalid values
189
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , false } } , new Logger . DefaultLogger ( ) ) ) ;
190
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , new Logger . DefaultLogger ( ) ) ) ;
218
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , false } } , Logger ) ) ;
219
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , Logger ) ) ;
191
220
192
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , true } } , new Logger . DefaultLogger ( ) ) ) ;
193
- Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new int [ ] { } } } , new Logger . DefaultLogger ( ) ) ) ;
221
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , true } } , Logger ) ) ;
222
+ Assert . IsNull ( EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new int [ ] { } } } , Logger ) ) ;
194
223
195
- var numericValueArray = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new object [ ] { } } } , new Logger . DefaultLogger ( ) ) ;
224
+ var numericValueArray = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , new object [ ] { } } } , Logger ) ;
196
225
Assert . IsNull ( numericValueArray , string . Format ( "Array numeric value is {0}" , numericValueArray ) ) ;
197
226
198
227
199
- var numericValueNone = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , new Logger . DefaultLogger ( ) ) ;
228
+ var numericValueNone = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , null } } , Logger ) ;
200
229
Assert . IsNull ( numericValueNone , string . Format ( "None numeric value is {0}" , numericValueNone ) ) ;
201
230
202
231
203
- var numericValueOverflow = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , float . MaxValue * 10 } } , new Logger . DefaultLogger ( ) ) ;
232
+ var numericValueOverflow = EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , float . MaxValue * 10 } } , Logger ) ;
204
233
Assert . IsNull ( numericValueOverflow , string . Format ( "Max numeric value is {0}" , float . MaxValue * 10 ) ) ;
205
234
206
- Assert . AreEqual ( 0.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 0.0 } } , new Logger . DefaultLogger ( ) ) ) ;
235
+ Assert . AreEqual ( 0.0 , EventTagUtils . GetNumericValue ( new Dictionary < string , object > { { "value" , 0.0 } } , Logger ) ) ;
207
236
237
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The numeric metric value 12345 will be sent to results." ) , Times . Exactly ( 2 ) ) ;
238
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { maxFloat } will be sent to results.") , Times . Once ) ;
239
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , $ "The numeric metric value { minFloat } will be sent to results.") , Times . Once ) ;
240
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , $ "Provided numeric value { float . PositiveInfinity } is in an invalid format.") , Times . Once ) ;
241
+ LoggerMock . Verify ( l => l . Log ( LogLevel . INFO , "The numeric metric value 0 will be sent to results." ) , Times . Once ) ;
242
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Provided numeric value is boolean which is an invalid format." ) , Times . Exactly ( 2 ) ) ;
243
+ LoggerMock . Verify ( l => l . Log ( LogLevel . ERROR , "Numeric metric value is not in integer, float, or string form." ) , Times . Exactly ( 2 ) ) ;
208
244
209
245
/* Value is converted into 1234F */
210
246
//var numericValueInvalidLiteral = EventTagUtils.GetEventValue(new Dictionary<string, object> { { "value", "1,234" } });
0 commit comments