Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed Oct 6, 2023
1 parent d046e4d commit cf8c060
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
65 changes: 33 additions & 32 deletions tests/integration/itest_forget.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import asyncio
from typing import Tuple

import pytest
from aleph_message.models import PostMessage
from aleph_message.models import ItemHash

from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.models.message import MessageFilter
from aleph.sdk.types import Account

from .config import REFERENCE_NODE, TARGET_NODE, TEST_CHANNEL
from .toolkit import try_until
from .toolkit import has_messages, has_no_messages, try_until


async def create_and_forget_post(
account: Account, emitter_node: str, receiver_node: str, channel=TEST_CHANNEL
) -> str:
) -> Tuple[ItemHash, ItemHash]:
async with AuthenticatedAlephClient(
account=account, api_server=emitter_node
) as tx_session:
Expand All @@ -26,7 +29,7 @@ async def create_and_forget_post(
) as rx_session:
await try_until(
rx_session.get_messages,
lambda response: len(response.messages) > 0,
has_messages,
timeout=5,
message_filter=MessageFilter(
hashes=[post_message.item_hash],
Expand All @@ -46,40 +49,31 @@ async def create_and_forget_post(
assert forget_message.sender == account.get_address()
assert forget_message.content.reason == reason
assert forget_message.content.hashes == [post_hash]
forget_hash = forget_message.item_hash

# Wait until the message is forgotten
async with AuthenticatedAlephClient(
account=account, api_server=receiver_node
) as rx_session:
await try_until(
rx_session.get_messages,
lambda response: len(response.messages) > 0,
has_messages,
timeout=5,
message_filter=MessageFilter(
hashes=[forget_message.item_hash],
hashes=[forget_hash],
),
)

async with AuthenticatedAlephClient(
account=account, api_server=receiver_node
) as rx_session:
forgotten_messages = await try_until(
await try_until(
rx_session.get_messages,
lambda response: len(response.messages) == 0,
has_no_messages,
timeout=5,
message_filter=MessageFilter(
hashes=[post_hash],
),
)
# TODO: Should we now receive any messages? It seems they are completely gone

assert len(forgotten_messages.messages) == 1
forgotten_post = forgotten_messages.messages[0]
assert forgotten_post.forgotten_by == [forget_message.item_hash]
assert forgotten_post.item_content is None
print(forgotten_post)

return post_hash
return post_hash, forget_hash


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


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


@pytest.mark.asyncio
Expand All @@ -107,26 +101,33 @@ async def test_forget_a_forget_message(fixture_account):
"""

# TODO: this test should be moved to the PyAleph API tests, once a framework is in place.
post_hash = await create_and_forget_post(fixture_account, TARGET_NODE, TARGET_NODE)
post_hash, forget_hash = await create_and_forget_post(
fixture_account, TARGET_NODE, REFERENCE_NODE
)
async with AuthenticatedAlephClient(
account=fixture_account, api_server=TARGET_NODE
) as session:
get_post_message: PostMessage = await session.get_message(post_hash)

forget_message_hash = get_post_message.forgotten_by[0]
forget_message, forget_status = await session.forget(
hashes=[forget_message_hash],
) as tx_session:
forget_message, forget_status = await tx_session.forget(
hashes=[forget_hash],
reason="I want to remember this post. Maybe I can forget I forgot it?",
channel=TEST_CHANNEL,
)

print(forget_message)

get_forget_message_response = await session.get_messages(
# wait 5 seconds
await asyncio.sleep(5)

async with AuthenticatedAlephClient(
account=fixture_account, api_server=REFERENCE_NODE
) as rx_session:
get_forget_message_response = await try_until(
rx_session.get_messages,
has_messages,
timeout=5,
message_filter=MessageFilter(
hashes=[forget_message_hash],
channels=[TEST_CHANNEL],
)
hashes=[forget_hash],
),
)
assert len(get_forget_message_response.messages) == 1
forget_message = get_forget_message_response.messages[0]
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/itest_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.models.message import MessageFilter
from tests.integration.toolkit import try_until
from tests.integration.toolkit import has_messages, try_until

from .config import REFERENCE_NODE, TARGET_NODE

Expand All @@ -25,7 +25,7 @@ async def create_message_on_target(account, emitter_node: str, receiver_node: st
) as rx_session:
responses = await try_until(
rx_session.get_messages,
lambda response: len(response.messages) > 0,
has_messages,
timeout=5,
message_filter=MessageFilter(
hashes=[post_message.item_hash],
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import time
from typing import Awaitable, Callable, TypeVar

from aleph.sdk.models.message import MessagesResponse

T = TypeVar("T")


Expand All @@ -23,3 +25,11 @@ async def try_until(
await asyncio.sleep(time_between_attempts)
else:
raise TimeoutError(f"No success in {timeout} seconds.")


def has_messages(response: MessagesResponse) -> bool:
return len(response.messages) > 0


def has_no_messages(response: MessagesResponse) -> bool:
return len(response.messages) == 0

0 comments on commit cf8c060

Please sign in to comment.