Skip to content

Commit bc5c37a

Browse files
Only run fixtures when used
1 parent 032c578 commit bc5c37a

File tree

8 files changed

+112
-79
lines changed

8 files changed

+112
-79
lines changed

tests/conftest.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def pytest_generate_tests(metafunc):
4040

4141
if "metapool_token_type" in metafunc.fixturenames:
4242
# for meta pool only 1st coin is selected
43-
token_type_items = sorted(TOKEN_TYPES.items())
43+
token_type_items = get_tokens_for_metafunc(metafunc)
4444
metafunc.parametrize(
4545
"metapool_token_type",
46-
[v for k, v in token_type_items],
47-
ids=[f"(MetaTokenType={k})" for k, v in token_type_items],
46+
[number for name, number in token_type_items],
47+
ids=[f"(MetaTokenType={name})" for name, number in token_type_items],
4848
)
4949

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

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

7268

69+
def get_tokens_for_metafunc(metafunc):
70+
return [
71+
(name, number)
72+
for name, number in TOKEN_TYPES.items()
73+
if not metafunc.definition.get_closest_marker(f"skip_{name}_tokens")
74+
]
75+
76+
7377
@pytest.fixture(scope="session")
7478
def pool_size():
7579
return 2

tests/fixtures/accounts.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from tests.utils.tokens import mint_for_testing
88

9+
from ..constants import POOL_TYPES
910
from .constants import INITIAL_AMOUNT
1011

1112

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

6768
# <--------------------- Functions --------------------->
6869
def mint_account(account, pool_tokens, initial_balance, initial_amounts):
69-
mint_for_testing(account, initial_balance, None, True)
70+
mint_for_testing(account, initial_balance, token_contract=None, mint_eth=True)
7071
for pool_token, amount in zip(pool_tokens, initial_amounts):
71-
mint_for_testing(account, amount, pool_token, False)
72+
mint_for_testing(account, amount, pool_token, mint_eth=False)
7273

7374

7475
def approve_account(account, pool_tokens, swap):
@@ -196,7 +197,9 @@ def approve_meta_bob(bob, underlying_tokens, swap):
196197

197198

198199
@pytest.fixture()
199-
def basic_setup(alice, bob, mint_alice, deposit_amounts, basic_swap, initial_balance, initial_amounts, pool_tokens):
200+
def basic_setup(
201+
alice, approve_alice, bob, mint_alice, deposit_amounts, basic_swap, initial_balance, initial_amounts, pool_tokens
202+
):
200203
mint_for_testing(bob, 1 * 10**18, None, True)
201204

202205
with boa.env.prank(alice):
@@ -250,7 +253,10 @@ def meta_setup(
250253

251254

252255
@pytest.fixture()
253-
def initial_setup(meta_setup, basic_setup, pool_type):
254-
if pool_type == 0:
255-
return basic_setup
256-
return meta_setup
256+
def initial_setup(pool_type, request):
257+
"""
258+
Set up the initial state for a pool test.
259+
Run either basic_setup or meta_setup depending on the pool_type.
260+
"""
261+
fixture_name = {POOL_TYPES["basic"]: "basic_setup", POOL_TYPES["meta"]: "meta_setup"}[pool_type]
262+
return request.getfixturevalue(fixture_name)

tests/fixtures/mocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44

5-
@pytest.fixture(scope="module")
5+
@pytest.fixture()
66
def callback_contract(bob, swap, pool_tokens, underlying_tokens, callback_swap_deployer):
77
with boa.env.prank(bob):
88
callback = callback_swap_deployer.deploy(swap.address, bob)

tests/fixtures/pools.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
import pytest
33
from eth_utils import function_signature_to_4byte_selector
44

5+
from tests.constants import POOL_TYPES
6+
57
ORACLE_METHOD_ID = function_signature_to_4byte_selector("exchangeRate()")
68
OFFPEG_FEE_MULTIPLIER = 20000000000
79

810

911
@pytest.fixture()
1012
def basic_swap(deployer, factory, pool_size, pool_tokens, zero_address, amm_deployer, set_pool_implementations):
13+
# factory, set_metapool_implementations, zero_address, metapool_token, base_pool, meta_deployer, add_base_pool
14+
1115
A = 2000
1216
fee = 1000000
1317
method_ids = [b""] * pool_size
@@ -93,8 +97,9 @@ def meta_swap(
9397

9498

9599
@pytest.fixture()
96-
def swap(basic_swap, meta_swap, pool_type):
97-
return {0: basic_swap, 1: meta_swap}[pool_type]
100+
def swap(request, pool_type):
101+
fixture_name = {POOL_TYPES["basic"]: "basic_swap", POOL_TYPES["meta"]: "meta_swap"}[pool_type]
102+
return request.getfixturevalue(fixture_name)
98103

99104

100105
# <--------------------- Metapool configuration --------------------->

tests/fixtures/tokens.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import boa
22
import pytest
33

4+
from tests.constants import TOKEN_TYPES
5+
46

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

1921

2022
@pytest.fixture()
21-
def rebase_tokens(erc20_rebasing_deployer, deployer, decimals):
23+
def rebasing_tokens(erc20_rebasing_deployer, deployer, decimals):
2224
with boa.env.prank(deployer):
2325
return [
2426
erc20_rebasing_deployer.deploy(f"OR_TKN{i}", f"OR_TKN{i}", decimals[i], True)
@@ -27,15 +29,29 @@ def rebase_tokens(erc20_rebasing_deployer, deployer, decimals):
2729

2830

2931
@pytest.fixture()
30-
def pool_tokens(pool_token_types, plain_tokens, oracle_tokens, rebase_tokens):
31-
tokens = {0: plain_tokens, 1: oracle_tokens, 2: rebase_tokens}
32-
return [tokens[t][i] for i, t in enumerate(pool_token_types)]
32+
def pool_tokens(pool_token_types, request):
33+
fixtures = {
34+
TOKEN_TYPES["plain"]: "plain_tokens",
35+
TOKEN_TYPES["oracle"]: "oracle_tokens",
36+
TOKEN_TYPES["rebasing"]: "rebasing_tokens",
37+
}
38+
type1, type2 = pool_token_types
39+
first, _ = request.getfixturevalue(fixtures[type1])
40+
_, second = request.getfixturevalue(fixtures[type2])
41+
return first, second
3342

3443

3544
# <--------------------- Metapool configuration --------------------->
3645
@pytest.fixture()
37-
def metapool_token(metapool_token_type, plain_tokens, oracle_tokens, rebase_tokens):
38-
return {0: plain_tokens, 1: oracle_tokens, 2: rebase_tokens}[metapool_token_type][0]
46+
def metapool_token(metapool_token_type, request, initial_decimals):
47+
assert initial_decimals, "Fixture required for requesting `decimals` downstream"
48+
fixture = {
49+
TOKEN_TYPES["plain"]: "plain_tokens",
50+
TOKEN_TYPES["oracle"]: "oracle_tokens",
51+
TOKEN_TYPES["rebasing"]: "rebasing_tokens",
52+
}
53+
metapool_token, _ = request.getfixturevalue(fixture[metapool_token_type])
54+
return metapool_token
3955

4056

4157
@pytest.fixture()

tests/pools/exchange/test_exchange_received.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def _transfer_and_swap(pool, sending: int, receiving: int, underlying: bool):
9898
return _transfer_and_swap
9999

100100

101-
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
102101
@pytest.mark.skip_rebasing_tokens
102+
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
103103
def test_exchange_received_nonrebasing(bob, swap, pool_tokens, sending, receiving, transfer_and_swap):
104104
swap_data = transfer_and_swap(swap, sending, receiving, False)
105105

tests/pools/exchange/test_exchange_reverts.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ def test_insufficient_balance(charlie, pool_tokens, underlying_tokens, swap, sen
1212
assert token.balanceOf(charlie) == 0
1313

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

2118

2219
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])

tests/pools/oracle/test_oracle.py

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import boa
22
import pytest
33

4+
from tests.constants import POOL_TYPES, TOKEN_TYPES
45
from tests.fixtures.accounts import add_base_pool_liquidity, mint_account
56
from tests.fixtures.constants import INITIAL_AMOUNT
67
from tests.utils.tokens import mint_for_testing
@@ -9,14 +10,27 @@
910

1011

1112
@pytest.fixture()
12-
def initial_setup_alice(pool_type, basic_setup_alice, meta_setup_alice):
13-
if pool_type == 0:
14-
return basic_setup_alice
15-
return meta_setup_alice
13+
def initial_setup_alice(pool_type, request):
14+
"""
15+
Set up the initial state for Alice.
16+
Run either the basic or meta fixture depending on the pool type.
17+
"""
18+
fixtures = {POOL_TYPES["basic"]: "basic_setup_alice", POOL_TYPES["meta"]: "meta_setup_alice"}
19+
return request.getfixturevalue(fixtures[pool_type])
1620

1721

1822
@pytest.fixture()
19-
def basic_setup_alice(alice, initial_amounts, initial_balance, oracle_tokens, basic_swap):
23+
def basic_setup_alice(
24+
alice,
25+
initial_amounts,
26+
initial_balance,
27+
oracle_tokens,
28+
basic_swap,
29+
base_pool_tokens,
30+
base_pool,
31+
base_pool_decimals,
32+
underlying_tokens,
33+
):
2034
mint_for_testing(alice, 1 * 10**18, None, True)
2135
mint_account(alice, oracle_tokens, initial_balance, initial_amounts)
2236
with boa.env.prank(alice):
@@ -48,22 +62,22 @@ def test_initial_liquidity(
4862
oracle_tokens,
4963
metapool_token,
5064
):
51-
amounts = []
52-
5365
if pool_type == 0:
54-
for i, t in enumerate(pool_token_types):
55-
if t != 1:
56-
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i])
57-
else:
58-
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate())
66+
amounts = [
67+
DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate()
68+
if t == TOKEN_TYPES["oracle"]
69+
else DEPOSIT_AMOUNT * 10 ** decimals[i]
70+
for i, t in enumerate(pool_token_types)
71+
]
5972
else:
60-
if metapool_token_type == 1:
61-
amounts = [
73+
amounts = (
74+
[
6275
DEPOSIT_AMOUNT * 10**meta_decimals * 10**18 // metapool_token.exchangeRate(),
6376
DEPOSIT_AMOUNT * 10**18,
6477
]
65-
else:
66-
amounts = [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
78+
if metapool_token_type == 1
79+
else [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
80+
)
6781

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

7892

79-
def test_get_dy(
80-
alice,
81-
initial_setup_alice,
82-
swap,
83-
pool_type,
84-
pool_token_types,
85-
metapool_token_type,
86-
decimals,
87-
meta_decimals,
88-
oracle_tokens,
89-
metapool_token,
93+
def test_get_dy_basic(
94+
alice, initial_setup_alice, basic_swap, pool_token_types, decimals, meta_decimals, oracle_tokens, metapool_token
9095
):
91-
amounts = []
96+
amounts = [
97+
DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate()
98+
if t == 1
99+
else DEPOSIT_AMOUNT * 10 ** decimals[i]
100+
for i, t in enumerate(pool_token_types)
101+
]
92102

93-
if pool_type == 0:
94-
for i, t in enumerate(pool_token_types):
95-
if t != 1:
96-
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i])
97-
else:
98-
amounts.append(DEPOSIT_AMOUNT * 10 ** decimals[i] * 10**18 // oracle_tokens[i].exchangeRate())
99-
else:
100-
if metapool_token_type == 1:
101-
amounts = [
102-
DEPOSIT_AMOUNT * 10**meta_decimals * 10**18 // metapool_token.exchangeRate(),
103-
DEPOSIT_AMOUNT * 10**18,
104-
]
105-
else:
106-
amounts = [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
103+
basic_swap.add_liquidity(amounts, 0, sender=alice)
107104

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

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

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

116-
else:
117-
rate_1 = 1 if metapool_token_type != 1 else metapool_token.exchangeRate()
111+
def test_get_dy_meta(
112+
alice, initial_setup_alice, meta_swap, metapool_token_type, decimals, meta_decimals, oracle_tokens, metapool_token
113+
):
114+
amounts = (
115+
[DEPOSIT_AMOUNT * 10**meta_decimals * 10**18 // metapool_token.exchangeRate(), DEPOSIT_AMOUNT * 10**18]
116+
if metapool_token_type == 1
117+
else [DEPOSIT_AMOUNT * 10**meta_decimals, DEPOSIT_AMOUNT * 10**18]
118+
)
119+
120+
meta_swap.add_liquidity(amounts, 0, sender=alice)
121+
122+
rate_1 = 1 if metapool_token_type != 1 else metapool_token.exchangeRate()
118123

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

0 commit comments

Comments
 (0)