Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
open-runtime-module-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noxim
open-runtime-module-library
Commits
d3630e35
Unverified
Commit
d3630e35
authored
4 years ago
by
wangjj9219
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add unit tests for auction (#244)
parent
3f391a6c
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
auction/src/mock.rs
+15
-5
15 additions, 5 deletions
auction/src/mock.rs
auction/src/tests.rs
+37
-6
37 additions, 6 deletions
auction/src/tests.rs
with
52 additions
and
11 deletions
auction/src/mock.rs
+
15
−
5
View file @
d3630e35
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
auction/src/tests.rs
+
37
−
6
View file @
d3630e35
...
@@ -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
(
1
00
)
end
:
Some
(
1
1
)
})
})
);
);
});
});
...
@@ -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
,
);
);
});
});
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment