Skip to content

Commit

Permalink
Fixed additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfior committed Dec 12, 2024
1 parent 65eed8a commit 85e2d87
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions tests/agents/microchain/test_messages_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import polars as pl
import pytest
from eth_typing import ChecksumAddress
from prediction_market_agent_tooling.gtypes import xdai_type
from prediction_market_agent_tooling.tools.web3_utils import xdai_to_wei
from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes
from pydantic import SecretStr
from web3 import Web3

Expand All @@ -22,9 +21,18 @@
from prediction_market_agent.tools.message_utils import compress_message


@pytest.fixture(scope="module")
def agent2_address() -> ChecksumAddress:
return Web3.to_checksum_address("0xb4D8C8BedE2E49b08d2A22485f72fA516116FE7F")
@pytest.fixture(scope="session")
def account2_address() -> ChecksumAddress:
# anvil account # 2
return Web3.to_checksum_address("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")


@pytest.fixture(scope="session")
def account2_private_key() -> SecretStr:
"Anvil test account private key. It's public already."
return SecretStr(
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
)


# Random transactions found on Gnosisscan.
Expand All @@ -39,7 +47,7 @@ def mock_spice_query(query: str, api_key: str) -> pl.DataFrame:
return pl.DataFrame(
{
"hash": [MOCK_HASH_1, MOCK_HASH_2],
"value": [xdai_to_wei(xdai_type(1)), xdai_to_wei(xdai_type(2))],
"value": [Web3.to_wei(1, "ether"), Web3.to_wei(2, "ether")],
"block_number": [1, 2],
"from": [MOCK_SENDER_SPICE_QUERY, MOCK_SENDER_SPICE_QUERY],
"data": ["test", Web3.to_hex(compress_message("test"))],
Expand All @@ -66,15 +74,29 @@ def patch_spice() -> Generator[PropertyMock, None, None]:
yield mock_spice


@pytest.fixture(scope="module")
def patch_send_xdai() -> Generator[PropertyMock, None, None]:
# Note that we patch where the function is called (see https://docs.python.org/3/library/unittest.mock.html#where-to-patch).
with patch(
"prediction_market_agent.agents.microchain_agent.messages_functions.send_xdai_to",
return_value={"transactionHash": HexBytes(MOCK_HASH_1)},
) as mock_send_xdai:
yield mock_send_xdai


@pytest.fixture
def patch_public_key(
agent2_address: ChecksumAddress,
account2_address: ChecksumAddress, account2_private_key: SecretStr
) -> Generator[PropertyMock, None, None]:
with patch(
"prediction_market_agent.agents.microchain_agent.microchain_agent_keys.MicrochainAgentKeys.public_key",
new_callable=PropertyMock,
) as mock_public_key:
mock_public_key.return_value = agent2_address
) as mock_public_key, patch(
"prediction_market_agent.agents.microchain_agent.microchain_agent_keys.MicrochainAgentKeys.bet_from_private_key",
new_callable=PropertyMock,
) as mock_private_key:
mock_public_key.return_value = account2_address
mock_private_key.return_value = account2_private_key
yield mock_public_key


Expand All @@ -95,6 +117,7 @@ def test_receive_message_description(
patch_public_key: PropertyMock,
patch_spice: PropertyMock,
patch_dune_api_key: PropertyMock,
patch_send_xdai: PropertyMock,
) -> None:
r = ReceiveMessage()
description = r.description
Expand All @@ -107,6 +130,7 @@ def test_receive_message_description(


def test_receive_message_call(
patch_send_xdai: PropertyMock,
patch_pytest_db: PropertyMock,
patch_public_key: PropertyMock,
patch_spice: PropertyMock,
Expand All @@ -124,6 +148,7 @@ def test_receive_message_then_check_count_unseen_messages(
patch_public_key: PropertyMock,
patch_spice: typing.Any,
patch_dune_api_key: PropertyMock,
patch_send_xdai: PropertyMock,
) -> None:
# Idea here is to fetch the next message, and then fetch the count of unseen messages, asserting that
# this number decreased by 1.
Expand Down

0 comments on commit 85e2d87

Please sign in to comment.