github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_info_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  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestUnitAccountInfoQueryValidate(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	client, err := _NewMockClient()
    38  	client.SetLedgerID(*NewLedgerIDTestnet())
    39  	require.NoError(t, err)
    40  	client.SetAutoValidateChecksums(true)
    41  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    42  	require.NoError(t, err)
    43  
    44  	infoQuery := NewAccountInfoQuery().
    45  		SetAccountID(accountID)
    46  
    47  	err = infoQuery.validateNetworkOnIDs(client)
    48  	require.NoError(t, err)
    49  }
    50  func TestAccountInfoQuery_Get(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	checksum := "dmqui"
    54  	deadline := time.Duration(time.Minute)
    55  	accountId := AccountID{Account: 3, checksum: &checksum}
    56  	transactionID := TransactionIDGenerate(accountId)
    57  	query := NewAccountInfoQuery().
    58  		SetAccountID(accountId).
    59  		SetQueryPayment(NewHbar(2)).
    60  		SetMaxQueryPayment(NewHbar(10)).
    61  		SetNodeAccountIDs([]AccountID{{Account: 3}, {Account: 4}}).
    62  		SetMaxRetry(5).
    63  		SetMaxBackoff(10 * time.Second).
    64  		SetMinBackoff(1 * time.Second).
    65  		SetPaymentTransactionID(transactionID).
    66  		SetGrpcDeadline(&deadline)
    67  	client, err := _NewMockClient()
    68  	client.SetLedgerID(*NewLedgerIDTestnet())
    69  	require.NoError(t, err)
    70  	client.SetAutoValidateChecksums(true)
    71  	err = query.validateNetworkOnIDs(client)
    72  	require.NoError(t, err)
    73  	require.Equal(t, accountId, query.GetAccountID())
    74  	require.Equal(t, NewHbar(2), query.GetQueryPayment())
    75  	require.Equal(t, NewHbar(10), query.GetMaxQueryPayment())
    76  	require.Equal(t, []AccountID{{Account: 3}, {Account: 4}}, query.GetNodeAccountIDs())
    77  	require.Equal(t, 5, query.GetMaxRetryCount())
    78  	require.Equal(t, 10*time.Second, query.GetMaxBackoff())
    79  	require.Equal(t, 1*time.Second, query.GetMinBackoff())
    80  	require.Equal(t, transactionID, query.GetPaymentTransactionID())
    81  	require.Equal(t, &deadline, query.GetGrpcDeadline())
    82  }
    83  
    84  func TestUnitAccountInfoQueryValidateWrong(t *testing.T) {
    85  	t.Parallel()
    86  
    87  	client, err := _NewMockClient()
    88  	client.SetLedgerID(*NewLedgerIDTestnet())
    89  	require.NoError(t, err)
    90  	client.SetAutoValidateChecksums(true)
    91  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    92  	require.NoError(t, err)
    93  
    94  	infoQuery := NewAccountInfoQuery().
    95  		SetAccountID(accountID)
    96  
    97  	err = infoQuery.validateNetworkOnIDs(client)
    98  	require.Error(t, err)
    99  	if err != nil {
   100  		require.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
   101  	}
   102  }
   103  
   104  func TestUnitAccountInfoQuerySetNothing(t *testing.T) {
   105  	t.Parallel()
   106  
   107  	balance := NewAccountInfoQuery()
   108  
   109  	require.Equal(t, AccountID{}, balance.GetAccountID())
   110  	require.Equal(t, []AccountID{}, balance.GetNodeAccountIDs())
   111  	require.Equal(t, 250*time.Millisecond, balance.GetMinBackoff())
   112  	require.Equal(t, 8*time.Second, balance.GetMaxBackoff())
   113  	require.Equal(t, 10, balance.GetMaxRetryCount())
   114  	require.Equal(t, TransactionID{}, balance.GetPaymentTransactionID())
   115  	require.Equal(t, Hbar{}, balance.GetQueryPayment())
   116  	require.Equal(t, Hbar{}, balance.GetMaxQueryPayment())
   117  }
   118  
   119  func Test_AccountInfoQueryMapStatusError(t *testing.T) {
   120  	t.Parallel()
   121  
   122  	response := services.Response{
   123  		Response: &services.Response_CryptoGetInfo{
   124  			CryptoGetInfo: &services.CryptoGetInfoResponse{
   125  				Header: &services.ResponseHeader{
   126  					NodeTransactionPrecheckCode: services.ResponseCodeEnum(StatusInvalidAccountID),
   127  					ResponseType:                services.ResponseType_COST_ANSWER,
   128  				},
   129  			},
   130  		},
   131  	}
   132  
   133  	query := NewAccountInfoQuery()
   134  	actualError := query.mapStatusError(query, &response)
   135  
   136  	expectedError := ErrHederaPreCheckStatus{
   137  		Status: StatusInvalidAccountID,
   138  	}
   139  
   140  	require.Equal(t, expectedError, actualError)
   141  }
   142  
   143  func TestUnitAccountInfoQueryMock(t *testing.T) {
   144  	t.Parallel()
   145  
   146  	responses := [][]interface{}{{
   147  		&services.Response{
   148  			Response: &services.Response_CryptoGetInfo{
   149  				CryptoGetInfo: &services.CryptoGetInfoResponse{
   150  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   151  				},
   152  			},
   153  		},
   154  	}}
   155  
   156  	client, server := NewMockClientAndServer(responses)
   157  	defer server.Close()
   158  
   159  	query := NewAccountInfoQuery().
   160  		SetAccountID(AccountID{Account: 1234}).
   161  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   162  		SetMaxQueryPayment(NewHbar(1))
   163  
   164  	cost, err := query.GetCost(client)
   165  	require.NoError(t, err)
   166  	require.Equal(t, HbarFromTinybar(2), cost)
   167  }