github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_records_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 TestUnitAccountRecordQueryValidate(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  	recordQuery := NewAccountRecordsQuery().
    48  		SetAccountID(accountID)
    49  
    50  	err = recordQuery.validateNetworkOnIDs(client)
    51  	require.NoError(t, err)
    52  }
    53  
    54  func TestUnitAccountRecordQueryValidateWrong(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  	recordQuery := NewAccountRecordsQuery().
    65  		SetAccountID(accountID)
    66  
    67  	err = recordQuery.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 TestUnitAccountRecordsQueryMock(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	responses := [][]interface{}{{
    78  		&services.Response{
    79  			Response: &services.Response_CryptoGetAccountRecords{
    80  				CryptoGetAccountRecords: &services.CryptoGetAccountRecordsResponse{
    81  					Header:    &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
    82  					AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 1800}},
    83  				},
    84  			},
    85  		},
    86  		&services.Response{
    87  			Response: &services.Response_CryptoGetAccountRecords{
    88  				CryptoGetAccountRecords: &services.CryptoGetAccountRecordsResponse{
    89  					Header:    &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
    90  					AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 1800}},
    91  				},
    92  			},
    93  		},
    94  		&services.Response{
    95  			Response: &services.Response_CryptoGetAccountRecords{
    96  				CryptoGetAccountRecords: &services.CryptoGetAccountRecordsResponse{
    97  					Header:    &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 1},
    98  					AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 1800}},
    99  					Records: []*services.TransactionRecord{
   100  						{
   101  							TransactionHash:    []byte{1},
   102  							ConsensusTimestamp: &services.Timestamp{Nanos: 12313123, Seconds: 2313},
   103  							TransactionID: &services.TransactionID{
   104  								TransactionValidStart: &services.Timestamp{Nanos: 12313123, Seconds: 2313},
   105  								AccountID:             &services.AccountID{Account: &services.AccountID_AccountNum{AccountNum: 1800}},
   106  								Scheduled:             false,
   107  								Nonce:                 0,
   108  							},
   109  							Memo:           "",
   110  							TransactionFee: 0,
   111  						},
   112  					},
   113  				},
   114  			},
   115  		},
   116  	}}
   117  
   118  	client, server := NewMockClientAndServer(responses)
   119  	defer server.Close()
   120  
   121  	query := NewAccountRecordsQuery().
   122  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   123  		SetAccountID(AccountID{Account: 1800}).
   124  		SetMaxQueryPayment(NewHbar(1))
   125  
   126  	_, err := query.GetCost(client)
   127  	require.NoError(t, err)
   128  	recordsQuery, err := query.Execute(client)
   129  	require.NoError(t, err)
   130  
   131  	require.Equal(t, len(recordsQuery), 1)
   132  	require.Equal(t, recordsQuery[0].TransactionID.AccountID.Account, uint64(1800))
   133  }
   134  
   135  func TestUnitAccountRecordsQueryGet(t *testing.T) {
   136  	t.Parallel()
   137  
   138  	spenderAccountID1 := AccountID{Account: 7}
   139  
   140  	balance := NewAccountRecordsQuery().
   141  		SetAccountID(spenderAccountID1).
   142  		SetQueryPayment(NewHbar(2)).
   143  		SetMaxQueryPayment(NewHbar(10)).
   144  		SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}})
   145  
   146  	balance.GetAccountID()
   147  	balance.GetNodeAccountIDs()
   148  	balance.GetMinBackoff()
   149  	balance.GetMaxBackoff()
   150  	balance.GetMaxRetryCount()
   151  	balance.GetPaymentTransactionID()
   152  	balance.GetQueryPayment()
   153  	balance.GetMaxQueryPayment()
   154  }
   155  
   156  func TestUnitAccountRecordsQuerySetNothing(t *testing.T) {
   157  	t.Parallel()
   158  
   159  	balance := NewAccountRecordsQuery()
   160  
   161  	balance.GetAccountID()
   162  	balance.GetNodeAccountIDs()
   163  	balance.GetMinBackoff()
   164  	balance.GetMaxBackoff()
   165  	balance.GetMaxRetryCount()
   166  	balance.GetPaymentTransactionID()
   167  	balance.GetQueryPayment()
   168  	balance.GetMaxQueryPayment()
   169  }
   170  
   171  func TestUnitAccountRecordsQueryCoverage(t *testing.T) {
   172  	t.Parallel()
   173  
   174  	checksum := "dmqui"
   175  	grpc := time.Second * 3
   176  	account := AccountID{Account: 3, checksum: &checksum}
   177  	nodeAccountID := []AccountID{{Account: 10}}
   178  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   179  
   180  	client, err := _NewMockClient()
   181  	client.SetLedgerID(*NewLedgerIDTestnet())
   182  	require.NoError(t, err)
   183  	client.SetAutoValidateChecksums(true)
   184  
   185  	query := NewAccountRecordsQuery().
   186  		SetMaxRetry(3).
   187  		SetMaxBackoff(time.Second * 30).
   188  		SetMinBackoff(time.Second * 10).
   189  		SetAccountID(account).
   190  		SetNodeAccountIDs(nodeAccountID).
   191  		SetPaymentTransactionID(transactionID).
   192  		SetMaxQueryPayment(NewHbar(23)).
   193  		SetQueryPayment(NewHbar(3)).
   194  		SetGrpcDeadline(&grpc)
   195  
   196  	err = query.validateNetworkOnIDs(client)
   197  
   198  	require.NoError(t, err)
   199  	query.GetNodeAccountIDs()
   200  	query.GetMaxBackoff()
   201  	query.GetMinBackoff()
   202  	query.getName()
   203  	query.GetAccountID()
   204  }