github.com/Finschia/finschia-sdk@v0.48.1/client/test_helpers.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  
     6  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  )
     9  
    10  var (
    11  	_ AccountRetriever = TestAccountRetriever{}
    12  	_ Account          = TestAccount{}
    13  )
    14  
    15  // TestAccount represents a client Account that can be used in unit tests
    16  type TestAccount struct {
    17  	Address sdk.AccAddress
    18  	Num     uint64
    19  	Seq     uint64
    20  }
    21  
    22  // GetAddress implements client Account.GetAddress
    23  func (t TestAccount) GetAddress() sdk.AccAddress {
    24  	return t.Address
    25  }
    26  
    27  // GetPubKey implements client Account.GetPubKey
    28  func (t TestAccount) GetPubKey() cryptotypes.PubKey {
    29  	return nil
    30  }
    31  
    32  // GetAccountNumber implements client Account.GetAccountNumber
    33  func (t TestAccount) GetAccountNumber() uint64 {
    34  	return t.Num
    35  }
    36  
    37  // GetSequence implements client Account.GetSequence
    38  func (t TestAccount) GetSequence() uint64 {
    39  	return t.Seq
    40  }
    41  
    42  // TestAccountRetriever is an AccountRetriever that can be used in unit tests
    43  type TestAccountRetriever struct {
    44  	Accounts map[string]TestAccount
    45  }
    46  
    47  // GetAccount implements AccountRetriever.GetAccount
    48  func (t TestAccountRetriever) GetAccount(_ Context, addr sdk.AccAddress) (Account, error) {
    49  	acc, ok := t.Accounts[addr.String()]
    50  	if !ok {
    51  		return nil, fmt.Errorf("account %s not found", addr)
    52  	}
    53  
    54  	return acc, nil
    55  }
    56  
    57  // GetAccountWithHeight implements AccountRetriever.GetAccountWithHeight
    58  func (t TestAccountRetriever) GetAccountWithHeight(clientCtx Context, addr sdk.AccAddress) (Account, int64, error) {
    59  	acc, err := t.GetAccount(clientCtx, addr)
    60  	if err != nil {
    61  		return nil, 0, err
    62  	}
    63  
    64  	return acc, 0, nil
    65  }
    66  
    67  // EnsureExists implements AccountRetriever.EnsureExists
    68  func (t TestAccountRetriever) EnsureExists(_ Context, addr sdk.AccAddress) error {
    69  	_, ok := t.Accounts[addr.String()]
    70  	if !ok {
    71  		return fmt.Errorf("account %s not found", addr)
    72  	}
    73  	return nil
    74  }
    75  
    76  // GetAccountNumberSequence implements AccountRetriever.GetAccountNumberSequence
    77  func (t TestAccountRetriever) GetAccountNumberSequence(_ Context, addr sdk.AccAddress) (accNum uint64, accSeq uint64, err error) {
    78  	acc, ok := t.Accounts[addr.String()]
    79  	if !ok {
    80  		return 0, 0, fmt.Errorf("account %s not found", addr)
    81  	}
    82  	return acc.Num, acc.Seq, nil
    83  }