Skip to content

Commit 0bf5343

Browse files
unit test for 'endpoint.py'
Signed-off-by: mukundkumarjha <mukundiiitg@gmail.com>
1 parent 011f388 commit 0bf5343

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
@@ -7,6 +7,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Added
10+
- Added unit test for 'endpoint.py' to increase coverage.
1011
- Automated assignment guard for `advanced` issues; requires completion of at least one `good first issue` and one `intermediate` issue before assignment (exempts maintainers, committers, and triage members). (#1142)
1112
- Added Hbar object support for TransferTransaction HBAR transfers:
1213
- Methods now accept `Union[int, Hbar]` for amount parameters with immediate normalization to tinybars

tests/unit/endpoint_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 unittest.mock import MagicMock
3+
from src.hiero_sdk_python.address_book.endpoint import Endpoint
4+
5+
pytestmark = pytest.mark.unit
6+
7+
def test_getter_setter():
8+
9+
''' Test for Endpoint constructor'''
10+
11+
endpoint = Endpoint(address=None, port=None, domain_name=None)
12+
endpoint.set_address(b'127.0.1.1')
13+
endpoint.set_port(77777)
14+
endpoint.set_domain_name("redpanda.com")
15+
16+
assert endpoint.get_address() == b'127.0.1.1'
17+
assert endpoint.get_port() == 77777
18+
assert endpoint.get_domain_name() == "redpanda.com"
19+
20+
@pytest.mark.parametrize("input_port, expected_port", [
21+
(0, 50211),
22+
(50111, 50211),
23+
(80, 80)
24+
])
25+
26+
def test_from_proto_port_mapping(input_port, expected_port):
27+
28+
''' Tests the logic that converts a Protobuf ServiceEndpoint into an Endpoint object.'''
29+
30+
mock_proto = MagicMock()
31+
mock_proto.port = input_port
32+
mock_proto.ipAddressV4 = b"127.0.1.1"
33+
mock_proto.domain_name = "redpanda.com"
34+
35+
endpoint = Endpoint._from_proto(mock_proto)
36+
assert endpoint.get_port() == expected_port
37+
38+
def test_to_proto():
39+
40+
'''Verifies that an Endpoint instance can be correctly serialized back into
41+
a Protobuf ServiceEndpoint object with all fields intact.'''
42+
43+
endpoint = Endpoint(address=b'127.0.1.1', port=77777, domain_name="redpanda.com")
44+
proto = endpoint._to_proto()
45+
assert proto.ipAddressV4 == b'127.0.1.1'
46+
assert proto.port == 77777
47+
assert proto.domain_name == "redpanda.com"
48+
49+
def test_str():
50+
51+
''' Tests the human-readable string representation of the Endpoint. '''
52+
53+
endpoint = Endpoint(address=b'127.0.1.1', port=77777, domain_name="redpanda.com")
54+
assert str(endpoint) == '127.0.1.1:77777'
55+
56+
def test_from_dict_error():
57+
58+
'''Validates 'Guard Clause' error handling'''
59+
60+
invalid_data = {"port": 77777}
61+
with pytest.raises(ValueError, match="JSON data must contain"):
62+
Endpoint.from_dict(invalid_data)
63+
64+
def test_from_dict_success():
65+
''' Tests successful creation of an Endpoint from a dictionary (JSON format) '''
66+
data = {
67+
"ip_address_v4": "127.0.0.1",
68+
"port": 77777,
69+
"domain_name": "redpanda.com"
70+
}
71+
endpoint = Endpoint.from_dict(data)
72+
73+
assert endpoint.get_address() == b"127.0.0.1"
74+
assert endpoint.get_port() == 77777
75+
assert endpoint.get_domain_name() == "redpanda.com"

0 commit comments

Comments
 (0)