-
Notifications
You must be signed in to change notification settings - Fork 189
feat: unit test for 'endpoint.py' #1290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0bf5343
unit test for 'endpoint.py'
mukundkumarjha 0f7f6ec
Created tests for endpoint.py to reach 90% test coverage
mukundkumarjha 3308abc
changes as per coderabbit
mukundkumarjha 88bc489
changes as per coderabbit
mukundkumarjha fb7d500
round serialization test added
mukundkumarjha a3e08cc
code updated as per pytest convention
mukundkumarjha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import pytest | ||
| from unittest.mock import MagicMock | ||
| from src.hiero_sdk_python.address_book.endpoint import Endpoint | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
| def test_getter_setter(): | ||
|
|
||
| ''' Test for Endpoint constructor''' | ||
|
|
||
| endpoint = Endpoint(address=None, port=None, domain_name=None) | ||
| endpoint.set_address(b'127.0.1.1') | ||
| endpoint.set_port(77777) | ||
| endpoint.set_domain_name("redpanda.com") | ||
|
|
||
| assert endpoint.get_address() == b'127.0.1.1' | ||
| assert endpoint.get_port() == 77777 | ||
| assert endpoint.get_domain_name() == "redpanda.com" | ||
exploreriii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
exploreriii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @pytest.mark.parametrize("input_port, expected_port", [ | ||
| (0, 50211), | ||
| (50111, 50211), | ||
| (80, 80) | ||
| ]) | ||
|
|
||
| def test_from_proto_port_mapping(input_port, expected_port): | ||
|
|
||
| ''' Tests the logic that converts a Protobuf ServiceEndpoint into an Endpoint object.''' | ||
|
|
||
| mock_proto = MagicMock() | ||
| mock_proto.port = input_port | ||
| mock_proto.ipAddressV4 = b"127.0.1.1" | ||
| mock_proto.domain_name = "redpanda.com" | ||
|
|
||
| endpoint = Endpoint._from_proto(mock_proto) | ||
| assert endpoint.get_port() == expected_port | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_to_proto(): | ||
|
|
||
| '''Verifies that an Endpoint instance can be correctly serialized back into | ||
| a Protobuf ServiceEndpoint object with all fields intact.''' | ||
|
|
||
| endpoint = Endpoint(address=b'127.0.1.1', port=77777, domain_name="redpanda.com") | ||
| proto = endpoint._to_proto() | ||
| assert proto.ipAddressV4 == b'127.0.1.1' | ||
| assert proto.port == 77777 | ||
| assert proto.domain_name == "redpanda.com" | ||
exploreriii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_str(): | ||
|
|
||
| ''' Tests the human-readable string representation of the Endpoint. ''' | ||
|
|
||
| endpoint = Endpoint(address=b'127.0.1.1', port=77777, domain_name="redpanda.com") | ||
| assert str(endpoint) == '127.0.1.1:77777' | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_from_dict_error(): | ||
|
|
||
| '''Validates 'Guard Clause' error handling''' | ||
|
|
||
| invalid_data = {"port": 77777} | ||
| with pytest.raises(ValueError, match="JSON data must contain"): | ||
| Endpoint.from_dict(invalid_data) | ||
|
|
||
| def test_from_dict_success(): | ||
| ''' Tests successful creation of an Endpoint from a dictionary (JSON format) ''' | ||
| data = { | ||
| "ip_address_v4": "127.0.0.1", | ||
| "port": 77777, | ||
| "domain_name": "redpanda.com" | ||
| } | ||
| endpoint = Endpoint.from_dict(data) | ||
|
|
||
| assert endpoint.get_address() == b"127.0.0.1" | ||
| assert endpoint.get_port() == 77777 | ||
| assert endpoint.get_domain_name() == "redpanda.com" | ||
exploreriii marked this conversation as resolved.
Show resolved
Hide resolved
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.