diff --git a/auction/src/mock.rs b/auction/src/mock.rs index 4a401597b49dd820f05d3ee31d0dafa6dfc9a963..f766a241f5b035f0ce520c4b50b28acd29c948ff 100644 --- a/auction/src/mock.rs +++ b/auction/src/mock.rs @@ -66,19 +66,27 @@ impl frame_system::Trait for Runtime { type BaseCallFilter = (); type SystemWeightInfo = (); } +pub type System = frame_system::Module<Runtime>; pub struct Handler; impl AuctionHandler<AccountId, Balance, BlockNumber, AuctionId> for Handler { fn on_new_bid( - _now: BlockNumber, + now: BlockNumber, _id: AuctionId, - _new_bid: (AccountId, Balance), + new_bid: (AccountId, Balance), _last_bid: Option<(AccountId, Balance)>, ) -> OnNewBidResult<BlockNumber> { - OnNewBidResult { - accept_bid: true, - auction_end_change: Change::NoChange, + if new_bid.0 == ALICE { + OnNewBidResult { + accept_bid: true, + auction_end_change: Change::NewValue(Some(now + BID_EXTEND_BLOCK)), + } + } else { + OnNewBidResult { + accept_bid: false, + auction_end_change: Change::NoChange, + } } } @@ -94,6 +102,8 @@ impl Trait for Runtime { pub type AuctionModule = Module<Runtime>; pub const ALICE: AccountId = 1; +pub const BOB: AccountId = 2; +pub const BID_EXTEND_BLOCK: BlockNumber = 10; pub struct ExtBuilder; diff --git a/auction/src/tests.rs b/auction/src/tests.rs index 5c071e5fd8e339e44674e7b622057c12fdcc7434..346bbe5bfd414371ed4eab055f5a99a07760c34e 100644 --- a/auction/src/tests.rs +++ b/auction/src/tests.rs @@ -17,6 +17,17 @@ fn new_auction_should_work() { fn update_auction_should_work() { ExtBuilder::default().build().execute_with(|| { assert_ok!(AuctionModule::new_auction(10, Some(100)), 0); + assert_noop!( + AuctionModule::update_auction( + 1, + AuctionInfo { + bid: Some((ALICE, 100)), + start: 10, + end: Some(100) + } + ), + Error::<Runtime>::AuctionNotExist, + ); assert_ok!(AuctionModule::update_auction( 0, AuctionInfo { @@ -46,14 +57,25 @@ fn auction_info_should_work() { #[test] fn bid_should_work() { ExtBuilder::default().build().execute_with(|| { - assert_ok!(AuctionModule::new_auction(0, Some(100)), 0); - assert_ok!(AuctionModule::bid(Some(ALICE).into(), 0, 20)); + System::set_block_number(1); + assert_ok!(AuctionModule::new_auction(0, Some(5)), 0); + assert_eq!( + AuctionModule::auction_info(0), + Some(AuctionInfo { + bid: None, + start: 0, + end: Some(5) + }) + ); + assert_ok!(AuctionModule::bid(Origin::signed(ALICE), 0, 20)); + let bid_event = TestEvent::auction(RawEvent::Bid(0, ALICE, 20)); + assert!(System::events().iter().any(|record| record.event == bid_event)); assert_eq!( AuctionModule::auction_info(0), Some(AuctionInfo { bid: Some((ALICE, 20)), start: 0, - end: Some(100) + end: Some(11) }) ); }); @@ -63,9 +85,18 @@ fn bid_should_work() { fn bid_should_fail() { ExtBuilder::default().build().execute_with(|| { assert_ok!(AuctionModule::new_auction(10, Some(100)), 0); - assert_eq!( - AuctionModule::bid(Some(ALICE).into(), 0, 20), - Err(Error::<Runtime>::AuctionNotStarted.into()) + assert_ok!(AuctionModule::new_auction(0, Some(100)), 1); + assert_noop!( + AuctionModule::bid(Origin::signed(ALICE), 0, 20), + Error::<Runtime>::AuctionNotStarted + ); + assert_noop!( + AuctionModule::bid(Origin::signed(BOB), 1, 20), + Error::<Runtime>::BidNotAccepted, + ); + assert_noop!( + AuctionModule::bid(Origin::signed(ALICE), 1, 0), + Error::<Runtime>::InvalidBidPrice, ); }); }