Skip to content

Commit 6b9c39e

Browse files
committed
payout::get_payout - test
1 parent ae6b87e commit 6b9c39e

File tree

1 file changed

+111
-13
lines changed

1 file changed

+111
-13
lines changed

sentry/src/payout.rs

Lines changed: 111 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::Session;
22
use chrono::Utc;
33
use primitives::{
44
sentry::Event,
5-
targeting::{get_pricing_bounds, input, Error, Error as EvalError, Input, Output, Rule},
5+
targeting::{get_pricing_bounds, input, Error, Error as EvalError, Input, Output, Rule, eval_multiple},
66
BigNum, Channel, ValidatorId,
77
};
88
use std::{
@@ -77,7 +77,7 @@ pub fn get_payout(channel: &Channel, event: &Event, session: &Session) -> Result
7777
.collect(),
7878
};
7979

80-
eval_multiple(&targeting_rules, &input, &mut output);
80+
eval_and_log(&targeting_rules, &input, &mut output);
8181

8282
if output.show {
8383
let price = match output.price.get(&event_type) {
@@ -97,13 +97,15 @@ pub fn get_payout(channel: &Channel, event: &Event, session: &Session) -> Result
9797
}
9898
}
9999

100-
// @TODO: Logging & move to Targeting when ready
101-
fn eval_multiple(rules: &[Rule], input: &Input, output: &mut Output) {
102-
for rule in rules {
103-
match rule.eval(input, output) {
100+
fn eval_and_log(/* logger: &Logger, channel_id: ChannelId, */rules: &[Rule], input: &Input, output: &mut Output) {
101+
for result in eval_multiple(rules, input, output) {
102+
match result {
104103
Ok(_) => {}
105104
Err(EvalError::UnknownVariable) => {}
106-
Err(EvalError::TypeError) => todo!("OnTypeErr logging"),
105+
Err(EvalError::TypeError) => {
106+
todo!();
107+
// error!(logger, "`WARNING: rule for {:?} failing", channel_id; "rule" => rule, "err" => ?result)
108+
}
107109
}
108110

109111
if !output.show {
@@ -112,9 +114,105 @@ fn eval_multiple(rules: &[Rule], input: &Input, output: &mut Output) {
112114
}
113115
}
114116

115-
// #[cfg(test)]
116-
// mod tests {
117-
// use super::*;
118-
// use primitives::channel::{Pricing, PricingBounds};
119-
// use primitives::util::tests::prep_db::{DUMMY_CHANNEL, IDS};
120-
// }
117+
#[cfg(test)]
118+
mod test {
119+
use super::*;
120+
use primitives::channel::{Pricing, PricingBounds};
121+
use primitives::util::tests::prep_db::{DUMMY_CHANNEL, IDS};
122+
123+
#[test]
124+
fn get_event_payouts_pricing_bounds_impression_event() {
125+
let mut channel = DUMMY_CHANNEL.clone();
126+
channel.deposit_amount = 100.into();
127+
channel.spec.min_per_impression = 8.into();
128+
channel.spec.max_per_impression = 64.into();
129+
channel.spec.pricing_bounds = Some(PricingBounds {
130+
impression: None,
131+
click: Some(Pricing {
132+
min: 23.into(),
133+
max: 100.into(),
134+
}),
135+
});
136+
137+
let event = Event::Impression {
138+
publisher: IDS["leader"],
139+
ad_unit: None,
140+
ad_slot: None,
141+
referrer: None,
142+
};
143+
144+
let session = Session {
145+
ip: None,
146+
country: None,
147+
referrer_header: None,
148+
os: None,
149+
};
150+
151+
let payout = get_payout(&channel, &event, &session).expect("Should be OK");
152+
153+
let expected_option = Some((IDS["leader"], 8.into()));
154+
assert_eq!(expected_option, payout, "pricingBounds: impression event");
155+
}
156+
157+
#[test]
158+
fn get_event_payouts_pricing_bounds_click_event() {
159+
let mut channel = DUMMY_CHANNEL.clone();
160+
channel.deposit_amount = 100.into();
161+
channel.spec.min_per_impression = 8.into();
162+
channel.spec.max_per_impression = 64.into();
163+
channel.spec.pricing_bounds = Some(PricingBounds {
164+
impression: None,
165+
click: Some(Pricing {
166+
min: 23.into(),
167+
max: 100.into(),
168+
}),
169+
});
170+
171+
let event = Event::Click {
172+
publisher: IDS["leader"],
173+
ad_unit: None,
174+
ad_slot: None,
175+
referrer: None,
176+
};
177+
178+
let session = Session {
179+
ip: None,
180+
country: None,
181+
referrer_header: None,
182+
os: None,
183+
};
184+
185+
let payout = get_payout(&channel, &event, &session).expect("Should be OK");
186+
187+
let expected_option = Some((IDS["leader"], 23.into()));
188+
assert_eq!(expected_option, payout, "pricingBounds: click event");
189+
}
190+
191+
#[test]
192+
fn get_event_payouts_pricing_bounds_close_event() {
193+
let mut channel = DUMMY_CHANNEL.clone();
194+
channel.deposit_amount = 100.into();
195+
channel.spec.min_per_impression = 8.into();
196+
channel.spec.max_per_impression = 64.into();
197+
channel.spec.pricing_bounds = Some(PricingBounds {
198+
impression: None,
199+
click: Some(Pricing {
200+
min: 23.into(),
201+
max: 100.into(),
202+
}),
203+
});
204+
205+
let event = Event::Close;
206+
207+
let session = Session {
208+
ip: None,
209+
country: None,
210+
referrer_header: None,
211+
os: None,
212+
};
213+
214+
let payout = get_payout(&channel, &event, &session).expect("Should be OK");
215+
216+
assert_eq!(None, payout, "pricingBounds: click event");
217+
}
218+
}

0 commit comments

Comments
 (0)