55import pytest
66
77from hiero_sdk_python .account .account_balance import AccountBalance
8+ from hiero_sdk_python .account .account_id import AccountId
9+ from hiero_sdk_python .hapi .services .basic_types_pb2 import TokenBalance
10+ from hiero_sdk_python .hapi .services .crypto_get_account_balance_pb2 import CryptoGetAccountBalanceResponse
811from hiero_sdk_python .hbar import Hbar
912from hiero_sdk_python .tokens .token_id import TokenId
1013
@@ -26,13 +29,14 @@ def test_account_balance_str_with_hbars_only():
2629 assert "Token Balances:" not in result
2730
2831
29- def test_account_balance_str_with_token_balances ():
30- """Test __str__ method with hbars and token balances."""
32+ def test_account_balance_str_with_token_balances_and_decimal ():
33+ """Test __str__ method with hbars and token balances and decimals ."""
3134 hbars = Hbar (10 )
3235 token_id_1 = TokenId (0 , 0 , 100 )
3336 token_id_2 = TokenId (0 , 0 , 200 )
3437 token_balances = {token_id_1 : 1000 , token_id_2 : 500 }
35- account_balance = AccountBalance (hbars = hbars , token_balances = token_balances )
38+ token_decimals = {token_id_1 : 1 , token_id_2 : 2 }
39+ account_balance = AccountBalance (hbars = hbars , token_balances = token_balances , token_decimals = token_decimals )
3640
3741 result = str (account_balance )
3842
@@ -42,10 +46,13 @@ def test_account_balance_str_with_token_balances():
4246 assert "Token Balances:" in result
4347 assert " - Token ID 0.0.100: 1000 units" in result
4448 assert " - Token ID 0.0.200: 500 units" in result
49+ assert "Token Decimals:" in result
50+ assert " - Token ID 0.0.100: 1 decimals" in result
51+ assert " - Token ID 0.0.200: 2 decimals" in result
4552
4653
47- def test_account_balance_str_with_empty_token_balances ():
48- """Test __str__ method with empty token balances dict."""
54+ def test_account_balance_str_with_empty_token_balances_and_decimals ():
55+ """Test __str__ method with empty token balances and decimals dict."""
4956 hbars = Hbar (5.5 )
5057 account_balance = AccountBalance (hbars = hbars , token_balances = {})
5158
@@ -56,6 +63,7 @@ def test_account_balance_str_with_empty_token_balances():
5663 assert " hbars" in result
5764 # Should not include token balances section when empty
5865 assert "Token Balances:" not in result
66+ assert "Token Decimals:" not in result
5967
6068
6169def test_account_balance_repr_with_hbars_only ():
@@ -77,7 +85,8 @@ def test_account_balance_repr_with_token_balances():
7785 token_id_1 = TokenId (0 , 0 , 100 )
7886 token_id_2 = TokenId (0 , 0 , 200 )
7987 token_balances = {token_id_1 : 1000 , token_id_2 : 500 }
80- account_balance = AccountBalance (hbars = hbars , token_balances = token_balances )
88+ token_decimals = {token_id_1 : 1 , token_id_2 : 2 }
89+ account_balance = AccountBalance (hbars = hbars , token_balances = token_balances , token_decimals = token_decimals )
8190
8291 result = repr (account_balance )
8392
@@ -87,3 +96,28 @@ def test_account_balance_repr_with_token_balances():
8796 assert "0.0.100" in result or "TokenId" in result
8897 assert "1000" in result
8998 assert "500" in result
99+ assert "token_decimals=" in result
100+ assert "1" in result
101+ assert "2" in result
102+
103+
104+ def test_create_account_balance_from_proto ():
105+ token_blances_proto = [
106+ TokenBalance (tokenId = TokenId (0 , 0 , 100 )._to_proto (), balance = 100 , decimals = 1 ),
107+ TokenBalance (tokenId = TokenId (0 , 0 , 102 )._to_proto (), balance = 0 , decimals = 0 ),
108+ ]
109+
110+ proto = CryptoGetAccountBalanceResponse (
111+ accountID = AccountId (0 , 0 , 1 )._to_proto (), balance = 10 , tokenBalances = token_blances_proto
112+ )
113+
114+ account_balance = AccountBalance ._from_proto (proto = proto )
115+
116+ assert account_balance is not None
117+ assert account_balance .hbars .to_tinybars () == 10
118+
119+ assert len (account_balance .token_balances ) == 2
120+ assert account_balance .token_balances == {TokenId (0 , 0 , 100 ): 100 , TokenId (0 , 0 , 102 ): 0 }
121+
122+ assert len (account_balance .token_decimals ) == 2
123+ assert account_balance .token_decimals == {TokenId (0 , 0 , 100 ): 1 , TokenId (0 , 0 , 102 ): 0 }
0 commit comments