Skip to content
Snippets Groups Projects
Unverified Commit d3630e35 authored by wangjj9219's avatar wangjj9219 Committed by GitHub
Browse files

add unit tests for auction (#244)

parent 3f391a6c
No related branches found
No related tags found
No related merge requests found
...@@ -66,19 +66,27 @@ impl frame_system::Trait for Runtime { ...@@ -66,19 +66,27 @@ impl frame_system::Trait for Runtime {
type BaseCallFilter = (); type BaseCallFilter = ();
type SystemWeightInfo = (); type SystemWeightInfo = ();
} }
pub type System = frame_system::Module<Runtime>;
pub struct Handler; pub struct Handler;
impl AuctionHandler<AccountId, Balance, BlockNumber, AuctionId> for Handler { impl AuctionHandler<AccountId, Balance, BlockNumber, AuctionId> for Handler {
fn on_new_bid( fn on_new_bid(
_now: BlockNumber, now: BlockNumber,
_id: AuctionId, _id: AuctionId,
_new_bid: (AccountId, Balance), new_bid: (AccountId, Balance),
_last_bid: Option<(AccountId, Balance)>, _last_bid: Option<(AccountId, Balance)>,
) -> OnNewBidResult<BlockNumber> { ) -> OnNewBidResult<BlockNumber> {
OnNewBidResult { if new_bid.0 == ALICE {
accept_bid: true, OnNewBidResult {
auction_end_change: Change::NoChange, 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 { ...@@ -94,6 +102,8 @@ impl Trait for Runtime {
pub type AuctionModule = Module<Runtime>; pub type AuctionModule = Module<Runtime>;
pub const ALICE: AccountId = 1; pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const BID_EXTEND_BLOCK: BlockNumber = 10;
pub struct ExtBuilder; pub struct ExtBuilder;
......
...@@ -17,6 +17,17 @@ fn new_auction_should_work() { ...@@ -17,6 +17,17 @@ fn new_auction_should_work() {
fn update_auction_should_work() { fn update_auction_should_work() {
ExtBuilder::default().build().execute_with(|| { ExtBuilder::default().build().execute_with(|| {
assert_ok!(AuctionModule::new_auction(10, Some(100)), 0); 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( assert_ok!(AuctionModule::update_auction(
0, 0,
AuctionInfo { AuctionInfo {
...@@ -46,14 +57,25 @@ fn auction_info_should_work() { ...@@ -46,14 +57,25 @@ fn auction_info_should_work() {
#[test] #[test]
fn bid_should_work() { fn bid_should_work() {
ExtBuilder::default().build().execute_with(|| { ExtBuilder::default().build().execute_with(|| {
assert_ok!(AuctionModule::new_auction(0, Some(100)), 0); System::set_block_number(1);
assert_ok!(AuctionModule::bid(Some(ALICE).into(), 0, 20)); 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!( assert_eq!(
AuctionModule::auction_info(0), AuctionModule::auction_info(0),
Some(AuctionInfo { Some(AuctionInfo {
bid: Some((ALICE, 20)), bid: Some((ALICE, 20)),
start: 0, start: 0,
end: Some(100) end: Some(11)
}) })
); );
}); });
...@@ -63,9 +85,18 @@ fn bid_should_work() { ...@@ -63,9 +85,18 @@ fn bid_should_work() {
fn bid_should_fail() { fn bid_should_fail() {
ExtBuilder::default().build().execute_with(|| { ExtBuilder::default().build().execute_with(|| {
assert_ok!(AuctionModule::new_auction(10, Some(100)), 0); assert_ok!(AuctionModule::new_auction(10, Some(100)), 0);
assert_eq!( assert_ok!(AuctionModule::new_auction(0, Some(100)), 1);
AuctionModule::bid(Some(ALICE).into(), 0, 20), assert_noop!(
Err(Error::<Runtime>::AuctionNotStarted.into()) 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,
); );
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment