github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/schedule_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/assert"
    32  
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestUnitScheduleInfoQueryValidate(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	client, err := _NewMockClient()
    40  	client.SetLedgerID(*NewLedgerIDTestnet())
    41  	require.NoError(t, err)
    42  	client.SetAutoValidateChecksums(true)
    43  	scheduleID, err := ScheduleIDFromString("0.0.123-esxsf")
    44  	require.NoError(t, err)
    45  
    46  	scheduleInfo := NewScheduleInfoQuery().
    47  		SetScheduleID(scheduleID)
    48  
    49  	err = scheduleInfo.validateNetworkOnIDs(client)
    50  	require.NoError(t, err)
    51  }
    52  
    53  func TestUnitScheduleInfoQueryValidateWrong(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	client, err := _NewMockClient()
    57  	client.SetLedgerID(*NewLedgerIDTestnet())
    58  	require.NoError(t, err)
    59  	client.SetAutoValidateChecksums(true)
    60  	scheduleID, err := ScheduleIDFromString("0.0.123-rmkykd")
    61  	require.NoError(t, err)
    62  
    63  	scheduleInfo := NewScheduleInfoQuery().
    64  		SetScheduleID(scheduleID)
    65  
    66  	err = scheduleInfo.validateNetworkOnIDs(client)
    67  	assert.Error(t, err)
    68  	if err != nil {
    69  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    70  	}
    71  }
    72  
    73  func TestUnitScheduleInfoQueryGet(t *testing.T) {
    74  	t.Parallel()
    75  
    76  	checksum := "dmqui"
    77  	accountId := AccountID{Account: 123}
    78  	deadline := time.Duration(time.Minute)
    79  	validStart := time.Now().Add(10 * time.Minute)
    80  	scheduleID := ScheduleID{Schedule: 3, checksum: &checksum}
    81  
    82  	query := NewScheduleInfoQuery().
    83  		SetScheduleID(scheduleID).
    84  		SetQueryPayment(NewHbar(2)).
    85  		SetMaxQueryPayment(NewHbar(1)).
    86  		SetQueryPayment(HbarFromTinybar(25)).
    87  		SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}).
    88  		SetMaxRetry(3).
    89  		SetMinBackoff(300 * time.Millisecond).
    90  		SetMaxBackoff(10 * time.Second).
    91  		SetPaymentTransactionID(TransactionID{AccountID: &accountId, ValidStart: &validStart}).
    92  		SetMaxQueryPayment(NewHbar(500)).
    93  		SetGrpcDeadline(&deadline)
    94  	client, err := _NewMockClient()
    95  	client.SetLedgerID(*NewLedgerIDTestnet())
    96  	require.NoError(t, err)
    97  	client.SetAutoValidateChecksums(true)
    98  	err = query.validateNetworkOnIDs(client)
    99  	require.NoError(t, err)
   100  	require.Equal(t, scheduleID, query.GetScheduleID())
   101  	require.Equal(t, []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}, query.GetNodeAccountIDs())
   102  	require.Equal(t, 300*time.Millisecond, query.GetMinBackoff())
   103  	require.Equal(t, 10*time.Second, query.GetMaxBackoff())
   104  	require.Equal(t, 3, query.GetMaxRetryCount())
   105  	require.Equal(t, TransactionID{AccountID: &AccountID{Account: 123}, ValidStart: &validStart}, query.GetPaymentTransactionID())
   106  	require.Equal(t, HbarFromTinybar(25), query.GetQueryPayment())
   107  	require.Equal(t, NewHbar(500), query.GetMaxQueryPayment())
   108  	require.Equal(t, &deadline, query.GetGrpcDeadline())
   109  }
   110  
   111  func TestUnitScheduleInfoQuerySetNothing(t *testing.T) {
   112  	t.Parallel()
   113  
   114  	info := NewScheduleInfoQuery()
   115  
   116  	require.Equal(t, ScheduleID{}, info.GetScheduleID())
   117  	require.Equal(t, []AccountID{}, info.GetNodeAccountIDs())
   118  	require.Equal(t, 250*time.Millisecond, info.GetMinBackoff())
   119  	require.Equal(t, 8*time.Second, info.GetMaxBackoff())
   120  	require.Equal(t, 10, info.GetMaxRetryCount())
   121  	require.Equal(t, TransactionID{}, info.GetPaymentTransactionID())
   122  	require.Equal(t, Hbar{}, info.GetQueryPayment())
   123  	require.Equal(t, Hbar{}, info.GetMaxQueryPayment())
   124  }
   125  
   126  func TestUnitScheduleInfoQueryCoverage(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	checksum := "dmqui"
   130  	deadline := time.Second * 3
   131  	schedule := ScheduleID{Schedule: 3, checksum: &checksum}
   132  	nodeAccountID := []AccountID{{Account: 10}}
   133  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   134  
   135  	client, err := _NewMockClient()
   136  	client.SetLedgerID(*NewLedgerIDTestnet())
   137  	require.NoError(t, err)
   138  	client.SetAutoValidateChecksums(true)
   139  
   140  	query := NewScheduleInfoQuery().
   141  		SetScheduleID(schedule).
   142  		SetMaxRetry(3).
   143  		SetMaxBackoff(time.Second * 30).
   144  		SetMinBackoff(time.Second * 10).
   145  		SetNodeAccountIDs(nodeAccountID).
   146  		SetPaymentTransactionID(transactionID).
   147  		SetMaxQueryPayment(NewHbar(23)).
   148  		SetQueryPayment(NewHbar(3)).
   149  		SetGrpcDeadline(&deadline)
   150  
   151  	err = query.validateNetworkOnIDs(client)
   152  	require.NoError(t, err)
   153  
   154  	require.Equal(t, nodeAccountID, query.GetNodeAccountIDs())
   155  	require.Equal(t, 30*time.Second, query.GetMaxBackoff())
   156  	require.Equal(t, 10*time.Second, query.GetMinBackoff())
   157  	require.NotEmpty(t, query.getName())
   158  	require.Equal(t, schedule, query.GetScheduleID())
   159  	require.Equal(t, NewHbar(3), query.GetQueryPayment())
   160  	require.Equal(t, NewHbar(23), query.GetMaxQueryPayment())
   161  	require.Equal(t, &deadline, query.GetGrpcDeadline())
   162  }
   163  
   164  func TestUnitScheduleInfoQueryMock(t *testing.T) {
   165  	t.Parallel()
   166  
   167  	responses := [][]interface{}{{
   168  		&services.Response{
   169  			Response: &services.Response_ScheduleGetInfo{
   170  				ScheduleGetInfo: &services.ScheduleGetInfoResponse{
   171  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   172  				},
   173  			},
   174  		},
   175  		&services.Response{
   176  			Response: &services.Response_ScheduleGetInfo{
   177  				ScheduleGetInfo: &services.ScheduleGetInfoResponse{
   178  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   179  				},
   180  			},
   181  		},
   182  		&services.Response{
   183  			Response: &services.Response_ScheduleGetInfo{
   184  				ScheduleGetInfo: &services.ScheduleGetInfoResponse{
   185  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2},
   186  					ScheduleInfo: &services.ScheduleInfo{
   187  						ScheduleID:               nil,
   188  						Data:                     nil,
   189  						ExpirationTime:           nil,
   190  						ScheduledTransactionBody: nil,
   191  						Memo:                     "",
   192  						AdminKey:                 nil,
   193  						Signers:                  nil,
   194  						CreatorAccountID:         nil,
   195  						PayerAccountID:           nil,
   196  						ScheduledTransactionID:   nil,
   197  						LedgerId:                 nil,
   198  						WaitForExpiry:            false,
   199  					},
   200  				},
   201  			},
   202  		},
   203  	}}
   204  
   205  	client, server := NewMockClientAndServer(responses)
   206  	defer server.Close()
   207  
   208  	query := NewScheduleInfoQuery().
   209  		SetScheduleID(ScheduleID{Schedule: 3}).
   210  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   211  		SetMaxQueryPayment(NewHbar(1))
   212  
   213  	cost, err := query.GetCost(client)
   214  	require.NoError(t, err)
   215  	require.Equal(t, HbarFromTinybar(2), cost)
   216  	_, err = query.Execute(client)
   217  	require.NoError(t, err)
   218  }