Skip to content

Commit 45c7b48

Browse files
committed
feat: new test: test_delete_instance_with_insufficient_funds
1 parent b6866e4 commit 45c7b48

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/unit/test_instance.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import pytest
1010
from aiohttp import InvalidURL
11+
from aleph.sdk.exceptions import InsufficientFundsError
12+
from aleph.sdk.types import TokenType
1113
from aleph_message.models import Chain, ItemHash
1214
from aleph_message.models.execution.base import Payment, PaymentType
1315
from aleph_message.models.execution.environment import (
@@ -16,6 +18,7 @@
1618
HypervisorType,
1719
)
1820
from multidict import CIMultiDict, CIMultiDictProxy
21+
from typer import Exit
1922

2023
from aleph_client.commands import help_strings
2124
from aleph_client.commands.instance import (
@@ -556,11 +559,18 @@ async def test_delete_instance():
556559
mock_auth_client_class, mock_auth_client = create_mock_auth_client(mock_account)
557560
mock_fetch_vm_info = create_mock_fetch_vm_info()
558561
mock_vm_client_class, mock_vm_client = create_mock_vm_client()
562+
mock_fetch_settings = AsyncMock(
563+
return_value={
564+
"community_wallet_timestamp": 900000, # Before creation time
565+
"community_wallet_address": "0xcommunity",
566+
}
567+
)
559568

560569
@patch("aleph_client.commands.instance._load_account", mock_load_account)
561570
@patch("aleph_client.commands.instance.AuthenticatedAlephHttpClient", mock_auth_client_class)
562571
@patch("aleph_client.commands.instance.fetch_vm_info", mock_fetch_vm_info)
563572
@patch("aleph_client.commands.instance.VmClient", mock_vm_client_class)
573+
@patch("aleph_client.commands.instance.fetch_settings", mock_fetch_settings)
564574
@patch.object(asyncio, "sleep", AsyncMock())
565575
async def delete_instance():
566576
print() # For better display when pytest -v -s
@@ -573,6 +583,46 @@ async def delete_instance():
573583
await delete_instance()
574584

575585

586+
@pytest.mark.asyncio
587+
async def test_delete_instance_with_insufficient_funds():
588+
589+
mock_load_account = create_mock_load_account()
590+
mock_account = mock_load_account.return_value
591+
592+
# Configure manage_flow to raise InsufficientFundsError
593+
insufficient_funds_error = InsufficientFundsError(
594+
token_type=TokenType.GAS, required_funds=10.0, available_funds=5.0
595+
)
596+
mock_account.manage_flow = AsyncMock(side_effect=insufficient_funds_error)
597+
598+
mock_auth_client_class, mock_auth_client = create_mock_auth_client(mock_account)
599+
mock_fetch_vm_info = create_mock_fetch_vm_info()
600+
mock_vm_client_class, mock_vm_client = create_mock_vm_client()
601+
mock_fetch_settings = AsyncMock(
602+
return_value={
603+
"community_wallet_timestamp": 900000, # Before creation time
604+
"community_wallet_address": "0xcommunity",
605+
}
606+
)
607+
608+
@patch("aleph_client.commands.instance._load_account", mock_load_account)
609+
@patch("aleph_client.commands.instance.AuthenticatedAlephHttpClient", mock_auth_client_class)
610+
@patch("aleph_client.commands.instance.fetch_vm_info", mock_fetch_vm_info)
611+
@patch("aleph_client.commands.instance.VmClient", mock_vm_client_class)
612+
@patch("aleph_client.commands.instance.fetch_settings", mock_fetch_settings)
613+
@patch.object(asyncio, "sleep", AsyncMock())
614+
async def delete_instance_with_insufficient_funds():
615+
print() # For better display when pytest -v -s
616+
with pytest.raises(Exit): # We expect Exit to be raised
617+
await delete(FAKE_VM_HASH)
618+
mock_auth_client.get_message.assert_called_once()
619+
mock_vm_client.erase_instance.assert_called_once()
620+
mock_account.manage_flow.assert_called_once() # First manage_flow call raises the error
621+
mock_auth_client.forget.assert_not_called() # This should not be called as we exit early
622+
623+
await delete_instance_with_insufficient_funds()
624+
625+
576626
@pytest.mark.asyncio
577627
async def test_reboot_instance():
578628
mock_load_account = create_mock_load_account()

0 commit comments

Comments
 (0)