|
1 |
| -//use chrono::{DateTime, Utc}; |
2 |
| -//use fake::faker::*; |
3 |
| -//use time::Duration; |
4 |
| -// |
5 |
| -//use crate::asset::fixtures::get_asset; |
6 |
| -//use crate::fixtures::{get_targeting_tags, get_validator}; |
7 |
| -//use crate::test_util; |
8 |
| -//use crate::BigNum; |
9 |
| -// |
10 |
| -//use super::{Channel, ChannelId, ChannelSpec, SpecValidators, ValidatorDesc}; |
11 |
| -// |
| 1 | +use chrono::{DateTime, TimeZone, Utc}; |
| 2 | +use fake::faker::*; |
| 3 | +use time::Duration; |
| 4 | + |
| 5 | +use crate::targeting_tag::fixtures::get_targeting_tags; |
| 6 | +use crate::validator::fixtures::get_validator; |
| 7 | +use crate::{BigNum, EventSubmission}; |
| 8 | + |
| 9 | +use super::{Channel, ChannelSpec, SpecValidators, ValidatorDesc}; |
| 10 | +use crate::util::tests::take_one; |
| 11 | + |
| 12 | +const ASSETS_LIST: [&str; 8] = ["DAI", "BGN", "EUR", "USD", "ADX", "BTC", "LIT", "ETH"]; |
| 13 | + |
12 | 14 | ///// It will get the length of channel_id bytes and will fill enough bytes in front
|
13 | 15 | ///// If > 32 bytes &str is passed it will `panic!`
|
14 | 16 | //pub fn get_channel_id(channel_id: &str) -> ChannelId {
|
|
24 | 26 | //
|
25 | 27 | // ChannelId { bytes: id }
|
26 | 28 | //}
|
27 |
| -// |
28 |
| -//pub fn get_channel( |
29 |
| -// id: &str, |
30 |
| -// valid_until: &Option<DateTime<Utc>>, |
31 |
| -// spec: Option<ChannelSpec>, |
32 |
| -//) -> Channel { |
33 |
| -// let channel_id = get_channel_id(id); |
34 |
| -// let deposit_amount = BigNum::from(<Faker as Number>::between(100, 5000)); |
35 |
| -// let valid_until: DateTime<Utc> = valid_until.unwrap_or_else(|| { |
36 |
| -// let future_from = Utc::now() + Duration::days(7); |
37 |
| -// test_util::time::datetime_between(&future_from, None) |
38 |
| -// }); |
39 |
| -// let creator = <Faker as Name>::name(); |
40 |
| -// let deposit_asset = get_asset(); |
41 |
| -// let spec = spec.unwrap_or_else(|| { |
42 |
| -// get_channel_spec(ValidatorsOption::Generate { |
43 |
| -// validators_prefix: id, |
44 |
| -// }) |
45 |
| -// }); |
46 |
| -// |
47 |
| -// Channel { |
48 |
| -// id: channel_id, |
49 |
| -// creator, |
50 |
| -// deposit_asset, |
51 |
| -// deposit_amount, |
52 |
| -// valid_until, |
53 |
| -// spec, |
54 |
| -// } |
55 |
| -//} |
| 29 | + |
| 30 | +pub fn get_dummy_channel() -> Channel { |
| 31 | + let leader = ValidatorDesc { |
| 32 | + id: "awesomeLeader".to_string(), |
| 33 | + url: "http://localhost:8005".to_string(), |
| 34 | + fee: 100.into(), |
| 35 | + }; |
| 36 | + let follower = ValidatorDesc { |
| 37 | + id: "awesomeFollower".to_string(), |
| 38 | + url: "http://localhost:8006".to_string(), |
| 39 | + fee: 100.into(), |
| 40 | + }; |
| 41 | + let nonce = BigNum::from(<Faker as Number>::between(100_000_000, 999_999_999)); |
| 42 | + |
| 43 | + Channel { |
| 44 | + id: "awesomeTestChannel".to_string(), |
| 45 | + creator: "awesomeCreator".to_string(), |
| 46 | + deposit_asset: "DAI".to_string(), |
| 47 | + deposit_amount: 1_000.into(), |
| 48 | + // UNIX timestamp for 2100-01-01 |
| 49 | + valid_until: Utc.timestamp(4_102_444_800, 0), |
| 50 | + spec: ChannelSpec { |
| 51 | + title: None, |
| 52 | + validators: SpecValidators([leader, follower]), |
| 53 | + max_per_impression: 10.into(), |
| 54 | + min_per_impression: 10.into(), |
| 55 | + targeting: vec![], |
| 56 | + min_targeting_score: None, |
| 57 | + event_submission: EventSubmission { allow: vec![] }, |
| 58 | + // July 29, 2019 7:00:00 AM |
| 59 | + created: Utc.timestamp(1_564_383_600, 0), |
| 60 | + active_from: None, |
| 61 | + nonce, |
| 62 | + withdraw_period_start: Utc.timestamp_millis(4_073_414_400_000), |
| 63 | + ad_units: vec![], |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub fn get_channel( |
| 69 | + id: &str, |
| 70 | + valid_until: &Option<DateTime<Utc>>, |
| 71 | + spec: Option<ChannelSpec>, |
| 72 | +) -> Channel { |
| 73 | + let deposit_amount = BigNum::from(<Faker as Number>::between(100, 5000)); |
| 74 | + let valid_until: DateTime<Utc> = valid_until.unwrap_or_else(|| { |
| 75 | + let future_from = Utc::now() + Duration::days(7); |
| 76 | + crate::util::tests::time::datetime_between(&future_from, None) |
| 77 | + }); |
| 78 | + let creator = <Faker as Name>::name(); |
| 79 | + let deposit_asset = take_one(&ASSETS_LIST).into(); |
| 80 | + let spec = spec.unwrap_or_else(|| { |
| 81 | + get_channel_spec(ValidatorsOption::Generate { |
| 82 | + validators_prefix: id, |
| 83 | + }) |
| 84 | + }); |
| 85 | + |
| 86 | + Channel { |
| 87 | + id: id.into(), |
| 88 | + creator, |
| 89 | + deposit_asset, |
| 90 | + deposit_amount, |
| 91 | + valid_until, |
| 92 | + spec, |
| 93 | + } |
| 94 | +} |
56 | 95 | //
|
57 | 96 | //pub fn get_channels(count: usize, valid_until_ge: Option<DateTime<Utc>>) -> Vec<Channel> {
|
58 | 97 | // (1..=count)
|
|
67 | 106 | // .collect()
|
68 | 107 | //}
|
69 | 108 | //
|
70 |
| -//pub enum ValidatorsOption<'a> { |
71 |
| -// Pair { |
72 |
| -// leader: ValidatorDesc, |
73 |
| -// follower: ValidatorDesc, |
74 |
| -// }, |
75 |
| -// SpecValidators(SpecValidators), |
76 |
| -// Generate { |
77 |
| -// validators_prefix: &'a str, |
78 |
| -// }, |
79 |
| -//} |
80 |
| -// |
81 |
| -//pub fn get_channel_spec(validators_option: ValidatorsOption<'_>) -> ChannelSpec { |
82 |
| -// use crate::EventSubmission; |
83 |
| -// use test_util::take_one; |
84 |
| -// |
85 |
| -// let validators = match validators_option { |
86 |
| -// ValidatorsOption::Pair { leader, follower } => [leader, follower].into(), |
87 |
| -// ValidatorsOption::SpecValidators(spec_validators) => spec_validators, |
88 |
| -// ValidatorsOption::Generate { validators_prefix } => [ |
89 |
| -// get_validator(&format!("{} leader", validators_prefix), None), |
90 |
| -// get_validator(&format!("{} follower", validators_prefix), None), |
91 |
| -// ] |
92 |
| -// .into(), |
93 |
| -// }; |
94 |
| -// |
95 |
| -// let title_string = Some(<Faker as Lorem>::sentence(3, 4)); |
96 |
| -// |
97 |
| -// let title = take_one(&[&title_string, &None]).to_owned(); |
98 |
| -// let max_per_impression = BigNum::from(<Faker as Number>::between(250, 500)); |
99 |
| -// let min_per_impression = BigNum::from(<Faker as Number>::between(1, 250)); |
100 |
| -// let nonce = BigNum::from(<Faker as Number>::between(100_000_000, 999_999_999)); |
101 |
| -// let min_targeting_score = |
102 |
| -// take_one(&[&None, &Some(<Faker as Number>::between(1, 500))]).to_owned(); |
103 |
| -// |
104 |
| -// ChannelSpec { |
105 |
| -// validators, |
106 |
| -// title, |
107 |
| -// max_per_impression, |
108 |
| -// min_per_impression, |
109 |
| -// targeting: get_targeting_tags(<Faker as Number>::between(0, 5)), |
110 |
| -// min_targeting_score, |
111 |
| -// // @TODO: `EventSubmission` fixture issue #27 |
112 |
| -// event_submission: EventSubmission { allow: vec![] }, |
113 |
| -// created: Utc::now(), |
114 |
| -// active_from: Some(Utc::now()), |
115 |
| -// nonce, |
116 |
| -// withdraw_period_start: Utc::now(), |
117 |
| -// ad_units: Vec::new(), |
118 |
| -// } |
119 |
| -//} |
| 109 | +pub enum ValidatorsOption<'a> { |
| 110 | + Pair { |
| 111 | + leader: ValidatorDesc, |
| 112 | + follower: ValidatorDesc, |
| 113 | + }, |
| 114 | + SpecValidators(SpecValidators), |
| 115 | + Generate { |
| 116 | + validators_prefix: &'a str, |
| 117 | + }, |
| 118 | +} |
| 119 | + |
| 120 | +pub fn get_channel_spec(validators_option: ValidatorsOption<'_>) -> ChannelSpec { |
| 121 | + let validators = match validators_option { |
| 122 | + ValidatorsOption::Pair { leader, follower } => [leader, follower].into(), |
| 123 | + ValidatorsOption::SpecValidators(spec_validators) => spec_validators, |
| 124 | + ValidatorsOption::Generate { validators_prefix } => [ |
| 125 | + get_validator(&format!("{} leader", validators_prefix), None), |
| 126 | + get_validator(&format!("{} follower", validators_prefix), None), |
| 127 | + ] |
| 128 | + .into(), |
| 129 | + }; |
| 130 | + |
| 131 | + let title_string = Some(<Faker as Lorem>::sentence(3, 4)); |
| 132 | + |
| 133 | + let title = take_one(&[&title_string, &None]).to_owned(); |
| 134 | + let max_per_impression = BigNum::from(<Faker as Number>::between(250, 500)); |
| 135 | + let min_per_impression = BigNum::from(<Faker as Number>::between(1, 250)); |
| 136 | + let nonce = BigNum::from(<Faker as Number>::between(100_000_000, 999_999_999)); |
| 137 | + let min_targeting_score = |
| 138 | + take_one(&[&None, &Some(<Faker as Number>::between(1_f64, 500_f64))]).to_owned(); |
| 139 | + |
| 140 | + ChannelSpec { |
| 141 | + validators, |
| 142 | + title, |
| 143 | + max_per_impression, |
| 144 | + min_per_impression, |
| 145 | + targeting: get_targeting_tags(<Faker as Number>::between(0, 5)), |
| 146 | + min_targeting_score, |
| 147 | + // @TODO: `EventSubmission` fixture issue #27 |
| 148 | + event_submission: EventSubmission { allow: vec![] }, |
| 149 | + created: Utc::now(), |
| 150 | + active_from: Some(Utc::now()), |
| 151 | + nonce, |
| 152 | + withdraw_period_start: Utc::now(), |
| 153 | + ad_units: Vec::new(), |
| 154 | + } |
| 155 | +} |
0 commit comments