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