Skip to content

Commit

Permalink
fix: Adding specific error checks to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 authored and janaakhterov committed Dec 22, 2020
1 parent 3374950 commit 4ee8527
Show file tree
Hide file tree
Showing 44 changed files with 785 additions and 362 deletions.
2 changes: 1 addition & 1 deletion account_create_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func Test_AccountCreate_NoKey(t *testing.T) {
resp, err := NewAccountCreateTransaction().
Execute(client)
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf("exceptional precheck status KEY_REQUIRED received for transaction %s", resp.TransactionID))
assert.Equal(t, fmt.Sprintf("exceptional precheck status KEY_REQUIRED received for transaction %s", resp.TransactionID), err.Error())
}
11 changes: 7 additions & 4 deletions account_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ func Test_AccountDelete_NoTransferAccountID(t *testing.T) {

resp, err = tx.Sign(newKey).Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status ACCOUNT_ID_DOES_NOT_EXIST received for transaction %s", resp.TransactionID), err.Error())

_, err = resp.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("Invalid node AccountID was set for transaction: %s", resp.NodeID), err.Error())

}

func Test_AccountDelete_NoAccountID(t *testing.T) {
Expand All @@ -97,7 +100,7 @@ func Test_AccountDelete_NoAccountID(t *testing.T) {
SetInitialBalance(newBalance).
Execute(client)
assert.NoError(t, err)
fmt.Printf("%v\n", resp)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

Expand All @@ -109,8 +112,7 @@ func Test_AccountDelete_NoAccountID(t *testing.T) {

resp, err = tx.Sign(newKey).Execute(client)
assert.Error(t, err)
assert.Equal(t, err.Error(), fmt.Sprintf("exceptional precheck status KEY_REQUIRED received for transaction %s", resp.TransactionID))

assert.Equal(t, fmt.Sprintf("exceptional precheck status ACCOUNT_ID_DOES_NOT_EXIST received for transaction %s", resp.TransactionID), err.Error())
}

func Test_AccountDelete_NoSinging(t *testing.T) {
Expand Down Expand Up @@ -146,5 +148,6 @@ func Test_AccountDelete_NoSinging(t *testing.T) {
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_SIGNATURE"), err.Error())
}
19 changes: 19 additions & 0 deletions account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func accountInfoFromProtobuf(pb *proto.CryptoGetInfoResponse_AccountInfo) (Accou
return AccountInfo{}, err
}

tokenRelationship := make([]*TokenRelationship, len(pb.TokenRelationships))

if pb.TokenRelationships != nil {
for i, relationship := range pb.TokenRelationships {
singleRelationship := tokenRelationshipFromProtobuf(relationship)
tokenRelationship[i] = &singleRelationship
}
}

return AccountInfo{
AccountID: accountIDFromProtobuf(pb.AccountID),
ContractAccountID: pb.ContractAccountID,
Expand All @@ -42,11 +51,20 @@ func accountInfoFromProtobuf(pb *proto.CryptoGetInfoResponse_AccountInfo) (Accou
GenerateSendRecordThreshold: HbarFromTinybar(int64(pb.GenerateSendRecordThreshold)),
GenerateReceiveRecordThreshold: HbarFromTinybar(int64(pb.GenerateReceiveRecordThreshold)),
ReceiverSigRequired: pb.ReceiverSigRequired,
TokenRelationships: tokenRelationship,
ExpirationTime: timeFromProtobuf(pb.ExpirationTime),
}, nil
}

func (info AccountInfo) toProtobuf() ([]byte, error) {

tokenRelationship := make([]*proto.TokenRelationship, len(info.TokenRelationships))

for i, relationship := range info.TokenRelationships {
singleRelationship := relationship.toProtobuf()
tokenRelationship[i] = singleRelationship
}

pb, err := protobuf.Marshal(&proto.CryptoGetInfoResponse_AccountInfo{
AccountID: info.AccountID.toProtobuf(),
ContractAccountID: info.ContractAccountID,
Expand All @@ -58,6 +76,7 @@ func (info AccountInfo) toProtobuf() ([]byte, error) {
GenerateSendRecordThreshold: uint64(info.GenerateSendRecordThreshold.tinybar),
GenerateReceiveRecordThreshold: uint64(info.GenerateReceiveRecordThreshold.tinybar),
ReceiverSigRequired: info.ReceiverSigRequired,
TokenRelationships: tokenRelationship,
ExpirationTime: timeToProtobuf(info.ExpirationTime),
})

Expand Down
2 changes: 2 additions & 0 deletions account_info_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
Expand Down Expand Up @@ -72,4 +73,5 @@ func Test_AccountInfo_NoAccountID(t *testing.T) {
_, err := NewAccountInfoQuery().
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID"), err.Error())
}
2 changes: 2 additions & 0 deletions account_records_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
Expand Down Expand Up @@ -55,4 +56,5 @@ func Test_AccountRecord_NoAccountID(t *testing.T) {
_, err := NewAccountRecordsQuery().
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID"), err.Error())
}
2 changes: 2 additions & 0 deletions account_stakers_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
Expand Down Expand Up @@ -30,4 +31,5 @@ func TestAccountStakersNoAccountID_Execute(t *testing.T) {
_, err := NewAccountStakersQuery().
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status NOT_SUPPORTED"), err.Error())
}
16 changes: 15 additions & 1 deletion account_update_transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -134,11 +135,24 @@ func Test_AccountUpdate_NoSigning(t *testing.T) {

assert.NoError(t, err)

txDelete.Sign(newKey2)
txDelete.Sign(newKey)

resp, err = txDelete.Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
}

func Test_AccountUpdate_AccoundIDNotSet(t *testing.T) {
client := newTestClient(t)

resp, err := NewAccountUpdateTransaction().
SetMaxTransactionFee(NewHbar(1)).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_ACCOUNT_ID"), err.Error())
}
2 changes: 2 additions & 0 deletions contract_bytecode_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
Expand Down Expand Up @@ -123,6 +124,7 @@ func Test_ContractBytecode_NoContractID(t *testing.T) {
SetQueryPayment(NewHbar(2)).
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_CONTRACT_ID"), err.Error())

resp, err = NewContractDeleteTransaction().
SetContractID(contractID).
Expand Down
60 changes: 5 additions & 55 deletions contract_call_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
Expand Down Expand Up @@ -110,66 +111,13 @@ func TestNewContractCallQuery_Execute(t *testing.T) {
func Test_NewContractCall_NoContractID(t *testing.T) {
client := newTestClient(t)

smartContractBytecode := []byte("608060405234801561001057600080fd5b506040516104d73803806104d78339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b506040525050600080546001600160a01b0319163317905550805161010890600190602084019061010f565b50506101aa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015057805160ff191683800117855561017d565b8280016001018555821561017d579182015b8281111561017d578251825591602001919060010190610162565b5061018992915061018d565b5090565b6101a791905b808211156101895760008155600101610193565b90565b61031e806101b96000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063368b87721461004657806341c0e1b5146100ee578063ce6d41de146100f6575b600080fd5b6100ec6004803603602081101561005c57600080fd5b81019060208101813564010000000081111561007757600080fd5b82018360208201111561008957600080fd5b803590602001918460018302840111640100000000831117156100ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610173945050505050565b005b6100ec6101a2565b6100fe6101ba565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610138578181015183820152602001610120565b50505050905090810190601f1680156101655780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000546001600160a01b0316331461018a5761019f565b805161019d906001906020840190610250565b505b50565b6000546001600160a01b03163314156101b85733ff5b565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102455780601f1061021a57610100808354040283529160200191610245565b820191906000526020600020905b81548152906001019060200180831161022857829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061029157805160ff19168380011785556102be565b828001600101855582156102be579182015b828111156102be5782518255916020019190600101906102a3565b506102ca9291506102ce565b5090565b61024d91905b808211156102ca57600081556001016102d456fea264697066735822122084964d4c3f6bc912a9d20e14e449721012d625aa3c8a12de41ae5519752fc89064736f6c63430006000033")

resp, err := NewFileCreateTransaction().
SetKeys(client.GetOperatorPublicKey()).
SetContents(smartContractBytecode).
SetMaxTransactionFee(NewHbar(5)).
Execute(client)
assert.NoError(t, err)

receipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

fileID := *receipt.FileID
assert.NotNil(t, fileID)

resp, err = NewContractCreateTransaction().
SetAdminKey(client.GetOperatorPublicKey()).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetGas(2000).
SetConstructorParameters(NewContractFunctionParameters().AddString("Hello from Hedera.")).
SetBytecodeFileID(fileID).
SetContractMemo("[e2e::ContractCreateTransaction]").
SetMaxTransactionFee(NewHbar(20)).
Execute(client)
assert.NoError(t, err)

contractReceipt, err := resp.GetReceipt(client)
assert.NoError(t, err)

assert.True(t, contractReceipt.ContractID.Contract > 0)

assert.NotNil(t, contractReceipt.ContractID)

contractID := *contractReceipt.ContractID

_, err = NewContractCallQuery().
SetNodeAccountIDs([]AccountID{resp.NodeID}).
_, err := NewContractCallQuery().
SetGas(2000).
//test getCost
SetFunction("getMessage", nil).
Execute(client)
assert.Error(t, err)

resp, err = NewContractDeleteTransaction().
SetContractID(contractID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_CONTRACT_ID"), err.Error())
}

func Test_NewContractCall_NoGas(t *testing.T) {
Expand Down Expand Up @@ -217,6 +165,7 @@ func Test_NewContractCall_NoGas(t *testing.T) {
SetFunction("getMessage", nil).
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INSUFFICIENT_GAS"), err.Error())

resp, err = NewContractDeleteTransaction().
SetContractID(contractID).
Expand Down Expand Up @@ -282,6 +231,7 @@ func Test_NewContractCall_NoFunction(t *testing.T) {
//test getCost
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status CONTRACT_REVERT_EXECUTED"), err.Error())

resp, err = NewContractDeleteTransaction().
SetContractID(contractID).
Expand Down
12 changes: 9 additions & 3 deletions contract_create_transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"

"testing"
Expand Down Expand Up @@ -107,6 +108,7 @@ func Test_ContractCreate_NoAdminKey(t *testing.T) {

_, err = resp.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status MODIFYING_IMMUTABLE_CONTRACT"), err.Error())

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
Expand Down Expand Up @@ -149,6 +151,7 @@ func Test_ContractCreate_NoGas(t *testing.T) {

receipt, err = resp.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INSUFFICIENT_GAS"), err.Error())

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
Expand Down Expand Up @@ -191,6 +194,7 @@ func Test_ContractCreate_NoBytecodeFileID(t *testing.T) {

receipt, err = resp.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_FILE_ID"), err.Error())

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
Expand All @@ -203,7 +207,7 @@ func Test_ContractCreate_NoBytecodeFileID(t *testing.T) {
assert.NoError(t, err)
}

func Test_ContractCreateTransactionNoMax_Fee(t *testing.T) {
func Test_ContractCreateTransaction_NoMaxFee(t *testing.T) {
// Note: this is the bytecode for the contract found in the example for ./examples/create_simple_contract
testContractByteCode := []byte(`608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506101cb806100606000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806341c0e1b51461004b578063cfae321714610062575b600080fd5b34801561005757600080fd5b506100606100f2565b005b34801561006e57600080fd5b50610077610162565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b757808201518184015260208101905061009c565b50505050905090810190601f1680156100e45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610160573373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60606040805190810160405280600d81526020017f48656c6c6f2c20776f726c64210000000000000000000000000000000000000081525090509056fea165627a7a72305820ae96fb3af7cde9c0abfe365272441894ab717f816f07f41f07b1cbede54e256e0029`)

Expand All @@ -223,16 +227,18 @@ func Test_ContractCreateTransactionNoMax_Fee(t *testing.T) {
fileID := *receipt.FileID
assert.NotNil(t, fileID)

resp, err = NewContractCreateTransaction().
resp2, err := NewContractCreateTransaction().
SetAdminKey(client.GetOperatorPublicKey()).
SetNodeAccountIDs([]AccountID{resp.NodeID}).
SetGas(2000).
SetBytecodeFileID(fileID).
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INSUFFICIENT_TX_FEE received for transaction %s", resp2.TransactionID), err.Error())

receipt, err = resp.GetReceipt(client)
_, err = resp2.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("Invalid node AccountID was set for transaction: %s", resp2.NodeID), err.Error())

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
Expand Down
7 changes: 5 additions & 2 deletions contract_delete_transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"github.com/stretchr/testify/assert"

"testing"
Expand Down Expand Up @@ -109,13 +110,15 @@ func Test_ContractDelete_NoContractID(t *testing.T) {
receipt, err = resp.GetReceipt(client)
assert.NoError(t, err)

resp, err = NewContractDeleteTransaction().
resp2, err := NewContractDeleteTransaction().
SetNodeAccountIDs([]AccountID{resp.NodeID}).
Execute(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("exceptional precheck status INVALID_CONTRACT_ID received for transaction %s", resp2.TransactionID), err.Error())

_, err = resp.GetReceipt(client)
_, err = resp2.GetReceipt(client)
assert.Error(t, err)
assert.Equal(t, fmt.Sprintf("Invalid node AccountID was set for transaction: %s", resp2.NodeID), err.Error())

resp, err = NewFileDeleteTransaction().
SetFileID(fileID).
Expand Down
Loading

0 comments on commit 4ee8527

Please sign in to comment.