Skip to content

Commit 795abfe

Browse files
committed
Improved unit test coverage for TransactionId class.
Signed-off-by: Adityarya11 <[email protected]>
1 parent 5195030 commit 795abfe

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
8181
- Added merge conflict bot workflow (`.github/workflows/bot-merge-conflict.yml`) and helper script (`.github/scripts/bot-merge-conflict.js`) to detect and notify about PR merge conflicts, with retry logic for unknown mergeable states, idempotent commenting, and push-to-main recheck logic (#1247)
8282
- Added workflow to prevent assigning intermediate issues to contributors without prior Good First Issue completion (#1143).
8383
- Added `Client.from_env()` and network-specific factory methods (e.g., `Client.for_testnet()`) to simplify client initialization and reduce boilerplate. [[#1251](https://github.com/hiero-ledger/hiero-sdk-python/issues/1251)]
84+
- Improved unit test coverage for `TransactionId` class, covering parsing logic, hashing, and scheduled transactions.
8485

8586
### Changed
8687

tests/unit/transaction_id_test.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import pytest
2+
from hiero_sdk_python import (
3+
TransactionId,
4+
AccountId
5+
)
6+
7+
def test_from_string_valid():
8+
"""Test parsing a valid transaction ID string."""
9+
tx_id_str = "[email protected]"
10+
tx_id = TransactionId.from_string(tx_id_str)
11+
12+
assert tx_id.account_id == AccountId(0, 0, 123)
13+
assert tx_id.valid_start.seconds == 1234567890
14+
assert tx_id.valid_start.nanos == 123456789
15+
assert tx_id.scheduled is False
16+
17+
def test_from_string_invalid_format_raises_error():
18+
"""Test that invalid formats raise ValueError (covers the try-except block)."""
19+
invalid_strings = [
20+
"invalid_string",
21+
"0.0.123.123456789", # Missing @ separator
22+
"0.0.123@12345", # Missing . in timestamp
23+
"[email protected]", # Non-numeric timestamp
24+
]
25+
26+
for s in invalid_strings:
27+
with pytest.raises(ValueError) as exc_info:
28+
TransactionId.from_string(s)
29+
assert f"Invalid TransactionId string format: {s}" in str(exc_info.value)
30+
31+
def test_hash_implementation():
32+
"""Test __hash__ implementation coverage."""
33+
tx_id1 = TransactionId.from_string("[email protected]")
34+
tx_id2 = TransactionId.from_string("[email protected]")
35+
tx_id3 = TransactionId.from_string("[email protected]")
36+
37+
# Hashes should be equal for equal objects
38+
assert hash(tx_id1) == hash(tx_id2)
39+
# Hashes should typically differ for different objects
40+
assert hash(tx_id1) != hash(tx_id3)
41+
42+
# Verify usage in sets
43+
unique_ids = {tx_id1, tx_id2, tx_id3}
44+
assert len(unique_ids) == 2
45+
46+
def test_equality():
47+
"""Test __eq__ implementation."""
48+
tx_id1 = TransactionId.from_string("[email protected]")
49+
tx_id2 = TransactionId.from_string("[email protected]")
50+
51+
assert tx_id1 == tx_id2
52+
assert tx_id1 != "some_string"
53+
assert tx_id1 != TransactionId.from_string("[email protected]")
54+
55+
def test_to_proto_sets_scheduled():
56+
"""Test that _to_proto sets the scheduled flag correctly."""
57+
tx_id = TransactionId.from_string("[email protected]")
58+
59+
# Default is False
60+
proto_default = tx_id._to_proto()
61+
assert proto_default.scheduled is False
62+
63+
# Set to True
64+
tx_id.scheduled = True
65+
proto_scheduled = tx_id._to_proto()
66+
assert proto_scheduled.scheduled is True
67+
68+
def test_generate():
69+
"""Test generating a new TransactionId."""
70+
account_id = AccountId(0, 0, 123)
71+
tx_id = TransactionId.generate(account_id)
72+
73+
assert tx_id.account_id == account_id
74+
assert tx_id.valid_start is not None
75+
assert tx_id.valid_start.seconds > 0

0 commit comments

Comments
 (0)