Skip to content

Commit

Permalink
Only run fixtures when used
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Jan 19, 2024
1 parent 032c578 commit bc5c37a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 79 deletions.
20 changes: 12 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def pytest_generate_tests(metafunc):

if "metapool_token_type" in metafunc.fixturenames:
# for meta pool only 1st coin is selected
token_type_items = sorted(TOKEN_TYPES.items())
token_type_items = get_tokens_for_metafunc(metafunc)
metafunc.parametrize(
"metapool_token_type",
[v for k, v in token_type_items],
ids=[f"(MetaTokenType={k})" for k, v in token_type_items],
[number for name, number in token_type_items],
ids=[f"(MetaTokenType={name})" for name, number in token_type_items],
)

if "initial_decimals" in metafunc.fixturenames:
Expand All @@ -57,11 +57,7 @@ def get_pool_token_pairs(metafunc):
if metafunc.definition.get_closest_marker(f"only_{name}_tokens"):
return [((name, number), (name, number))]

items = [
(name, number)
for name, number in TOKEN_TYPES.items()
if not metafunc.definition.get_closest_marker(f"skip_{name}_tokens")
]
items = get_tokens_for_metafunc(metafunc)
# make all combinations possible
all_combinations = list(combinations_with_replacement(items, 2))
# make sure we get the same result in each worker
Expand All @@ -70,6 +66,14 @@ def get_pool_token_pairs(metafunc):
return sorted(random.sample(all_combinations, k=2))


def get_tokens_for_metafunc(metafunc):
return [
(name, number)
for name, number in TOKEN_TYPES.items()
if not metafunc.definition.get_closest_marker(f"skip_{name}_tokens")
]


@pytest.fixture(scope="session")
def pool_size():
return 2
Expand Down
20 changes: 13 additions & 7 deletions tests/fixtures/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from tests.utils.tokens import mint_for_testing

from ..constants import POOL_TYPES
from .constants import INITIAL_AMOUNT


Expand Down Expand Up @@ -66,9 +67,9 @@ def accounts(bob, charlie, dave, erin, frank):

# <--------------------- Functions --------------------->
def mint_account(account, pool_tokens, initial_balance, initial_amounts):
mint_for_testing(account, initial_balance, None, True)
mint_for_testing(account, initial_balance, token_contract=None, mint_eth=True)
for pool_token, amount in zip(pool_tokens, initial_amounts):
mint_for_testing(account, amount, pool_token, False)
mint_for_testing(account, amount, pool_token, mint_eth=False)


def approve_account(account, pool_tokens, swap):
Expand Down Expand Up @@ -196,7 +197,9 @@ def approve_meta_bob(bob, underlying_tokens, swap):


@pytest.fixture()
def basic_setup(alice, bob, mint_alice, deposit_amounts, basic_swap, initial_balance, initial_amounts, pool_tokens):
def basic_setup(
alice, approve_alice, bob, mint_alice, deposit_amounts, basic_swap, initial_balance, initial_amounts, pool_tokens
):
mint_for_testing(bob, 1 * 10**18, None, True)

with boa.env.prank(alice):
Expand Down Expand Up @@ -250,7 +253,10 @@ def meta_setup(


@pytest.fixture()
def initial_setup(meta_setup, basic_setup, pool_type):
if pool_type == 0:
return basic_setup
return meta_setup
def initial_setup(pool_type, request):
"""
Set up the initial state for a pool test.
Run either basic_setup or meta_setup depending on the pool_type.
"""
fixture_name = {POOL_TYPES["basic"]: "basic_setup", POOL_TYPES["meta"]: "meta_setup"}[pool_type]
return request.getfixturevalue(fixture_name)
2 changes: 1 addition & 1 deletion tests/fixtures/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest


@pytest.fixture(scope="module")
@pytest.fixture()
def callback_contract(bob, swap, pool_tokens, underlying_tokens, callback_swap_deployer):
with boa.env.prank(bob):
callback = callback_swap_deployer.deploy(swap.address, bob)
Expand Down
9 changes: 7 additions & 2 deletions tests/fixtures/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import pytest
from eth_utils import function_signature_to_4byte_selector

from tests.constants import POOL_TYPES

ORACLE_METHOD_ID = function_signature_to_4byte_selector("exchangeRate()")
OFFPEG_FEE_MULTIPLIER = 20000000000


@pytest.fixture()
def basic_swap(deployer, factory, pool_size, pool_tokens, zero_address, amm_deployer, set_pool_implementations):
# factory, set_metapool_implementations, zero_address, metapool_token, base_pool, meta_deployer, add_base_pool

A = 2000
fee = 1000000
method_ids = [b""] * pool_size
Expand Down Expand Up @@ -93,8 +97,9 @@ def meta_swap(


@pytest.fixture()
def swap(basic_swap, meta_swap, pool_type):
return {0: basic_swap, 1: meta_swap}[pool_type]
def swap(request, pool_type):
fixture_name = {POOL_TYPES["basic"]: "basic_swap", POOL_TYPES["meta"]: "meta_swap"}[pool_type]
return request.getfixturevalue(fixture_name)


# <--------------------- Metapool configuration --------------------->
Expand Down
28 changes: 22 additions & 6 deletions tests/fixtures/tokens.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import boa
import pytest

from tests.constants import TOKEN_TYPES


@pytest.fixture()
def plain_tokens(erc20_deployer, deployer, decimals):
Expand All @@ -18,7 +20,7 @@ def oracle_tokens(erc20oracle_deployer, deployer, decimals):


@pytest.fixture()
def rebase_tokens(erc20_rebasing_deployer, deployer, decimals):
def rebasing_tokens(erc20_rebasing_deployer, deployer, decimals):
with boa.env.prank(deployer):
return [
erc20_rebasing_deployer.deploy(f"OR_TKN{i}", f"OR_TKN{i}", decimals[i], True)
Expand All @@ -27,15 +29,29 @@ def rebase_tokens(erc20_rebasing_deployer, deployer, decimals):


@pytest.fixture()
def pool_tokens(pool_token_types, plain_tokens, oracle_tokens, rebase_tokens):
tokens = {0: plain_tokens, 1: oracle_tokens, 2: rebase_tokens}
return [tokens[t][i] for i, t in enumerate(pool_token_types)]
def pool_tokens(pool_token_types, request):
fixtures = {
TOKEN_TYPES["plain"]: "plain_tokens",
TOKEN_TYPES["oracle"]: "oracle_tokens",
TOKEN_TYPES["rebasing"]: "rebasing_tokens",
}
type1, type2 = pool_token_types
first, _ = request.getfixturevalue(fixtures[type1])
_, second = request.getfixturevalue(fixtures[type2])
return first, second


# <--------------------- Metapool configuration --------------------->
@pytest.fixture()
def metapool_token(metapool_token_type, plain_tokens, oracle_tokens, rebase_tokens):
return {0: plain_tokens, 1: oracle_tokens, 2: rebase_tokens}[metapool_token_type][0]
def metapool_token(metapool_token_type, request, initial_decimals):
assert initial_decimals, "Fixture required for requesting `decimals` downstream"
fixture = {
TOKEN_TYPES["plain"]: "plain_tokens",
TOKEN_TYPES["oracle"]: "oracle_tokens",
TOKEN_TYPES["rebasing"]: "rebasing_tokens",
}
metapool_token, _ = request.getfixturevalue(fixture[metapool_token_type])
return metapool_token


@pytest.fixture()
Expand Down
2 changes: 1 addition & 1 deletion tests/pools/exchange/test_exchange_received.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def _transfer_and_swap(pool, sending: int, receiving: int, underlying: bool):
return _transfer_and_swap


@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
@pytest.mark.skip_rebasing_tokens
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
def test_exchange_received_nonrebasing(bob, swap, pool_tokens, sending, receiving, transfer_and_swap):
swap_data = transfer_and_swap(swap, sending, receiving, False)

Expand Down
5 changes: 1 addition & 4 deletions tests/pools/exchange/test_exchange_reverts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ def test_insufficient_balance(charlie, pool_tokens, underlying_tokens, swap, sen
assert token.balanceOf(charlie) == 0

# Charlie doesn't have any tokens, all balances are 0
try:
with boa.reverts():
swap.exchange(sending, receiving, amount + 1, 0, sender=charlie)
assert False
except: # noqa: E722
assert True


@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
Expand Down
105 changes: 55 additions & 50 deletions tests/pools/oracle/test_oracle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boa
import pytest

from tests.constants import POOL_TYPES, TOKEN_TYPES
from tests.fixtures.accounts import add_base_pool_liquidity, mint_account
from tests.fixtures.constants import INITIAL_AMOUNT
from tests.utils.tokens import mint_for_testing
Expand All @@ -9,14 +10,27 @@


@pytest.fixture()
def initial_setup_alice(pool_type, basic_setup_alice, meta_setup_alice):
if pool_type == 0:
return basic_setup_alice
return meta_setup_alice
def initial_setup_alice(pool_type, request):
"""
Set up the initial state for Alice.
Run either the basic or meta fixture depending on the pool type.
"""
fixtures = {POOL_TYPES["basic"]: "basic_setup_alice", POOL_TYPES["meta"]: "meta_setup_alice"}
return request.getfixturevalue(fixtures[pool_type])


@pytest.fixture()
def basic_setup_alice(alice, initial_amounts, initial_balance, oracle_tokens, basic_swap):
def basic_setup_alice(
alice,
initial_amounts,
initial_balance,
oracle_tokens,
basic_swap,
base_pool_tokens,
base_pool,
base_pool_decimals,
underlying_tokens,
):
mint_for_testing(alice, 1 * 10**18, None, True)
mint_account(alice, oracle_tokens, initial_balance, initial_amounts)
with boa.env.prank(alice):
Expand Down Expand Up @@ -48,22 +62,22 @@ def test_initial_liquidity(
oracle_tokens,
metapool_token,
):
amounts = []

if pool_type == 0:
for i, t in enumerate(pool_token_types):
if t != 1:
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i])
else:
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate())
amounts = [
DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate()
if t == TOKEN_TYPES["oracle"]
else DEPOSIT_AMOUNT * 10 ** decimals[i]
for i, t in enumerate(pool_token_types)
]
else:
if metapool_token_type == 1:
amounts = [
amounts = (
[
DEPOSIT_AMOUNT * 10**meta_decimals * 10**18 // metapool_token.exchangeRate(),
DEPOSIT_AMOUNT * 10**18,
]
else:
amounts = [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
if metapool_token_type == 1
else [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
)

swap.add_liquidity(amounts, 0, sender=alice)
swap.add_liquidity(amounts, 0, sender=alice)
Expand All @@ -76,44 +90,35 @@ def test_oracles(alice, swap, pool_size):
assert swap._immutables.rate_oracles.get() != [0] * pool_size


def test_get_dy(
alice,
initial_setup_alice,
swap,
pool_type,
pool_token_types,
metapool_token_type,
decimals,
meta_decimals,
oracle_tokens,
metapool_token,
def test_get_dy_basic(
alice, initial_setup_alice, basic_swap, pool_token_types, decimals, meta_decimals, oracle_tokens, metapool_token
):
amounts = []
amounts = [
DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate()
if t == 1
else DEPOSIT_AMOUNT * 10 ** decimals[i]
for i, t in enumerate(pool_token_types)
]

if pool_type == 0:
for i, t in enumerate(pool_token_types):
if t != 1:
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i])
else:
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate())
else:
if metapool_token_type == 1:
amounts = [
DEPOSIT_AMOUNT * 10**meta_decimals * 10**18 // metapool_token.exchangeRate(),
DEPOSIT_AMOUNT * 10**18,
]
else:
amounts = [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
basic_swap.add_liquidity(amounts, 0, sender=alice)

swap.add_liquidity(amounts, 0, sender=alice)
rate_1 = 10**18 if pool_token_types[0] != 1 else oracle_tokens[0].exchangeRate()
rate_2 = 10**18 if pool_token_types[1] != 1 else oracle_tokens[1].exchangeRate()

if pool_type == 0:
rate_1 = 10**18 if pool_token_types[0] != 1 else oracle_tokens[0].exchangeRate()
rate_2 = 10**18 if pool_token_types[1] != 1 else oracle_tokens[1].exchangeRate()
assert basic_swap.get_dy(0, 1, rate_2) == pytest.approx(rate_1, rel=1e-3)

assert swap.get_dy(0, 1, rate_2) == pytest.approx(rate_1, rel=1e-3)

else:
rate_1 = 1 if metapool_token_type != 1 else metapool_token.exchangeRate()
def test_get_dy_meta(
alice, initial_setup_alice, meta_swap, metapool_token_type, decimals, meta_decimals, oracle_tokens, metapool_token
):
amounts = (
[DEPOSIT_AMOUNT * 10**meta_decimals * 10**18 // metapool_token.exchangeRate(), DEPOSIT_AMOUNT * 10**18]
if metapool_token_type == 1
else [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
)

meta_swap.add_liquidity(amounts, 0, sender=alice)

rate_1 = 1 if metapool_token_type != 1 else metapool_token.exchangeRate()

assert swap.get_dy(0, 1, 10**18) == pytest.approx(rate_1, rel=1e-3)
assert meta_swap.get_dy(0, 1, 10**18) == pytest.approx(rate_1, rel=1e-3)

0 comments on commit bc5c37a

Please sign in to comment.