Skip to content

Commit 347b0e2

Browse files
committed
Fix: Integration tests fail after changes to client and node behavior; aggregate fetching could result in unhandled parsing errors
Solution: Adjust integration tests for current functionality; add raise_for_status to GET aggregates requests
1 parent bb208e7 commit 347b0e2

File tree

6 files changed

+83
-67
lines changed

6 files changed

+83
-67
lines changed

src/aleph/sdk/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ async def fetch_aggregate(self, address: str, key: str) -> Dict[str, Dict]:
475475
async with self.http_session.get(
476476
f"/api/v0/aggregates/{address}.json", params=params
477477
) as resp:
478+
resp.raise_for_status()
478479
result = await resp.json()
479480
data = result.get("data", dict())
480481
return data.get(key)
@@ -491,6 +492,7 @@ async def fetch_aggregates(
491492
f"/api/v0/aggregates/{address}.json",
492493
params=params,
493494
) as resp:
495+
resp.raise_for_status()
494496
result = await resp.json()
495497
data = result.get("data", dict())
496498
return data

tests/integration/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
TARGET_NODE = "http://163.172.70.92:4024"
1+
TARGET_NODE = "https://api1.aleph.im"
22
REFERENCE_NODE = "https://api2.aleph.im"
33
TEST_CHANNEL = "INTEGRATION_TESTS"

tests/integration/itest_forget.py

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
from typing import Callable, Dict
1+
import asyncio
2+
from typing import Tuple
23

34
import pytest
4-
from aleph_message.models import PostMessage
5+
from aleph_message.models import ItemHash
56

67
from aleph.sdk.client import AuthenticatedAlephClient
78
from aleph.sdk.models.message import MessageFilter
89
from aleph.sdk.types import Account
910

1011
from .config import REFERENCE_NODE, TARGET_NODE, TEST_CHANNEL
11-
from .toolkit import try_until
12+
from .toolkit import has_messages, has_no_messages, try_until
1213

1314

1415
async def create_and_forget_post(
1516
account: Account, emitter_node: str, receiver_node: str, channel=TEST_CHANNEL
16-
) -> str:
17-
async def wait_matching_posts(
18-
item_hash: str,
19-
condition: Callable[[Dict], bool],
20-
timeout: int = 5,
21-
):
22-
async with AuthenticatedAlephClient(
23-
account=account, api_server=receiver_node
24-
) as rx_session:
25-
return await try_until(
26-
rx_session.get_posts,
27-
condition,
28-
timeout=timeout,
29-
hashes=[item_hash],
30-
)
31-
17+
) -> Tuple[ItemHash, ItemHash]:
3218
async with AuthenticatedAlephClient(
3319
account=account, api_server=emitter_node
3420
) as tx_session:
@@ -38,13 +24,17 @@ async def wait_matching_posts(
3824
channel="INTEGRATION_TESTS",
3925
)
4026

41-
# Wait for the message to appear on the receiver. We don't check the values,
42-
# they're checked in other integration tests.
43-
get_post_response = await wait_matching_posts(
44-
post_message.item_hash,
45-
lambda response: len(response["posts"]) > 0,
46-
)
47-
print(get_post_response)
27+
async with AuthenticatedAlephClient(
28+
account=account, api_server=receiver_node
29+
) as rx_session:
30+
await try_until(
31+
rx_session.get_messages,
32+
has_messages,
33+
timeout=5,
34+
message_filter=MessageFilter(
35+
hashes=[post_message.item_hash],
36+
),
37+
)
4838

4939
post_hash = post_message.item_hash
5040
reason = "This well thought-out content offends me!"
@@ -56,27 +46,34 @@ async def wait_matching_posts(
5646
reason=reason,
5747
channel=channel,
5848
)
59-
6049
assert forget_message.sender == account.get_address()
6150
assert forget_message.content.reason == reason
6251
assert forget_message.content.hashes == [post_hash]
63-
64-
print(forget_message)
52+
forget_hash = forget_message.item_hash
6553

6654
# Wait until the message is forgotten
67-
forgotten_posts = await wait_matching_posts(
68-
post_hash,
69-
lambda response: "forgotten_by" in response["posts"][0],
70-
timeout=15,
71-
)
55+
async with AuthenticatedAlephClient(
56+
account=account, api_server=receiver_node
57+
) as rx_session:
58+
await try_until(
59+
rx_session.get_messages,
60+
has_messages,
61+
timeout=5,
62+
message_filter=MessageFilter(
63+
hashes=[forget_hash],
64+
),
65+
)
7266

73-
assert len(forgotten_posts["posts"]) == 1
74-
forgotten_post = forgotten_posts["posts"][0]
75-
assert forgotten_post["forgotten_by"] == [forget_message.item_hash]
76-
assert forgotten_post["item_content"] is None
77-
print(forgotten_post)
67+
await try_until(
68+
rx_session.get_messages,
69+
has_no_messages,
70+
timeout=5,
71+
message_filter=MessageFilter(
72+
hashes=[post_hash],
73+
),
74+
)
7875

79-
return post_hash
76+
return post_hash, forget_hash
8077

8178

8279
@pytest.mark.asyncio
@@ -85,7 +82,7 @@ async def test_create_and_forget_post_on_target(fixture_account):
8582
Create a post on the target node, then forget it and check that the change is propagated
8683
to the reference node.
8784
"""
88-
_ = await create_and_forget_post(fixture_account, TARGET_NODE, REFERENCE_NODE)
85+
_, _ = await create_and_forget_post(fixture_account, TARGET_NODE, REFERENCE_NODE)
8986

9087

9188
@pytest.mark.asyncio
@@ -94,7 +91,7 @@ async def test_create_and_forget_post_on_reference(fixture_account):
9491
Create a post on the reference node, then forget it and check that the change is propagated
9592
to the target node.
9693
"""
97-
_ = await create_and_forget_post(fixture_account, REFERENCE_NODE, TARGET_NODE)
94+
_, _ = await create_and_forget_post(fixture_account, REFERENCE_NODE, TARGET_NODE)
9895

9996

10097
@pytest.mark.asyncio
@@ -104,26 +101,33 @@ async def test_forget_a_forget_message(fixture_account):
104101
"""
105102

106103
# TODO: this test should be moved to the PyAleph API tests, once a framework is in place.
107-
post_hash = await create_and_forget_post(fixture_account, TARGET_NODE, TARGET_NODE)
104+
post_hash, forget_hash = await create_and_forget_post(
105+
fixture_account, TARGET_NODE, REFERENCE_NODE
106+
)
108107
async with AuthenticatedAlephClient(
109108
account=fixture_account, api_server=TARGET_NODE
110-
) as session:
111-
get_post_message: PostMessage = await session.get_message(post_hash)
112-
113-
forget_message_hash = get_post_message.forgotten_by[0]
114-
forget_message, forget_status = await session.forget(
115-
hashes=[forget_message_hash],
109+
) as tx_session:
110+
forget_message, forget_status = await tx_session.forget(
111+
hashes=[forget_hash],
116112
reason="I want to remember this post. Maybe I can forget I forgot it?",
117113
channel=TEST_CHANNEL,
118114
)
119115

120116
print(forget_message)
121117

122-
get_forget_message_response = await session.get_messages(
118+
# wait 5 seconds
119+
await asyncio.sleep(5)
120+
121+
async with AuthenticatedAlephClient(
122+
account=fixture_account, api_server=REFERENCE_NODE
123+
) as rx_session:
124+
get_forget_message_response = await try_until(
125+
rx_session.get_messages,
126+
has_messages,
127+
timeout=5,
123128
message_filter=MessageFilter(
124-
hashes=[forget_message_hash],
125-
channels=[TEST_CHANNEL],
126-
)
129+
hashes=[forget_hash],
130+
),
127131
)
128132
assert len(get_forget_message_response.messages) == 1
129133
forget_message = get_forget_message_response.messages[0]

tests/integration/itest_posts.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
import pytest
2-
from aleph_message.models import MessagesResponse
32

43
from aleph.sdk.client import AuthenticatedAlephClient
5-
from tests.integration.toolkit import try_until
4+
from aleph.sdk.models.message import MessageFilter
5+
from tests.integration.toolkit import has_messages, try_until
66

77
from .config import REFERENCE_NODE, TARGET_NODE
88

99

10-
async def create_message_on_target(
11-
fixture_account, emitter_node: str, receiver_node: str
12-
):
10+
async def create_message_on_target(account, emitter_node: str, receiver_node: str):
1311
"""
1412
Create a POST message on the target node, then fetch it from the reference node.
1513
"""
1614
async with AuthenticatedAlephClient(
17-
account=fixture_account, api_server=emitter_node
15+
account=account, api_server=emitter_node
1816
) as tx_session:
1917
post_message, message_status = await tx_session.create_post(
2018
post_content=None,
2119
post_type="POST",
2220
channel="INTEGRATION_TESTS",
2321
)
2422

25-
def response_contains_messages(response: MessagesResponse) -> bool:
26-
return len(response.messages) > 0
27-
2823
async with AuthenticatedAlephClient(
29-
account=fixture_account, api_server=receiver_node
24+
account=account, api_server=receiver_node
3025
) as rx_session:
3126
responses = await try_until(
3227
rx_session.get_messages,
33-
response_contains_messages,
28+
has_messages,
3429
timeout=5,
35-
hashes=[post_message.item_hash],
30+
message_filter=MessageFilter(
31+
hashes=[post_message.item_hash],
32+
),
3633
)
3734

3835
message_from_target = responses.messages[0]

tests/integration/toolkit.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
import time
33
from typing import Awaitable, Callable, TypeVar
44

5+
from aleph.sdk.models.message import MessagesResponse
6+
57
T = TypeVar("T")
68

79

810
async def try_until(
911
coroutine: Callable[..., Awaitable[T]],
1012
condition: Callable[[T], bool],
1113
timeout: float,
12-
time_between_attempts: float = 0.5,
14+
time_between_attempts: float = 1,
1315
*args,
1416
**kwargs,
1517
) -> T:
@@ -23,3 +25,11 @@ async def try_until(
2325
await asyncio.sleep(time_between_attempts)
2426
else:
2527
raise TimeoutError(f"No success in {timeout} seconds.")
28+
29+
30+
def has_messages(response: MessagesResponse) -> bool:
31+
return len(response.messages) > 0
32+
33+
34+
def has_no_messages(response: MessagesResponse) -> bool:
35+
return len(response.messages) == 0

tests/unit/test_asynchronous_get.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
2323
def status(self):
2424
return 200
2525

26+
def raise_for_status(self):
27+
...
28+
2629
async def json(self):
2730
return get_return_value
2831

0 commit comments

Comments
 (0)