@@ -17,43 +17,71 @@ import (
1717)
1818
1919func TestWalletOperationRunner_SubmitsAndConfirmsPendingOperation (t * testing.T ) {
20- db := testutil .NewTestDB (t )
21- repos := repository .NewRepositories (db )
22- ctx := context .Background ()
23- op , _ , err := repos .WalletOperations .CreateOrGet (ctx , repository.CreateWalletOperationInput {
24- Type : model .WalletOperationTypeFund ,
25- ClientRequestID : "fund-1" ,
26- Amount : "100" ,
27- })
28- if err != nil {
29- t .Fatalf ("CreateOrGet: %v" , err )
30- }
31-
32- txHash := common .HexToHash ("0xabc" )
33- operator := & fakeWalletOperator {fundHash : txHash .Hex ()}
34- receipts := & fakeWalletReceiptChecker {receipts : map [common.Hash ]* ethtypes.Receipt {
35- txHash : {Status : ethtypes .ReceiptStatusSuccessful },
36- }}
37- publisher := & fakeWalletEventPublisher {}
38- runner := NewWalletOperationRunner (repos , operator , receipts , time .Millisecond , nil , WithWalletOperationEventPublisher (publisher ))
39-
40- runner .runOnce (ctx )
41-
42- got , err := repos .WalletOperations .GetByID (ctx , op .ID )
43- if err != nil {
44- t .Fatalf ("GetByID: %v" , err )
45- }
46- if got .Status != model .WalletOperationStatusConfirmed {
47- t .Fatalf ("status = %q, want confirmed" , got .Status )
48- }
49- if got .TxHash == nil || * got .TxHash != txHash .Hex () {
50- t .Fatalf ("tx_hash = %v, want %s" , got .TxHash , txHash .Hex ())
51- }
52- if operator .fundAmount .String () != "100" {
53- t .Fatalf ("fund amount = %s, want 100" , operator .fundAmount )
54- }
55- if ! publisher .hasStatus (model .WalletOperationStatusSubmitted ) || ! publisher .hasStatus (model .WalletOperationStatusConfirmed ) {
56- t .Fatalf ("published statuses = %v, want submitted and confirmed" , publisher .statuses ())
20+ tests := []struct {
21+ name string
22+ opType model.WalletOperationType
23+ amount string
24+ }{
25+ {name : "fund" , opType : model .WalletOperationTypeFund , amount : "100" },
26+ {name : "withdraw" , opType : model .WalletOperationTypeWithdraw , amount : "50" },
27+ }
28+
29+ for _ , tt := range tests {
30+ t .Run (tt .name , func (t * testing.T ) {
31+ db := testutil .NewTestDB (t )
32+ repos := repository .NewRepositories (db )
33+ ctx := context .Background ()
34+ op , _ , err := repos .WalletOperations .CreateOrGet (ctx , repository.CreateWalletOperationInput {
35+ Type : tt .opType ,
36+ ClientRequestID : string (tt .opType ) + "-1" ,
37+ Amount : tt .amount ,
38+ })
39+ if err != nil {
40+ t .Fatalf ("CreateOrGet: %v" , err )
41+ }
42+
43+ txHash := common .HexToHash ("0xabc" )
44+ operator := & fakeWalletOperator {fundHash : txHash .Hex (), withdrawHash : txHash .Hex ()}
45+ receipts := & fakeWalletReceiptChecker {receipts : map [common.Hash ]* ethtypes.Receipt {
46+ txHash : {Status : ethtypes .ReceiptStatusSuccessful },
47+ }}
48+ publisher := & fakeWalletEventPublisher {}
49+ runner := NewWalletOperationRunner (repos , operator , receipts , time .Millisecond , nil , WithWalletOperationEventPublisher (publisher ))
50+
51+ runner .runOnce (ctx )
52+
53+ got , err := repos .WalletOperations .GetByID (ctx , op .ID )
54+ if err != nil {
55+ t .Fatalf ("GetByID: %v" , err )
56+ }
57+ if got .Status != model .WalletOperationStatusConfirmed {
58+ t .Fatalf ("status = %q, want confirmed" , got .Status )
59+ }
60+ if got .TxHash == nil || * got .TxHash != txHash .Hex () {
61+ t .Fatalf ("tx_hash = %v, want %s" , got .TxHash , txHash .Hex ())
62+ }
63+ switch tt .opType {
64+ case model .WalletOperationTypeFund :
65+ if operator .fundAmount == nil || operator .fundAmount .String () != tt .amount {
66+ t .Fatalf ("fund amount = %v, want %s" , operator .fundAmount , tt .amount )
67+ }
68+ if operator .withdrawAmount != nil {
69+ t .Fatalf ("withdraw amount = %s, want no withdraw" , operator .withdrawAmount )
70+ }
71+ case model .WalletOperationTypeWithdraw :
72+ if operator .withdrawAmount == nil || operator .withdrawAmount .String () != tt .amount {
73+ t .Fatalf ("withdraw amount = %v, want %s" , operator .withdrawAmount , tt .amount )
74+ }
75+ if operator .fundAmount != nil {
76+ t .Fatalf ("fund amount = %s, want no fund" , operator .fundAmount )
77+ }
78+ default :
79+ t .Fatalf ("unexpected operation type %q" , tt .opType )
80+ }
81+ if ! publisher .hasStatus (model .WalletOperationStatusSubmitted ) || ! publisher .hasStatus (model .WalletOperationStatusConfirmed ) {
82+ t .Fatalf ("published statuses = %v, want submitted and confirmed" , publisher .statuses ())
83+ }
84+ })
5785 }
5886}
5987
0 commit comments