github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_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  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestUnitTopicInfoQueryValidate(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	client, err := _NewMockClient()
    39  	client.SetLedgerID(*NewLedgerIDTestnet())
    40  	require.NoError(t, err)
    41  	client.SetAutoValidateChecksums(true)
    42  	topicID, err := TopicIDFromString("0.0.123-esxsf")
    43  	require.NoError(t, err)
    44  	err = topicID.Validate(client)
    45  	require.NoError(t, err)
    46  	topicInfo := NewTopicInfoQuery().
    47  		SetTopicID(topicID)
    48  
    49  	err = topicInfo.validateNetworkOnIDs(client)
    50  	require.NoError(t, err)
    51  }
    52  
    53  func TestUnitTopicInfoQueryValidateWrong(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  	topicID, err := TopicIDFromString("0.0.123-rmkykd")
    61  	require.NoError(t, err)
    62  	err = topicID.Validate(client)
    63  	require.Error(t, err)
    64  	topicInfo := NewTopicInfoQuery().
    65  		SetTopicID(topicID)
    66  
    67  	err = topicInfo.validateNetworkOnIDs(client)
    68  	require.Error(t, err)
    69  	if err != nil {
    70  		require.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    71  	}
    72  }
    73  
    74  func TestUnitTopicInfoQueryGet(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	checksum := "dmqui"
    78  	topicID := TopicID{Topic: 3, checksum: &checksum}
    79  	deadline := time.Duration(time.Minute)
    80  	accountId := AccountID{Account: 123}
    81  	transactionID := TransactionIDGenerate(accountId)
    82  	query := NewTopicInfoQuery().
    83  		SetTopicID(topicID).
    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).
    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, topicID, query.GetTopicID())
   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, 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 TestUnitTopicInfoQueryNothingSet(t *testing.T) {
   112  	t.Parallel()
   113  
   114  	query := NewTopicInfoQuery()
   115  
   116  	require.Equal(t, TopicID{}, query.GetTopicID())
   117  	require.Equal(t, []AccountID{}, query.GetNodeAccountIDs())
   118  	require.Equal(t, 250*time.Millisecond, query.GetMinBackoff())
   119  	require.Equal(t, 8*time.Second, query.GetMaxBackoff())
   120  	require.Equal(t, 10, query.GetMaxRetryCount())
   121  	require.Equal(t, TransactionID{}, query.GetPaymentTransactionID())
   122  	require.Equal(t, Hbar{}, query.GetQueryPayment())
   123  	require.Equal(t, Hbar{}, query.GetMaxQueryPayment())
   124  }
   125  
   126  func TestUnitTopicInfoQueryMock(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	responses := [][]interface{}{{
   130  		&services.Response{
   131  			Response: &services.Response_ConsensusGetTopicInfo{
   132  				ConsensusGetTopicInfo: &services.ConsensusGetTopicInfoResponse{
   133  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   134  				},
   135  			},
   136  		},
   137  		&services.Response{
   138  			Response: &services.Response_ConsensusGetTopicInfo{
   139  				ConsensusGetTopicInfo: &services.ConsensusGetTopicInfoResponse{
   140  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   141  				},
   142  			},
   143  		},
   144  		&services.Response{
   145  			Response: &services.Response_ConsensusGetTopicInfo{
   146  				ConsensusGetTopicInfo: &services.ConsensusGetTopicInfoResponse{
   147  					Header:  &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2},
   148  					TopicID: nil,
   149  					TopicInfo: &services.ConsensusTopicInfo{
   150  						Memo:             "",
   151  						RunningHash:      nil,
   152  						SequenceNumber:   0,
   153  						ExpirationTime:   nil,
   154  						AdminKey:         nil,
   155  						SubmitKey:        nil,
   156  						AutoRenewPeriod:  nil,
   157  						AutoRenewAccount: nil,
   158  						LedgerId:         nil,
   159  					},
   160  				},
   161  			},
   162  		},
   163  	}}
   164  
   165  	client, server := NewMockClientAndServer(responses)
   166  	defer server.Close()
   167  
   168  	checksum := "dmqui"
   169  	topic := TopicID{Topic: 3, checksum: &checksum}
   170  
   171  	query := NewTopicInfoQuery().
   172  		SetTopicID(topic).
   173  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   174  		SetMaxQueryPayment(NewHbar(1))
   175  
   176  	cost, err := query.GetCost(client)
   177  	require.NoError(t, err)
   178  	require.Equal(t, HbarFromTinybar(2), cost)
   179  	_, err = query.Execute(client)
   180  	require.NoError(t, err)
   181  }