Skip to content

Commit ec94f0b

Browse files
committed
chore: cleanup unit test
Signed-off-by: exploreriii <[email protected]>
1 parent 162a427 commit ec94f0b

File tree

2 files changed

+121
-103
lines changed

2 files changed

+121
-103
lines changed

tests/unit/conftest.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import time
23
from hiero_sdk_python.account.account_id import AccountId
34
from hiero_sdk_python.client.network import Network
45
from hiero_sdk_python.client.client import Client
@@ -9,6 +10,7 @@
910
from hiero_sdk_python.tokens.nft_id import NftId
1011
from hiero_sdk_python.tokens.token_id import TokenId
1112
from hiero_sdk_python.transaction.transaction_id import TransactionId
13+
from hiero_sdk_python.hapi.services import timestamp_pb2
1214

1315
@pytest.fixture
1416
def mock_account_ids():
@@ -20,6 +22,34 @@ def mock_account_ids():
2022
token_id_2 = TokenId(2, 2, 2)
2123
return account_id_sender, account_id_recipient, node_account_id, token_id_1, token_id_2
2224

25+
@pytest.fixture
26+
def mock_client():
27+
"""Fixture to provide a mock client with hardcoded nodes for testing purposes."""
28+
nodes = [_Node(AccountId(0, 0, 3), "node1.example.com:50211", None)]
29+
network = Network(nodes=nodes)
30+
client = Client(network)
31+
client.logger.set_level(LogLevel.DISABLED)
32+
33+
operator_key = PrivateKey.generate()
34+
operator_id = AccountId(0, 0, 1984)
35+
client.set_operator(operator_id, operator_key)
36+
37+
return client
38+
39+
@pytest.fixture
40+
def generate_transaction_id():
41+
"""
42+
Return a factory which, given an AccountId proto, returns a TransactionId
43+
stamped with the current time.
44+
"""
45+
def _make(account_id_proto):
46+
now = time.time()
47+
secs = int(now)
48+
nanos = int((now - secs) * 1e9)
49+
ts = timestamp_pb2.Timestamp(seconds=secs, nanos=nanos)
50+
return TransactionId(valid_start=ts, account_id=account_id_proto)
51+
return _make
52+
2353
@pytest.fixture
2454
def amount():
2555
"""Fixture to provide a default amount for fungible tokens."""
@@ -52,16 +82,3 @@ def nft_id():
5282
serial_number = 8
5383
return NftId(tokenId=token_id, serialNumber=serial_number)
5484

55-
@pytest.fixture
56-
def mock_client():
57-
"""Fixture to provide a mock client with hardcoded nodes for testing purposes."""
58-
nodes = [_Node(AccountId(0, 0, 3), "node1.example.com:50211", None)]
59-
network = Network(nodes=nodes)
60-
client = Client(network)
61-
client.logger.set_level(LogLevel.DISABLED)
62-
63-
operator_key = PrivateKey.generate()
64-
operator_id = AccountId(0, 0, 1984)
65-
client.set_operator(operator_id, operator_key)
66-
67-
return client
Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,135 @@
1+
import time
12
import pytest
23
from unittest.mock import MagicMock
34

5+
from hiero_sdk_python.hapi.services import (
6+
response_header_pb2,
7+
response_pb2,
8+
timestamp_pb2,
9+
transaction_get_receipt_pb2,
10+
)
411
from hiero_sdk_python.hapi.services.transaction_receipt_pb2 import TransactionReceipt as TransactionReceiptProto
512
from hiero_sdk_python.hapi.services.transaction_response_pb2 import TransactionResponse as TransactionResponseProto
13+
614
from hiero_sdk_python.response_code import ResponseCode
715
from hiero_sdk_python.tokens.token_pause_transaction import TokenPauseTransaction
8-
from hiero_sdk_python.hapi.services import response_header_pb2, response_pb2, timestamp_pb2, transaction_get_receipt_pb2
9-
from hiero_sdk_python.transaction.transaction_id import TransactionId
1016
from hiero_sdk_python.tokens.token_id import TokenId
17+
from hiero_sdk_python.transaction.transaction_id import TransactionId
1118

1219
from tests.unit.mock_server import mock_hedera_servers
1320

1421
pytestmark = pytest.mark.unit
1522

16-
def generate_transaction_id(account_id_proto):
17-
"""Generate a unique transaction ID based on the account ID and the current timestamp."""
18-
import time
19-
current_time = time.time()
20-
timestamp_seconds = int(current_time)
21-
timestamp_nanos = int((current_time - timestamp_seconds) * 1e9)
23+
@pytest.fixture
24+
def built_pause_tx(mock_account_ids, mock_client, generate_transaction_id):
25+
"""
26+
Factory fixture: given a pause_key, returns a TokenPauseTransaction
27+
that’s already set up, frozen, and signed with that key.
28+
"""
29+
sender, _, node_account, fungible_token, _ = mock_account_ids
2230

23-
tx_timestamp = timestamp_pb2.Timestamp(seconds=timestamp_seconds, nanos=timestamp_nanos)
31+
def _make(pause_key):
32+
tx = TokenPauseTransaction().set_token_id(fungible_token)
33+
tx.transaction_id = generate_transaction_id(sender)
34+
tx.node_account_id = node_account
35+
tx.freeze_with(mock_client)
36+
tx.sign(pause_key)
37+
return tx
38+
39+
return _make
2440

25-
tx_id = TransactionId(
26-
valid_start=tx_timestamp,
27-
account_id=account_id_proto
28-
)
29-
return tx_id
3041

31-
def test_builds_token_pause_body_with_correct_token_id(mock_account_ids):
42+
def test_builds_token_pause_body_with_correct_ids(mock_account_ids, generate_transaction_id):
3243
"""
33-
Given a TokenPauseTransaction with a valid token_id set, when building the
34-
transaction body, then the inner `tokenPause.token` fields should match.
44+
build_transaction_body() should embed:
45+
- tokenPause.token == token_id.to_proto()
46+
- transactionID == transaction_id.to_proto()
47+
- nodeAccountID == node_account_id.to_proto()
3548
"""
36-
account_id, node_account_id, token_id, _ = mock_account_ids
49+
sender, _, node_account, token_id, _ = mock_account_ids
3750

38-
pause_tx = (
39-
TokenPauseTransaction()
40-
.set_token_id(token_id)
41-
)
42-
43-
pause_tx.transaction_id = generate_transaction_id(account_id)
44-
pause_tx.node_account_id = node_account_id
51+
pause_tx = TokenPauseTransaction().set_token_id(token_id)
52+
pause_tx.transaction_id = generate_transaction_id(sender)
53+
pause_tx.node_account_id = node_account
4554

46-
transaction_body = pause_tx.build_transaction_body()
55+
body = pause_tx.build_transaction_body()
4756

48-
expected = token_id.to_proto()
49-
assert transaction_body.tokenPause.token == expected
57+
assert body.tokenPause.token == token_id.to_proto()
58+
assert body.transactionID == pause_tx.transaction_id.to_proto()
59+
assert body.nodeAccountID == pause_tx.node_account_id.to_proto()
5060

51-
assert transaction_body.transactionID == pause_tx.transaction_id.to_proto()
52-
assert transaction_body.nodeAccountID == pause_tx.node_account_id.to_proto()
5361

54-
@pytest.mark.parametrize("bad_token", [None, TokenId(0,0,0)])
62+
@pytest.mark.parametrize("bad_token", [None, TokenId(0, 0, 0)])
5563
def test_build_transaction_body_without_valid_token_id_raises(bad_token):
56-
"""Building a transaction body without a valid token_id must raise ValueError."""
57-
tx = TokenPauseTransaction()
64+
"""
65+
If token_id is missing or zero, build_transaction_body() must ValueError.
66+
"""
67+
pause_tx = TokenPauseTransaction()
5868
if bad_token is not None:
59-
tx.token_id = bad_token
60-
with pytest.raises(ValueError):
61-
tx.build_transaction_body()
62-
63-
# This test uses fixture (mock_account_ids, mock_client) as parameter
64-
def test_signed_bytes_include_token_pause_transaction(mock_account_ids, mock_client):
65-
"""Test converting the token pause transaction to protobuf format after signing."""
66-
account_id, _, token_id, _ = mock_account_ids
67-
68-
pause_tx = (
69-
TokenPauseTransaction()
70-
.set_token_id(token_id)
71-
)
72-
73-
pause_tx.transaction_id = generate_transaction_id(account_id)
69+
pause_tx.token_id = bad_token
7470

71+
with pytest.raises(ValueError, match="token_id must be set"):
72+
pause_tx.build_transaction_body()
73+
74+
def test_signed_bytes_include_token_pause_transaction(built_pause_tx):
75+
"""
76+
After freeze() and sign(pause_key), to_proto() must embed a non-empty
77+
signedTransactionBytes blob.
78+
"""
79+
# Arrange: stub pause key + public key
7580
pause_key = MagicMock()
76-
pause_key.sign.return_value = b'signature'
77-
pause_key.public_key().to_bytes_raw.return_value = b'public_key'
78-
79-
pause_tx.freeze_with(mock_client)
81+
pause_key.sign.return_value = b'__FAKE_SIG__'
8082

81-
pause_tx.sign(pause_key)
83+
fake_pub = pause_key.public_key()
84+
fake_pub.to_bytes_raw.return_value = b'__FAKE_PUB__'
85+
86+
# Act
87+
pause_tx = built_pause_tx(pause_key)
8288
proto = pause_tx.to_proto()
8389

90+
# Assert
8491
assert proto.signedTransactionBytes
8592
assert len(proto.signedTransactionBytes) > 0
8693

87-
# This test uses fixture mock_account_ids as parameter
8894
def test_pause_transaction_can_execute(mock_account_ids):
89-
"""Test that a pause transaction can be executed successfully."""
90-
account_id, node_account_id, token_id, _ = mock_account_ids
91-
92-
# Create test transaction responses
93-
ok_response = TransactionResponseProto()
94-
ok_response.nodeTransactionPrecheckCode = ResponseCode.OK
95-
96-
# Create a mock receipt for a successful token pause
97-
mock_receipt_proto = TransactionReceiptProto(
98-
status=ResponseCode.SUCCESS
99-
)
100-
101-
# Create a response for the receipt query
102-
receipt_query_response = response_pb2.Response(
95+
"""
96+
A properly built & signed TokenPauseTransaction against a mock server
97+
should return a SUCCESS receipt.
98+
"""
99+
sender, _, node_account, fungible_token, _ = mock_account_ids
100+
101+
# 1) Precheck-ok response
102+
ok_resp = TransactionResponseProto(nodeTransactionPrecheckCode=ResponseCode.OK)
103+
# 2) Receipt SUCCESS response
104+
success_receipt = TransactionReceiptProto(status=ResponseCode.SUCCESS)
105+
receipt_query = response_pb2.Response(
103106
transactionGetReceipt=transaction_get_receipt_pb2.TransactionGetReceiptResponse(
104107
header=response_header_pb2.ResponseHeader(
105108
nodeTransactionPrecheckCode=ResponseCode.OK
106109
),
107-
receipt=mock_receipt_proto
110+
receipt=success_receipt
108111
)
109112
)
110-
113+
111114
response_sequences = [
112-
[ok_response, receipt_query_response],
115+
[ok_resp, receipt_query],
113116
]
114-
115117
with mock_hedera_servers(response_sequences) as client:
116-
# Build the transaction
117-
transaction = (
118-
TokenPauseTransaction()
119-
.set_token_id(token_id)
120-
)
121-
# Set identifiers so freeze/sign can populate the payload correctly
122-
transaction.transaction_id = generate_transaction_id(account_id)
123-
transaction.node_account_id = node_account_id
124-
125-
# Freeze and sign before execute
126-
transaction.freeze_with(client)
127-
dummy_key = MagicMock()
128-
dummy_key.sign.return_value = b'__SIG__'
129-
dummy_key.public_key().to_bytes_raw.return_value = b'PUB'
130-
transaction.sign(dummy_key)
118+
pause_tx = TokenPauseTransaction().set_token_id(fungible_token)
119+
pause_tx.transaction_id = TransactionId.generate(sender)
120+
pause_tx.node_account_id = node_account
121+
122+
pause_tx.freeze_with(client)
123+
124+
dummy_priv_key = MagicMock()
125+
dummy_priv_key.sign.return_value = b'__SIG__'
131126

132-
# Execute and assert
133-
receipt = transaction.execute(client)
134-
assert receipt.status == ResponseCode.SUCCESS, "Transaction should have succeeded"
127+
dummy_pub_key = dummy_priv_key.public_key()
128+
dummy_pub_key.to_bytes_raw.return_value = b'PUB'
129+
130+
pause_tx.sign(dummy_priv_key)
131+
132+
receipt = pause_tx.execute(client)
133+
assert receipt.status == ResponseCode.SUCCESS
134+
135+

0 commit comments

Comments
 (0)