github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_balance_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 TestUnitTokenBalanceMapGet(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	var tokenBalances TokenBalanceMap
    37  	tokenBalances.balances = make(map[string]uint64)
    38  	tokenBalances.balances["0.0.123"] = 100
    39  
    40  	assert.Equal(t, uint64(100), tokenBalances.Get(TokenID{Shard: 0, Realm: 0, Token: 123}))
    41  }
    42  
    43  func TestUnitTokenBalanceMapProtobuf(t *testing.T) {
    44  	t.Parallel()
    45  
    46  	var tokenBalances TokenBalanceMap
    47  	tokenBalances.balances = make(map[string]uint64)
    48  	tokenBalances.balances["0.0.123"] = 100
    49  
    50  	pb := tokenBalances._ToProtobuf()
    51  	tokenBalances2 := _TokenBalanceMapFromProtobuf(pb)
    52  
    53  	assert.Equal(t, tokenBalances.balances, tokenBalances2.balances)
    54  }
    55  
    56  func TestUnitTokenBalanceMapEmpty(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	var tokenBalances TokenBalanceMap
    60  	tokenBalances.balances = make(map[string]uint64)
    61  
    62  	// Breaks token, err := TokenIDFromString(s)
    63  	tokenBalances.balances["0.123"] = 100
    64  
    65  	pb := tokenBalances._ToProtobuf()
    66  
    67  	// test that we get an empty array back
    68  	assert.Equal(t, []*services.TokenBalance{}, pb)
    69  }