github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_association_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  
    29  	"github.com/hashgraph/hedera-protobufs-go/services"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestTokenAssociationFromProtobuf(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	var pbAssociation *services.TokenAssociation
    37  	var association TokenAssociation
    38  
    39  	association = tokenAssociationFromProtobuf(pbAssociation)
    40  	assert.Equal(t, TokenAssociation{}, association)
    41  
    42  	pbAssociation = &services.TokenAssociation{}
    43  	association = tokenAssociationFromProtobuf(pbAssociation)
    44  	assert.Equal(t, TokenAssociation{}, association)
    45  
    46  	pbAssociation = &services.TokenAssociation{
    47  		TokenId:   &services.TokenID{ShardNum: 0, RealmNum: 0, TokenNum: 3},
    48  		AccountId: &services.AccountID{ShardNum: 0, RealmNum: 0, Account: &services.AccountID_AccountNum{AccountNum: 6}},
    49  	}
    50  
    51  	association = tokenAssociationFromProtobuf(pbAssociation)
    52  	assert.Equal(t, TokenAssociation{
    53  		TokenID:   &TokenID{Shard: 0, Realm: 0, Token: 3},
    54  		AccountID: &AccountID{Shard: 0, Realm: 0, Account: 6},
    55  	}, association)
    56  }
    57  
    58  func TestTokenAssociationToProtobuf(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	var association TokenAssociation
    62  	var pbAssociation *services.TokenAssociation
    63  
    64  	association = TokenAssociation{}
    65  	pbAssociation = association.toProtobuf()
    66  	assert.Equal(t, &services.TokenAssociation{}, pbAssociation)
    67  
    68  	association = TokenAssociation{
    69  		TokenID:   &TokenID{Shard: 0, Realm: 0, Token: 3},
    70  		AccountID: &AccountID{Shard: 0, Realm: 0, Account: 6},
    71  	}
    72  	pbAssociation = association.toProtobuf()
    73  	assert.Equal(t, &services.TokenAssociation{
    74  		TokenId:   &services.TokenID{ShardNum: 0, RealmNum: 0, TokenNum: 3},
    75  		AccountId: &services.AccountID{ShardNum: 0, RealmNum: 0, Account: &services.AccountID_AccountNum{AccountNum: 6}},
    76  	}, pbAssociation)
    77  
    78  }
    79  
    80  func TestTokenAssociationToAndFromBytes(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	association := TokenAssociation{
    84  		TokenID:   &TokenID{Shard: 0, Realm: 0, Token: 3},
    85  		AccountID: &AccountID{Shard: 0, Realm: 0, Account: 6},
    86  	}
    87  
    88  	bytes := association.ToBytes()
    89  	fromBytes, err := TokenAssociationFromBytes(bytes)
    90  	assert.NoError(t, err)
    91  	assert.Equal(t, association, fromBytes)
    92  
    93  	association = TokenAssociation{}
    94  	bytes = association.ToBytes()
    95  
    96  	// Test empty bytes
    97  	data, err := TokenAssociationFromBytes(bytes)
    98  	assert.Nil(t, err)
    99  	assert.Equal(t, TokenAssociation{}, data)
   100  
   101  	// Test invalid bytes
   102  	_, err = TokenAssociationFromBytes([]byte{0x00})
   103  	assert.Error(t, err)
   104  
   105  	// Test nil bytes
   106  	_, err = TokenAssociationFromBytes(nil)
   107  	assert.Error(t, err)
   108  }