From 0bea594741ef677a74221b9c3629b7efc36f93b8 Mon Sep 17 00:00:00 2001 From: blnkoff Date: Sun, 29 Sep 2024 20:34:34 +0300 Subject: [PATCH] Adding tests --- tests/__init__.py | 0 tests/conftest.py | 34 +++++++++++++++++++++++++++++++++ tests/test_async_wallet.py | 39 ++++++++++++++++++++++++++++++++++++++ tests/test_wallet.py | 23 ++++++++++++++++++++++ tests/utils.py | 13 +++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_async_wallet.py create mode 100644 tests/test_wallet.py create mode 100644 tests/utils.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..7dae1bf --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +import os +import pytest +from typing import Optional +from ether import Network, Token +from ether import AsyncWallet, Wallet +from web3 import AsyncWeb3 + + +@pytest.fixture(scope="session") +def make_wallet(): + def _make_wallet( + network: Network | str, + private_key: Optional[str] = None, + is_async: bool = True + ) -> AsyncWallet | Wallet: + if not private_key: + private_key = os.getenv('TEST_PRIVATE_KEY') + return AsyncWallet(private_key, network) if is_async else Wallet(private_key, network) + + return _make_wallet + + +@pytest.fixture(scope="session") +def eth_amount(): + amount = AsyncWeb3.to_wei(0.001, 'ether') + return amount + + +@pytest.fixture(scope="session") +def usdc(make_wallet) -> Token: + wallet = make_wallet('BSC', is_async=False) + return wallet.get_token('0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d') diff --git a/tests/test_async_wallet.py b/tests/test_async_wallet.py new file mode 100644 index 0000000..e3f0cca --- /dev/null +++ b/tests/test_async_wallet.py @@ -0,0 +1,39 @@ +import pytest +from dotenv import dotenv_values + +dotenv_values = dotenv_values() + + +@pytest.fixture() +def wallet(make_wallet): + return make_wallet(network='BSC', is_async=True, private_key=dotenv_values.get('TEST_PRIVATE_KEY')) + + +@pytest.mark.asyncio +async def test_get_balance(wallet): + balance = await wallet.get_balance() + assert isinstance(balance, int) + + +@pytest.mark.asyncio +async def test_build_tx_params(wallet): + tx_params = await wallet.build_tx_params(0) + assert 'value' in tx_params and 'gasPrice' in tx_params + + +@pytest.mark.asyncio +async def test_get_balance_of(wallet, usdc): + assert isinstance(await wallet.get_balance_of(usdc), int) + + +@pytest.mark.asyncio +async def test_transaction(wallet, eth_amount): + recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f' + params = await wallet.build_tx_params(eth_amount, recipient=recipient) + return await wallet.transact(params) + + +@pytest.mark.asyncio +async def test_transfer(wallet, eth_amount, usdc): + recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f' + return await wallet.transfer(usdc, recipient, 10 ** (usdc.decimals - 2)) diff --git a/tests/test_wallet.py b/tests/test_wallet.py new file mode 100644 index 0000000..1b716fd --- /dev/null +++ b/tests/test_wallet.py @@ -0,0 +1,23 @@ +import pytest +from dotenv import dotenv_values + +dotenv_values = dotenv_values() + + +@pytest.fixture +def wallet(make_wallet): + return make_wallet(network='BSC', is_async=False, private_key=dotenv_values.get('TEST_PRIVATE_KEY')) + + +def test_get_balance(wallet): + balance = wallet.get_balance() + assert isinstance(balance, int) + + +def test_build_tx_params(wallet): + tx_params = wallet.build_tx_params(0) + assert 'value' in tx_params and 'gasPrice' in tx_params + + +def test_get_balance_of(wallet, usdc): + assert isinstance(wallet.get_balance_of(usdc), int) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..107a8ab --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,13 @@ +from functools import wraps + + +def validate_status(func): + @wraps(func) + async def wrapper(*args, **kwargs): + wallet = kwargs['wallet'] + tx_hash = await func(*args, **kwargs) + status = bool(wallet.provider.eth.wait_for_transaction_receipt(tx_hash)) + + assert status + + return wrapper