|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hiero_sdk_python.account.account_id import AccountId |
| 4 | +from hiero_sdk_python.hapi.services.custom_fees_pb2 import AssessedCustomFee as AssessedCustomFeeProto |
| 5 | +from hiero_sdk_python.tokens.assessed_custom_fee import AssessedCustomFee |
| 6 | +from hiero_sdk_python.tokens.token_id import TokenId |
| 7 | + |
| 8 | + |
| 9 | +pytestmark = pytest.mark.unit |
| 10 | + |
| 11 | + |
| 12 | +# If conftest.py has fixtures like sample_account_id or sample_token_id, use them. |
| 13 | +# Otherwise, define simple ones here (adjust shard/realm/num as needed for realism). |
| 14 | +@pytest.fixture |
| 15 | +def sample_account_id() -> AccountId: |
| 16 | + return AccountId(shard=0, realm=0, num=123456) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def sample_token_id() -> TokenId: |
| 21 | + return TokenId(shard=0, realm=0, num=789012) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def another_account_id() -> AccountId: |
| 26 | + return AccountId(shard=0, realm=0, num=999999) |
| 27 | + |
| 28 | + |
| 29 | +def test_constructor_all_fields( |
| 30 | + sample_account_id: AccountId, |
| 31 | + sample_token_id: TokenId, |
| 32 | + another_account_id: AccountId, |
| 33 | +): |
| 34 | + payers = [sample_account_id, another_account_id] |
| 35 | + fee = AssessedCustomFee( |
| 36 | + amount=1_500_000_000, |
| 37 | + token_id=sample_token_id, |
| 38 | + fee_collector_account_id=sample_account_id, |
| 39 | + effective_payer_account_ids=payers, |
| 40 | + ) |
| 41 | + assert fee.amount == 1_500_000_000 |
| 42 | + assert fee.token_id == sample_token_id |
| 43 | + assert fee.fee_collector_account_id == sample_account_id |
| 44 | + assert fee.effective_payer_account_ids == payers |
| 45 | + # Protect against breaking changes |
| 46 | + assert hasattr(fee, 'amount') |
| 47 | + assert hasattr(fee, 'token_id') |
| 48 | + assert hasattr(fee, 'fee_collector_account_id') |
| 49 | + assert hasattr(fee, 'effective_payer_account_ids') |
| 50 | + |
| 51 | + |
| 52 | +def test_constructor_hbar_case(sample_account_id: AccountId): |
| 53 | + fee = AssessedCustomFee( |
| 54 | + amount=100_000_000, |
| 55 | + token_id=None, |
| 56 | + fee_collector_account_id=sample_account_id, |
| 57 | + ) |
| 58 | + assert fee.amount == 100_000_000 |
| 59 | + assert fee.token_id is None |
| 60 | + assert fee.fee_collector_account_id == sample_account_id |
| 61 | + assert fee.effective_payer_account_ids == [] |
| 62 | + |
| 63 | + |
| 64 | +def test_constructor_empty_payers(sample_account_id: AccountId, sample_token_id: TokenId): |
| 65 | + fee = AssessedCustomFee( |
| 66 | + amount=420, |
| 67 | + token_id=sample_token_id, |
| 68 | + fee_collector_account_id=sample_account_id, |
| 69 | + effective_payer_account_ids=[], |
| 70 | + ) |
| 71 | + assert fee.effective_payer_account_ids == [] |
| 72 | + assert fee.token_id == sample_token_id |
| 73 | + |
| 74 | +def test_constructor_missing_fee_collector_raises(): |
| 75 | + """Verify that omitting fee_collector_account_id raises ValueError.""" |
| 76 | + with pytest.raises(ValueError, match="fee_collector_account_id is required"): |
| 77 | + AssessedCustomFee( |
| 78 | + amount=100, |
| 79 | + token_id=None, |
| 80 | + fee_collector_account_id=None, |
| 81 | + ) |
| 82 | + |
| 83 | +def test_from_proto_missing_token_id(sample_account_id: AccountId): |
| 84 | + """Verify that absence of token_id in protobuf correctly maps to None.""" |
| 85 | + proto = AssessedCustomFeeProto( |
| 86 | + amount=750_000, |
| 87 | + fee_collector_account_id=sample_account_id._to_proto(), |
| 88 | + # intentionally no token_id → proto.HasField("token_id") should be False |
| 89 | + ) |
| 90 | + |
| 91 | + fee = AssessedCustomFee._from_proto(proto) |
| 92 | + |
| 93 | + assert fee.amount == 750_000 |
| 94 | + assert fee.token_id is None, "token_id should be None when not present in proto" |
| 95 | + assert fee.fee_collector_account_id == sample_account_id |
| 96 | + assert fee.effective_payer_account_ids == [], "effective payers should default to empty list" |
| 97 | + |
| 98 | +def test_from_proto_with_token_id(sample_account_id: AccountId, sample_token_id: TokenId): |
| 99 | + """Verify that token_id is correctly deserialized when present in proto.""" |
| 100 | + proto = AssessedCustomFeeProto( |
| 101 | + amount=500_000, |
| 102 | + token_id=sample_token_id._to_proto(), |
| 103 | + fee_collector_account_id=sample_account_id._to_proto(), |
| 104 | + ) |
| 105 | + proto.effective_payer_account_id.append(sample_account_id._to_proto()) |
| 106 | + |
| 107 | + fee = AssessedCustomFee._from_proto(proto) |
| 108 | + |
| 109 | + assert fee.amount == 500_000 |
| 110 | + assert fee.token_id is not None |
| 111 | + assert fee.token_id == sample_token_id |
| 112 | + assert fee.fee_collector_account_id == sample_account_id |
| 113 | + assert len(fee.effective_payer_account_ids) == 1 |
| 114 | + |
| 115 | +def test_from_proto_missing_fee_collector_raises(): |
| 116 | + """Verify that missing fee_collector_account_id in proto raises ValueError.""" |
| 117 | + proto = AssessedCustomFeeProto(amount=750_000) |
| 118 | + with pytest.raises(ValueError, match="fee_collector_account_id is required"): |
| 119 | + AssessedCustomFee._from_proto(proto) |
| 120 | + |
| 121 | +def test_to_proto_basic_fields( |
| 122 | + sample_account_id: AccountId, |
| 123 | + sample_token_id: TokenId, |
| 124 | + another_account_id: AccountId, |
| 125 | +): |
| 126 | + """Verify that all basic fields are correctly serialized to protobuf.""" |
| 127 | + payers = [sample_account_id, another_account_id] |
| 128 | + |
| 129 | + fee = AssessedCustomFee( |
| 130 | + amount=2_000_000, |
| 131 | + token_id=sample_token_id, |
| 132 | + fee_collector_account_id=sample_account_id, |
| 133 | + effective_payer_account_ids=payers, |
| 134 | + ) |
| 135 | + |
| 136 | + proto = fee._to_proto() |
| 137 | + |
| 138 | + # Core presence and value checks |
| 139 | + assert proto.amount == 2_000_000 |
| 140 | + assert proto.HasField("token_id"), "token_id should be set when present" |
| 141 | + assert proto.HasField("fee_collector_account_id") |
| 142 | + assert len(proto.effective_payer_account_id) == 2, "should serialize both effective payers" |
| 143 | + |
| 144 | + # Deeper structural checks (helps catch broken _to_proto implementations) |
| 145 | + assert proto.token_id.shardNum == sample_token_id.shard |
| 146 | + assert proto.token_id.realmNum == sample_token_id.realm |
| 147 | + assert proto.token_id.tokenNum == sample_token_id.num |
| 148 | + |
| 149 | + # Optional: verify collector (often useful when debugging) |
| 150 | + assert proto.fee_collector_account_id.shardNum == sample_account_id.shard |
| 151 | + assert proto.fee_collector_account_id.realmNum == sample_account_id.realm |
| 152 | + assert proto.fee_collector_account_id.accountNum == sample_account_id.num |
| 153 | + |
| 154 | + |
| 155 | +def test_round_trip_conversion( |
| 156 | + sample_account_id: AccountId, |
| 157 | + sample_token_id: TokenId, |
| 158 | +): |
| 159 | + original = AssessedCustomFee( |
| 160 | + amount=987_654_321, |
| 161 | + token_id=sample_token_id, |
| 162 | + fee_collector_account_id=sample_account_id, |
| 163 | + effective_payer_account_ids=[sample_account_id], |
| 164 | + ) |
| 165 | + |
| 166 | + proto = original._to_proto() |
| 167 | + reconstructed = AssessedCustomFee._from_proto(proto) |
| 168 | + |
| 169 | + assert reconstructed.amount == original.amount |
| 170 | + assert reconstructed.token_id == original.token_id |
| 171 | + assert reconstructed.fee_collector_account_id == original.fee_collector_account_id |
| 172 | + assert reconstructed.effective_payer_account_ids == original.effective_payer_account_ids |
| 173 | + |
| 174 | + |
| 175 | +def test_str_contains_expected_fields( |
| 176 | + sample_account_id: AccountId, |
| 177 | + sample_token_id: TokenId, |
| 178 | +): |
| 179 | + fee = AssessedCustomFee( |
| 180 | + amount=5_000_000, |
| 181 | + token_id=sample_token_id, |
| 182 | + fee_collector_account_id=sample_account_id, |
| 183 | + effective_payer_account_ids=[sample_account_id], |
| 184 | + ) |
| 185 | + |
| 186 | + s = str(fee) |
| 187 | + assert "AssessedCustomFee" in s |
| 188 | + assert "amount=5000000" in s |
| 189 | + assert str(sample_token_id) in s |
| 190 | + assert str(sample_account_id) in s |
| 191 | + assert "effective_payer_account_ids" in s |
| 192 | + |
| 193 | + # HBAR case |
| 194 | + hbar_fee = AssessedCustomFee( |
| 195 | + amount=123_456, |
| 196 | + fee_collector_account_id=sample_account_id, |
| 197 | + ) |
| 198 | + hbar_str = str(hbar_fee) |
| 199 | + assert "token_id=None" in hbar_str |
0 commit comments