Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create additional from/to bytes tests - part 3 #1222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sdk/live_hash_delete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
protobuf "google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -122,3 +123,15 @@ func TestUnitLiveHashDeleteTransactionMock(t *testing.T) {
_, err = freez.Sign(newKey).Execute(client)
require.NoError(t, err)
}

func TestUnitLiveHashDeleteTransactionFromToBytes(t *testing.T) {
tx := NewLiveHashDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(LiveHashDeleteTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/node_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,15 @@ func TestUnitNodeCreateTransactionCoverage(t *testing.T) {
b.AddSignature(key.PublicKey(), sig)
}
}

func TestUnitNodeCreateTransactionFromToBytes(t *testing.T) {
tx := NewNodeCreateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(NodeCreateTransaction).buildProtoBody())
}
13 changes: 13 additions & 0 deletions sdk/node_delete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

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

Expand Down Expand Up @@ -233,3 +234,15 @@ func TestUnitNodeDeleteTransactionCoverage(t *testing.T) {
b.AddSignature(key.PublicKey(), sig)
}
}

func TestUnitNodeDeleteTransactionFromToBytes(t *testing.T) {
tx := NewNodeDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(NodeDeleteTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/node_update_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,15 @@ func TestUnitNodeUpdateTransactionCoverage(t *testing.T) {
b.AddSignature(key.PublicKey(), sig)
}
}

func TestUnitNodeUpdateTransactionFromToBytes(t *testing.T) {
tx := NewNodeUpdateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(NodeUpdateTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/prng_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ func TestUnitPrngTransactionScheduleProtobuf(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected.GetUtilPrng(), actual.GetUtilPrng())
}

func TestUnitPrngTransactionFromToBytes(t *testing.T) {
tx := NewPrngTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(PrngTransaction).buildProtoBody())
}
33 changes: 24 additions & 9 deletions sdk/schedule_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ScheduleCreateTransaction struct {
payerAccountID *AccountID
adminKey Key
schedulableBody *services.SchedulableTransactionBody
memo string
memo *string
expirationTime *time.Time
waitForExpiry bool
}
Expand All @@ -39,18 +39,27 @@ func NewScheduleCreateTransaction() *ScheduleCreateTransaction {
}

func _ScheduleCreateTransactionFromProtobuf(tx Transaction[*ScheduleCreateTransaction], pb *services.TransactionBody) ScheduleCreateTransaction {
key, _ := _KeyFromProtobuf(pb.GetScheduleCreate().GetAdminKey())
var expirationTime time.Time
var key Key
if pb.GetScheduleCreate().GetAdminKey() != nil {
key, _ = _KeyFromProtobuf(pb.GetScheduleCreate().GetAdminKey())
}
var expirationTime *time.Time
if pb.GetScheduleCreate().GetExpirationTime() != nil {
expirationTime = _TimeFromProtobuf(pb.GetScheduleCreate().GetExpirationTime())
expirationTimeVal := _TimeFromProtobuf(pb.GetScheduleCreate().GetExpirationTime())
expirationTime = &expirationTimeVal
}
var memo *string
if pb.GetScheduleCreate().GetMemo() != "" {
memoVal := pb.GetScheduleCreate().GetMemo()
memo = &memoVal
}

scheduleCreateTransaction := ScheduleCreateTransaction{
payerAccountID: _AccountIDFromProtobuf(pb.GetScheduleCreate().GetPayerAccountID()),
adminKey: key,
schedulableBody: pb.GetScheduleCreate().GetScheduledTransactionBody(),
memo: pb.GetScheduleCreate().GetMemo(),
expirationTime: &expirationTime,
memo: memo,
expirationTime: expirationTime,
waitForExpiry: pb.GetScheduleCreate().WaitForExpiry,
}

Expand Down Expand Up @@ -141,14 +150,17 @@ func (tx *ScheduleCreateTransaction) GetAdminKey() *Key {
// SetScheduleMemo Sets an optional memo with a UTF-8 encoding of no more than 100 bytes which does not contain the zero byte.
func (tx *ScheduleCreateTransaction) SetScheduleMemo(memo string) *ScheduleCreateTransaction {
tx._RequireNotFrozen()
tx.memo = memo
tx.memo = &memo

return tx
}

// GetScheduleMemo returns the optional memo with a UTF-8 encoding of no more than 100 bytes which does not contain the zero byte.
func (tx *ScheduleCreateTransaction) GetScheduleMemo() string {
return tx.memo
if tx.memo == nil {
return ""
}
return *tx.memo
}

// SetScheduledTransaction Sets the scheduled transaction
Expand Down Expand Up @@ -202,10 +214,13 @@ func (tx ScheduleCreateTransaction) buildScheduled() (*services.SchedulableTrans

func (tx ScheduleCreateTransaction) buildProtoBody() *services.ScheduleCreateTransactionBody {
body := &services.ScheduleCreateTransactionBody{
Memo: tx.memo,
WaitForExpiry: tx.waitForExpiry,
}

if tx.memo != nil {
body.Memo = *tx.memo
}

if tx.payerAccountID != nil {
body.PayerAccountID = tx.payerAccountID._ToProtobuf()
}
Expand Down
24 changes: 24 additions & 0 deletions sdk/schedule_create_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,27 @@ func TestUnitScheduleDeleteTransactionMock(t *testing.T) {
_, err = freez.Sign(newKey).Execute(client)
require.NoError(t, err)
}

func TestUnitScheduleCreateTransactionFromToBytes(t *testing.T) {
tx := NewScheduleCreateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ScheduleCreateTransaction).buildProtoBody())
}

func TestUnitScheduleDeleteTransactionFromToBytes(t *testing.T) {
tx := NewScheduleDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ScheduleDeleteTransaction).buildProtoBody())
}
13 changes: 13 additions & 0 deletions sdk/schedule_sign_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
protobuf "google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -126,3 +127,15 @@ func TestUnitScheduleSignTransactionMock(t *testing.T) {
_, err = transaction.Execute(client)
require.NoError(t, err)
}

func TestUnitScheduleSignTransactionFromToBytes(t *testing.T) {
tx := NewScheduleSignTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(ScheduleSignTransaction).buildProtoBody())
}
12 changes: 6 additions & 6 deletions sdk/system_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
}

func _SystemDeleteTransactionFromProtobuf(tx Transaction[*SystemDeleteTransaction], pb *services.TransactionBody) SystemDeleteTransaction {
expiration := time.Date(
time.Now().Year(), time.Now().Month(), time.Now().Day(),
time.Now().Hour(), time.Now().Minute(),
int(pb.GetSystemDelete().ExpirationTime.Seconds), time.Now().Nanosecond(), time.Now().Location(),
)
var expiration *time.Time
if pb.GetCryptoUpdateAccount().GetExpirationTime() != nil {
expirationVal := _TimeFromProtobuf(pb.GetCryptoUpdateAccount().GetExpirationTime())
expiration = &expirationVal
}

Check warning on line 40 in sdk/system_delete_transaction.go

View check run for this annotation

Codecov / codecov/patch

sdk/system_delete_transaction.go#L38-L40

Added lines #L38 - L40 were not covered by tests
systemDeleteTransaction := SystemDeleteTransaction{
contractID: _ContractIDFromProtobuf(pb.GetSystemDelete().GetContractID()),
fileID: _FileIDFromProtobuf(pb.GetSystemDelete().GetFileID()),
expirationTime: &expiration,
expirationTime: expiration,
}

tx.childTransaction = &systemDeleteTransaction
Expand Down
12 changes: 12 additions & 0 deletions sdk/system_delete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -113,3 +114,14 @@ func _SetupSystemDeleteTrx() *SystemDeleteTransaction {
SetTransactionValidDuration(testTrxValidDuration).
SetTransactionMemo("memo")
}
func TestUnitSystemDeleteTransactionFromToBytes(t *testing.T) {
tx := NewSystemDeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(SystemDeleteTransaction).buildProtoBody())
}
13 changes: 13 additions & 0 deletions sdk/system_undelete_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/hiero-ledger/hiero-sdk-go/v2/proto/services"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -108,3 +109,15 @@ func _SetupSystemUndeleteTrx() *SystemUndeleteTransaction {
SetGrpcDeadline(&testTrxValidDuration).
SetNodeAccountIDs([]AccountID{testAccountID})
}

func TestUnitSystemUndeleteTransactionFromToBytes(t *testing.T) {
tx := NewSystemUndeleteTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(SystemUndeleteTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/token_airdrop_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,15 @@ func TestUnitTokenAirdropTransactionScheduleProtobuf(t *testing.T) {
require.NoError(t, err)
require.ElementsMatch(t, expected.GetTokenAirdrop().TokenTransfers, actual.GetTokenAirdrop().TokenTransfers)
}

func TestUnitTokenAirdropTransactionFromToBytes(t *testing.T) {
tx := NewTokenAirdropTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(TokenAirdropTransaction).buildProtoBody())
}
11 changes: 11 additions & 0 deletions sdk/token_associate_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,14 @@ func TestUnitTokenAssociateTransactionCoverage(t *testing.T) {
b.AddSignature(newKey.PublicKey(), sig)
}
}
func TestUnitTokenAssociateTransactionFromToBytes(t *testing.T) {
tx := NewTokenAssociateTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(TokenAssociateTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/token_burn_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,15 @@ func TestUnitTokenBurnTransactionMock(t *testing.T) {
_, err = freez.Sign(newKey).Execute(client)
require.NoError(t, err)
}

func TestUnitTokenBurnTransactionFromToBytes(t *testing.T) {
tx := NewTokenBurnTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(TokenBurnTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/token_cancel_airdrop_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ func TestUnitTokenCancelAirdropTransactionValidateNetworkOnIDs(t *testing.T) {
err = transaction.validateNetworkOnIDs(client)
require.NoError(t, err)
}

func TestUnitTokenCancelAirdropTransactionFromToBytes(t *testing.T) {
tx := NewTokenCancelAirdropTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(TokenCancelAirdropTransaction).buildProtoBody())
}
12 changes: 12 additions & 0 deletions sdk/token_claim_airdrop_transaction_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ func TestUnitTokenClaimAirdropTransactionValidateNetworkOnIDs(t *testing.T) {
err = transaction.validateNetworkOnIDs(client)
require.NoError(t, err)
}

func TestUnitTokenClaimAirdropTransactionFromToBytes(t *testing.T) {
tx := NewTokenClaimAirdropTransaction()

txBytes, err := tx.ToBytes()
require.NoError(t, err)

txFromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)

assert.Equal(t, tx.buildProtoBody(), txFromBytes.(TokenClaimAirdropTransaction).buildProtoBody())
}
Loading
Loading