Skip to content

Commit 16c444c

Browse files
committed
chore: test
Signed-off-by: Manish Dait <daitmanish88@gmail.com>
1 parent c5c4fae commit 16c444c

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

tests/unit/subscription_handle_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66

77

88
def test_not_cancelled_by_default():
9+
"""Test a new handle starts in a non-cancelled state."""
910
handle = SubscriptionHandle()
1011
assert not handle.is_cancelled()
1112

1213

1314
def test_cancel_marks_as_cancelled():
15+
"""Test calling cancel updates the is_cancelled status."""
1416
handle = SubscriptionHandle()
1517
handle.cancel()
1618
assert handle.is_cancelled()
1719

1820

1921
def test_set_thread_and_join_calls_thread_join_with_timeout():
22+
"""Test that join correctly forwards the timeout to the underlying thread."""
2023
handle = SubscriptionHandle()
2124
mock_thread = Mock()
2225
handle.set_thread(mock_thread)
@@ -25,6 +28,25 @@ def test_set_thread_and_join_calls_thread_join_with_timeout():
2528

2629

2730
def test_join_without_thread_raises_nothing():
31+
"""Test join is a no-op if no thread has been associated."""
2832
handle = SubscriptionHandle()
2933
# should not raise
3034
handle.join()
35+
36+
37+
def test_cancel_triggers_grpc_termination():
38+
"""Test that cancelling the handle terminates the active gRPC call."""
39+
handle = SubscriptionHandle()
40+
mock_call = Mock()
41+
handle._set_call(mock_call)
42+
handle.cancel()
43+
mock_call.cancel.assert_called_once()
44+
45+
46+
def test_immediate_cancellation_of_late_call():
47+
"""Test a gRPC call is cancelled immediately if set after the handle was cancelled."""
48+
handle = SubscriptionHandle()
49+
mock_call = Mock()
50+
handle.cancel()
51+
handle._set_call(mock_call)
52+
mock_call.cancel.assert_called_once()

tests/unit/topic_message_query_test.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from __future__ import annotations
22

3+
import time
34
from datetime import datetime, timezone
45
from unittest.mock import MagicMock, patch
56

67
import pytest
78

9+
from hiero_sdk_python.account.account_id import AccountId
810
from hiero_sdk_python.client.client import Client
911
from hiero_sdk_python.consensus.topic_id import TopicId
1012
from hiero_sdk_python.hapi.mirror import consensus_service_pb2 as mirror_proto
1113
from hiero_sdk_python.hapi.services import timestamp_pb2 as hapi_timestamp_pb2
14+
from hiero_sdk_python.hapi.services.consensus_submit_message_pb2 import ConsensusMessageChunkInfo
1215
from hiero_sdk_python.query.topic_message_query import TopicMessageQuery
16+
from hiero_sdk_python.transaction.transaction_id import TransactionId
1317

1418

1519
pytestmark = pytest.mark.unit
@@ -19,7 +23,8 @@
1923
def mock_client():
2024
"""Fixture to provide a mock Client instance."""
2125
client = MagicMock(spec=Client)
22-
client.operator_account_id = "0.0.12345"
26+
client.operator_account_id = AccountId(0, 0, 12345)
27+
2328
return client
2429

2530

@@ -40,11 +45,20 @@ def mock_subscription_response():
4045
)
4146

4247

48+
def test_topic_message_query_initialization():
49+
"""Test initializing the query with various parameter types and setters."""
50+
start = datetime(2023, 1, 1, tzinfo=timezone.utc)
51+
query = TopicMessageQuery().set_topic_id("0.0.123").set_start_time(start).set_limit(5).set_chunking_enabled(True)
52+
53+
assert query._topic_id.topicNum == 123
54+
assert query._start_time.seconds == int(start.timestamp())
55+
assert query._limit == 5
56+
assert query._chunking_enabled is True
57+
58+
4359
# This test uses fixtures (mock_client, mock_topic_id, mock_subscription_response) as parameters
4460
def test_topic_message_query_subscription(mock_client, mock_topic_id, mock_subscription_response):
45-
"""
46-
Test subscribing to topic messages using TopicMessageQuery.
47-
"""
61+
"""Test subscribing to topic messages using TopicMessageQuery."""
4862
query = TopicMessageQuery().set_topic_id(mock_topic_id).set_start_time(datetime.now(tz=timezone.utc))
4963

5064
with patch("hiero_sdk_python.query.topic_message_query.TopicMessageQuery.subscribe") as mock_subscribe:
@@ -69,3 +83,29 @@ def side_effect(client, on_message, on_error): # noqa: ARG001
6983
on_error.assert_not_called()
7084

7185
print("Test passed: Subscription handled messages correctly.")
86+
87+
88+
def test_chunk_message_handling(mock_client):
89+
"""Test that multiple chunks are correctly buffered and released as a single message."""
90+
query = TopicMessageQuery(topic_id="0.0.123", chunking_enabled=True)
91+
92+
# Mocking two chunks for the same transaction
93+
tx_id = TransactionId.generate(mock_client.operator_account_id)._to_proto()
94+
chunk1 = mirror_proto.ConsensusTopicResponse(
95+
message=b"part1", chunkInfo=ConsensusMessageChunkInfo(initialTransactionID=tx_id, total=2, number=1)
96+
)
97+
chunk2 = mirror_proto.ConsensusTopicResponse(
98+
message=b"part2", chunkInfo=ConsensusMessageChunkInfo(initialTransactionID=tx_id, total=2, number=2)
99+
)
100+
101+
mock_client.mirror_stub.subscribeTopic.return_value = [chunk1, chunk2]
102+
103+
received_messages = []
104+
query.subscribe(mock_client, on_message=lambda m: received_messages.append(m))
105+
106+
# Wait for thread execution
107+
time.sleep(0.1)
108+
109+
assert len(received_messages) == 1
110+
# Assuming TopicMessage.of_many joins messages
111+
assert b"part1" in received_messages[0].message

0 commit comments

Comments
 (0)