Skip to content

Commit 868d72f

Browse files
committed
Add test for handling an InsufficientFundsError while creating an instance.
1 parent 35b47a6 commit 868d72f

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed

tests/unit/conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ def json_messages():
5757
return json.load(f)
5858

5959

60+
@pytest.fixture
61+
def rejected_message():
62+
message_path = Path(__file__).parent / "rejected_message.json"
63+
with open(message_path) as f:
64+
return json.load(f)
65+
66+
6067
@pytest.fixture
6168
def aleph_messages() -> List[AlephMessage]:
6269
return [
@@ -213,3 +220,23 @@ def get(self, *_args, **_kwargs):
213220
client.http_session = http_session
214221

215222
return client
223+
224+
225+
@pytest.fixture
226+
def mock_session_with_rejected_message(ethereum_account, rejected_message) -> AuthenticatedAlephHttpClient:
227+
class MockHttpSession(AsyncMock):
228+
def get(self, *_args, **_kwargs):
229+
return make_custom_mock_response(rejected_message)
230+
231+
def post(self, *_args, **_kwargs):
232+
return make_custom_mock_response({
233+
"message_status": "rejected",
234+
"publication_status": {"status": "success", "failed": []},
235+
}, status=422)
236+
237+
http_session = MockHttpSession()
238+
239+
client = AuthenticatedAlephHttpClient(account=ethereum_account, api_server="http://localhost")
240+
client.http_session = http_session
241+
242+
return client

tests/unit/rejected_message.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"status": "rejected",
3+
"item_hash": "7091b61632e0385adb6978dccbec1da40e9c400ffac7233076a3ad0219f49de0",
4+
"reception_time": "2023-12-03T16:00:53.432607+00:00",
5+
"message": {
6+
"time": 1701619253.309467,
7+
"type": "INSTANCE",
8+
"chain": "ETH",
9+
"sender": "0x48A87924135892bEE2F264547a0D69909b3bA5C6",
10+
"channel": null,
11+
"content": {
12+
"time": 1701619253.3092096,
13+
"rootfs": {
14+
"parent": {
15+
"ref": "549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613",
16+
"use_latest": true
17+
},
18+
"size_mib": 2000,
19+
"persistence": "host"
20+
},
21+
"address": "0x48A87924135892bEE2F264547a0D69909b3bA5C6",
22+
"volumes": [],
23+
"resources": {
24+
"vcpus": 1,
25+
"memory": 256,
26+
"seconds": 30
27+
},
28+
"allow_amend": false,
29+
"environment": {
30+
"internet": true,
31+
"aleph_api": true,
32+
"reproducible": false,
33+
"shared_cache": false
34+
},
35+
"authorized_keys": [
36+
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCdU/B01GaUQ3l8aIt0XR6WajhM/5oTdxsON3b90quwQjH38Ba+Z3z74gf4+f56vxRw7XqtC8pg8zuh2L8+Fo3IHctULJD24YbLr2CagwrPsd9fsK+DsJSrcnTKaBBjvgloQi96hl4hPtlR9jA5bRYked0bFxdbFUMCmF17tpU0AC/dLeH3FwVDfh5dF11SX6ezg8brKd9SHYDRkOBHyOLIOWMtOgIgfEy9OsNdAS9OlOHyKxz4MEl5xkq5lRk9nS1n45A3rrIwi91qamjphHNrEutKaN/ramuW+davCP4xfnEVyu2DmoteDM+lmmf4skN90CwxhlrYpc5xpie5vTTYxgUCSnN0PyYiD0m7YHpZwMg1yHKB+aZghOIYUyKVJ8fIH/uU1DKwqcZ5qmH8sPvB4tCy3QOn7m7xTYVd/R3CECdG/R/55IUfGbg8KoAHBgmQ13/hSJ3IshMYOTWwMenma29D8UqKsyTfiMfof6Yf6NYOeDkAsUSIOEetxcv3MaE= mhh@mhh-TUXEDO"
37+
]
38+
},
39+
"item_hash": "7091b61632e0385adb6978dccbec1da40e9c400ffac7233076a3ad0219f49de0",
40+
"item_type": "inline",
41+
"signature": "0xa8e549c5228b379bede875016ed25215c6cd7c3e9131ec1b3bdb49af4859c8840251362c3c421995f520648026621245b4896c0a88d6a5735e5e2620812f22ff1b",
42+
"item_content": "{\"address\":\"0x48A87924135892bEE2F264547a0D69909b3bA5C6\",\"time\":1701619253.3092096,\"allow_amend\":false,\"authorized_keys\":[\"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCdU/B01GaUQ3l8aIt0XR6WajhM/5oTdxsON3b90quwQjH38Ba+Z3z74gf4+f56vxRw7XqtC8pg8zuh2L8+Fo3IHctULJD24YbLr2CagwrPsd9fsK+DsJSrcnTKaBBjvgloQi96hl4hPtlR9jA5bRYked0bFxdbFUMCmF17tpU0AC/dLeH3FwVDfh5dF11SX6ezg8brKd9SHYDRkOBHyOLIOWMtOgIgfEy9OsNdAS9OlOHyKxz4MEl5xkq5lRk9nS1n45A3rrIwi91qamjphHNrEutKaN/ramuW+davCP4xfnEVyu2DmoteDM+lmmf4skN90CwxhlrYpc5xpie5vTTYxgUCSnN0PyYiD0m7YHpZwMg1yHKB+aZghOIYUyKVJ8fIH/uU1DKwqcZ5qmH8sPvB4tCy3QOn7m7xTYVd/R3CECdG/R/55IUfGbg8KoAHBgmQ13/hSJ3IshMYOTWwMenma29D8UqKsyTfiMfof6Yf6NYOeDkAsUSIOEetxcv3MaE= mhh@mhh-TUXEDO\"],\"environment\":{\"reproducible\":false,\"internet\":true,\"aleph_api\":true,\"shared_cache\":false},\"resources\":{\"vcpus\":1,\"memory\":256,\"seconds\":30},\"volumes\":[],\"rootfs\":{\"parent\":{\"ref\":\"549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613\",\"use_latest\":true},\"persistence\":\"host\",\"size_mib\":2000}}"
43+
},
44+
"error_code": 5,
45+
"details": {
46+
"errors": [
47+
{
48+
"account_balance": "0",
49+
"required_balance": "4213.265726725260407192763523"
50+
}
51+
]
52+
}
53+
}

tests/unit/test_asynchronous.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
import datetime
12
from unittest.mock import AsyncMock
23

34
import pytest as pytest
45
from aleph_message.models import (
56
AggregateMessage,
67
ForgetMessage,
78
InstanceMessage,
9+
MessageType,
810
PostMessage,
911
ProgramMessage,
1012
StoreMessage,
1113
)
14+
from aleph_message.models.execution.environment import MachineResources
1215
from aleph_message.status import MessageStatus
1316

17+
from aleph.sdk.exceptions import InsufficientFundsError
1418
from aleph.sdk.types import StorageEnum
1519

1620

@@ -121,3 +125,78 @@ async def test_forget(mock_session_with_post_success):
121125

122126
assert mock_session_with_post_success.http_session.post.called_once
123127
assert isinstance(forget_message, ForgetMessage)
128+
129+
130+
@pytest.mark.asyncio
131+
@pytest.mark.parametrize(
132+
"message_type, content",
133+
[
134+
(
135+
MessageType.aggregate,
136+
{
137+
"content": {"Hello": datetime.datetime.now()},
138+
"key": "test",
139+
"address": "0x1",
140+
"time": 1.0,
141+
},
142+
),
143+
(
144+
MessageType.aggregate,
145+
{
146+
"content": {"Hello": datetime.date.today()},
147+
"key": "test",
148+
"address": "0x1",
149+
"time": 1.0,
150+
},
151+
),
152+
(
153+
MessageType.aggregate,
154+
{
155+
"content": {"Hello": datetime.time()},
156+
"key": "test",
157+
"address": "0x1",
158+
"time": 1.0,
159+
},
160+
),
161+
(
162+
MessageType.aggregate,
163+
{
164+
"content": {
165+
"Hello": MachineResources(
166+
vcpus=1,
167+
memory=1024,
168+
seconds=1,
169+
)
170+
},
171+
"key": "test",
172+
"address": "0x1",
173+
"time": 1.0,
174+
},
175+
),
176+
],
177+
)
178+
async def test_prepare_aleph_message(
179+
mock_session_with_post_success, message_type, content
180+
):
181+
# Call the function under test
182+
async with mock_session_with_post_success as session:
183+
await session._prepare_aleph_message(
184+
message_type=message_type,
185+
content=content,
186+
channel="TEST",
187+
)
188+
189+
190+
@pytest.mark.asyncio
191+
async def test_create_instance_insufficient_funds_error(
192+
mock_session_with_rejected_message
193+
):
194+
async with mock_session_with_rejected_message as session:
195+
with pytest.raises(InsufficientFundsError):
196+
await session.create_instance(
197+
rootfs="cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe",
198+
rootfs_size=1,
199+
rootfs_name="rootfs",
200+
channel="TEST",
201+
metadata={"tags": ["test"]},
202+
)

tests/unit/test_asynchronous_get.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from aleph_message.models import MessagesResponse, MessageType
66

7-
from aleph.sdk.exceptions import ForgottenMessageError
7+
from aleph.sdk.exceptions import ForgottenMessageError, InvalidMessageError
88
from aleph.sdk.query.filters import MessageFilter, PostFilter
99
from aleph.sdk.query.responses import PostsResponse
1010
from tests.unit.conftest import make_mock_get_session
@@ -85,5 +85,15 @@ async def test_get_forgotten_message():
8585
await session.get_message("cafebabe")
8686

8787

88+
@pytest.mark.asyncio
89+
async def test_get_message_error(rejected_message):
90+
mock_session = make_mock_get_session(
91+
rejected_message
92+
)
93+
async with mock_session as session:
94+
error = await session.get_message_error(rejected_message["item_hash"])
95+
assert error["error_code"] == rejected_message["error_code"]
96+
assert error["details"] == rejected_message["details"]
97+
8898
if __name__ == "__main __":
8999
unittest.main()

0 commit comments

Comments
 (0)