github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_balance_query_unit_test.go (about) 1 //go:build all || unit 2 // +build all unit 3 4 package hedera 5 6 /*- 7 * 8 * Hedera Go SDK 9 * 10 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 * 24 */ 25 26 import ( 27 "testing" 28 "time" 29 30 "github.com/hashgraph/hedera-protobufs-go/services" 31 32 "github.com/stretchr/testify/assert" 33 34 "github.com/stretchr/testify/require" 35 ) 36 37 func TestUnitAccountBalanceQueryValidate(t *testing.T) { 38 t.Parallel() 39 40 client, err := _NewMockClient() 41 client.SetLedgerID(*NewLedgerIDTestnet()) 42 require.NoError(t, err) 43 client.SetAutoValidateChecksums(true) 44 accountID, err := AccountIDFromString("0.0.123-esxsf") 45 require.NoError(t, err) 46 47 balanceQuery := NewAccountBalanceQuery(). 48 SetAccountID(accountID) 49 50 err = balanceQuery.validateNetworkOnIDs(client) 51 require.NoError(t, err) 52 } 53 54 func TestUnitAccountBalanceQueryValidateWrong(t *testing.T) { 55 t.Parallel() 56 57 client, err := _NewMockClient() 58 client.SetLedgerID(*NewLedgerIDTestnet()) 59 require.NoError(t, err) 60 client.SetAutoValidateChecksums(true) 61 accountID, err := AccountIDFromString("0.0.123-rmkykd") 62 require.NoError(t, err) 63 64 balanceQuery := NewAccountBalanceQuery(). 65 SetAccountID(accountID) 66 67 err = balanceQuery.validateNetworkOnIDs(client) 68 assert.Error(t, err) 69 if err != nil { 70 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 71 } 72 } 73 74 func TestUnitAccountBalanceQueryGet(t *testing.T) { 75 t.Parallel() 76 77 spenderAccountID1 := AccountID{Account: 7} 78 79 balance := NewAccountBalanceQuery(). 80 SetAccountID(spenderAccountID1). 81 SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}) 82 83 balance.GetAccountID() 84 balance.GetNodeAccountIDs() 85 balance.GetPaymentTransactionID() 86 } 87 88 func TestUnitAccountBalanceQuerySetNothing(t *testing.T) { 89 t.Parallel() 90 91 balance := NewAccountBalanceQuery() 92 93 balance.GetAccountID() 94 balance.GetNodeAccountIDs() 95 balance.GetPaymentTransactionID() 96 } 97 98 func TestUnitAccountBalanceQueryCoverage(t *testing.T) { 99 t.Parallel() 100 101 checksum := "dmqui" 102 contract := ContractID{Contract: 3, checksum: &checksum} 103 account := AccountID{Account: 3, checksum: &checksum} 104 nodeAccountID := []AccountID{{Account: 10}} 105 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 106 107 client, err := _NewMockClient() 108 client.SetLedgerID(*NewLedgerIDTestnet()) 109 require.NoError(t, err) 110 client.SetAutoValidateChecksums(true) 111 112 query := NewAccountBalanceQuery(). 113 SetMaxRetry(3). 114 SetMaxBackoff(time.Second * 30). 115 SetMinBackoff(time.Second * 10). 116 SetAccountID(account). 117 SetContractID(contract). 118 SetNodeAccountIDs(nodeAccountID). 119 SetPaymentTransactionID(transactionID). 120 SetMaxQueryPayment(NewHbar(23)). 121 SetQueryPayment(NewHbar(3)) 122 123 err = query.validateNetworkOnIDs(client) 124 125 require.NoError(t, err) 126 query.GetNodeAccountIDs() 127 query.GetMaxBackoff() 128 query.GetMinBackoff() 129 query.getName() 130 query.GetAccountID() 131 query.GetContractID() 132 133 _AccountBalanceFromProtobuf(nil) 134 bal := AccountBalance{Hbars: NewHbar(2)} 135 bal._ToProtobuf() 136 } 137 138 func TestUnitAccountBalanceQueryMock(t *testing.T) { 139 t.Parallel() 140 141 responses := [][]interface{}{ 142 { 143 &services.Response{ 144 Response: &services.Response_CryptogetAccountBalance{ 145 CryptogetAccountBalance: &services.CryptoGetAccountBalanceResponse{ 146 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 0}, 147 AccountID: &services.AccountID{ShardNum: 0, RealmNum: 0, Account: &services.AccountID_AccountNum{ 148 AccountNum: 1800, 149 }}, 150 Balance: 2000, 151 }, 152 }, 153 }, 154 &services.Response{ 155 Response: &services.Response_CryptogetAccountBalance{ 156 CryptogetAccountBalance: &services.CryptoGetAccountBalanceResponse{ 157 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 0}, 158 AccountID: &services.AccountID{ShardNum: 0, RealmNum: 0, Account: &services.AccountID_AccountNum{ 159 AccountNum: 1800, 160 }}, 161 Balance: 2000, 162 }, 163 }, 164 }, 165 }, 166 } 167 168 client, server := NewMockClientAndServer(responses) 169 defer server.Close() 170 171 query := NewAccountBalanceQuery(). 172 SetNodeAccountIDs([]AccountID{{Account: 3}}). 173 SetAccountID(AccountID{Account: 1800}). 174 SetContractID(ContractID{Contract: 3}) 175 176 _, err := query.Execute(client) 177 require.NoError(t, err) 178 } 179 180 func TestUnitAccountBalanceQueryNoClient(t *testing.T) { 181 t.Parallel() 182 183 _, err := NewAccountBalanceQuery(). 184 Execute(nil) 185 186 require.ErrorContains(t, err, "client` must be provided and have an _Operator") 187 }