Skip to content

Commit e424a5e

Browse files
committed
[test] primitives - eval - Function::Get
1 parent e2302a6 commit e424a5e

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

primitives/src/targeting.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl Input {
2525
let spec = &self.global.channel.spec;
2626

2727
match key {
28+
// AdView scope, accessible only on the AdView
2829
"adView.secondsSinceCampaignImpression" => self
2930
.ad_view
3031
.as_ref()
@@ -35,9 +36,22 @@ impl Input {
3536
.as_ref()
3637
.map(|ad_view| Value::Bool(ad_view.has_custom_preferences))
3738
.ok_or(Error::UnknownVariable),
39+
"adView.navigatorLanguage" => self
40+
.ad_view
41+
.as_ref()
42+
.map(|ad_view| Value::String(ad_view.navigator_language.clone()))
43+
.ok_or(Error::UnknownVariable),
44+
// Global scope, accessible everywhere
3845
"adSlotId" => Ok(Value::String(self.global.ad_slot_id.clone())),
3946
"adSlotType" => Ok(Value::String(self.global.ad_slot_type.clone())),
4047
"publisherId" => Ok(Value::String(self.global.publisher_id.to_checksum())),
48+
"country" => self
49+
.global
50+
.country
51+
.clone()
52+
.ok_or(Error::UnknownVariable)
53+
.map(Value::String),
54+
"eventType" => Ok(Value::String(self.global.event_type.clone())),
4155
"secondsSinceEpoch" => Ok(Value::Number(self.global.seconds_since_epoch.into())),
4256
"userAgentOS" => self
4357
.global
@@ -51,7 +65,7 @@ impl Input {
5165
.clone()
5266
.map(Value::String)
5367
.ok_or(Error::UnknownVariable),
54-
68+
// Global scope, accessible everywhere, campaign-dependant
5569
"adUnitId" => {
5670
let ipfs = self
5771
.global
@@ -113,6 +127,7 @@ impl Input {
113127

114128
Ok(Value::BigNum(earned))
115129
}
130+
// adSlot scope, accessible on Supermarket and AdView
116131
"adSlot.categories" => self
117132
.ad_slot
118133
.as_ref()
@@ -221,12 +236,15 @@ impl Output {
221236
"boost" => {
222237
let boost = Number::from_f64(self.boost).ok_or(Error::TypeError)?;
223238
Ok(Value::Number(boost))
224-
},
239+
}
225240
price_key if price_key.starts_with("price.") => {
226-
let price = self.price.get(price_key.trim_start_matches("price.")).ok_or(Error::UnknownVariable)?;
241+
let price = self
242+
.price
243+
.get(price_key.trim_start_matches("price."))
244+
.ok_or(Error::UnknownVariable)?;
227245
Ok(Value::BigNum(price.clone()))
228-
},
229-
_ => Err(Error::UnknownVariable)
246+
}
247+
_ => Err(Error::UnknownVariable),
230248
}
231249
}
232250
}
@@ -352,7 +370,12 @@ mod test {
352370
};
353371

354372
assert_eq!(Ok(Value::Bool(false)), output.try_get("show"));
355-
assert_eq!(Ok(Value::Number(Number::from_f64(5.5).expect("Should make a number"))), output.try_get("boost"));
373+
assert_eq!(
374+
Ok(Value::Number(
375+
Number::from_f64(5.5).expect("Should make a number")
376+
)),
377+
output.try_get("boost")
378+
);
356379
assert_eq!(Ok(Value::BigNum(100.into())), output.try_get("price.one"));
357380
assert_eq!(Err(Error::UnknownVariable), output.try_get("price.unknown"));
358381
assert_eq!(Err(Error::UnknownVariable), output.try_get("unknown"));

primitives/src/targeting/eval.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ fn eval(input: &Input, output: &mut Output, rule: &Rule) -> Result<Option<Value>
895895
.try_bignum()?;
896896
let deposit_asset = &input.global.channel.deposit_asset;
897897

898-
let divisor = DEPOSIT_ASSETS_MAP.get(deposit_asset).ok_or(Error::TypeError)?;
898+
let divisor = DEPOSIT_ASSETS_MAP
899+
.get(deposit_asset)
900+
.ok_or(Error::TypeError)?;
899901
let amount_in_usd = amount.div(divisor).to_f64().ok_or(Error::TypeError)?;
900902
let amount_as_number = Number::from_f64(amount_in_usd).ok_or(Error::TypeError)?;
901903
Some(Value::Number(amount_as_number))

primitives/src/targeting/eval_test.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ mod dsl_test {
253253

254254
assert_eq!(Some(&BigNum::from(20)), output.price.get("IMPRESSION"));
255255
}
256+
257+
#[test]
258+
fn test_get_eval() {
259+
let input = get_default_input();
260+
let mut output = Output {
261+
show: true,
262+
boost: 42.0,
263+
price: Default::default(),
264+
};
265+
266+
let input_country = Function::Get("country".to_string())
267+
.eval(&input, &mut output)
268+
.expect("Should get input.global.country");
269+
assert_eq!(Some(Value::String("bg".to_string())), input_country);
270+
271+
let output_boost = Function::Get("boost".to_string())
272+
.eval(&input, &mut output)
273+
.expect("Should get output.boost");
274+
let expected_output_boost = Number::from_f64(42.0).expect("should create Number");
275+
276+
assert_eq!(Some(Value::Number(expected_output_boost)), output_boost);
277+
}
256278
}
257279

258280
mod math_functions {
@@ -1320,7 +1342,9 @@ mod string_and_array {
13201342
for (key, value) in &*DEPOSIT_ASSETS_MAP {
13211343
input.global.channel.deposit_asset = key.to_string();
13221344
let amount_crypto = BigNum::from(100).mul(value);
1323-
let amount_usd = Some(Value::Number(Number::from_f64(100.0).expect("should create a float")));
1345+
let amount_usd = Some(Value::Number(
1346+
Number::from_f64(100.0).expect("should create a float"),
1347+
));
13241348
let rule = Rule::Function(Function::new_get_price_in_usd(Rule::Value(Value::BigNum(
13251349
amount_crypto,
13261350
))));

0 commit comments

Comments
 (0)