Skip to content

Commit 3694682

Browse files
committed
chore: added test for the changes
Signed-off-by: Manish Dait <daitmanish88@gmail.com>
1 parent 9c2eb67 commit 3694682

6 files changed

Lines changed: 120 additions & 40 deletions

File tree

src/hiero_sdk_python/executable.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,23 @@ def __init__(self):
9191

9292
self._used_node_account_id: AccountId | None = None
9393
self._node_account_ids_index: int = 0
94-
94+
9595
@property
9696
def node_account_id(self) -> AccountId | None:
9797
warnings.warn(
98-
"'node_account_id' is deprecated; use 'node_account_ids' instead.",
99-
DeprecationWarning,
100-
stacklevel=2
98+
"'node_account_id' is deprecated; use 'node_account_ids' instead.", DeprecationWarning, stacklevel=2
10199
)
102100

103-
return self._node_account_id
101+
return self.node_account_ids[0] if len(self.node_account_ids) > 0 else None
104102

105103
@node_account_id.setter
106104
def node_account_id(self, account_id: AccountId) -> None:
107105
warnings.warn(
108-
"'node_account_id' is deprecated; use 'node_account_ids' instead.",
109-
DeprecationWarning,
110-
stacklevel=2
106+
"'node_account_id' is deprecated; use 'node_account_ids' instead.", DeprecationWarning, stacklevel=2
111107
)
112108

113109
self.node_account_ids = [account_id] if account_id is not None else []
114110

115-
116111
def set_node_account_ids(self, node_account_ids: list[AccountId]):
117112
"""
118113
Explicitly set the node account IDs to execute against.
@@ -139,7 +134,7 @@ def set_node_account_id(self, node_account_id: AccountId):
139134
warnings.warn(
140135
"Method 'set_node_account_id()' is deprecated; use 'set_node_account_ids()' instead.",
141136
DeprecationWarning,
142-
stacklevel=2
137+
stacklevel=2,
143138
)
144139

145140
return self.set_node_account_ids([node_account_id])
@@ -374,9 +369,7 @@ def _resolve_execution_config(self, client: Client, timeout: int | float | None)
374369
setattr(self, attr, default)
375370

376371
if not self.node_account_ids:
377-
self.node_account_ids = [
378-
node._account_id for node in client.network.nodes
379-
]
372+
self.node_account_ids = [node._account_id for node in client.network.nodes]
380373

381374
if not self.node_account_ids:
382375
raise RuntimeError("No healthy nodes available for execution")

src/hiero_sdk_python/query/query.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ def _make_request_header(self) -> query_header_pb2.QueryHeader:
179179
header.responseType = query_header_pb2.ResponseType.COST_ANSWER
180180
return header
181181

182-
if self.operator is not None and self.node_account_ids[self._node_account_ids_index] is not None and self.payment_amount is not None:
182+
if (
183+
self.operator is not None
184+
and self.node_account_ids[self._node_account_ids_index] is not None
185+
and self.payment_amount is not None
186+
):
183187
payment_tx = self._build_query_payment_transaction(
184188
payer_account_id=self.operator.account_id,
185189
payer_private_key=self.operator.private_key,

tests/unit/token_airdrop_transaction_test.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,6 @@ def test_add_zero_transfer_amount(mock_account_ids):
161161
airdrop_tx.add_approved_token_transfer_with_decimals(token_id, account_id, 0, 1)
162162

163163

164-
# TODO: Flaing wrong
165-
def test_add_unbalanced_transfer_amount(mock_account_ids):
166-
sender, receiver, _, token_id, _ = mock_account_ids
167-
airdrop_tx = TokenAirdropTransaction()
168-
airdrop_tx.add_token_transfer(token_id, sender, -1)
169-
airdrop_tx.add_token_transfer(token_id, receiver, -2)
170-
171-
with pytest.raises(ValueError):
172-
airdrop_tx.freeze()
173-
174-
175-
# TODO: Flaing wrong
176-
def test_add_invalid_transfer(mock_account_ids):
177-
_, _, _, _, _ = mock_account_ids
178-
airdrop_tx = TokenAirdropTransaction()
179-
180-
with pytest.raises(ValueError):
181-
airdrop_tx.freeze()
182-
183-
184164
def test_sign_transaction(mock_account_ids, mock_client):
185165
"""Test signing the token airdrop transaction with a private key."""
186166
sender, receiver, _, token_id_1, token_id_2 = mock_account_ids

tests/unit/token_pause_transaction_test.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ def test_build_transaction_body_nft(mock_account_ids, nft_id):
5454
assert transaction_body.token_pause.token == base_token_id._to_proto()
5555

5656

57-
# TODO: Test that freeze set the transactionId and the nodeId to transaction_proto_body
58-
59-
6057
# This test uses fixture (token_id, mock_client) as parameter
6158
def test__to_proto(token_id, mock_client):
6259
"""Test converting the token pause transaction to protobuf format after signing."""

tests/unit/topic_create_transaction_test.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ def test_build_scheduled_body(mock_account_ids, custom_fixed_fee, key_type, use_
220220
)
221221

222222

223-
# TODO: Test for the freeze set the node/transaction id
224-
225-
226223
# This test uses fixtures (mock_account_ids, private_key) as parameters
227224
def test_sign_topic_create_transaction(mock_account_ids, private_key):
228225
"""

tests/unit/transaction_test.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from hiero_sdk_python.account.account_id import AccountId
77
from hiero_sdk_python.consensus.topic_message_submit_transaction import TopicMessageSubmitTransaction
88
from hiero_sdk_python.crypto.private_key import PrivateKey
9+
from hiero_sdk_python.Duration import Duration
910
from hiero_sdk_python.exceptions import ReceiptStatusError
1011
from hiero_sdk_python.file.file_append_transaction import FileAppendTransaction
1112
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction
@@ -27,6 +28,7 @@
2728
from hiero_sdk_python.transaction.transaction_id import TransactionId
2829
from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt
2930
from hiero_sdk_python.transaction.transaction_response import TransactionResponse
31+
from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction
3032
from tests.unit.mock_server import mock_hedera_servers
3133

3234

@@ -553,3 +555,110 @@ def test_transaction_default_max_fee(account_id):
553555

554556
assert tx_body is not None
555557
assert tx_body.transactionFee == Hbar(2).to_tinybars()
558+
559+
560+
def test_build_transaction_body():
561+
"""Test that build_transaction_body create transaction body with basic fields."""
562+
tx = TransferTransaction()
563+
transaction_body = tx.build_base_transaction_body()
564+
565+
assert transaction_body is not None
566+
assert transaction_body.high_volume == False
567+
assert transaction_body.transactionValidDuration == Duration(120)._to_proto()
568+
# TODO: Should be change to 2hbar in future.
569+
# Since the deault fee for AbstractTokenTransfer can be remove, a default_fee is now updated to 2-hbar
570+
assert transaction_body.transactionFee == Hbar(1).to_tinybars()
571+
572+
assert not transaction_body.HasField("transactionID")
573+
assert not transaction_body.HasField("nodeAccountID")
574+
575+
576+
def test_set_node_account_id_updates_node_account_ids():
577+
"""Test that setting a single node account ID updates node_account_ids."""
578+
tx = TransferTransaction().set_transaction_id(TransactionId.generate(AccountId(0, 0, 1)))
579+
580+
account_node_id1 = AccountId(0, 0, 3)
581+
tx.set_node_account_id(account_node_id1)
582+
583+
assert tx.node_account_ids == [account_node_id1]
584+
assert tx.node_account_id == account_node_id1 # Deprecated
585+
586+
# Test backward compatibility
587+
node_account_id2 = AccountId(0, 0, 4)
588+
tx.node_account_id = node_account_id2
589+
590+
assert tx.node_account_ids == [node_account_id2]
591+
assert tx.node_account_id == node_account_id2 # Deprecated
592+
593+
594+
def test_set_node_account_ids_updates_node_account_ids():
595+
"""Test that setting a node account ID list updates node_account_ids."""
596+
tx = TransferTransaction().set_transaction_id(TransactionId.generate(AccountId(0, 0, 1)))
597+
598+
node_account_ids1 = [AccountId(0, 0, 3), AccountId(0, 0, 4)]
599+
tx.set_node_account_ids(node_account_ids1)
600+
601+
assert tx.node_account_ids == node_account_ids1
602+
assert tx.node_account_id == node_account_ids1[0] # Deprecated, will return 1st element of list
603+
604+
# Test property setter
605+
node_account_ids2 = [AccountId(0, 0, 5), AccountId(0, 0, 6)]
606+
tx.node_account_ids = node_account_ids2
607+
608+
assert tx.node_account_ids == node_account_ids2
609+
assert tx.node_account_id == node_account_ids2[0] # Deprecated, will return 1st element of list
610+
611+
612+
def test_freeze_transaction_sets_transaction_id_and_node_ids():
613+
"""Test that freeze() preserves the transaction ID and builds body bytes for all node IDs."""
614+
tx_id = TransactionId.generate(AccountId(0, 0, 1))
615+
node_account_ids = [AccountId(0, 0, 3), AccountId(0, 0, 4)]
616+
617+
tx = TransferTransaction().set_transaction_id(tx_id).set_node_account_ids(node_account_ids).freeze()
618+
619+
assert tx.transaction_id == tx_id
620+
assert tx.node_account_ids == node_account_ids
621+
622+
assert len(tx._transaction_body_bytes) == len(node_account_ids)
623+
assert set(tx._transaction_body_bytes.keys()) == set(node_account_ids)
624+
625+
for node_id in node_account_ids:
626+
body_bytes = tx._transaction_body_bytes[node_id]
627+
assert body_bytes is not None
628+
assert len(body_bytes) > 0
629+
630+
631+
def test_freeze_with_sets_transaction_id_and_node_ids_from_client(mock_client):
632+
"""Test that freeze_with() populates the transaction ID and node IDs from the client."""
633+
tx = TransferTransaction().freeze_with(mock_client)
634+
expected_node_ids = [node._account_id for node in mock_client.network.nodes]
635+
636+
assert tx.transaction_id is not None
637+
assert tx.node_account_ids == expected_node_ids
638+
639+
assert len(tx._transaction_body_bytes) == len(mock_client.network.nodes)
640+
assert set(tx._transaction_body_bytes.keys()) == set(expected_node_ids)
641+
642+
for node_id in expected_node_ids:
643+
body_bytes = tx._transaction_body_bytes[node_id]
644+
assert body_bytes is not None
645+
assert len(body_bytes) > 0
646+
647+
648+
# Deprecated, Only to test backward compatibility.
649+
def test_freeze_transaction_sets_transaction_id_and_node_id():
650+
"""Test that freeze() with a single node account ID builds the transaction body."""
651+
tx_id = TransactionId.generate(AccountId(0, 0, 1))
652+
node_account_id = AccountId(0, 0, 3)
653+
654+
tx = TransferTransaction().set_transaction_id(tx_id).set_node_account_id(node_account_id).freeze()
655+
656+
assert tx.transaction_id == tx_id
657+
assert tx.node_account_id == node_account_id
658+
659+
assert len(tx._transaction_body_bytes) == 1
660+
assert set(tx._transaction_body_bytes.keys()) == {node_account_id}
661+
662+
body_bytes = tx._transaction_body_bytes[node_account_id]
663+
assert body_bytes is not None
664+
assert len(body_bytes) > 0

0 commit comments

Comments
 (0)