github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_decimal_map_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 TestUnitTokenDecimalMapGet(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	tokenDecimals := TokenDecimalMap{
    37  		decimals: map[string]uint64{
    38  			"0.0.123": 9,
    39  			"0.0.124": 10,
    40  		},
    41  	}
    42  
    43  	assert.Equal(t, uint64(9), tokenDecimals.Get(TokenID{Shard: 0, Realm: 0, Token: 123}))
    44  	assert.Equal(t, uint64(10), tokenDecimals.Get(TokenID{Shard: 0, Realm: 0, Token: 124}))
    45  }
    46  
    47  func TestUnitTokenDecimalMapToProtobuf(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	tokenDecimals := TokenDecimalMap{
    51  		decimals: map[string]uint64{
    52  			"0.0.123": 9,
    53  			"0.0.124": 10,
    54  		},
    55  	}
    56  
    57  	decimals := tokenDecimals._ToProtobuf()
    58  
    59  	assert.Equal(t, 2, len(decimals))
    60  
    61  	// The order of the decimals is not guaranteed
    62  	for _, dec := range decimals {
    63  		switch dec.TokenId.TokenNum {
    64  		case 123:
    65  			assert.Equal(t, uint32(9), dec.Decimals)
    66  		case 124:
    67  			assert.Equal(t, uint32(10), dec.Decimals)
    68  		default:
    69  			t.Errorf("Unexpected TokenID: %v", dec.TokenId.String())
    70  		}
    71  	}
    72  }
    73  
    74  func TestUnitTokenDecimalMapFromProtobuf(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	decimals := make([]*services.TokenBalance, 0)
    78  	decimals = append(decimals, &services.TokenBalance{
    79  		TokenId:  &services.TokenID{ShardNum: 0, RealmNum: 0, TokenNum: 123},
    80  		Decimals: uint32(9),
    81  	})
    82  	decimals = append(decimals, &services.TokenBalance{
    83  		TokenId:  &services.TokenID{ShardNum: 0, RealmNum: 0, TokenNum: 124},
    84  		Decimals: uint32(10),
    85  	})
    86  
    87  	tokenDecimals := _TokenDecimalMapFromProtobuf(decimals)
    88  
    89  	assert.Equal(t, uint64(9), tokenDecimals.Get(TokenID{Shard: 0, Realm: 0, Token: 123}))
    90  	assert.Equal(t, uint64(10), tokenDecimals.Get(TokenID{Shard: 0, Realm: 0, Token: 124}))
    91  }
    92  
    93  func TestUnitTokenDecimalMapFromProtobufEmpty(t *testing.T) {
    94  	t.Parallel()
    95  
    96  	tokenDecimals := TokenDecimalMap{
    97  		decimals: map[string]uint64{
    98  			"0.123":   9, // invalid token
    99  			"0.0.124": 10,
   100  		},
   101  	}
   102  	pb := tokenDecimals._ToProtobuf()
   103  	assert.Equal(t, []*services.TokenBalance{}, pb)
   104  }