Skip to content

Commit

Permalink
feat: Adding GetCost tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 authored and mehcode committed Mar 4, 2021
1 parent af25097 commit edda932
Show file tree
Hide file tree
Showing 32 changed files with 1,849 additions and 84 deletions.
59 changes: 59 additions & 0 deletions account_balance_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,65 @@ func TestAccountBalanceQueryCost_Execute(t *testing.T) {
assert.NoError(t, err)
}

func TestAccountBalanceQueryCost_BigMax_Execute(t *testing.T) {
client := newTestClient(t)

balance := NewAccountBalanceQuery().
SetMaxQueryPayment(NewHbar(10000)).
SetAccountID(client.GetOperatorAccountID())

cost, err := balance.GetCost(client)
assert.NoError(t, err)

_, err = balance.SetQueryPayment(cost).Execute(client)
assert.NoError(t, err)
}

func TestAccountBalanceQueryCost_SmallMax_Execute(t *testing.T) {
client := newTestClient(t)

balance := NewAccountBalanceQuery().
SetMaxQueryPayment(HbarFromTinybar(1)).
SetAccountID(client.GetOperatorAccountID())

_, err := balance.GetCost(client)
assert.NoError(t, err)

_, err = balance.Execute(client)
assert.NoError(t, err)
}

func TestAccountBalanceQueryCost_SetPayment_Execute(t *testing.T) {
client := newTestClient(t)

balance := NewAccountBalanceQuery().
SetMaxQueryPayment(NewHbar(10000)).
SetQueryPayment(NewHbar(0)).
SetAccountID(client.GetOperatorAccountID())

cost, err := balance.GetCost(client)
assert.NoError(t, err)

_, err = balance.SetQueryPayment(cost).Execute(client)
assert.NoError(t, err)
}

func TestAccountBalanceQueryCost_SetPaymentOneTinybar_Execute(t *testing.T) {
client := newTestClient(t)

balance := NewAccountBalanceQuery().
SetMaxQueryPayment(NewHbar(10000)).
SetQueryPayment(NewHbar(0)).
SetAccountID(client.GetOperatorAccountID())

_, err := balance.GetCost(client)
assert.NoError(t, err)

_, err = balance.SetQueryPayment(HbarFromTinybar(1)).Execute(client)
assert.NoError(t, err)
}


func Test_AccountBalance_NoAccount(t *testing.T) {
client := newTestClient(t)

Expand Down
15 changes: 11 additions & 4 deletions account_info_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,29 @@ func (query *AccountInfoQuery) Execute(client *Client) (AccountInfo, error) {
query.SetNodeAccountIDs(client.network.getNodeAccountIDsForExecute())
}

query.queryPayment = NewHbar(2)
query.paymentTransactionID = TransactionIDGenerate(client.operator.accountID)

var cost Hbar
if query.queryPayment.tinybar != 0 {
cost = query.queryPayment
} else {
cost = client.maxQueryPayment
if query.maxQueryPayment.tinybar == 0 {
cost = client.maxQueryPayment
} else {
cost = query.maxQueryPayment
}

actualCost, err := query.GetCost(client)
if err != nil {
return AccountInfo{}, err
}

if cost.tinybar > actualCost.tinybar {
return AccountInfo{}, ErrMaxQueryPaymentExceeded{}
if cost.tinybar < actualCost.tinybar {
return AccountInfo{}, ErrMaxQueryPaymentExceeded{
QueryCost: actualCost,
MaxQueryPayment: cost,
query: "AccountInfoQuery",
}
}

cost = actualCost
Expand Down
159 changes: 158 additions & 1 deletion account_info_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestAccountInfoQuery_Execute(t *testing.T) {
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetMaxQueryPayment(NewHbar(1)).
SetQueryPayment(HbarFromTinybar(25)).
Execute(client)
assert.NoError(t, err)

Expand Down Expand Up @@ -94,7 +95,7 @@ func TestAccountInfoQueryCost_Execute(t *testing.T) {
cost, err := accountInfo.GetCost(client)
assert.NoError(t, err)

info, err := accountInfo.SetMaxQueryPayment(cost).Execute(client)
info, err := accountInfo.SetQueryPayment(cost).Execute(client)
assert.NoError(t, err)

assert.Equal(t, accountID, info.AccountID)
Expand All @@ -119,6 +120,162 @@ func TestAccountInfoQueryCost_Execute(t *testing.T) {
assert.NoError(t, err)
}

func TestAccountInfoQueryCost_InsufficientFee_Execute(t *testing.T) {
client := newTestClient(t)

newKey, err := GeneratePrivateKey()
assert.NoError(t, err)

newBalance := NewHbar(2)
assert.Equal(t, 2*HbarUnits.Hbar.numberOfTinybar(), newBalance.tinybar)

resp, err := NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetInitialBalance(newBalance).
Execute(client)
assert.NoError(t, err)

receipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

accountID := *receipt.AccountID
assert.NoError(t, err)

accountInfo := NewAccountInfoQuery().
SetAccountID(accountID).
SetMaxQueryPayment(NewHbar(1)).
SetNodeAccountIDs([]AccountID{resp.NodeID})

_, err = accountInfo.GetCost(client)
assert.NoError(t, err)

_, err = accountInfo.SetQueryPayment(HbarFromTinybar(1)).Execute(client)
if err != nil {
assert.Equal(t, fmt.Sprintf("exceptional precheck status INSUFFICIENT_TX_FEE"), err.Error())
}

tx, err := NewAccountDeleteTransaction().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetTransferAccountID(client.GetOperatorAccountID()).
SetTransactionID(TransactionIDGenerate(accountID)).
FreezeWith(client)
assert.NoError(t, err)

resp, err = tx.
Sign(newKey).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}

func TestAccountInfoQueryCost_BigMax_Execute(t *testing.T) {
client := newTestClient(t)

newKey, err := GeneratePrivateKey()
assert.NoError(t, err)

newBalance := NewHbar(2)
assert.Equal(t, 2*HbarUnits.Hbar.numberOfTinybar(), newBalance.tinybar)

resp, err := NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetInitialBalance(newBalance).
Execute(client)
assert.NoError(t, err)

receipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

accountID := *receipt.AccountID
assert.NoError(t, err)

accountInfo := NewAccountInfoQuery().
SetAccountID(accountID).
SetMaxQueryPayment(NewHbar(1000000)).
SetNodeAccountIDs([]AccountID{resp.NodeID})

_, err = accountInfo.GetCost(client)
assert.NoError(t, err)

info, err := accountInfo.SetQueryPayment(NewHbar(1000)).Execute(client)
assert.NoError(t, err)

assert.Equal(t, accountID, info.AccountID)
assert.Equal(t, false, info.IsDeleted)
assert.Equal(t, newKey.PublicKey(), info.Key)
assert.Equal(t, newBalance.tinybar, info.Balance.tinybar)

tx, err := NewAccountDeleteTransaction().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetTransferAccountID(client.GetOperatorAccountID()).
SetTransactionID(TransactionIDGenerate(accountID)).
FreezeWith(client)
assert.NoError(t, err)

resp, err = tx.
Sign(newKey).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}

func TestAccountInfoQueryCost_SmallMax_Execute(t *testing.T) {
client := newTestClient(t)

newKey, err := GeneratePrivateKey()
assert.NoError(t, err)

newBalance := NewHbar(2)
assert.Equal(t, 2*HbarUnits.Hbar.numberOfTinybar(), newBalance.tinybar)

resp, err := NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetInitialBalance(newBalance).
Execute(client)
assert.NoError(t, err)

receipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

accountID := *receipt.AccountID
assert.NoError(t, err)

accountInfo := NewAccountInfoQuery().
SetAccountID(accountID).
SetMaxQueryPayment(HbarFromTinybar(1)).
SetNodeAccountIDs([]AccountID{resp.NodeID})

cost, err := accountInfo.GetCost(client)
assert.NoError(t, err)

_, err = accountInfo.Execute(client)
if err != nil {
assert.Equal(t, fmt.Sprintf("cost of AccountInfoQuery ("+cost.String()+") without explicit payment is greater than the max query payment of 1 tħ"), err.Error())
}

tx, err := NewAccountDeleteTransaction().
SetAccountID(accountID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetTransferAccountID(client.GetOperatorAccountID()).
SetTransactionID(TransactionIDGenerate(accountID)).
FreezeWith(client)
assert.NoError(t, err)

resp, err = tx.
Sign(newKey).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}

func Test_AccountInfo_NoAccountID(t *testing.T) {
client := newTestClient(t)

Expand Down
15 changes: 11 additions & 4 deletions account_records_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,29 @@ func (query *AccountRecordsQuery) Execute(client *Client) ([]TransactionRecord,

records := make([]TransactionRecord, 0)

query.queryPayment = NewHbar(2)
query.paymentTransactionID = TransactionIDGenerate(client.operator.accountID)

var cost Hbar
if query.queryPayment.tinybar != 0 {
cost = query.queryPayment
} else {
cost = client.maxQueryPayment
if query.maxQueryPayment.tinybar == 0 {
cost = client.maxQueryPayment
} else {
cost = query.maxQueryPayment
}

actualCost, err := query.GetCost(client)
if err != nil {
return []TransactionRecord{}, err
}

if cost.tinybar > actualCost.tinybar {
return []TransactionRecord{}, ErrMaxQueryPaymentExceeded{}
if cost.tinybar < actualCost.tinybar {
return []TransactionRecord{}, ErrMaxQueryPaymentExceeded{
QueryCost: actualCost,
MaxQueryPayment: cost,
query: "AccountRecordsQuery",
}
}

cost = actualCost
Expand Down
Loading

0 comments on commit edda932

Please sign in to comment.