From d3630e35ce737aef3f9c7857e9b4bd540f37530c Mon Sep 17 00:00:00 2001
From: wangjj9219 <183318287@qq.com>
Date: Fri, 7 Aug 2020 15:23:41 +0800
Subject: [PATCH] add unit tests for auction (#244)

---
 auction/src/mock.rs  | 20 +++++++++++++++-----
 auction/src/tests.rs | 43 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/auction/src/mock.rs b/auction/src/mock.rs
index 4a40159..f766a24 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 5c071e5..346bbe5 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,
 		);
 	});
 }
-- 
GitLab