Skip to content

Commit b607cdb

Browse files
committed
primitives - targeting - test Output::from(&Channel) & Function::Set
1 parent 1b62ed3 commit b607cdb

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

primitives/src/targeting.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,29 @@ mod test {
198198

199199
assert_eq!(Value::BigNum(BigNum::from(50)), global_campaign_budget);
200200
}
201+
202+
#[test]
203+
fn test_output_from_channel() {
204+
use crate::channel::{Pricing, PricingBounds};
205+
use crate::util::tests::prep_db::DUMMY_CHANNEL;
206+
207+
let mut channel = DUMMY_CHANNEL.clone();
208+
channel.spec.pricing_bounds = Some(PricingBounds {
209+
impression: Some(Pricing {
210+
min: 1_000.into(),
211+
max: 2_000.into(),
212+
}),
213+
click: Some(Pricing {
214+
min: 3_000.into(),
215+
max: 4_000.into(),
216+
}),
217+
});
218+
219+
let output = Output::from(&channel);
220+
221+
assert_eq!(true, output.show);
222+
assert_eq!(1.0, output.boost);
223+
assert_eq!(Some(&BigNum::from(1_000)), output.price.get("IMPRESSION"));
224+
assert_eq!(Some(&BigNum::from(3_000)), output.price.get("CLICK"));
225+
}
201226
}

primitives/src/targeting/eval.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ impl Function {
123123
Self::Get(key.to_string())
124124
}
125125

126+
pub fn new_set(key: &str, eval: impl Into<Rule>) -> Self {
127+
Self::Set(key.to_string(), Box::new(eval.into()))
128+
}
129+
126130
pub fn new_bn(value: impl Into<Value>) -> Self {
127131
Self::Bn(value.into())
128132
}
@@ -492,4 +496,34 @@ mod test {
492496
assert_eq!(Err(Error::TypeError), rule.eval(&input, &mut output));
493497
}
494498
}
499+
500+
#[test]
501+
fn test_set_eval() {
502+
use crate::channel::{Pricing, PricingBounds};
503+
use crate::util::tests::prep_db::DUMMY_CHANNEL;
504+
505+
let mut channel = DUMMY_CHANNEL.clone();
506+
channel.spec.pricing_bounds = Some(PricingBounds {
507+
impression: Some(Pricing {
508+
min: 1_000.into(),
509+
max: 2_000.into(),
510+
}),
511+
click: Some(Pricing {
512+
min: 3_000.into(),
513+
max: 4_000.into(),
514+
}),
515+
});
516+
517+
let input = Input::default();
518+
let mut output = Output::from(&channel);
519+
520+
assert_eq!(Some(&BigNum::from(1_000)), output.price.get("IMPRESSION"));
521+
522+
let set_to = Value::BigNum(BigNum::from(20));
523+
let rule = Rule::Function(Function::new_set("price.IMPRESSION", set_to));
524+
525+
assert_eq!(Ok(None), rule.eval(&input, &mut output));
526+
527+
assert_eq!(Some(&BigNum::from(20)), output.price.get("IMPRESSION"));
528+
}
495529
}

0 commit comments

Comments
 (0)