Skip to content

Commit

Permalink
Fix: Solved bad typing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres D. Molins committed Oct 24, 2024
1 parent abe0f95 commit 7cbfce6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/aleph/schemas/pending_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class BasePendingMessage(AlephBaseMessage, Generic[MType, ContentType]):
A raw Aleph message, as sent by users to the Aleph network.
"""

@classmethod
@model_validator(mode="before")
def load_content(cls, values):
@classmethod
def load_content(cls, values: Any):
"""
Preload inline content. We let the CCN populate this field later
on for ipfs and storage item types.
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/toolkit/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def timestamp_to_datetime(timestamp: float) -> dt.datetime:
object.
"""

return pytz.utc.localize(dt.datetime.fromtimestamp(timestamp, dt.UTC))
return pytz.utc.localize(dt.datetime.utcfromtimestamp(timestamp))


def coerce_to_datetime(
Expand Down
30 changes: 25 additions & 5 deletions tests/api/test_list_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ async def test_get_messages(fixture_messages: Sequence[Dict[str, Any]], ccn_api_
@pytest.mark.asyncio
async def test_get_messages_filter_by_channel(fixture_messages, ccn_api_client):
async def fetch_messages_by_channel(channel: str) -> Dict:
response = await ccn_api_client.get(MESSAGES_URI, params={"channels": [channel]})
response = await ccn_api_client.get(
MESSAGES_URI, params={"channels": [channel]}
)
assert response.status == 200, await response.text()
return await response.json()

Expand Down Expand Up @@ -192,7 +194,9 @@ async def test_get_messages_filter_by_tags(
assert len(messages) == 2

# Matching tags for both messages
response = await ccn_api_client.get(MESSAGES_URI, params={"tags": ["original,amend"]})
response = await ccn_api_client.get(
MESSAGES_URI, params={"tags": ["original,amend"]}
)
assert response.status == 200, await response.text()
messages = (await response.json())["messages"]
assert len(messages) == 2
Expand Down Expand Up @@ -227,16 +231,32 @@ async def test_get_messages_filter_by_tags(
assert messages[0]["item_hash"] == amend_message_db.item_hash


@pytest.mark.parametrize("type_field", ("msgType", "msgTypes"))
@pytest.mark.asyncio
async def test_get_by_message_type(fixture_messages, ccn_api_client, type_field: str):
async def test_get_by_deprecated_message_type(fixture_messages, ccn_api_client):
messages_by_type = defaultdict(list)
for message in fixture_messages:
messages_by_type[message["type"]].append(message)

for message_type, expected_messages in messages_by_type.items():
response = await ccn_api_client.get(
MESSAGES_URI, params={"msgType": message_type}
)
assert response.status == 200, await response.text()
messages = (await response.json())["messages"]
assert set(msg["item_hash"] for msg in messages) == set(
msg["item_hash"] for msg in expected_messages
)


@pytest.mark.asyncio
async def test_get_by_message_type(fixture_messages, ccn_api_client):
messages_by_type = defaultdict(list)
for message in fixture_messages:
messages_by_type[message["type"]].append(message)

for message_type, expected_messages in messages_by_type.items():
response = await ccn_api_client.get(
MESSAGES_URI, params={type_field: message_type}
MESSAGES_URI, params={"msgTypes": [message_type]}
)
assert response.status == 200, await response.text()
messages = (await response.json())["messages"]
Expand Down
4 changes: 3 additions & 1 deletion tests/api/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ async def test_get_amended_posts_tags(
session.commit()

# Match one tag
response = await ccn_api_client.get("/api/v0/posts.json", params={"tags": ["amend"]})
response = await ccn_api_client.get(
"/api/v0/posts.json", params={"tags": ["amend"]}
)
assert response.status == 200
response_json = await response.json()
assert len(response_json["posts"]) == 1
Expand Down
16 changes: 12 additions & 4 deletions tests/message_processing/test_process_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ async def test_process_instance_missing_volumes(
assert rejected_message.error_code == ErrorCode.VM_VOLUME_NOT_FOUND

if fixture_instance_message.item_content:
content = InstanceContent.model_validate_json(fixture_instance_message.item_content)
content = InstanceContent.model_validate_json(
fixture_instance_message.item_content
)
volume_refs = set(volume.ref for volume in get_volume_refs(content))
assert isinstance(rejected_message.details, dict)
assert set(rejected_message.details["errors"]) == volume_refs
Expand Down Expand Up @@ -453,7 +455,9 @@ async def test_get_volume_size(
session.commit()

if fixture_instance_message.item_content:
content = InstanceContent.model_validate_json(fixture_instance_message.item_content)
content = InstanceContent.model_validate_json(
fixture_instance_message.item_content
)
with session_factory() as session:
volume_size = get_volume_size(session=session, content=content)
assert volume_size == 21512585216
Expand All @@ -469,7 +473,9 @@ async def test_get_additional_storage_price(
session.commit()

if fixture_instance_message.item_content:
content = InstanceContent.model_validate_json(fixture_instance_message.item_content)
content = InstanceContent.model_validate_json(
fixture_instance_message.item_content
)
with session_factory() as session:
additional_price = get_additional_storage_price(
content=content, session=session
Expand All @@ -487,7 +493,9 @@ async def test_get_compute_cost(
session.commit()

if fixture_instance_message.item_content:
content = InstanceContent.model_validate_json(fixture_instance_message.item_content)
content = InstanceContent.model_validate_json(
fixture_instance_message.item_content
)
with session_factory() as session:
price: Decimal = compute_cost(content=content, session=session)
assert price == Decimal("2001.8")
Expand Down

0 comments on commit 7cbfce6

Please sign in to comment.