Skip to content

Commit f12b5a2

Browse files
authored
feat: Refactor Transaction Test (hiero-ledger#2627)
Signed-off-by: aceppaluni <aceppaluni@gmail.com>
1 parent 7f739eb commit f12b5a2

1 file changed

Lines changed: 54 additions & 52 deletions

File tree

tests/unit/transaction_test.py

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from hiero_sdk_python.exceptions import ReceiptStatusError
1010
from hiero_sdk_python.file.file_append_transaction import FileAppendTransaction
1111
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction
12-
from hiero_sdk_python.file.file_id import FileId
1312
from hiero_sdk_python.hapi.services import (
1413
basic_types_pb2,
1514
response_header_pb2,
@@ -33,24 +32,6 @@
3332
pytestmark = pytest.mark.unit
3433

3534

36-
@pytest.fixture
37-
def file_id():
38-
"""Returns a file_id for test."""
39-
return FileId.from_string("0.0.1")
40-
41-
42-
@pytest.fixture
43-
def account_id():
44-
"""Returns an account_id for test."""
45-
return AccountId.from_string("0.0.9")
46-
47-
48-
@pytest.fixture
49-
def transaction_id():
50-
"""Returns a transaction_id for test."""
51-
return TransactionId.from_string("0.0.9@1770911831.331000137")
52-
53-
5435
def test_execute_waits_for_receipt_receipt():
5536
"""Test execute return TransactionReceipt when wait_for_receipt is True (default)."""
5637
ok_response = transaction_response_pb2.TransactionResponse(nodeTransactionPrecheckCode=ResponseCode.OK)
@@ -187,16 +168,17 @@ def test_multiple_keys_still_work():
187168
assert pubkey_prefixes == expected_prefixes, "Signatures should match key1 and key2 exactly"
188169

189170

190-
def test_same_size_for_identical_transactions(transaction_id, account_id):
171+
def test_same_size_for_identical_transactions(transaction_id, mock_account_ids):
191172
"""Test two identical transactions should have the same size."""
173+
_, _, node_account_id, _, _ = mock_account_ids
192174
key = PrivateKey.generate()
193175

194176
tx1 = (
195177
AccountCreateTransaction()
196178
.set_key_without_alias(key)
197179
.set_initial_balance(Hbar(2))
198180
.set_transaction_id(transaction_id)
199-
.set_node_account_ids([account_id])
181+
.set_node_account_ids([node_account_id])
200182
.freeze()
201183
)
202184

@@ -205,23 +187,24 @@ def test_same_size_for_identical_transactions(transaction_id, account_id):
205187
.set_key_without_alias(key)
206188
.set_initial_balance(Hbar(2))
207189
.set_transaction_id(transaction_id)
208-
.set_node_account_ids([account_id])
190+
.set_node_account_ids([node_account_id])
209191
.freeze()
210192
)
211193

212194
assert tx1.size == tx2.size
213195

214196

215-
def test_signed_tx_have_larger_size(transaction_id, account_id):
197+
def test_signed_tx_have_larger_size(transaction_id, mock_account_ids):
216198
"""Test signed Transaction should have larger size."""
199+
_, _, node_account_id, _, _ = mock_account_ids
217200
key = PrivateKey.generate()
218201

219202
tx1 = (
220203
AccountCreateTransaction()
221204
.set_key_without_alias(key)
222205
.set_initial_balance(Hbar(2))
223206
.set_transaction_id(transaction_id)
224-
.set_node_account_ids([account_id])
207+
.set_node_account_ids([node_account_id])
225208
.freeze()
226209
.sign(PrivateKey.generate())
227210
)
@@ -231,43 +214,47 @@ def test_signed_tx_have_larger_size(transaction_id, account_id):
231214
.set_key_without_alias(key)
232215
.set_initial_balance(Hbar(2))
233216
.set_transaction_id(transaction_id)
234-
.set_node_account_ids([account_id])
217+
.set_node_account_ids([node_account_id])
235218
.freeze()
236219
)
237220

238221
assert tx1.size > tx2.size
239222

240223

241-
def test_tx_with_larger_content_should_have_larger_tx_body(transaction_id, account_id):
224+
def test_tx_with_larger_content_should_have_larger_tx_body(transaction_id, mock_account_ids):
242225
"""Test transaction with larger content should have larger transaction body."""
226+
_, _, node_account_id, _, _ = mock_account_ids
227+
243228
tx1 = (
244229
FileCreateTransaction()
245230
.set_contents("smallBody")
246231
.set_transaction_id(transaction_id)
247-
.set_node_account_ids([account_id])
232+
.set_node_account_ids([node_account_id])
248233
.freeze()
249234
)
250235

251236
tx2 = (
252237
FileCreateTransaction()
253238
.set_contents("veryLargeBody")
254239
.set_transaction_id(transaction_id)
255-
.set_node_account_ids([account_id])
240+
.set_node_account_ids([node_account_id])
256241
.freeze()
257242
)
258243

259244
assert tx1.body_size < tx2.body_size
260245

261246

262-
def test_tx_without_optional_fields_should_have_smaller_tx_body(transaction_id, account_id):
247+
def test_tx_without_optional_fields_should_have_smaller_tx_body(transaction_id, mock_account_ids):
263248
"""Test transaction with without optional fields should have smaller transaction body."""
249+
_, _, node_account_id, _, _ = mock_account_ids
264250
key = PrivateKey.generate()
251+
265252
tx1 = (
266253
AccountCreateTransaction()
267254
.set_key_without_alias(key)
268255
.set_initial_balance(Hbar(2))
269256
.set_transaction_id(transaction_id)
270-
.set_node_account_ids([account_id])
257+
.set_node_account_ids([node_account_id])
271258
.freeze()
272259
)
273260

@@ -276,7 +263,7 @@ def test_tx_without_optional_fields_should_have_smaller_tx_body(transaction_id,
276263
.set_key_without_alias(key)
277264
.set_initial_balance(Hbar(2))
278265
.set_transaction_id(transaction_id)
279-
.set_node_account_ids([account_id])
266+
.set_node_account_ids([node_account_id])
280267
.set_alias(PrivateKey.generate_ecdsa().public_key().to_evm_address())
281268
.set_transaction_valid_duration(10)
282269
.freeze()
@@ -285,8 +272,9 @@ def test_tx_without_optional_fields_should_have_smaller_tx_body(transaction_id,
285272
assert tx1.body_size < tx2.body_size
286273

287274

288-
def test_file_append_chunk_tx_should_return_list_of_body_sizes(file_id, account_id, transaction_id):
275+
def test_file_append_chunk_tx_should_return_list_of_body_sizes(file_id, transaction_id, mock_account_ids):
289276
"""Test file append tx should return array of body sizes for multi-chunk transaction."""
277+
_, _, node_account_id, _, _ = mock_account_ids
290278
chunk_size = 1024
291279
content = "a" * (chunk_size * 3)
292280

@@ -296,7 +284,7 @@ def test_file_append_chunk_tx_should_return_list_of_body_sizes(file_id, account_
296284
.set_chunk_size(chunk_size)
297285
.set_contents(content)
298286
.set_transaction_id(transaction_id)
299-
.set_node_account_ids([account_id])
287+
.set_node_account_ids([node_account_id])
300288
.freeze()
301289
)
302290

@@ -305,15 +293,17 @@ def test_file_append_chunk_tx_should_return_list_of_body_sizes(file_id, account_
305293
assert len(sizes) == 3
306294

307295

308-
def test_file_append_single_chunk_tx_return_list_of_len_one(file_id, account_id, transaction_id):
296+
def test_file_append_single_chunk_tx_return_list_of_len_one(file_id, transaction_id, mock_account_ids):
309297
"""Test file append tx should return array of one size for single-chunk transaction."""
298+
_, _, node_account_id, _, _ = mock_account_ids
310299
content = "small_content"
300+
311301
tx = (
312302
FileAppendTransaction()
313303
.set_file_id(file_id)
314304
.set_contents(content)
315305
.set_transaction_id(transaction_id)
316-
.set_node_account_ids([account_id])
306+
.set_node_account_ids([node_account_id])
317307
.freeze()
318308
)
319309

@@ -322,8 +312,9 @@ def test_file_append_single_chunk_tx_return_list_of_len_one(file_id, account_id,
322312
assert len(sizes) == 1
323313

324314

325-
def test_message_submit_chunk_tx_should_return_list_of_body_sizes(topic_id, account_id, transaction_id):
315+
def test_message_submit_chunk_tx_should_return_list_of_body_sizes(topic_id, transaction_id, mock_account_ids):
326316
"""Test topic message submit tx should return array of body sizes for multi-chunk transaction."""
317+
_, _, node_account_id, _, _ = mock_account_ids
327318
chunk_size = 1024
328319
message = "a" * (chunk_size * 3)
329320

@@ -333,7 +324,7 @@ def test_message_submit_chunk_tx_should_return_list_of_body_sizes(topic_id, acco
333324
.set_chunk_size(chunk_size)
334325
.set_message(message)
335326
.set_transaction_id(transaction_id)
336-
.set_node_account_ids([account_id])
327+
.set_node_account_ids([node_account_id])
337328
.freeze()
338329
)
339330

@@ -343,15 +334,17 @@ def test_message_submit_chunk_tx_should_return_list_of_body_sizes(topic_id, acco
343334
assert tx._current_chunk_index == 0
344335

345336

346-
def test_message_submit_single_chunk_tx_return_list_of_len_one(topic_id, account_id, transaction_id):
337+
def test_message_submit_single_chunk_tx_return_list_of_len_one(topic_id, transaction_id, mock_account_ids):
347338
"""Test topic message submit tx should return array of one size for single-chunk transaction."""
339+
_, _, node_account_id, _, _ = mock_account_ids
348340
message = "small_content"
341+
349342
tx = (
350343
TopicMessageSubmitTransaction()
351344
.set_topic_id(topic_id)
352345
.set_message(message)
353346
.set_transaction_id(transaction_id)
354-
.set_node_account_ids([account_id])
347+
.set_node_account_ids([node_account_id])
355348
.freeze()
356349
)
357350

@@ -360,14 +353,16 @@ def test_message_submit_single_chunk_tx_return_list_of_len_one(topic_id, account
360353
assert len(sizes) == 1
361354

362355

363-
def test_tx_with_no_content_should_return_single_body_chunk(file_id, account_id, transaction_id):
356+
def test_tx_with_no_content_should_return_single_body_chunk(file_id, transaction_id, mock_account_ids):
364357
"""Test should return single body chunk for transaction with no content."""
358+
_, _, node_account_id, _, _ = mock_account_ids
359+
365360
tx = (
366361
FileAppendTransaction()
367362
.set_file_id(file_id)
368363
.set_contents(" ")
369364
.set_transaction_id(transaction_id)
370-
.set_node_account_ids([account_id])
365+
.set_node_account_ids([node_account_id])
371366
.freeze()
372367
)
373368

@@ -376,16 +371,17 @@ def test_tx_with_no_content_should_return_single_body_chunk(file_id, account_id,
376371
assert len(sizes) == 1
377372

378373

379-
def test_chunked_tx_return_proper_sizes(file_id, account_id, transaction_id):
374+
def test_chunked_tx_return_proper_sizes(file_id, transaction_id, mock_account_ids):
380375
"""Test should return proper sizes for FileAppend transactions when chunking occurs."""
376+
_, _, node_account_id, _, _ = mock_account_ids
381377
large_content = "a" * 2048
382378

383379
large_tx = (
384380
FileAppendTransaction()
385381
.set_file_id(file_id)
386382
.set_contents(large_content)
387383
.set_transaction_id(transaction_id)
388-
.set_node_account_ids([account_id])
384+
.set_node_account_ids([node_account_id])
389385
.freeze()
390386
)
391387

@@ -398,7 +394,7 @@ def test_chunked_tx_return_proper_sizes(file_id, account_id, transaction_id):
398394
.set_file_id(file_id)
399395
.set_contents(small_content)
400396
.set_transaction_id(transaction_id)
401-
.set_node_account_ids([account_id])
397+
.set_node_account_ids([node_account_id])
402398
.freeze()
403399
)
404400

@@ -411,8 +407,9 @@ def test_chunked_tx_return_proper_sizes(file_id, account_id, transaction_id):
411407
assert large_tx._current_chunk_index == 0
412408

413409

414-
def test_chunked_tx_differ_size_if_chunk_are_not_equal(topic_id, account_id, transaction_id):
410+
def test_chunked_tx_differ_size_if_chunk_are_not_equal(topic_id, transaction_id, mock_account_ids):
415411
"""Test that the last chunk's size is different from the full chunks if the chunk size is not even."""
412+
_, _, node_account_id, _, _ = mock_account_ids
416413
chunk_size = 1024
417414
message = "a" * (chunk_size + 512)
418415

@@ -422,7 +419,7 @@ def test_chunked_tx_differ_size_if_chunk_are_not_equal(topic_id, account_id, tra
422419
.set_chunk_size(chunk_size)
423420
.set_message(message)
424421
.set_transaction_id(transaction_id)
425-
.set_node_account_ids([account_id])
422+
.set_node_account_ids([node_account_id])
426423
.freeze()
427424
)
428425

@@ -453,13 +450,15 @@ def test_high_volume_can_be_serialized(mock_client):
453450
assert transaction_from_bytes.high_volume is True
454451

455452

456-
def test_high_volume_cannot_change_after_freeze(transaction_id, account_id):
453+
def test_high_volume_cannot_change_after_freeze(transaction_id, mock_account_ids):
457454
"""Test that high_volume cannot be modified after freezing."""
455+
_, _, node_account_id, _, _ = mock_account_ids
456+
458457
transaction = (
459458
AccountCreateTransaction()
460459
.set_key_without_alias(PrivateKey.generate_ed25519())
461460
.set_transaction_id(transaction_id)
462-
.set_node_account_ids([account_id])
461+
.set_node_account_ids([node_account_id])
463462
.freeze()
464463
)
465464

@@ -469,16 +468,17 @@ def test_high_volume_cannot_change_after_freeze(transaction_id, account_id):
469468

470469
def test_high_volume_is_included_in_protobuf_output(
471470
transaction_id,
472-
account_id,
471+
mock_account_ids,
473472
):
474473
"""Test that high_volume is correctly serialized into protobuf output."""
474+
_, _, node_account_id, _, _ = mock_account_ids
475475

476476
# Test with high_volume=True
477477
transaction = (
478478
AccountCreateTransaction()
479479
.set_key_without_alias(PrivateKey.generate_ed25519())
480480
.set_transaction_id(transaction_id)
481-
.set_node_account_ids([account_id])
481+
.set_node_account_ids([node_account_id])
482482
.set_high_volume(True)
483483
.freeze()
484484
)
@@ -497,7 +497,7 @@ def test_high_volume_is_included_in_protobuf_output(
497497
AccountCreateTransaction()
498498
.set_key_without_alias(PrivateKey.generate_ed25519())
499499
.set_transaction_id(transaction_id)
500-
.set_node_account_ids([account_id])
500+
.set_node_account_ids([node_account_id])
501501
.set_high_volume(False)
502502
.freeze()
503503
)
@@ -543,8 +543,10 @@ def test_transaction_fee_rejects_negative_int():
543543
tx.transaction_fee = -1
544544

545545

546-
def test_transaction_default_max_fee(account_id):
546+
def test_transaction_default_max_fee(mock_account_ids):
547547
"""Test default transaction fee is set if no transaction fee is set."""
548+
account_id = mock_account_ids[0]
549+
548550
tx = TopicMessageSubmitTransaction()
549551
tx.set_node_account_ids([AccountId(0, 0, 3)])
550552
tx.operator_account_id = account_id

0 commit comments

Comments
 (0)