|
10 | 10 |
|
11 | 11 | import pytest |
12 | 12 |
|
13 | | -from hiero_sdk_python import AccountId, Client, PrivateKey |
| 13 | +from hiero_sdk_python import AccountId, Client |
14 | 14 | from hiero_sdk_python.client import client as client_module |
| 15 | +from hiero_sdk_python.client.network import Network |
| 16 | +from hiero_sdk_python.crypto.private_key import PrivateKey |
15 | 17 | from hiero_sdk_python.hbar import Hbar |
16 | 18 | from hiero_sdk_python.node import _Node |
17 | 19 | from hiero_sdk_python.transaction.transaction_id import TransactionId |
@@ -527,3 +529,78 @@ def test_get_node_account_ids_raises_when_no_nodes(): |
527 | 529 | client.get_node_account_ids() |
528 | 530 |
|
529 | 531 | client.close() |
| 532 | + |
| 533 | + |
| 534 | +def test_for_network_initializes_with_custom_map(): |
| 535 | + """Test for_network correctly maps strings to AccountIds and Nodes.""" |
| 536 | + network_map = {"127.0.0.1:50211": AccountId(0, 0, 3), "127.0.0.1:50212": AccountId(0, 0, 4)} |
| 537 | + |
| 538 | + client = Client.for_network(network_map) |
| 539 | + |
| 540 | + assert isinstance(client.network, Network) |
| 541 | + assert len(client.network.nodes) == 2 |
| 542 | + |
| 543 | + node_accounts = [node._account_id.__str__() for node in client.network.nodes] |
| 544 | + assert "0.0.3" in node_accounts |
| 545 | + assert "0.0.4" in node_accounts |
| 546 | + |
| 547 | + |
| 548 | +@pytest.mark.parametrize("network", ["mainnet", "testnet", "previewnet"]) |
| 549 | +def test_for_network_with_hosted_network_forces_tls(network): |
| 550 | + """Test that if hosted-net the port 50211 is upgraded to 50212.""" |
| 551 | + network_map = {"34.94.106.61:50211": AccountId(0, 0, 3)} |
| 552 | + client = Client.for_network(network_map, network_name=network) |
| 553 | + |
| 554 | + node = client.network.nodes[0] |
| 555 | + assert str(node._address) == "34.94.106.61:50212" |
| 556 | + assert node._address._is_transport_security() is True |
| 557 | + assert client.network.is_transport_security() is True |
| 558 | + |
| 559 | + |
| 560 | +@pytest.mark.parametrize("network", ["local", "localhost", "solo", "custom"]) |
| 561 | +def test_for_network_with_non_hosted_network_not_forces_tls(network): |
| 562 | + """Test that if non hosted-net the port 50211 does not change.""" |
| 563 | + network_map = {"127.0.0.1:50211": AccountId(0, 0, 3)} |
| 564 | + client = Client.for_network(network_map, network_name=network) |
| 565 | + |
| 566 | + node = client.network.nodes[0] |
| 567 | + assert str(node._address) == "127.0.0.1:50211" |
| 568 | + assert node._address._is_transport_security() is False |
| 569 | + assert client.network.is_transport_security() is False |
| 570 | + |
| 571 | + |
| 572 | +@pytest.mark.parametrize("network", ["local", "localhost", "solo", "custom"]) |
| 573 | +def test_for_network_with_non_hosted_network_not_downgrade_tls(network): |
| 574 | + """Test that if non hosted-net the port 50212 does not change.""" |
| 575 | + network_map = {"127.0.0.1:50212": AccountId(0, 0, 3)} |
| 576 | + client = Client.for_network(network_map, network_name=network) |
| 577 | + |
| 578 | + node = client.network.nodes[0] |
| 579 | + assert str(node._address) == "127.0.0.1:50212" |
| 580 | + assert node._address._is_transport_security() is True |
| 581 | + assert client.network.is_transport_security() is False # Non hosted network. |
| 582 | + |
| 583 | + |
| 584 | +def test_for_network_with_empty_map_raises_error(): |
| 585 | + """Test that for_network raises ValueError when map is empty.""" |
| 586 | + with pytest.raises(ValueError, match="network_map cannot be empty"): |
| 587 | + Client.for_network({}) |
| 588 | + |
| 589 | + |
| 590 | +@pytest.mark.parametrize( |
| 591 | + "invalid_map, error_msg", |
| 592 | + [ |
| 593 | + ( |
| 594 | + {"127.0.0.1:50211": AccountId(0, 0, 3), "127.0.0.1:50212": AccountId(1, 0, 4)}, |
| 595 | + "network is not valid, all nodes must be in the same shard and realm", |
| 596 | + ), |
| 597 | + ( |
| 598 | + {"127.0.0.1:50211": AccountId(0, 0, 3), "127.0.0.1:50212": AccountId(0, 1, 4)}, |
| 599 | + "network is not valid, all nodes must be in the same shard and realm", |
| 600 | + ), |
| 601 | + ], |
| 602 | +) |
| 603 | +def test_for_network_invalid_shard_realm_raises_error(invalid_map, error_msg): |
| 604 | + """Test that for_network catches mismatched shards or realms.""" |
| 605 | + with pytest.raises(ValueError, match=error_msg): |
| 606 | + Client.for_network(invalid_map) |
0 commit comments