@@ -384,14 +384,14 @@ pub async fn get_accounting_for_channel<A: Adapter + 'static>(
384
384
#[ cfg( test) ]
385
385
mod test {
386
386
use super :: * ;
387
- use crate :: test_util:: setup_dummy_app;
388
387
use crate :: db:: { accounting:: spend_amount, insert_channel} ;
388
+ use crate :: test_util:: setup_dummy_app;
389
+ use hyper:: StatusCode ;
389
390
use primitives:: {
390
391
adapter:: Deposit ,
391
392
util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN , IDS } ,
392
393
BigNum ,
393
394
} ;
394
- use hyper:: StatusCode ;
395
395
396
396
#[ tokio:: test]
397
397
async fn create_and_fetch_spendable ( ) {
@@ -490,7 +490,9 @@ mod test {
490
490
async fn get_accountings_for_channel ( ) {
491
491
let app = setup_dummy_app ( ) . await ;
492
492
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" ) ;
494
496
let build_request = |channel : Channel | {
495
497
Request :: builder ( )
496
498
. extension ( channel)
@@ -499,7 +501,9 @@ mod test {
499
501
} ;
500
502
// Testing for no accounting yet
501
503
{
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" ) ;
503
507
assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
504
508
505
509
let accounting_response = res_to_accounting_response ( res) . await ;
@@ -508,54 +512,85 @@ mod test {
508
512
}
509
513
510
514
// 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" ) ;
517
515
{
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" ) ;
519
538
assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
520
539
521
540
let accounting_response = res_to_accounting_response ( res) . await ;
522
541
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) ;
526
543
}
527
544
528
545
// 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" ) ;
539
546
{
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" ) ;
541
569
assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
542
570
543
571
let accounting_response = res_to_accounting_response ( res) . await ;
544
572
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)
548
574
}
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
554
577
{
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" ) ;
558
588
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" ) ) ;
559
594
}
560
595
}
561
596
}
0 commit comments