@@ -2,7 +2,7 @@ use crate::Session;
2
2
use chrono:: Utc ;
3
3
use primitives:: {
4
4
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 } ,
6
6
BigNum , Channel , ValidatorId ,
7
7
} ;
8
8
use std:: {
@@ -77,7 +77,7 @@ pub fn get_payout(channel: &Channel, event: &Event, session: &Session) -> Result
77
77
. collect ( ) ,
78
78
} ;
79
79
80
- eval_multiple ( & targeting_rules, & input, & mut output) ;
80
+ eval_and_log ( & targeting_rules, & input, & mut output) ;
81
81
82
82
if output. show {
83
83
let price = match output. price . get ( & event_type) {
@@ -97,13 +97,15 @@ pub fn get_payout(channel: &Channel, event: &Event, session: &Session) -> Result
97
97
}
98
98
}
99
99
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 {
104
103
Ok ( _) => { }
105
104
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
+ }
107
109
}
108
110
109
111
if !output. show {
@@ -112,9 +114,105 @@ fn eval_multiple(rules: &[Rule], input: &Input, output: &mut Output) {
112
114
}
113
115
}
114
116
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