github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_nft_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 TestUnitTokenNftGetInfoByNftIDValidate(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  	nftID, err := NftIDFromString("2@0.0.123-esxsf")
    43  	require.NoError(t, err)
    44  
    45  	nftInfo := NewTokenNftInfoQuery().
    46  		SetNftID(nftID)
    47  
    48  	err = nftInfo.validateNetworkOnIDs(client)
    49  	require.NoError(t, err)
    50  }
    51  
    52  func TestUnitTokenNftGetInfoByNftIDValidateWrong(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	client, err := _NewMockClient()
    56  	client.SetLedgerID(*NewLedgerIDTestnet())
    57  	require.NoError(t, err)
    58  	client.SetAutoValidateChecksums(true)
    59  	nftID, err := NftIDFromString("2@0.0.123-rmkykd")
    60  	require.NoError(t, err)
    61  
    62  	nftInfo := NewTokenNftInfoQuery().
    63  		SetNftID(nftID)
    64  
    65  	err = nftInfo.validateNetworkOnIDs(client)
    66  	require.Error(t, err)
    67  	if err != nil {
    68  		require.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    69  	}
    70  }
    71  
    72  func TestUnitTokenNftInfoQueryNothingSet(t *testing.T) {
    73  	t.Parallel()
    74  
    75  	query := NewTokenNftInfoQuery()
    76  
    77  	require.Equal(t, NftID{TokenID: TokenID{Shard: 0x0, Realm: 0x0, Token: 0x0, checksum: (*string)(nil)}, SerialNumber: 0}, query.GetNftID())
    78  	require.Equal(t, []AccountID{}, query.GetNodeAccountIDs())
    79  	require.Equal(t, 250*time.Millisecond, query.GetMinBackoff())
    80  	require.Equal(t, 8*time.Second, query.GetMaxBackoff())
    81  	require.Equal(t, 10, query.GetMaxRetryCount())
    82  	require.Equal(t, TransactionID{}, query.GetPaymentTransactionID())
    83  	require.Equal(t, Hbar{}, query.GetQueryPayment())
    84  	require.Equal(t, Hbar{}, query.GetMaxQueryPayment())
    85  }
    86  
    87  func TestUnitTokenNftInfoQueryGet(t *testing.T) {
    88  	t.Parallel()
    89  
    90  	checksum := "dmqui"
    91  	deadline := time.Second * 3
    92  	token := TokenID{Token: 3, checksum: &checksum}
    93  	account := AccountID{Account: 3, checksum: &checksum}
    94  	nodeAccountID := []AccountID{{Account: 10}}
    95  	transactionID := TransactionIDGenerate(account)
    96  
    97  	client, err := _NewMockClient()
    98  	client.SetLedgerID(*NewLedgerIDTestnet())
    99  	require.NoError(t, err)
   100  	client.SetAutoValidateChecksums(true)
   101  
   102  	query := NewTokenNftInfoQuery().
   103  		SetTokenID(token).
   104  		SetNftID(token.Nft(334)).
   105  		SetAccountID(account).
   106  		SetEnd(5).
   107  		SetStart(4).
   108  		ByAccountID(account).
   109  		ByTokenID(token).
   110  		ByNftID(token.Nft(334)).
   111  		SetMaxRetry(3).
   112  		SetMaxBackoff(time.Second * 30).
   113  		SetMinBackoff(time.Second * 10).
   114  		SetNodeAccountIDs(nodeAccountID).
   115  		SetPaymentTransactionID(transactionID).
   116  		SetMaxQueryPayment(NewHbar(23)).
   117  		SetQueryPayment(NewHbar(3)).
   118  		SetGrpcDeadline(&deadline)
   119  
   120  	err = query.validateNetworkOnIDs(client)
   121  	require.NoError(t, err)
   122  
   123  	// Some assertions like SetStart, SetEnd, etc. are missing, because those fucntions are deprecated and empty
   124  	require.Equal(t, token.Nft(334).String(), query.GetNftID().String())
   125  	require.Equal(t, token.Nft(334).String(), query.GetNftID().String())
   126  	require.Equal(t, nodeAccountID, nodeAccountID, query.GetNodeAccountIDs())
   127  	require.Equal(t, time.Second*30, query.GetMaxBackoff())
   128  	require.Equal(t, time.Second*10, query.GetMinBackoff())
   129  	require.Equal(t, NewHbar(3), query.GetQueryPayment())
   130  	require.Equal(t, NewHbar(23), query.GetMaxQueryPayment())
   131  	require.Equal(t, &deadline, query.GetGrpcDeadline())
   132  }
   133  
   134  func TestUnitTokenNftInfoQueryMock(t *testing.T) {
   135  	t.Parallel()
   136  
   137  	responses := [][]interface{}{{
   138  		&services.Response{
   139  			Response: &services.Response_TokenGetNftInfo{
   140  				TokenGetNftInfo: &services.TokenGetNftInfoResponse{
   141  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   142  				},
   143  			},
   144  		},
   145  		&services.Response{
   146  			Response: &services.Response_TokenGetNftInfo{
   147  				TokenGetNftInfo: &services.TokenGetNftInfoResponse{
   148  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
   149  				},
   150  			},
   151  		},
   152  		&services.Response{
   153  			Response: &services.Response_TokenGetNftInfo{
   154  				TokenGetNftInfo: &services.TokenGetNftInfoResponse{
   155  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2},
   156  					Nft: &services.TokenNftInfo{
   157  						NftID:        nil,
   158  						AccountID:    nil,
   159  						CreationTime: nil,
   160  						Metadata:     nil,
   161  						LedgerId:     nil,
   162  						SpenderId:    nil,
   163  					},
   164  				},
   165  			},
   166  		},
   167  	}}
   168  
   169  	client, server := NewMockClientAndServer(responses)
   170  	defer server.Close()
   171  
   172  	checksum := "dmqui"
   173  	token := TokenID{Token: 3, checksum: &checksum}
   174  
   175  	query := NewTokenNftInfoQuery().
   176  		SetNftID(token.Nft(43)).
   177  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   178  		SetMaxQueryPayment(NewHbar(1))
   179  
   180  	cost, err := query.GetCost(client)
   181  	require.NoError(t, err)
   182  	require.Equal(t, HbarFromTinybar(2), cost)
   183  	_, err = query.Execute(client)
   184  	require.NoError(t, err)
   185  }