|
4 | 4 |
|
5 | 5 | from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction |
6 | 6 | from hiero_sdk_python.crypto.private_key import PrivateKey |
| 7 | +from hiero_sdk_python.exceptions import PrecheckError |
7 | 8 | from hiero_sdk_python.hbar import Hbar |
8 | 9 | from hiero_sdk_python.response_code import ResponseCode |
9 | 10 | from hiero_sdk_python.tokens.token_associate_transaction import TokenAssociateTransaction |
10 | 11 | from hiero_sdk_python.tokens.token_dissociate_transaction import TokenDissociateTransaction |
11 | | -from tests.integration.utils import IntegrationTestEnv, create_fungible_token |
| 12 | +from tests.integration.utils import create_fungible_token |
| 13 | + |
| 14 | + |
| 15 | +def _create_associated_account(env): |
| 16 | + """Creates an account and associates it with a fungible token.""" |
| 17 | + private_key = PrivateKey.generate() |
| 18 | + |
| 19 | + receipt = ( |
| 20 | + AccountCreateTransaction() |
| 21 | + .set_key_without_alias(private_key) |
| 22 | + .set_initial_balance(Hbar(2)) |
| 23 | + .set_account_memo("Recipient Account") |
| 24 | + .freeze_with(env.client) |
| 25 | + .execute(env.client) |
| 26 | + ) |
| 27 | + |
| 28 | + account_id = receipt.account_id |
| 29 | + token_id = create_fungible_token(env) |
| 30 | + |
| 31 | + associate_tx = TokenAssociateTransaction().set_account_id(account_id).set_token_ids([token_id]) |
| 32 | + associate_tx.freeze_with(env.client) |
| 33 | + associate_tx.sign(private_key) |
| 34 | + |
| 35 | + receipt = associate_tx.execute(env.client) |
| 36 | + |
| 37 | + assert receipt.status == ResponseCode.SUCCESS, ( |
| 38 | + f"Token association failed with status: {ResponseCode(receipt.status).name}" |
| 39 | + ) |
| 40 | + |
| 41 | + return account_id, private_key, token_id |
12 | 42 |
|
13 | 43 |
|
14 | 44 | @pytest.mark.integration |
15 | | -def test_integration_token_dissociate_transaction_can_execute(): |
16 | | - env = IntegrationTestEnv() |
| 45 | +def test_integration_token_dissociate_transaction_can_execute(env): |
| 46 | + """Test token dissociate transaction can be executed successfully.""" |
| 47 | + account_id, account_private_key, token_id = _create_associated_account(env) |
17 | 48 |
|
18 | | - try: |
19 | | - new_account_private_key = PrivateKey.generate() |
20 | | - new_account_public_key = new_account_private_key.public_key() |
| 49 | + dissociate_transaction = TokenDissociateTransaction(account_id=account_id, token_ids=[token_id]) |
| 50 | + dissociate_transaction.freeze_with(env.client) |
| 51 | + dissociate_transaction.sign(account_private_key) |
21 | 52 |
|
22 | | - initial_balance = Hbar(2) |
| 53 | + receipt = dissociate_transaction.execute(env.client) |
23 | 54 |
|
24 | | - assert initial_balance.to_tinybars() == 200000000 |
| 55 | + assert receipt.status == ResponseCode.SUCCESS, ( |
| 56 | + f"Token dissociation failed with status: {ResponseCode(receipt.status).name}" |
| 57 | + ) |
25 | 58 |
|
26 | | - account_transaction = AccountCreateTransaction( |
27 | | - key=new_account_public_key, initial_balance=initial_balance, memo="Recipient Account" |
28 | | - ) |
29 | 59 |
|
30 | | - account_transaction.freeze_with(env.client) |
31 | | - account_receipt = account_transaction.execute(env.client) |
32 | | - new_account_id = account_receipt.account_id |
| 60 | +def test_token_dissociate_transaction_can_execute_with_no_tokensId(env): |
| 61 | + """Test token dissociate transaction can be executed without setting the tokenIds.""" |
| 62 | + account_id, account_private_key, _ = _create_associated_account(env) |
33 | 63 |
|
34 | | - token_id = create_fungible_token(env) |
| 64 | + dissociate_transaction = TokenDissociateTransaction(account_id=account_id) |
| 65 | + dissociate_transaction.freeze_with(env.client) |
| 66 | + dissociate_transaction.sign(account_private_key) |
35 | 67 |
|
36 | | - associate_transaction = TokenAssociateTransaction(account_id=new_account_id, token_ids=[token_id]) |
37 | | - associate_transaction.freeze_with(env.client) |
38 | | - associate_transaction.sign(new_account_private_key) |
| 68 | + receipt = dissociate_transaction.execute(env.client) |
39 | 69 |
|
40 | | - receipt = associate_transaction.execute(env.client) |
| 70 | + assert receipt.status == ResponseCode.SUCCESS, ( |
| 71 | + f"Token dissociation failed with status: {ResponseCode(receipt.status).name}" |
| 72 | + ) |
41 | 73 |
|
42 | | - assert receipt.status == ResponseCode.SUCCESS, ( |
43 | | - f"Token association failed with status: {ResponseCode(receipt.status).name}" |
44 | | - ) |
45 | 74 |
|
46 | | - dissociate_transaction = TokenDissociateTransaction(account_id=new_account_id, token_ids=[token_id]) |
47 | | - dissociate_transaction.freeze_with(env.client) |
48 | | - dissociate_transaction.sign(new_account_private_key) |
| 75 | +def test_token_dissociate_transaction_raise_error_if_account_id_not_set(env): |
| 76 | + """Test token dissociate transaction raises PrecheckError if accountId not set.""" |
| 77 | + a_, account_private_key, token_id = _create_associated_account(env) |
49 | 78 |
|
50 | | - receipt = dissociate_transaction.execute(env.client) |
| 79 | + dissociate_transaction = TokenDissociateTransaction(token_ids=[token_id]) |
| 80 | + dissociate_transaction.freeze_with(env.client) |
| 81 | + dissociate_transaction.sign(account_private_key) |
51 | 82 |
|
52 | | - assert receipt.status == ResponseCode.SUCCESS, ( |
53 | | - f"Token dissociation failed with status: {ResponseCode(receipt.status).name}" |
54 | | - ) |
55 | | - finally: |
56 | | - env.close() |
| 83 | + with pytest.raises(PrecheckError): |
| 84 | + dissociate_transaction.execute(env.client) |
0 commit comments