Skip to content

Commit 63e1a54

Browse files
committed
sentry - routes - channel - clean up tests
1 parent ae29cbb commit 63e1a54

File tree

1 file changed

+71
-36
lines changed

1 file changed

+71
-36
lines changed

sentry/src/routes/channel.rs

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,14 @@ pub async fn get_accounting_for_channel<A: Adapter + 'static>(
384384
#[cfg(test)]
385385
mod test {
386386
use super::*;
387-
use crate::test_util::setup_dummy_app;
388387
use crate::db::{accounting::spend_amount, insert_channel};
388+
use crate::test_util::setup_dummy_app;
389+
use hyper::StatusCode;
389390
use primitives::{
390391
adapter::Deposit,
391392
util::tests::prep_db::{ADDRESSES, DUMMY_CAMPAIGN, IDS},
392393
BigNum,
393394
};
394-
use hyper::StatusCode;
395395

396396
#[tokio::test]
397397
async fn create_and_fetch_spendable() {
@@ -490,7 +490,9 @@ mod test {
490490
async fn get_accountings_for_channel() {
491491
let app = setup_dummy_app().await;
492492
let channel = DUMMY_CAMPAIGN.channel.clone();
493-
insert_channel(&app.pool, channel).await.expect("should insert channel");
493+
insert_channel(&app.pool, channel)
494+
.await
495+
.expect("should insert channel");
494496
let build_request = |channel: Channel| {
495497
Request::builder()
496498
.extension(channel)
@@ -499,7 +501,9 @@ mod test {
499501
};
500502
// Testing for no accounting yet
501503
{
502-
let res = get_accounting_for_channel(build_request(channel.clone()), &app).await.expect("should get response");
504+
let res = get_accounting_for_channel(build_request(channel.clone()), &app)
505+
.await
506+
.expect("should get response");
503507
assert_eq!(StatusCode::OK, res.status());
504508

505509
let accounting_response = res_to_accounting_response(res).await;
@@ -508,54 +512,85 @@ mod test {
508512
}
509513

510514
// Testing for 2 accountings - first channel
511-
let mut balances = Balances::<CheckedState>::new();
512-
balances.earners.insert(ADDRESSES["publisher"], UnifiedNum::from_u64(200));
513-
balances.earners.insert(ADDRESSES["publisher2"], UnifiedNum::from_u64(100));
514-
balances.spenders.insert(ADDRESSES["creator"], UnifiedNum::from_u64(200));
515-
balances.spenders.insert(ADDRESSES["tester"], UnifiedNum::from_u64(100));
516-
spend_amount(app.pool.clone(), channel.id(), balances.clone()).await.expect("should spend");
517515
{
518-
let res = get_accounting_for_channel(build_request(channel.clone()), &app).await.expect("should get response");
516+
let mut balances = Balances::<CheckedState>::new();
517+
balances
518+
.spend(
519+
ADDRESSES["creator"],
520+
ADDRESSES["publisher"],
521+
UnifiedNum::from_u64(200),
522+
)
523+
.expect("should not overflow");
524+
balances
525+
.spend(
526+
ADDRESSES["tester"],
527+
ADDRESSES["publisher2"],
528+
UnifiedNum::from_u64(100),
529+
)
530+
.expect("Should not overflow");
531+
spend_amount(app.pool.clone(), channel.id(), balances.clone())
532+
.await
533+
.expect("should spend");
534+
535+
let res = get_accounting_for_channel(build_request(channel.clone()), &app)
536+
.await
537+
.expect("should get response");
519538
assert_eq!(StatusCode::OK, res.status());
520539

521540
let accounting_response = res_to_accounting_response(res).await;
522541

523-
let sum = accounting_response.balances.sum().expect("shouldn't overflow");
524-
assert_eq!(sum.0, UnifiedNum::from_u64(300));
525-
assert_eq!(sum.1, UnifiedNum::from_u64(300));
542+
assert_eq!(balances, accounting_response.balances);
526543
}
527544

528545
// Testing for 2 accountings - second channel (same address is both an earner and a spender)
529-
let mut second_channel = DUMMY_CAMPAIGN.channel.clone();
530-
second_channel.leader = IDS["user"]; // channel.id() will be different now
531-
insert_channel(&app.pool, second_channel).await.expect("should insert channel");
532-
533-
let mut balances = Balances::<CheckedState>::new();
534-
balances.earners.insert(ADDRESSES["publisher"], UnifiedNum::from_u64(300));
535-
balances.spenders.insert(ADDRESSES["publisher"], UnifiedNum::from_u64(300));
536-
spend_amount(app.pool.clone(), second_channel.id(), balances).await.expect("should spend");
537-
538-
insert_channel(&app.pool, second_channel).await.expect("should insert channel");
539546
{
540-
let res = get_accounting_for_channel(build_request(second_channel.clone()), &app).await.expect("should get response");
547+
let mut second_channel = DUMMY_CAMPAIGN.channel.clone();
548+
second_channel.leader = IDS["user"]; // channel.id() will be different now
549+
insert_channel(&app.pool, second_channel)
550+
.await
551+
.expect("should insert channel");
552+
553+
let mut balances = Balances::<CheckedState>::new();
554+
balances
555+
.spend(ADDRESSES["tester"], ADDRESSES["publisher"], 300.into())
556+
.expect("Should not overflow");
557+
558+
balances
559+
.spend(ADDRESSES["publisher"], ADDRESSES["user"], 300.into())
560+
.expect("Should not overflow");
561+
562+
spend_amount(app.pool.clone(), second_channel.id(), balances.clone())
563+
.await
564+
.expect("should spend");
565+
566+
let res = get_accounting_for_channel(build_request(second_channel.clone()), &app)
567+
.await
568+
.expect("should get response");
541569
assert_eq!(StatusCode::OK, res.status());
542570

543571
let accounting_response = res_to_accounting_response(res).await;
544572

545-
let sum = accounting_response.balances.sum().expect("shouldn't overflow");
546-
assert_eq!(sum.0, UnifiedNum::from_u64(300));
547-
assert_eq!(sum.1, UnifiedNum::from_u64(300));
573+
assert_eq!(balances, accounting_response.balances)
548574
}
549-
// Testing for when sums dont match - Error case
550-
let mut balances = Balances::<CheckedState>::new();
551-
balances.earners.insert(ADDRESSES["publisher"], UnifiedNum::from_u64(100));
552-
balances.spenders.insert(ADDRESSES["creator"], UnifiedNum::from_u64(200));
553-
spend_amount(app.pool.clone(), channel.id(), balances).await.expect("should spend");
575+
576+
// Testing for when sums don't match on first channel - Error case
554577
{
555-
let res = get_accounting_for_channel(build_request(channel.clone()), &app).await;
556-
assert!(res.is_err());
557-
assert!(matches!(res, Err(ResponseError::FailedValidation(_))));
578+
let mut balances = Balances::<CheckedState>::new();
579+
balances
580+
.earners
581+
.insert(ADDRESSES["publisher"], UnifiedNum::from_u64(100));
582+
balances
583+
.spenders
584+
.insert(ADDRESSES["creator"], UnifiedNum::from_u64(200));
585+
spend_amount(app.pool.clone(), channel.id(), balances)
586+
.await
587+
.expect("should spend");
558588

589+
let res = get_accounting_for_channel(build_request(channel.clone()), &app).await;
590+
let expected = ResponseError::FailedValidation(
591+
"Earners sum is not equal to spenders sum for channel".to_string(),
592+
);
593+
assert_eq!(expected, res.expect_err("Should return an error"));
559594
}
560595
}
561596
}

0 commit comments

Comments
 (0)