Skip to content

Commit f0f08d4

Browse files
authored
Merge pull request #464 from AmbireTech/general-improvements-and-bug-fixes
Issue #450 Sentry - improve test for spendable
2 parents 61e0798 + 4258035 commit f0f08d4

File tree

5 files changed

+126
-96
lines changed

5 files changed

+126
-96
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adapter/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ parse-display = "^0.5.0"
4242
[dev-dependencies]
4343
byteorder = "^1.4"
4444
tokio = { version = "^1", features = ["macros", "rt-multi-thread"] }
45+
pretty_assertions = "1"

primitives/src/spender.rs

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ pub struct Spendable {
1515
pub deposit: Deposit<UnifiedNum>,
1616
}
1717

18+
impl PartialEq<Spendable> for &Spendable {
19+
fn eq(&self, other: &Spendable) -> bool {
20+
self.spender == other.spender
21+
&& self.channel == other.channel
22+
&& self.deposit == other.deposit
23+
}
24+
}
25+
26+
impl PartialEq<&Spendable> for Spendable {
27+
fn eq(&self, other: &&Spendable) -> bool {
28+
self.spender == other.spender
29+
&& self.channel == other.channel
30+
&& self.deposit == other.deposit
31+
}
32+
}
33+
1834
#[cfg(feature = "postgres")]
1935
mod postgres {
2036
use super::*;

sentry/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ serde_urlencoded = "^0.7"
5353
# Other
5454
thiserror = "^1.0"
5555
once_cell = "1.5.2"
56+
57+
[dev-dependencies]
58+
pretty_assertions = "1"
59+

sentry/src/db/spendable.rs

+103-96
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use primitives::{sentry::Pagination, spender::Spendable, Address, ChannelId};
55
use super::{DbPool, PoolError};
66

77
/// ```text
8-
/// INSERT INTO spendable (spender, channel_id, total, still_on_create2)
9-
/// values ('0xce07CbB7e054514D590a0262C93070D838bFBA2e', '0x061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088', 10.00000000, 2.00000000);
8+
/// INSERT INTO spendable (spender, channel_id, total, still_on_create2, created)
9+
/// values ('0xce07CbB7e054514D590a0262C93070D838bFBA2e', '0x061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088', 10.00000000, 2.00000000, NOW());
1010
/// ```
1111
pub async fn insert_spendable(pool: DbPool, spendable: &Spendable) -> Result<bool, PoolError> {
1212
let client = pool.get().await?;
@@ -114,8 +114,11 @@ async fn list_spendable_total_count<'a>(
114114

115115
#[cfg(test)]
116116
mod test {
117+
use std::collections::HashMap;
118+
117119
use primitives::{
118120
spender::Spendable,
121+
test_util::{ADVERTISER, CREATOR, FOLLOWER, GUARDIAN, GUARDIAN_2, PUBLISHER},
119122
util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN},
120123
Deposit, UnifiedNum,
121124
};
@@ -165,12 +168,21 @@ mod test {
165168
.expect("Should fetch successfully");
166169

167170
assert_eq!(Some(spendable), fetched_spendable);
171+
}
168172

169-
// TODO: Update spendable
173+
fn new_spendable_with(spender: &Address) -> Spendable {
174+
Spendable {
175+
spender: *spender,
176+
channel: DUMMY_CAMPAIGN.channel,
177+
deposit: Deposit {
178+
total: UnifiedNum::from(100_000_000),
179+
still_on_create2: UnifiedNum::from(500_000),
180+
},
181+
}
170182
}
171183

172184
#[tokio::test]
173-
async fn gets_all_spendables_for_channel() {
185+
async fn insert_and_get_single_spendable_for_channel() {
174186
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
175187

176188
setup_test_migrations(database.pool.clone())
@@ -179,7 +191,7 @@ mod test {
179191

180192
let channel = DUMMY_CAMPAIGN.channel;
181193

182-
insert_channel(&database, DUMMY_CAMPAIGN.channel)
194+
insert_channel(&database, channel)
183195
.await
184196
.expect("Should insert");
185197

@@ -192,14 +204,7 @@ mod test {
192204
assert_eq!(pagination.total_pages, 1);
193205

194206
// Test for 1 pages
195-
let spendable_user = Spendable {
196-
spender: ADDRESSES["user"],
197-
channel: DUMMY_CAMPAIGN.channel,
198-
deposit: Deposit {
199-
total: UnifiedNum::from(100_000_000),
200-
still_on_create2: UnifiedNum::from(500_000),
201-
},
202-
};
207+
let spendable_user = new_spendable_with(&FOLLOWER);
203208

204209
insert_spendable(database.pool.clone(), &spendable_user)
205210
.await
@@ -215,96 +220,98 @@ mod test {
215220
page: 0,
216221
total_pages: 1,
217222
};
218-
assert_eq!(spendables, expected_spendables);
219-
assert_eq!(pagination, expected_pagination);
223+
pretty_assertions::assert_eq!(spendables, expected_spendables);
224+
pretty_assertions::assert_eq!(pagination, expected_pagination);
225+
}
220226

221-
// Test for multiple pages
222-
let spendable_publisher = Spendable {
223-
spender: ADDRESSES["publisher"],
224-
channel: DUMMY_CAMPAIGN.channel,
225-
deposit: Deposit {
226-
total: UnifiedNum::from(100_000_000),
227-
still_on_create2: UnifiedNum::from(500_000),
228-
},
229-
};
230-
insert_spendable(database.pool.clone(), &spendable_publisher)
231-
.await
232-
.expect("should insert spendable");
233-
sleep(Duration::from_millis(100)).await;
227+
#[tokio::test]
228+
async fn gets_multiple_pages_of_spendables_for_channel() {
229+
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
234230

235-
let spendable_publisher2 = Spendable {
236-
spender: ADDRESSES["publisher2"],
237-
channel: DUMMY_CAMPAIGN.channel,
238-
deposit: Deposit {
239-
total: UnifiedNum::from(100_000_000),
240-
still_on_create2: UnifiedNum::from(500_000),
241-
},
242-
};
243-
insert_spendable(database.pool.clone(), &spendable_publisher2)
231+
setup_test_migrations(database.pool.clone())
244232
.await
245-
.expect("should insert spendable");
246-
sleep(Duration::from_millis(100)).await;
233+
.expect("Migrations should succeed");
247234

248-
let spendable_creator = Spendable {
249-
spender: ADDRESSES["creator"],
250-
channel: DUMMY_CAMPAIGN.channel,
251-
deposit: Deposit {
252-
total: UnifiedNum::from(100_000_000),
253-
still_on_create2: UnifiedNum::from(500_000),
254-
},
255-
};
256-
insert_spendable(database.pool.clone(), &spendable_creator)
257-
.await
258-
.expect("should insert spendable");
259-
sleep(Duration::from_millis(100)).await;
235+
let channel = DUMMY_CAMPAIGN.channel;
260236

261-
let spendable_tester = Spendable {
262-
spender: ADDRESSES["tester"],
263-
channel: DUMMY_CAMPAIGN.channel,
264-
deposit: Deposit {
265-
total: UnifiedNum::from(100_000_000),
266-
still_on_create2: UnifiedNum::from(500_000),
267-
},
268-
};
269-
insert_spendable(database.pool.clone(), &spendable_tester)
237+
insert_channel(&database, channel)
270238
.await
271-
.expect("should insert spendable");
272-
sleep(Duration::from_millis(100)).await;
273-
274-
let (spendables, pagination) =
275-
get_all_spendables_for_channel(database.clone(), &channel.id(), 0, 2)
276-
.await
277-
.expect("should get result");
278-
let expected_spendables = vec![spendable_user, spendable_publisher];
279-
let expected_pagination = Pagination {
280-
page: 0,
281-
total_pages: 3,
282-
};
283-
assert_eq!(spendables, expected_spendables);
284-
assert_eq!(pagination, expected_pagination);
285-
286-
let (spendables, pagination) =
287-
get_all_spendables_for_channel(database.clone(), &channel.id(), 2, 2)
288-
.await
289-
.expect("should get result");
290-
let expected_spendables = vec![spendable_publisher2, spendable_creator];
291-
let expected_pagination = Pagination {
292-
page: 1,
293-
total_pages: 3,
294-
};
295-
assert_eq!(spendables, expected_spendables);
296-
assert_eq!(pagination, expected_pagination);
239+
.expect("Should insert");
297240

298-
let (spendables, pagination) =
299-
get_all_spendables_for_channel(database.clone(), &channel.id(), 4, 2)
241+
let create_spendables: Vec<(Address, Spendable)> = vec![
242+
(*PUBLISHER, new_spendable_with(&PUBLISHER)),
243+
(*ADVERTISER, new_spendable_with(&ADVERTISER)),
244+
(*CREATOR, new_spendable_with(&CREATOR)),
245+
(*GUARDIAN, new_spendable_with(&GUARDIAN)),
246+
(*GUARDIAN_2, new_spendable_with(&GUARDIAN_2)),
247+
];
248+
249+
// insert all spendables
250+
for (address, spendable) in create_spendables.iter() {
251+
insert_spendable(database.pool.clone(), spendable)
300252
.await
301-
.expect("should get result");
302-
let expected_spendables = vec![spendable_tester];
303-
let expected_pagination = Pagination {
304-
page: 2,
305-
total_pages: 3,
306-
};
307-
assert_eq!(spendables, expected_spendables);
308-
assert_eq!(pagination, expected_pagination);
253+
.expect(&format!(
254+
"Failed to insert spendable for {:?} with Spendable: {:?}",
255+
address, spendable
256+
));
257+
// use sleep to make all spendables with different time
258+
// they will follow the order in which they were defined in the variable
259+
sleep(Duration::from_millis(100)).await;
260+
}
261+
262+
let spendables = create_spendables.into_iter().collect::<HashMap<_, _>>();
263+
264+
let expected_pages = vec![
265+
(
266+
vec![&spendables[&PUBLISHER], &spendables[&ADVERTISER]],
267+
Pagination {
268+
page: 0,
269+
total_pages: 3,
270+
},
271+
),
272+
(
273+
vec![&spendables[&CREATOR], &spendables[&GUARDIAN]],
274+
Pagination {
275+
page: 1,
276+
total_pages: 3,
277+
},
278+
),
279+
(
280+
vec![&spendables[&GUARDIAN_2]],
281+
Pagination {
282+
page: 2,
283+
total_pages: 3,
284+
},
285+
),
286+
];
287+
288+
for (expected_spendables, expected_pagination) in expected_pages.iter() {
289+
// for page = 0; skip = 0
290+
// for page = 1; skip = 2
291+
// etc.
292+
let limit = 2;
293+
let skip = expected_pagination.page * limit;
294+
295+
let debug_msg = format!(
296+
"{:?} page = {} with skip = {} & limit = {}",
297+
channel.id(),
298+
expected_pagination.page,
299+
skip,
300+
limit
301+
);
302+
303+
let (spendables, pagination) =
304+
get_all_spendables_for_channel(database.clone(), &channel.id(), skip, limit)
305+
.await
306+
.expect(&format!("could not fetch spendables {}", debug_msg));
307+
308+
pretty_assertions::assert_eq!(
309+
&pagination,
310+
expected_pagination,
311+
"Unexpected pagination for {}",
312+
debug_msg
313+
);
314+
pretty_assertions::assert_eq!(&spendables, expected_spendables);
315+
}
309316
}
310317
}

0 commit comments

Comments
 (0)