github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/live_hash_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 TestUnitLiveHashQueryValidate(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 liveHashQuery := NewLiveHashQuery(). 48 SetAccountID(accountID) 49 50 err = liveHashQuery.validateNetworkOnIDs(client) 51 require.NoError(t, err) 52 } 53 54 func TestUnitLiveHashQueryValidateWrong(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 liveHashQuery := NewLiveHashQuery(). 65 SetAccountID(accountID) 66 67 err = liveHashQuery.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 TestUnitLiveHashQueryGet(t *testing.T) { 75 t.Parallel() 76 77 accountID := AccountID{Account: 7} 78 79 balance := NewLiveHashQuery(). 80 SetAccountID(accountID). 81 SetHash([]byte{}). 82 SetQueryPayment(NewHbar(2)). 83 SetMaxQueryPayment(NewHbar(1)). 84 SetQueryPayment(HbarFromTinybar(25)). 85 SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}) 86 87 balance.GetAccountID() 88 balance.GetGetHash() 89 balance.GetNodeAccountIDs() 90 balance.GetMinBackoff() 91 balance.GetMaxBackoff() 92 balance.GetMaxRetryCount() 93 balance.GetPaymentTransactionID() 94 balance.GetQueryPayment() 95 balance.GetMaxQueryPayment() 96 } 97 98 func TestUnitLiveHashQuerySetNothing(t *testing.T) { 99 t.Parallel() 100 101 balance := NewLiveHashQuery() 102 103 balance.GetAccountID() 104 balance.GetGetHash() 105 balance.GetNodeAccountIDs() 106 balance.GetMinBackoff() 107 balance.GetMaxBackoff() 108 balance.GetMaxRetryCount() 109 balance.GetPaymentTransactionID() 110 balance.GetQueryPayment() 111 balance.GetMaxQueryPayment() 112 } 113 114 func TestUnitLiveHashQueryCoverage(t *testing.T) { 115 t.Parallel() 116 117 checksum := "dmqui" 118 grpc := time.Second * 3 119 account := AccountID{Account: 3, checksum: &checksum} 120 nodeAccountID := []AccountID{{Account: 10}} 121 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 122 123 client, err := _NewMockClient() 124 client.SetLedgerID(*NewLedgerIDTestnet()) 125 require.NoError(t, err) 126 client.SetAutoValidateChecksums(true) 127 128 query := NewLiveHashQuery(). 129 SetAccountID(account). 130 SetHash([]byte{1}). 131 SetMaxRetry(3). 132 SetMaxBackoff(time.Second * 30). 133 SetMinBackoff(time.Second * 10). 134 SetNodeAccountIDs(nodeAccountID). 135 SetPaymentTransactionID(transactionID). 136 SetMaxQueryPayment(NewHbar(23)). 137 SetQueryPayment(NewHbar(3)). 138 SetGrpcDeadline(&grpc) 139 140 err = query.validateNetworkOnIDs(client) 141 require.NoError(t, err) 142 query.GetNodeAccountIDs() 143 query.GetMaxBackoff() 144 query.GetMinBackoff() 145 query.getName() 146 query.GetAccountID() 147 query.GetGetHash() 148 query.GetQueryPayment() 149 query.GetMaxQueryPayment() 150 } 151 152 func TestUnitLiveHashQueryMock(t *testing.T) { 153 t.Parallel() 154 155 responses := [][]interface{}{{ 156 &services.Response{ 157 Response: &services.Response_CryptoGetLiveHash{ 158 CryptoGetLiveHash: &services.CryptoGetLiveHashResponse{ 159 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2}, 160 }, 161 }, 162 }, 163 &services.Response{ 164 Response: &services.Response_CryptoGetLiveHash{ 165 CryptoGetLiveHash: &services.CryptoGetLiveHashResponse{ 166 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2}, 167 }, 168 }, 169 }, 170 &services.Response{ 171 Response: &services.Response_CryptoGetLiveHash{ 172 CryptoGetLiveHash: &services.CryptoGetLiveHashResponse{ 173 Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2}, 174 LiveHash: &services.LiveHash{ 175 Hash: []byte{1}, 176 AccountId: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 3}}, 177 }, 178 }, 179 }, 180 }, 181 }} 182 183 client, server := NewMockClientAndServer(responses) 184 defer server.Close() 185 186 query := NewLiveHashQuery(). 187 SetAccountID(AccountID{Account: 3}). 188 SetHash([]byte{1}). 189 SetNodeAccountIDs([]AccountID{{Account: 3}}). 190 SetMaxQueryPayment(NewHbar(1)) 191 192 _, err := query.GetCost(client) 193 require.NoError(t, err) 194 195 _, err = query.Execute(client) 196 require.NoError(t, err) 197 }