@@ -98,7 +98,7 @@ mod test {
98
98
use super :: * ;
99
99
use primitives:: {
100
100
sentry:: Analytics ,
101
- util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN } ,
101
+ util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN , DUMMY_IPFS } ,
102
102
UnifiedNum ,
103
103
} ;
104
104
@@ -117,15 +117,8 @@ mod test {
117
117
Ok ( event_analytics)
118
118
}
119
119
120
- #[ tokio:: test]
121
- async fn test_analytics_recording ( ) {
122
- let database = DATABASE_POOL . get ( ) . await . expect ( "Should get a DB pool" ) ;
123
-
124
- setup_test_migrations ( database. pool . clone ( ) )
125
- . await
126
- . expect ( "Migrations should succeed" ) ;
127
-
128
- let test_events = vec ! [
120
+ fn get_test_events ( ) -> HashMap < String , ( Event , Address , UnifiedNum ) > {
121
+ vec ! [
129
122
(
130
123
"click_empty" . into( ) ,
131
124
(
@@ -152,11 +145,61 @@ mod test {
152
145
UnifiedNum :: from_u64( 1_000_000 ) ,
153
146
) ,
154
147
) ,
148
+ (
149
+ "click_with_unit_and_slot" . into( ) ,
150
+ (
151
+ Event :: Click {
152
+ publisher: ADDRESSES [ "publisher" ] ,
153
+ ad_unit: Some ( DUMMY_IPFS [ 0 ] ) ,
154
+ ad_slot: Some ( DUMMY_IPFS [ 1 ] ) ,
155
+ referrer: Some ( "http://127.0.0.1" . into( ) ) ,
156
+ } ,
157
+ ADDRESSES [ "publisher" ] ,
158
+ UnifiedNum :: from_u64( 1_000_000 ) ,
159
+ ) ,
160
+ ) ,
161
+ (
162
+ "click_with_different_data" . into( ) ,
163
+ (
164
+ Event :: Click {
165
+ publisher: ADDRESSES [ "publisher" ] ,
166
+ ad_unit: Some ( DUMMY_IPFS [ 2 ] ) ,
167
+ ad_slot: Some ( DUMMY_IPFS [ 3 ] ) ,
168
+ referrer: Some ( "http://127.0.0.1" . into( ) ) ,
169
+ } ,
170
+ ADDRESSES [ "publisher" ] ,
171
+ UnifiedNum :: from_u64( 1_000_000 ) ,
172
+ ) ,
173
+ ) ,
174
+ (
175
+ "impression_with_slot_unit_and_referrer" . into( ) ,
176
+ (
177
+ Event :: Impression {
178
+ publisher: ADDRESSES [ "publisher" ] ,
179
+ ad_unit: Some ( DUMMY_IPFS [ 0 ] ) ,
180
+ ad_slot: Some ( DUMMY_IPFS [ 1 ] ) ,
181
+ referrer: Some ( "http://127.0.0.1" . into( ) ) ,
182
+ } ,
183
+ ADDRESSES [ "publisher" ] ,
184
+ UnifiedNum :: from_u64( 1_000_000 ) ,
185
+ ) ,
186
+ ) ,
155
187
]
156
188
. into_iter ( )
157
- . collect :: < HashMap < String , _ > > ( ) ;
189
+ . collect :: < HashMap < String , _ > > ( )
190
+ }
191
+
192
+ #[ tokio:: test]
193
+ async fn test_analytics_recording_with_empty_events ( ) {
194
+ let test_events = get_test_events ( ) ;
195
+ let database = DATABASE_POOL . get ( ) . await . expect ( "Should get a DB pool" ) ;
196
+
197
+ setup_test_migrations ( database. pool . clone ( ) )
198
+ . await
199
+ . expect ( "Migrations should succeed" ) ;
158
200
159
201
let campaign = DUMMY_CAMPAIGN . clone ( ) ;
202
+
160
203
let session = Session {
161
204
ip : None ,
162
205
country : None ,
@@ -167,6 +210,7 @@ mod test {
167
210
let input_events = vec ! [
168
211
test_events[ "click_empty" ] . clone( ) ,
169
212
test_events[ "impression_empty" ] . clone( ) ,
213
+ test_events[ "impression_empty" ] . clone( ) ,
170
214
] ;
171
215
172
216
record ( & database. clone ( ) , & campaign, & session, input_events. clone ( ) )
@@ -176,6 +220,7 @@ mod test {
176
220
let analytics = get_all_analytics ( & database. pool )
177
221
. await
178
222
. expect ( "should get all analytics" ) ;
223
+ assert_eq ! ( analytics. len( ) , 2 ) ;
179
224
180
225
let click_analytics = analytics
181
226
. iter ( )
@@ -193,35 +238,101 @@ mod test {
193
238
194
239
assert_eq ! (
195
240
impression_analytics. payout_amount,
196
- UnifiedNum :: from_u64( 1_000_000 )
241
+ UnifiedNum :: from_u64( 2_000_000 )
197
242
) ;
198
- assert_eq ! ( impression_analytics. payout_count, 1 ) ;
243
+ assert_eq ! ( impression_analytics. payout_count, 2 ) ;
244
+ }
245
+
246
+ #[ tokio:: test]
247
+ async fn test_recording_with_session ( ) {
248
+ let database = DATABASE_POOL . get ( ) . await . expect ( "Should get a DB pool" ) ;
249
+
250
+ setup_test_migrations ( database. pool . clone ( ) )
251
+ . await
252
+ . expect ( "Migrations should succeed" ) ;
253
+
254
+ let test_events = get_test_events ( ) ;
255
+
256
+ let campaign = DUMMY_CAMPAIGN . clone ( ) ;
257
+
258
+ let session = Session {
259
+ ip : Default :: default ( ) ,
260
+ country : Some ( "Bulgaria" . into ( ) ) ,
261
+ referrer_header : Some ( "http://127.0.0.1" . into ( ) ) ,
262
+ os : Some ( "Windows" . into ( ) ) ,
263
+ } ;
264
+
265
+ let input_events = vec ! [
266
+ test_events[ "click_with_unit_and_slot" ] . clone( ) ,
267
+ test_events[ "click_with_unit_and_slot" ] . clone( ) ,
268
+ test_events[ "click_with_different_data" ] . clone( ) ,
269
+ test_events[ "click_with_different_data" ] . clone( ) ,
270
+ test_events[ "click_with_different_data" ] . clone( ) ,
271
+ test_events[ "impression_with_slot_unit_and_referrer" ] . clone( ) ,
272
+ test_events[ "impression_with_slot_unit_and_referrer" ] . clone( ) ,
273
+ test_events[ "impression_with_slot_unit_and_referrer" ] . clone( ) ,
274
+ test_events[ "impression_with_slot_unit_and_referrer" ] . clone( ) ,
275
+ ] ;
199
276
200
- record ( & database. clone ( ) , & campaign, & session, input_events)
277
+ record ( & database. clone ( ) , & campaign, & session, input_events. clone ( ) )
201
278
. await
202
279
. expect ( "should record" ) ;
203
280
204
281
let analytics = get_all_analytics ( & database. pool )
205
282
. await
206
- . expect ( "should find analytics" ) ;
207
- let click_analytics = analytics
208
- . iter ( )
209
- . find ( |a| a. event_type == "CLICK" )
210
- . expect ( "There should be a click event" ) ;
211
- let impression_analytics = analytics
283
+ . expect ( "should get all analytics" ) ;
284
+ assert_eq ! ( analytics. len( ) , 3 ) ;
285
+
286
+ assert ! (
287
+ analytics
288
+ . iter( )
289
+ . all( |a| a. country == Some ( "Bulgaria" . into( ) ) ) ,
290
+ "all analytics should have the same country as the one in the session"
291
+ ) ;
292
+ assert ! (
293
+ analytics
294
+ . iter( )
295
+ . all( |a| a. os_name == OperatingSystem :: map_os( "Windows" ) ) ,
296
+ "all analytics should have the same os as the one in the session"
297
+ ) ;
298
+
299
+ let with_slot_and_unit: Analytics = analytics
212
300
. iter ( )
213
- . find ( |a| a. event_type == "IMPRESSION" )
214
- . expect ( "There should be an impression event" ) ;
301
+ . find ( |a| {
302
+ a. ad_unit == Some ( DUMMY_IPFS [ 0 ] )
303
+ && a. ad_slot == Some ( DUMMY_IPFS [ 1 ] )
304
+ && a. event_type == "CLICK" . to_string ( )
305
+ } )
306
+ . expect ( "entry should exist" )
307
+ . to_owned ( ) ;
308
+ assert_eq ! ( with_slot_and_unit. hostname, Some ( "127.0.0.1" . to_string( ) ) ) ;
309
+ assert_eq ! ( with_slot_and_unit. payout_count, 2 ) ;
215
310
assert_eq ! (
216
- click_analytics . payout_amount,
311
+ with_slot_and_unit . payout_amount,
217
312
UnifiedNum :: from_u64( 2_000_000 )
218
313
) ;
219
- assert_eq ! ( click_analytics. payout_count, 2 ) ;
220
314
315
+ let with_different_slot_and_unit: Analytics = analytics
316
+ . iter ( )
317
+ . find ( |a| a. ad_unit == Some ( DUMMY_IPFS [ 2 ] ) && a. ad_slot == Some ( DUMMY_IPFS [ 3 ] ) )
318
+ . expect ( "entry should exist" )
319
+ . to_owned ( ) ;
320
+ assert_eq ! ( with_different_slot_and_unit. payout_count, 3 ) ;
221
321
assert_eq ! (
222
- impression_analytics . payout_amount,
223
- UnifiedNum :: from_u64( 2_000_000 )
322
+ with_different_slot_and_unit . payout_amount,
323
+ UnifiedNum :: from_u64( 3_000_000 )
224
324
) ;
225
- assert_eq ! ( impression_analytics. payout_count, 2 ) ;
325
+
326
+ let with_referrer: Analytics = analytics
327
+ . iter ( )
328
+ . find ( |a| {
329
+ a. ad_unit == Some ( DUMMY_IPFS [ 0 ] )
330
+ && a. ad_slot == Some ( DUMMY_IPFS [ 1 ] )
331
+ && a. event_type == "IMPRESSION" . to_string ( )
332
+ } )
333
+ . expect ( "entry should exist" )
334
+ . to_owned ( ) ;
335
+ assert_eq ! ( with_referrer. payout_count, 4 ) ;
336
+ assert_eq ! ( with_referrer. payout_amount, UnifiedNum :: from_u64( 4_000_000 ) ) ;
226
337
}
227
338
}
0 commit comments