Skip to content

Commit

Permalink
Finish up tests for at least 65% test coverage for token prefixed fil…
Browse files Browse the repository at this point in the history
…es (#773)

* Finish up 65% test coverage for token prefixed files

---------

Signed-off-by: Coiling-Dragon <[email protected]>
  • Loading branch information
Coiling-Dragon authored Jul 27, 2023
1 parent b0298b7 commit ee7a8d6
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 9 deletions.
3 changes: 1 addition & 2 deletions account_info_flow_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func TestIntegrationVerifySignatureFlowCanExecute(t *testing.T) {
newKey, err := PrivateKeyGenerateEd25519()
require.NoError(t, err)

newBalance := NewHbar(2)
assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
newBalance := NewHbar(10)

resp, err := NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
Expand Down
14 changes: 7 additions & 7 deletions contract_execute_transaction_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ func TestIntegrationContractExecuteTransactionNoGas(t *testing.T) {
fileID := *receipt.FileID
assert.NotNil(t, fileID)

node_ids := []AccountID{resp.NodeID}

resp, err = NewContractCreateTransaction().
SetAdminKey(env.Client.GetOperatorPublicKey()).
SetGas(100000).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetNodeAccountIDs(node_ids).
SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")).
SetBytecodeFileID(fileID).
SetContractMemo("hedera-sdk-go::TestContractDeleteTransaction_Execute").
Expand All @@ -156,21 +158,19 @@ func TestIntegrationContractExecuteTransactionNoGas(t *testing.T) {

resp, err = NewContractExecuteTransaction().
SetContractID(contractID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetNodeAccountIDs(node_ids).
SetFunction("setMessage", NewContractFunctionParameters().AddString("new message")).
Execute(env.Client)
require.NoError(t, err)

_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
assert.Error(t, err)
if err != nil {
assert.Equal(t, "exceptional receipt status: INSUFFICIENT_GAS", err.Error())
assert.Contains(t, err.Error(), "INSUFFICIENT_GAS")
}

resp, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetTransferAccountID(env.Client.GetOperatorAccountID()).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetNodeAccountIDs(node_ids).
Execute(env.Client)
require.NoError(t, err)

Expand All @@ -179,7 +179,7 @@ func TestIntegrationContractExecuteTransactionNoGas(t *testing.T) {

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetNodeAccountIDs(node_ids).
Execute(env.Client)
require.NoError(t, err)

Expand Down
60 changes: 60 additions & 0 deletions token_nft_allowance_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//go:build all || unit
// +build all unit

package hedera

/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestUnitNewTokenNftAllowance(t *testing.T) {
tokenID := TokenID{Token: 1}
owner := AccountID{Account: 2}
spender := AccountID{Account: 3}
serialNumbers := []int64{1, 2, 3}
approvedForAll := true
delegatingSpender := AccountID{Account: 4}

allowance := NewTokenNftAllowance(tokenID, owner, spender, serialNumbers, approvedForAll, delegatingSpender)

assert.Equal(t, &tokenID, allowance.TokenID)
assert.Equal(t, &owner, allowance.OwnerAccountID)
assert.Equal(t, &spender, allowance.SpenderAccountID)
assert.Equal(t, serialNumbers, allowance.SerialNumbers)
assert.Equal(t, approvedForAll, allowance.AllSerials)
assert.Equal(t, &delegatingSpender, allowance.DelegatingSpender)
}

func TestUnitTokenNftAllowance_String(t *testing.T) {
approval := TokenNftAllowance{
TokenID: &TokenID{Token: 1},
SpenderAccountID: &AccountID{Account: 2},
OwnerAccountID: &AccountID{Account: 3},
SerialNumbers: []int64{1, 2, 3},
AllSerials: true,
DelegatingSpender: &AccountID{Account: 4},
}

assert.Equal(t, "OwnerAccountID: 0.0.3, SpenderAccountID: 0.0.2, TokenID: 0.0.1, Serials: 1, 2, 3, , ApprovedForAll: true", approval.String())
}
56 changes: 56 additions & 0 deletions token_nft_transfer_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:build all || unit
// +build all unit

package hedera

/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import (
"testing"

"github.com/stretchr/testify/assert"
)

// testinf func (transfer TokenNftTransfer) ToBytes() and func NftTransferFromBytes(data []byte)
func TestUnitTokenNftTransferToBytes(t *testing.T) {
t.Parallel()

transfer := TokenNftTransfer{
SenderAccountID: AccountID{Account: 3},
ReceiverAccountID: AccountID{Account: 4},
SerialNumber: 5,
IsApproved: true,
}

transferBytes := transfer.ToBytes()
transferFromBytes, err := NftTransferFromBytes(transferBytes)

assert.NoError(t, err)
assert.Equal(t, transfer, transferFromBytes)

// test invalid data from and to bytes
_, err = NftTransferFromBytes([]byte{1, 2, 3})
assert.Error(t, err)

// test nil data from bytes and to bytes
_, err = NftTransferFromBytes(nil)
assert.Error(t, err)
}
169 changes: 169 additions & 0 deletions token_pause_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/hashgraph/hedera-protobufs-go/services"
protobuf "google.golang.org/protobuf/proto"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -327,3 +328,171 @@ func TestUnitTokenUnpauseTransactionMock(t *testing.T) {
_, err = freez.Sign(newKey).Execute(client)
require.NoError(t, err)
}

func TestUnitTokenPauseTransaction_SetMaxRetry(t *testing.T) {
t.Parallel()
transaction := NewTokenPauseTransaction()
transaction.SetMaxRetry(5)

require.Equal(t, 5, transaction.GetMaxRetry())
}

func TestUnitTokenPauseTransaction_AddSignature(t *testing.T) {
t.Parallel()
client, _ := ClientFromConfig([]byte(testClientJSONWithoutMirrorNetwork))

nodeAccountId, err := AccountIDFromString("0.0.3")
require.NoError(t, err)

nodeIdList := []AccountID{nodeAccountId}

transaction, err := NewTokenPauseTransaction().
SetNodeAccountIDs(nodeIdList).
FreezeWith(client)

privateKey, _ := PrivateKeyGenerateEd25519()

signature, err := privateKey.SignTransaction(&transaction.Transaction)
require.NoError(t, err)

signs, err := transaction.GetSignatures()
for key := range signs[nodeAccountId] {
require.Equal(t, signs[nodeAccountId][key], signature)
}

require.NoError(t, err)

privateKey2, _ := PrivateKeyGenerateEd25519()
publicKey2 := privateKey2.PublicKey()

signedTransaction := transaction.AddSignature(publicKey2, signature)
signs2, err := signedTransaction.GetSignatures()
require.NoError(t, err)

for key := range signs2[nodeAccountId] {
require.Equal(t, signs2[nodeAccountId][key], signature)
}
}

func TestUnitTokenPauseTransaction_SignWithOperator(t *testing.T) {
t.Parallel()
client, _ := ClientFromConfig([]byte(testClientJSONWithoutMirrorNetwork))
privateKey, _ := PrivateKeyGenerateEd25519()
publicKey := privateKey.PublicKey()
operatorId, _ := AccountIDFromString("0.0.10")

client.SetOperator(operatorId, privateKey)

nodeAccountId, err := AccountIDFromString("0.0.3")
require.NoError(t, err)
nodeIdList := []AccountID{nodeAccountId}

transaction, err := NewTokenPauseTransaction().
SetNodeAccountIDs(nodeIdList).
SetTokenID(TokenID{Token: 3}).
SignWithOperator(client)

require.NoError(t, err)
require.NotNil(t, transaction)

privateKey2, _ := PrivateKeyGenerateEd25519()
publicKey2 := privateKey2.PublicKey()
client.SetOperator(operatorId, privateKey2)

transactionSignedWithOp, err := transaction.SignWithOperator(client)
require.NoError(t, err)
require.NotNil(t, transactionSignedWithOp)

assert.Contains(t, transactionSignedWithOp.Transaction.publicKeys, publicKey)
assert.Contains(t, transactionSignedWithOp.Transaction.publicKeys, publicKey2)

// test errors
client.operator = nil
tx, err := NewTokenPauseTransaction().
SetNodeAccountIDs(nodeIdList).
SetTokenID(TokenID{Token: 3}).
SignWithOperator(client)

require.Error(t, err)
require.Nil(t, tx)

client = nil
tx, err = NewTokenPauseTransaction().
SetNodeAccountIDs(nodeIdList).
SetTokenID(TokenID{Token: 3}).
SignWithOperator(client)

require.Error(t, err)
require.Nil(t, tx)
}

func TestUnitTokenPauseTransaction_SetMaxBackoff(t *testing.T) {
t.Parallel()
transaction := NewTokenPauseTransaction()
maxBackoff := 10 * time.Second

transaction.SetMaxBackoff(maxBackoff)

require.Equal(t, maxBackoff, transaction.GetMaxBackoff())

// test max.Nanoseconds() < 0
transaction2 := NewTokenPauseTransaction()
maxBackoff2 := -1 * time.Second

require.Panics(t, func() { transaction2.SetMaxBackoff(maxBackoff2) })

// test max.Nanoseconds() < min.Nanoseconds()
transaction3 := NewTokenPauseTransaction()
maxBackoff3 := 1 * time.Second
minBackoff3 := 2 * time.Second

transaction3.SetMinBackoff(minBackoff3)

require.Panics(t, func() { transaction3.SetMaxBackoff(maxBackoff3) })
}

func TestUnitTokenPauseTransaction_GetMaxBackoff(t *testing.T) {
t.Parallel()
transaction := NewTokenPauseTransaction()

require.Equal(t, 8*time.Second, transaction.GetMaxBackoff())
}

func TestUnitTokenPauseTransaction_SetMinBackoff(t *testing.T) {
t.Parallel()
transaction := NewTokenPauseTransaction()
minBackoff := 1 * time.Second

transaction.SetMinBackoff(minBackoff)

require.Equal(t, minBackoff, transaction.GetMinBackoff())

// test min.Nanoseconds() < 0
transaction2 := NewTokenPauseTransaction()
minBackoff2 := -1 * time.Second

require.Panics(t, func() { transaction2.SetMinBackoff(minBackoff2) })

// test transaction.maxBackoff.Nanoseconds() < min.Nanoseconds()
transaction3 := NewTokenPauseTransaction()
minBackoff3 := 10 * time.Second

require.Panics(t, func() { transaction3.SetMinBackoff(minBackoff3) })
}

func TestUnitTokenPauseTransaction_GetMinBackoff(t *testing.T) {
t.Parallel()
transaction := NewTokenPauseTransaction()

require.Equal(t, 250*time.Millisecond, transaction.GetMinBackoff())
}

func TestUnitTokenPauseTransaction_SetLogLevel(t *testing.T) {
t.Parallel()
transaction := NewTokenPauseTransaction()

transaction.SetLogLevel(LoggerLevelDebug)

level := transaction.GetLogLevel()
require.Equal(t, LoggerLevelDebug, *level)
}

0 comments on commit ee7a8d6

Please sign in to comment.