|
7 | 7 | from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import ( |
8 | 8 | SchedulableTransactionBody, |
9 | 9 | ) |
| 10 | +from hiero_sdk_python.hbar import Hbar |
| 11 | +from hiero_sdk_python.hbar_unit import HbarUnit |
10 | 12 | from hiero_sdk_python.tokens.nft_id import NftId |
11 | 13 | from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction |
12 | 14 |
|
@@ -283,8 +285,8 @@ def test_zero_amount_validation(mock_account_ids): |
283 | 285 | account_id_1, _, _, token_id_1, _ = mock_account_ids |
284 | 286 | transfer_tx = TransferTransaction() |
285 | 287 |
|
286 | | - # Test zero HBAR amount should raise ValueError |
287 | | - with pytest.raises(ValueError, match="Amount must be a non-zero integer"): |
| 288 | + # Test zero HBAR amount should raise ValueError with updated message |
| 289 | + with pytest.raises(ValueError, match="Amount must be a non-zero value"): |
288 | 290 | transfer_tx.add_hbar_transfer(account_id_1, 0) |
289 | 291 |
|
290 | 292 | # Test zero token amount should raise ValueError |
@@ -510,3 +512,104 @@ def test_approved_token_transfer_validation(mock_account_ids): |
510 | 512 | # Test zero amount |
511 | 513 | with pytest.raises(ValueError, match="Amount must be a non-zero integer"): |
512 | 514 | transfer_tx.add_approved_token_transfer_with_decimals(token_id_1, account_id_1, 0, 6) |
| 515 | + |
| 516 | + |
| 517 | +def test_add_hbar_transfer_with_hbar_object(mock_account_ids): |
| 518 | + """Test adding HBAR transfers with Hbar objects (covers Hbar normalization).""" |
| 519 | + account_id_sender, account_id_recipient, _, _, _ = mock_account_ids |
| 520 | + transfer_tx = TransferTransaction() |
| 521 | + |
| 522 | + transfer_tx.add_hbar_transfer(account_id_sender, Hbar(-500)) |
| 523 | + transfer_tx.add_hbar_transfer(account_id_recipient, Hbar(500)) |
| 524 | + |
| 525 | + sender_transfer = next( |
| 526 | + t for t in transfer_tx.hbar_transfers if t.account_id == account_id_sender |
| 527 | + ) |
| 528 | + recipient_transfer = next( |
| 529 | + t for t in transfer_tx.hbar_transfers if t.account_id == account_id_recipient |
| 530 | + ) |
| 531 | + |
| 532 | + assert sender_transfer.amount == -50_000_000_000 |
| 533 | + assert recipient_transfer.amount == 50_000_000_000 |
| 534 | + |
| 535 | + |
| 536 | +def test_add_hbar_transfer_with_hbar_tinybars(mock_account_ids): |
| 537 | + """Test adding HBAR transfers with Hbar objects in TINYBAR units.""" |
| 538 | + account_id_sender, account_id_recipient, _, _, _ = mock_account_ids |
| 539 | + transfer_tx = TransferTransaction() |
| 540 | + |
| 541 | + transfer_tx.add_hbar_transfer(account_id_sender, Hbar(-500, HbarUnit.TINYBAR)) |
| 542 | + transfer_tx.add_hbar_transfer(account_id_recipient, Hbar(500, HbarUnit.TINYBAR)) |
| 543 | + |
| 544 | + sender_transfer = next( |
| 545 | + t for t in transfer_tx.hbar_transfers if t.account_id == account_id_sender |
| 546 | + ) |
| 547 | + recipient_transfer = next( |
| 548 | + t for t in transfer_tx.hbar_transfers if t.account_id == account_id_recipient |
| 549 | + ) |
| 550 | + |
| 551 | + assert sender_transfer.amount == -500 |
| 552 | + assert recipient_transfer.amount == 500 |
| 553 | + |
| 554 | + |
| 555 | +def test_add_approved_hbar_transfer_with_hbar_object(mock_account_ids): |
| 556 | + """Test adding approved HBAR transfers with Hbar objects.""" |
| 557 | + account_id_sender, _, _, _, _ = mock_account_ids |
| 558 | + transfer_tx = TransferTransaction() |
| 559 | + |
| 560 | + transfer_tx.add_approved_hbar_transfer(account_id_sender, Hbar(1000)) |
| 561 | + |
| 562 | + transfer = transfer_tx.hbar_transfers[0] |
| 563 | + assert transfer.account_id == account_id_sender |
| 564 | + assert transfer.amount == 100_000_000_000 |
| 565 | + assert transfer.is_approved is True |
| 566 | + |
| 567 | + |
| 568 | +def test_hbar_accumulation_with_mixed_int_and_hbar(mock_account_ids): |
| 569 | + """Test that HBAR transfers accumulate correctly with mixed int and Hbar inputs.""" |
| 570 | + account_id_1, _, _, _, _ = mock_account_ids |
| 571 | + transfer_tx = TransferTransaction() |
| 572 | + |
| 573 | + transfer_tx.add_hbar_transfer(account_id_1, 100) |
| 574 | + transfer_tx.add_hbar_transfer(account_id_1, Hbar(1)) |
| 575 | + transfer_tx.add_hbar_transfer(account_id_1, -50) |
| 576 | + |
| 577 | + transfer = transfer_tx.hbar_transfers[0] |
| 578 | + assert transfer.amount == 100 + 100_000_000 - 50 |
| 579 | + |
| 580 | + |
| 581 | +def test_zero_hbar_value_validation(mock_account_ids): |
| 582 | + """Test that zero Hbar amounts are properly rejected.""" |
| 583 | + account_id_1, _, _, _, _ = mock_account_ids |
| 584 | + transfer_tx = TransferTransaction() |
| 585 | + |
| 586 | + with pytest.raises(ValueError, match="Amount must be a non-zero value"): |
| 587 | + transfer_tx.add_hbar_transfer(account_id_1, Hbar(0)) |
| 588 | + |
| 589 | + with pytest.raises(ValueError, match="Amount must be a non-zero value"): |
| 590 | + transfer_tx.add_hbar_transfer(account_id_1, 0) |
| 591 | + |
| 592 | + |
| 593 | +def test_add_hbar_transfer_with_various_hbar_units(mock_account_ids): |
| 594 | + """Test adding HBAR transfers with various Hbar units.""" |
| 595 | + account_id_1, _, _, _, _ = mock_account_ids |
| 596 | + transfer_tx = TransferTransaction() |
| 597 | + |
| 598 | + transfer_tx.add_hbar_transfer(account_id_1, Hbar(1, HbarUnit.HBAR)) |
| 599 | + transfer_tx.add_hbar_transfer(account_id_1, Hbar(1000, HbarUnit.MICROBAR)) |
| 600 | + transfer_tx.add_hbar_transfer(account_id_1, Hbar(100, HbarUnit.MILLIBAR)) |
| 601 | + |
| 602 | + transfer = transfer_tx.hbar_transfers[0] |
| 603 | + assert transfer.amount == 100_000_000 + 100_000 + 10_000_000 |
| 604 | + |
| 605 | + |
| 606 | +def test_invalid_amount_type_hbar_transfer(mock_account_ids): |
| 607 | + """Test that invalid amount types raise TypeError (covers type checking).""" |
| 608 | + account_id_1, _, _, _, _ = mock_account_ids |
| 609 | + transfer_tx = TransferTransaction() |
| 610 | + |
| 611 | + with pytest.raises(TypeError, match="amount must be an int or Hbar instance"): |
| 612 | + transfer_tx.add_hbar_transfer(account_id_1, "invalid") |
| 613 | + |
| 614 | + with pytest.raises(TypeError, match="amount must be an int or Hbar instance"): |
| 615 | + transfer_tx.add_hbar_transfer(account_id_1, 123.45) |
0 commit comments