Skip to content

Commit 6c03ab3

Browse files
committed
primitives - add Market responses & AdUnits for testing
1 parent 7ba55bb commit 6c03ab3

File tree

2 files changed

+317
-7
lines changed

2 files changed

+317
-7
lines changed

primitives/src/market.rs

Lines changed: 249 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use chrono::serde::ts_milliseconds;
22
use chrono::{DateTime, Utc};
33
use serde::{Deserialize, Serialize};
4+
use url::Url;
45

56
use std::fmt;
67

7-
use crate::{BalancesMap, BigNum, Channel};
8+
use crate::{AdSlot, AdUnit, BalancesMap, BigNum, Channel};
9+
pub use ad_unit::AdUnitsResponse;
810

911
// Data structs specific to the market
1012
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
@@ -55,3 +57,249 @@ pub struct Campaign {
5557
pub channel: Channel,
5658
pub status: Status,
5759
}
60+
61+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62+
#[serde(
63+
rename_all = "camelCase",
64+
from = "ad_slot::ShimResponse",
65+
into = "ad_slot::ShimResponse"
66+
)]
67+
pub struct AdSlotResponse {
68+
pub slot: AdSlot,
69+
pub accepted_referrers: Vec<Url>,
70+
pub categories: Vec<String>,
71+
pub alexa_rank: Option<f64>,
72+
}
73+
74+
#[derive(Debug, Deserialize)]
75+
#[serde(
76+
rename_all = "camelCase",
77+
from = "ad_unit::ShimAdUnitResponse",
78+
into = "ad_unit::ShimAdUnitResponse"
79+
)]
80+
pub struct AdUnitResponse {
81+
pub unit: AdUnit,
82+
}
83+
84+
mod ad_unit {
85+
use crate::{AdUnit, ValidatorId, IPFS};
86+
use chrono::{DateTime, Utc};
87+
use serde::{Deserialize, Serialize};
88+
89+
use super::AdUnitResponse;
90+
91+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
92+
#[serde(rename_all = "camelCase", from = "Vec<Shim>", into = "Vec<Shim>")]
93+
pub struct AdUnitsResponse(pub Vec<AdUnit>);
94+
95+
impl From<Vec<Shim>> for AdUnitsResponse {
96+
fn from(vec_of_shims: Vec<Shim>) -> Self {
97+
Self(vec_of_shims.into_iter().map(Into::into).collect())
98+
}
99+
}
100+
101+
impl Into<Vec<Shim>> for AdUnitsResponse {
102+
fn into(self) -> Vec<Shim> {
103+
self.0.into_iter().map(Into::into).collect()
104+
}
105+
}
106+
107+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
108+
#[serde(rename_all = "camelCase")]
109+
pub struct ShimAdUnitResponse {
110+
unit: Shim,
111+
}
112+
113+
impl From<AdUnitResponse> for ShimAdUnitResponse {
114+
fn from(response: AdUnitResponse) -> Self {
115+
Self {
116+
unit: response.unit.into(),
117+
}
118+
}
119+
}
120+
121+
impl From<ShimAdUnitResponse> for AdUnitResponse {
122+
fn from(shim: ShimAdUnitResponse) -> Self {
123+
Self {
124+
unit: shim.unit.into(),
125+
}
126+
}
127+
}
128+
129+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
130+
#[serde(rename_all = "camelCase")]
131+
/// This AdUnit Shim has only one difference with the Validator [`AdUnit`](crate::AdUnit)
132+
/// The `created` and `modified` timestamps here are in strings (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Timestamp_string),
133+
/// instead of being millisecond timestamps
134+
pub struct Shim {
135+
pub ipfs: IPFS,
136+
#[serde(rename = "type")]
137+
pub ad_type: String,
138+
pub media_url: String,
139+
pub media_mime: String,
140+
pub target_url: String,
141+
#[serde(default, skip_serializing_if = "Option::is_none")]
142+
pub min_targeting_score: Option<f64>,
143+
pub owner: ValidatorId,
144+
pub created: DateTime<Utc>,
145+
#[serde(default, skip_serializing_if = "Option::is_none")]
146+
pub title: Option<String>,
147+
#[serde(default, skip_serializing_if = "Option::is_none")]
148+
pub description: Option<String>,
149+
#[serde(default)]
150+
pub archived: bool,
151+
#[serde(default, skip_serializing_if = "Option::is_none")]
152+
pub modified: Option<DateTime<Utc>>,
153+
}
154+
155+
impl Into<AdUnit> for Shim {
156+
fn into(self) -> AdUnit {
157+
AdUnit {
158+
ipfs: self.ipfs,
159+
ad_type: self.ad_type,
160+
media_url: self.media_url,
161+
media_mime: self.media_mime,
162+
target_url: self.target_url,
163+
min_targeting_score: self.min_targeting_score,
164+
owner: self.owner,
165+
created: self.created,
166+
title: self.title,
167+
description: self.description,
168+
archived: self.archived,
169+
modified: self.modified,
170+
}
171+
}
172+
}
173+
174+
impl From<AdUnit> for Shim {
175+
fn from(ad_unit: AdUnit) -> Self {
176+
Self {
177+
ipfs: ad_unit.ipfs,
178+
ad_type: ad_unit.ad_type,
179+
media_url: ad_unit.media_url,
180+
media_mime: ad_unit.media_mime,
181+
target_url: ad_unit.target_url,
182+
min_targeting_score: ad_unit.min_targeting_score,
183+
owner: ad_unit.owner,
184+
created: ad_unit.created,
185+
title: ad_unit.title,
186+
description: ad_unit.description,
187+
archived: ad_unit.archived,
188+
modified: ad_unit.modified,
189+
}
190+
}
191+
}
192+
}
193+
194+
mod ad_slot {
195+
use std::collections::HashMap;
196+
197+
use chrono::{DateTime, Utc};
198+
use serde::{Deserialize, Serialize};
199+
use url::Url;
200+
201+
use crate::{targeting::Rule, AdSlot, BigNum, ValidatorId};
202+
203+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
204+
#[serde(rename_all = "camelCase")]
205+
pub struct ShimResponse {
206+
pub slot: Shim,
207+
pub accepted_referrers: Vec<Url>,
208+
pub categories: Vec<String>,
209+
pub alexa_rank: Option<f64>,
210+
}
211+
212+
impl From<super::AdSlotResponse> for ShimResponse {
213+
fn from(response: super::AdSlotResponse) -> Self {
214+
Self {
215+
slot: Shim::from(response.slot),
216+
accepted_referrers: response.accepted_referrers,
217+
categories: response.categories,
218+
alexa_rank: response.alexa_rank,
219+
}
220+
}
221+
}
222+
223+
impl From<ShimResponse> for super::AdSlotResponse {
224+
fn from(shim_response: ShimResponse) -> Self {
225+
Self {
226+
slot: shim_response.slot.into(),
227+
accepted_referrers: shim_response.accepted_referrers,
228+
categories: shim_response.categories,
229+
alexa_rank: shim_response.alexa_rank,
230+
}
231+
}
232+
}
233+
234+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
235+
#[serde(rename_all = "camelCase")]
236+
/// This AdSlot Shim has only one difference with the Validator `primitives::AdSlot`
237+
/// The `created` and `modified` timestamps here are in strings (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Timestamp_string),
238+
/// instead of being millisecond timestamps
239+
pub struct Shim {
240+
pub ipfs: String,
241+
#[serde(rename = "type")]
242+
pub ad_type: String,
243+
#[serde(default)]
244+
pub min_per_impression: Option<HashMap<String, BigNum>>,
245+
#[serde(default)]
246+
pub rules: Vec<Rule>,
247+
#[serde(default)]
248+
pub fallback_unit: Option<String>,
249+
pub owner: ValidatorId,
250+
/// DateTime uses `RFC 3339` by default
251+
/// This is not the usual `milliseconds timestamp`
252+
/// as the original
253+
pub created: DateTime<Utc>,
254+
#[serde(default)]
255+
pub title: Option<String>,
256+
#[serde(default)]
257+
pub description: Option<String>,
258+
#[serde(default)]
259+
pub website: Option<String>,
260+
#[serde(default)]
261+
pub archived: bool,
262+
/// DateTime uses `RFC 3339` by default
263+
/// This is not the usual `milliseconds timestamp`
264+
/// as the original
265+
pub modified: Option<DateTime<Utc>>,
266+
}
267+
268+
impl From<AdSlot> for Shim {
269+
fn from(ad_slot: AdSlot) -> Self {
270+
Self {
271+
ipfs: ad_slot.ipfs,
272+
ad_type: ad_slot.ad_type,
273+
min_per_impression: ad_slot.min_per_impression,
274+
rules: ad_slot.rules,
275+
fallback_unit: ad_slot.fallback_unit,
276+
owner: ad_slot.owner,
277+
created: ad_slot.created,
278+
title: ad_slot.title,
279+
description: ad_slot.description,
280+
website: ad_slot.website,
281+
archived: ad_slot.archived,
282+
modified: ad_slot.modified,
283+
}
284+
}
285+
}
286+
287+
impl Into<AdSlot> for Shim {
288+
fn into(self) -> AdSlot {
289+
AdSlot {
290+
ipfs: self.ipfs,
291+
ad_type: self.ad_type,
292+
min_per_impression: self.min_per_impression,
293+
rules: self.rules,
294+
fallback_unit: self.fallback_unit,
295+
owner: self.owner,
296+
created: self.created,
297+
title: self.title,
298+
description: self.description,
299+
website: self.website,
300+
archived: self.archived,
301+
modified: self.modified,
302+
}
303+
}
304+
}
305+
}

primitives/src/util/tests/prep_db.rs

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use crate::{
2-
channel::{Pricing, PricingBounds},
3-
targeting::Rules,
4-
BigNum, Channel, ChannelId, ChannelSpec, EventSubmission, SpecValidators, ValidatorDesc,
5-
ValidatorId, IPFS,
6-
};
1+
use crate::{AdUnit, BigNum, Channel, ChannelId, ChannelSpec, EventSubmission, IPFS, SpecValidators, ValidatorDesc, ValidatorId, channel::{Pricing, PricingBounds}, targeting::Rules};
72
use chrono::{TimeZone, Utc};
83
use fake::faker::{Faker, Number};
94
use hex::FromHex;
@@ -86,6 +81,73 @@ lazy_static! {
8681
}
8782
};
8883

84+
pub static ref DUMMY_AD_UNITS: [AdUnit; 4] = [
85+
AdUnit {
86+
ipfs: IPFS::try_from("Qmasg8FrbuSQpjFu3kRnZF9beg8rEBFrqgi1uXDRwCbX5f")
87+
.expect("should convert"),
88+
media_url: "ipfs://QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR".to_string(),
89+
media_mime: "image/jpeg".to_string(),
90+
target_url: "https://www.adex.network/?stremio-test-banner-1".to_string(),
91+
archived: false,
92+
description: Some("Dummy AdUnit description 1".to_string()),
93+
ad_type: "legacy_250x250".to_string(),
94+
/// Timestamp: 1 564 383 600
95+
created: Utc.ymd(2019, 7, 29).and_hms(9, 0, 0),
96+
min_targeting_score: None,
97+
modified: None,
98+
owner: IDS["publisher"],
99+
title: Some("Dummy AdUnit 3".to_string()),
100+
},
101+
AdUnit {
102+
ipfs: IPFS::try_from("QmVhRDGXoM3Fg3HZD5xwMuxtb9ZErwC8wHt8CjsfxaiUbZ")
103+
.expect("should convert"),
104+
media_url: "ipfs://QmQB7uz7Gxfy7wqAnrnBcZFaVJLos8J9gn8mRcHQU6dAi1".to_string(),
105+
media_mime: "image/jpeg".to_string(),
106+
target_url: "https://www.adex.network/?adex-campaign=true&pub=stremio".to_string(),
107+
archived: false,
108+
description: Some("Dummy AdUnit description 2".to_string()),
109+
ad_type: "legacy_250x250".to_string(),
110+
/// Timestamp: 1 564 383 600
111+
created: Utc.ymd(2019, 7, 29).and_hms(9, 0, 0),
112+
min_targeting_score: None,
113+
modified: None,
114+
owner: IDS["publisher"],
115+
title: Some("Dummy AdUnit 2".to_string()),
116+
},
117+
AdUnit {
118+
ipfs: IPFS::try_from("QmYwcpMjmqJfo9ot1jGe9rfXsszFV1WbEA59QS7dEVHfJi")
119+
.expect("should convert"),
120+
media_url: "ipfs://QmQB7uz7Gxfy7wqAnrnBcZFaVJLos8J9gn8mRcHQU6dAi1".to_string(),
121+
media_mime: "image/jpeg".to_string(),
122+
target_url: "https://www.adex.network/?adex-campaign=true".to_string(),
123+
archived: false,
124+
description: Some("Dummy AdUnit description 3".to_string()),
125+
ad_type: "legacy_250x250".to_string(),
126+
/// Timestamp: 1 564 383 600
127+
created: Utc.ymd(2019, 7, 29).and_hms(9, 0, 0),
128+
min_targeting_score: None,
129+
modified: None,
130+
owner: IDS["publisher"],
131+
title: Some("Dummy AdUnit 3".to_string()),
132+
},
133+
AdUnit {
134+
ipfs: IPFS::try_from("QmTAF3FsFDS7Ru8WChoD9ofiHTH8gAQfR4mYSnwxqTDpJH")
135+
.expect("should convert"),
136+
media_url: "ipfs://QmQAcfBJpDDuH99A4p3pFtUmQwamS8UYStP5HxHC7bgYXY".to_string(),
137+
media_mime: "image/jpeg".to_string(),
138+
target_url: "https://adex.network".to_string(),
139+
archived: false,
140+
description: Some("Dummy AdUnit description 4".to_string()),
141+
ad_type: "legacy_250x250".to_string(),
142+
/// Timestamp: 1 564 383 600
143+
created: Utc.ymd(2019, 7, 29).and_hms(9, 0, 0),
144+
min_targeting_score: None,
145+
modified: None,
146+
owner: IDS["publisher"],
147+
title: Some("Dummy AdUnit 4".to_string()),
148+
},
149+
];
150+
89151
// CID V0
90152
pub static ref DUMMY_IPFS: [IPFS; 5] = [
91153
IPFS::try_from("QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR").expect("Valid IPFS V0"),

0 commit comments

Comments
 (0)