Skip to content

Commit 8a0aa63

Browse files
committed
finished requested changes
1 parent 2e2e3dc commit 8a0aa63

File tree

2 files changed

+152
-181
lines changed

2 files changed

+152
-181
lines changed

primitives/src/sentry.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,27 +931,33 @@ mod test {
931931
let client = POSTGRES_POOL.get().await.unwrap();
932932
let sql_type = "TIMESTAMPTZ";
933933

934-
let actual_datehour = DateHour::<Utc>::from_ymdh(2021, 1, 1, 1);
934+
let example_datehour = DateHour::<Utc>::from_ymdh(2021, 1, 1, 1);
935+
let expected_datehour =
936+
DateHour::try_from(Utc.ymd(2021, 1, 1).and_hms(1, 0, 0)).expect("Should get DateHour");
937+
assert_eq!(
938+
example_datehour, expected_datehour,
939+
"Example and expected datehour must be the same"
940+
);
935941

936942
// from SQL
937-
let row_datehour: DateHour<Utc> = client
943+
let actual_datehour: DateHour<Utc> = client
938944
.query_one(
939-
&*format!("SELECT '{:?}'::{}", actual_datehour, sql_type),
945+
&*format!("SELECT '{}'::{}", example_datehour.to_datetime(), sql_type),
940946
&[],
941947
)
942948
.await
943949
.unwrap()
944950
.get(0);
945951

946-
assert_eq!(&actual_datehour, &row_datehour);
952+
assert_eq!(&expected_datehour, &actual_datehour);
947953

948954
// to SQL
949-
let row_datehour: DateHour<Utc> = client
950-
.query_one(&*format!("SELECT $1::{}", sql_type), &[&actual_datehour])
955+
let actual_datehour: DateHour<Utc> = client
956+
.query_one(&*format!("SELECT $1::{}", sql_type), &[&example_datehour])
951957
.await
952958
.unwrap()
953959
.get(0);
954960

955-
assert_eq!(&actual_datehour, &row_datehour);
961+
assert_eq!(&expected_datehour, &actual_datehour);
956962
}
957963
}

sentry/src/analytics.rs

Lines changed: 139 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,8 @@ mod test {
117117
Ok(event_analytics)
118118
}
119119

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![
129122
(
130123
"click_empty".into(),
131124
(
@@ -193,181 +186,153 @@ mod test {
193186
),
194187
]
195188
.into_iter()
196-
.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");
197200

198201
let campaign = DUMMY_CAMPAIGN.clone();
199202

200-
// Testing record with empty events and session
201-
{
202-
let session = Session {
203-
ip: None,
204-
country: None,
205-
referrer_header: None,
206-
os: None,
207-
};
203+
let session = Session {
204+
ip: None,
205+
country: None,
206+
referrer_header: None,
207+
os: None,
208+
};
208209

209-
let input_events = vec![
210-
test_events["click_empty"].clone(),
211-
test_events["impression_empty"].clone(),
212-
];
210+
let input_events = vec![
211+
test_events["click_empty"].clone(),
212+
test_events["impression_empty"].clone(),
213+
test_events["impression_empty"].clone(),
214+
];
213215

214-
record(&database.clone(), &campaign, &session, input_events.clone())
215-
.await
216-
.expect("should record");
216+
record(&database.clone(), &campaign, &session, input_events.clone())
217+
.await
218+
.expect("should record");
217219

218-
let analytics = get_all_analytics(&database.pool)
219-
.await
220-
.expect("should get all analytics");
220+
let analytics = get_all_analytics(&database.pool)
221+
.await
222+
.expect("should get all analytics");
223+
assert_eq!(analytics.len(), 2);
224+
225+
let click_analytics = analytics
226+
.iter()
227+
.find(|a| a.event_type == "CLICK")
228+
.expect("There should be a click Analytics");
229+
let impression_analytics = analytics
230+
.iter()
231+
.find(|a| a.event_type == "IMPRESSION")
232+
.expect("There should be an impression Analytics");
233+
assert_eq!(
234+
click_analytics.payout_amount,
235+
UnifiedNum::from_u64(1_000_000)
236+
);
237+
assert_eq!(click_analytics.payout_count, 1);
238+
239+
assert_eq!(
240+
impression_analytics.payout_amount,
241+
UnifiedNum::from_u64(2_000_000)
242+
);
243+
assert_eq!(impression_analytics.payout_count, 2);
244+
}
221245

222-
let click_analytics = analytics
223-
.iter()
224-
.find(|a| a.event_type == "CLICK")
225-
.expect("There should be a click Analytics");
226-
let impression_analytics = analytics
227-
.iter()
228-
.find(|a| a.event_type == "IMPRESSION")
229-
.expect("There should be an impression Analytics");
230-
assert_eq!(
231-
click_analytics.payout_amount,
232-
UnifiedNum::from_u64(1_000_000)
233-
);
234-
assert_eq!(click_analytics.payout_count, 1);
235-
236-
assert_eq!(
237-
impression_analytics.payout_amount,
238-
UnifiedNum::from_u64(1_000_000)
239-
);
240-
assert_eq!(impression_analytics.payout_count, 1);
241-
242-
record(&database.clone(), &campaign, &session, input_events)
243-
.await
244-
.expect("should record");
245-
246-
let analytics = get_all_analytics(&database.pool)
247-
.await
248-
.expect("should find analytics");
249-
let click_analytics = analytics
250-
.iter()
251-
.find(|a| a.event_type == "CLICK")
252-
.expect("There should be a click event");
253-
let impression_analytics = analytics
254-
.iter()
255-
.find(|a| a.event_type == "IMPRESSION")
256-
.expect("There should be an impression event");
257-
assert_eq!(
258-
click_analytics.payout_amount,
259-
UnifiedNum::from_u64(2_000_000)
260-
);
261-
assert_eq!(click_analytics.payout_count, 2);
262-
263-
assert_eq!(
264-
impression_analytics.payout_amount,
265-
UnifiedNum::from_u64(2_000_000)
266-
);
267-
assert_eq!(impression_analytics.payout_count, 2);
268-
}
269-
270-
// Testing record with non-empty events and non-empty session
271-
{
272-
let session = Session {
273-
ip: Default::default(),
274-
country: Some("Bulgaria".into()),
275-
referrer_header: Some("http://127.0.0.1".into()),
276-
os: Some("Windows".into()),
277-
};
246+
#[tokio::test]
247+
async fn test_recording_with_session() {
248+
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
278249

279-
let other_session = Session {
280-
ip: Default::default(),
281-
country: Some("Japan".into()),
282-
referrer_header: Some("http://127.0.0.1".into()),
283-
os: Some("Android".into()),
284-
};
250+
setup_test_migrations(database.pool.clone())
251+
.await
252+
.expect("Migrations should succeed");
253+
254+
let test_events = get_test_events();
285255

286-
let input_events = vec![
287-
test_events["click_with_unit_and_slot"].clone(),
288-
test_events["click_with_different_data"].clone(),
289-
test_events["impression_with_slot_unit_and_referrer"].clone(),
290-
];
291-
292-
record(&database.clone(), &campaign, &session, input_events.clone())
293-
.await
294-
.expect("should record");
295-
296-
let analytics = get_all_analytics(&database.pool)
297-
.await
298-
.expect("should get all analytics");
299-
300-
assert_eq!(analytics.len(), 5);
301-
302-
let with_slot_and_unit: Vec<Analytics> = analytics
303-
.clone()
304-
.into_iter()
305-
.filter(|a| a.ad_unit == Some(DUMMY_IPFS[0]) && a.ad_slot == Some(DUMMY_IPFS[1]))
306-
.collect();
307-
assert_eq!(with_slot_and_unit.len(), 2);
308-
309-
let with_different_slot_and_unit: Vec<Analytics> = analytics
310-
.clone()
311-
.into_iter()
312-
.filter(|a| a.ad_unit == Some(DUMMY_IPFS[2]) && a.ad_slot == Some(DUMMY_IPFS[3]))
313-
.collect();
314-
assert_eq!(with_different_slot_and_unit.len(), 1);
315-
316-
record(
317-
&database.clone(),
318-
&campaign,
319-
&other_session,
320-
input_events.clone(),
321-
)
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+
];
276+
277+
record(&database.clone(), &campaign, &session, input_events.clone())
322278
.await
323279
.expect("should record");
324-
let analytics = get_all_analytics(&database.pool)
325-
.await
326-
.expect("should get all analytics");
327-
328-
assert_eq!(analytics.len(), 8);
329-
330-
let with_slot_and_unit: Vec<Analytics> = analytics
331-
.clone()
332-
.into_iter()
333-
.filter(|a| a.ad_unit == Some(DUMMY_IPFS[0]))
334-
.collect();
335-
assert_eq!(with_slot_and_unit.len(), 4);
336-
337-
let with_different_slot_and_unit: Vec<Analytics> = analytics
338-
.clone()
339-
.into_iter()
340-
.filter(|a| a.ad_unit == Some(DUMMY_IPFS[2]) && a.ad_slot == Some(DUMMY_IPFS[3]))
341-
.collect();
342-
assert_eq!(with_different_slot_and_unit.len(), 2);
343-
344-
let with_country: Vec<Analytics> = analytics
345-
.clone()
346-
.into_iter()
347-
.filter(|a| a.country == Some("Bulgaria".into()))
348-
.collect();
349-
assert_eq!(with_country.len(), 3);
350-
351-
let with_other_country: Vec<Analytics> = analytics
352-
.clone()
353-
.into_iter()
354-
.filter(|a| a.country == Some("Japan".into()))
355-
.collect();
356-
assert_eq!(with_other_country.len(), 3);
357-
358-
let with_os: Vec<Analytics> = analytics
359-
.clone()
360-
.into_iter()
361-
.filter(|a| a.os_name == OperatingSystem::map_os("Windows"))
362-
.collect();
363-
assert_eq!(with_os.len(), 3);
364-
365-
let with_other_os: Vec<Analytics> = analytics
366-
.clone()
367-
.into_iter()
368-
.filter(|a| a.os_name == OperatingSystem::map_os("Android"))
369-
.collect();
370-
assert_eq!(with_other_os.len(), 3);
371-
}
280+
281+
let analytics = get_all_analytics(&database.pool)
282+
.await
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
300+
.iter()
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);
310+
assert_eq!(
311+
with_slot_and_unit.payout_amount,
312+
UnifiedNum::from_u64(2_000_000)
313+
);
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);
321+
assert_eq!(
322+
with_different_slot_and_unit.payout_amount,
323+
UnifiedNum::from_u64(3_000_000)
324+
);
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));
372337
}
373338
}

0 commit comments

Comments
 (0)