Skip to content

Commit 7da734c

Browse files
committed
chore: cleaner unit test
Signed-off-by: exploreriii <[email protected]>
1 parent 7572375 commit 7da734c

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed
Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import time
21
import pytest
32
from unittest.mock import MagicMock
43

54
from hiero_sdk_python.hapi.services import (
65
response_header_pb2,
76
response_pb2,
8-
timestamp_pb2,
97
transaction_get_receipt_pb2,
108
)
119
from hiero_sdk_python.hapi.services.transaction_receipt_pb2 import TransactionReceipt as TransactionReceiptProto
@@ -23,8 +21,8 @@
2321
@pytest.fixture
2422
def built_pause_tx(mock_account_ids, mock_client, generate_transaction_id):
2523
"""
26-
Factory fixture: given a pause_key, returns a TokenPauseTransaction
27-
that’s already set up, frozen, and signed with that key.
24+
Factory: returns a TokenPauseTransaction (set, frozen, signed) given a pause_key.
25+
Usage: tx = built_pause_tx(pause_key)
2826
"""
2927
sender, _, node_account, fungible_token, _ = mock_account_ids
3028

@@ -47,31 +45,35 @@ def test_builds_token_pause_body_with_correct_ids(mock_account_ids, generate_tra
4745
"""
4846
sender, _, node_account, token_id, _ = mock_account_ids
4947

50-
pause_tx = TokenPauseTransaction().set_token_id(token_id)
51-
pause_tx.transaction_id = generate_transaction_id(sender)
52-
pause_tx.node_account_id = node_account
48+
tx = TokenPauseTransaction().set_token_id(token_id)
49+
tx.transaction_id = generate_transaction_id(sender)
50+
tx.node_account_id = node_account
5351

54-
body = pause_tx.build_transaction_body()
52+
body = tx.build_transaction_body()
5553

5654
assert body.tokenPause.token == token_id.to_proto()
57-
assert body.transactionID == pause_tx.transaction_id.to_proto()
58-
assert body.nodeAccountID == pause_tx.node_account_id.to_proto()
55+
assert body.transactionID == tx.transaction_id.to_proto()
56+
assert body.nodeAccountID == tx.node_account_id.to_proto()
57+
5958

6059
@pytest.mark.parametrize("bad_token", [None, TokenId(0, 0, 0)])
6160
def test_build_transaction_body_without_valid_token_id_raises(bad_token):
6261
"""
63-
If token_id is missing or zero, build_transaction_body() must ValueError.
62+
If token_id is missing or zero, build_transaction_body() must raise ValueError.
6463
"""
65-
pause_tx = TokenPauseTransaction()
64+
tx = TokenPauseTransaction()
6665
if bad_token is not None:
67-
pause_tx.token_id = bad_token
66+
tx.token_id = bad_token
6867

6968
with pytest.raises(ValueError, match="token_id must be set"):
70-
pause_tx.build_transaction_body()
69+
tx.build_transaction_body()
70+
7171

7272
def test__get_method_points_to_pauseToken(mock_channel):
7373
"""
74-
_get_method() should route to the gRPC stub method for pausing tokens.
74+
_get_method() should return:
75+
transaction_func = mock_channel.token.pauseToken
76+
query_func = None
7577
"""
7678
tx = TokenPauseTransaction().set_token_id(TokenId(1, 2, 3))
7779
method = tx._get_method(mock_channel)
@@ -81,45 +83,43 @@ def test__get_method_points_to_pauseToken(mock_channel):
8183

8284
def test__from_proto_restores_token_id():
8385
"""
84-
_from_proto() should deserialize a TokenPauseTransactionBody back into .token_id.
86+
_from_proto() must deserialize TokenPauseTransactionBody .token_id correctly.
8587
"""
8688
proto_body = TokenPauseTransaction._get_transaction_body_class()(
8789
token=TokenId(7, 8, 9).to_proto()
8890
)
89-
pause_tx = TokenPauseTransaction()._from_proto(proto_body)
91+
tx = TokenPauseTransaction()._from_proto(proto_body)
92+
93+
assert tx.token_id == TokenId(7, 8, 9)
9094

91-
assert pause_tx.token_id == TokenId(7, 8, 9)
9295

9396
def test_signed_bytes_include_token_pause_transaction(built_pause_tx):
9497
"""
95-
After freeze() and sign(pause_key), to_proto() must embed a non-empty
96-
signedTransactionBytes blob.
98+
After freeze() and sign(pause_key), to_proto() must produce non-empty
99+
signedTransactionBytes.
97100
"""
98-
# Arrange: stub pause key + public key
99101
pause_key = MagicMock()
100102
pause_key.sign.return_value = b'__FAKE_SIG__'
101-
102103
fake_pub = pause_key.public_key()
103104
fake_pub.to_bytes_raw.return_value = b'__FAKE_PUB__'
104105

105-
# Act
106-
pause_tx = built_pause_tx(pause_key)
107-
proto = pause_tx.to_proto()
106+
tx = built_pause_tx(pause_key)
107+
proto = tx.to_proto()
108108

109-
# Assert
110109
assert proto.signedTransactionBytes
111110
assert len(proto.signedTransactionBytes) > 0
112111

113-
def test_pause_transaction_can_execute(mock_account_ids):
112+
113+
def test_pause_transaction_can_execute(mock_account_ids, generate_transaction_id):
114114
"""
115-
A properly built & signed TokenPauseTransaction against a mock server
115+
A well-formed & signed TokenPauseTransaction against the mock server
116116
should return a SUCCESS receipt.
117117
"""
118118
sender, _, node_account, fungible_token, _ = mock_account_ids
119119

120-
# 1) Precheck-ok response
120+
# 1) Precheck OK
121121
ok_resp = TransactionResponseProto(nodeTransactionPrecheckCode=ResponseCode.OK)
122-
# 2) Receipt SUCCESS response
122+
# 2) Receipt SUCCESS
123123
success_receipt = TransactionReceiptProto(status=ResponseCode.SUCCESS)
124124
receipt_query = response_pb2.Response(
125125
transactionGetReceipt=transaction_get_receipt_pb2.TransactionGetReceiptResponse(
@@ -133,20 +133,17 @@ def test_pause_transaction_can_execute(mock_account_ids):
133133
response_sequences = [
134134
[ok_resp, receipt_query],
135135
]
136-
with mock_hedera_servers(response_sequences) as client:
137-
pause_tx = TokenPauseTransaction().set_token_id(fungible_token)
138-
pause_tx.transaction_id = TransactionId.generate(sender)
139-
pause_tx.node_account_id = node_account
140-
141-
pause_tx.freeze_with(client)
142-
143-
dummy_priv_key = MagicMock()
144-
dummy_priv_key.sign.return_value = b'__SIG__'
145-
146-
dummy_pub_key = dummy_priv_key.public_key()
147-
dummy_pub_key.to_bytes_raw.return_value = b'PUB'
136+
with mock_hedera_servers(response_sequences) as client:
137+
tx = TokenPauseTransaction().set_token_id(fungible_token)
138+
tx.transaction_id = generate_transaction_id(sender)
139+
tx.node_account_id = node_account
148140

149-
pause_tx.sign(dummy_priv_key)
141+
tx.freeze_with(client)
142+
fake_priv = MagicMock()
143+
fake_priv.sign.return_value = b'__SIG__'
144+
fake_pub = fake_priv.public_key()
145+
fake_pub.to_bytes_raw.return_value = b'PUB'
146+
tx.sign(fake_priv)
150147

151-
receipt = pause_tx.execute(client)
152-
assert receipt.status == ResponseCode.SUCCESS
148+
receipt = tx.execute(client)
149+
assert receipt.status == ResponseCode.SUCCESS

0 commit comments

Comments
 (0)