Skip to content

Commit 1638300

Browse files
committed
sentry - spender - calculate_fee tests
1 parent d0f8cc6 commit 1638300

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

sentry/src/payout.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ mod test {
9595
use super::*;
9696
use primitives::{
9797
campaign::{Pricing, PricingBounds},
98+
test_util::PUBLISHER,
9899
util::tests::{
99100
discard_logger,
100101
prep_db::{ADDRESSES, DUMMY_CAMPAIGN},
@@ -155,7 +156,7 @@ mod test {
155156
});
156157

157158
let event = Event::Click {
158-
publisher: ADDRESSES["leader"],
159+
publisher: *PUBLISHER,
159160
ad_unit: None,
160161
ad_slot: None,
161162
referrer: None,
@@ -170,7 +171,7 @@ mod test {
170171

171172
let payout = get_payout(&logger, &campaign, &event, &session).expect("Should be OK");
172173

173-
let expected_option = Some((ADDRESSES["leader"], 23.into()));
174+
let expected_option = Some((*PUBLISHER, 23.into()));
174175
assert_eq!(expected_option, payout, "pricingBounds: click event");
175176
}
176177
}

sentry/src/spender.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod fee {
33

44
use primitives::{Address, DomainError, UnifiedNum, ValidatorDesc};
55

6-
/// Calculates the fee for a specified validator
6+
/// Calculates the fee for a given payout of the specified validator
77
/// This function will return None if the provided validator is not part of the Campaign / Channel
88
/// In the case of overflow when calculating the payout, an error will be returned
99
pub fn calculate_fee(
@@ -16,4 +16,51 @@ pub mod fee {
1616
.map(|pro_mille_fee| pro_mille_fee.div_floor(&PRO_MILLE))
1717
.ok_or_else(|| DomainError::InvalidArgument("payout calculation overflow".to_string()))
1818
}
19+
20+
#[cfg(test)]
21+
mod test {
22+
use primitives::{
23+
test_util::{PUBLISHER, DUMMY_VALIDATOR_LEADER}, UnifiedNum,
24+
};
25+
26+
use crate::spender::fee::calculate_fee;
27+
28+
#[test]
29+
fn test_calcualtion_of_fee() {
30+
let dummy_leader = DUMMY_VALIDATOR_LEADER.clone();
31+
assert_eq!(
32+
UnifiedNum::from(100),
33+
dummy_leader.fee,
34+
"Dummy validator leader fee has changed, please revisit this test!"
35+
);
36+
37+
// normal payout - no flooring
38+
{
39+
let payout = (*PUBLISHER, UnifiedNum::from(300));
40+
41+
let validator_fee =
42+
calculate_fee(payout, &dummy_leader).expect("Should not overflow");
43+
44+
assert_eq!(UnifiedNum::from(30), validator_fee);
45+
}
46+
47+
// payout with flooring
48+
{
49+
// 66 * 100 / 1000 = 6.6 = 6
50+
let payout = (*PUBLISHER, UnifiedNum::from(66));
51+
let validator_fee =
52+
calculate_fee(payout, &dummy_leader).expect("Should not overflow");
53+
54+
assert_eq!(UnifiedNum::from(6), validator_fee);
55+
}
56+
57+
// Overflow
58+
{
59+
// u64::MAX * 100 (overflow) / 1000
60+
let payout = (*PUBLISHER, UnifiedNum::from(u64::MAX));
61+
62+
calculate_fee(payout, &dummy_leader).expect_err("Should overflow");
63+
}
64+
}
65+
}
1966
}

0 commit comments

Comments
 (0)