Skip to content

Commit

Permalink
Less test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Jan 16, 2024
1 parent 06aa427 commit 3c72fbb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 58 deletions.
12 changes: 8 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
import os
from itertools import combinations_with_replacement
from random import sample

import boa
import pytest
Expand Down Expand Up @@ -33,11 +34,14 @@ def pytest_generate_tests(metafunc):
items = [
(k, v) for k, v in TOKEN_TYPES.items() if not metafunc.definition.get_closest_marker(f"skip_{k}_tokens")
]
combinations = sorted(itertools.combinations_with_replacement(items, 2))
# make all combinations possible
all_combinations = list(combinations_with_replacement(items, 2))
# take 2 combinations for smaller test set
samples = sorted(sample(all_combinations, k=2))
metafunc.parametrize(
"pool_token_types",
[(v1, v2) for (k1, v1), (k2, v2) in combinations],
ids=[f"(PoolTokenTypes={k1}+{k2})" for (k1, v1), (k2, v2) in combinations],
[(v1, v2) for (k1, v1), (k2, v2) in samples],
ids=[f"(PoolTokenTypes={k1}+{k2})" for (k1, v1), (k2, v2) in samples],
)

if "metapool_token_type" in metafunc.fixturenames:
Expand Down
2 changes: 1 addition & 1 deletion tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
POOL_TYPES = {"basic": 0, "meta": 1}
TOKEN_TYPES = {"plain": 0, "oracle": 1, "rebasing": 2}
DECIMAL_PAIRS = [(18, 18), (10, 12)]
DECIMAL_PAIRS = [(18, 18)] # TODO: Do we need more pairs?
8 changes: 4 additions & 4 deletions tests/factory/test_factory_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ def test_get_underlying_coins(factory, meta_swap, underlying_tokens):
assert factory.get_underlying_coins(meta_swap.address) == [t.address for t in tokens]


def test_get_underlying_decimals(factory, meta_swap, base_pool_decimals, pool_type):
def test_get_underlying_decimals(factory, meta_swap, base_pool_decimals):
assert factory.get_underlying_decimals(meta_swap.address) == [18] + base_pool_decimals


def test_get_metapool_rates(factory, meta_swap, base_pool, initial_setup):
def test_get_metapool_rates(meta_setup, factory, meta_swap, base_pool, base_pool_lp_token):
assert factory.get_metapool_rates(meta_swap.address) == [10**18, base_pool.get_virtual_price()]


def test_get_underlying_balances(factory, meta_swap, base_pool, initial_setup):
def test_get_underlying_balances(meta_setup, factory, meta_swap, base_pool):
assert factory.get_metapool_rates(meta_swap.address) == [10**18, base_pool.get_virtual_price()]


@pytest.mark.parametrize("sending,receiving", itertools.permutations(range(1, 4), 2))
def test_find_pool_underlying_base_pool_only(self, factory, underlying_tokens, sending, receiving, zero_address):
def test_find_pool_underlying_base_pool_only(factory, underlying_tokens, sending, receiving, zero_address):
assert factory.find_pool_for_coins(underlying_tokens[sending], underlying_tokens[receiving]) == zero_address


Expand Down
7 changes: 2 additions & 5 deletions tests/factory/test_factory_plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@


@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
def test_find_pool_for_coins(factory, basic_swap, plain_tokens, sending, receiving):
assert (
factory.find_pool_for_coins(plain_tokens[sending].address, plain_tokens[receiving].address)
== basic_swap.address
)
def test_find_pool_for_coins(factory, swap, plain_tokens, sending, receiving):
assert factory.find_pool_for_coins(plain_tokens[sending].address, plain_tokens[receiving].address) == swap.address


def test_get_n_coins(factory, swap, plain_tokens, pool_size):
Expand Down
91 changes: 47 additions & 44 deletions tests/fixtures/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,58 +196,61 @@ def approve_meta_bob(bob, underlying_tokens, swap):


@pytest.fixture()
def initial_setup(
def basic_setup(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):
basic_swap.add_liquidity(deposit_amounts, 0)

mint_account(bob, pool_tokens, initial_balance, initial_amounts)
with boa.env.prank(bob):
for token in pool_tokens:
token.approve(basic_swap.address, 2**256 - 1)


@pytest.fixture()
def meta_setup(
alice,
bob,
approve_alice,
mint_alice,
approve_alice,
deposit_amounts,
swap,
pool_type,
meta_swap,
base_pool,
base_pool_tokens,
base_pool_decimals,
base_pool_lp_token,
initial_balance,
initial_amounts,
pool_tokens,
underlying_tokens,
):
with boa.env.anchor():
mint_for_testing(bob, 1 * 10**18, None, True)

if pool_type == 0:
with boa.env.prank(alice):
swap.add_liquidity(deposit_amounts, 0)

mint_account(bob, pool_tokens, initial_balance, initial_amounts)
with boa.env.prank(bob):
for token in pool_tokens:
token.approve(swap.address, 2**256 - 1)

else:
add_base_pool_liquidity(alice, base_pool, base_pool_tokens, base_pool_decimals)
alice_bp_balance_norm = base_pool_lp_token.balanceOf(alice) / 10**18
alice_mp_balance_norm = underlying_tokens[0].balanceOf(alice) / 10 ** underlying_tokens[0].decimals()

if alice_mp_balance_norm < alice_bp_balance_norm:
mint_for_testing(
alice,
int(math.ceil(alice_bp_balance_norm) * 10 ** underlying_tokens[0].decimals()),
underlying_tokens[0],
)

with boa.env.prank(alice):
underlying_tokens[0].approve(swap.address, 2**256 - 1)
base_pool_lp_token.approve(swap.address, 2**256 - 1)
swap.add_liquidity(deposit_amounts, 0)

add_base_pool_liquidity(bob, base_pool, base_pool_tokens, base_pool_decimals)
mint_for_testing(bob, initial_amounts[0], underlying_tokens[0], False)
assert underlying_tokens[0].balanceOf(bob) == pytest.approx(base_pool_lp_token.balanceOf(bob))

with boa.env.prank(bob):
for token in underlying_tokens:
token.approve(swap.address, 2**256 - 1)

yield
mint_for_testing(bob, 1 * 10**18, None, True)

underlying_token = underlying_tokens[0]
add_base_pool_liquidity(alice, base_pool, base_pool_tokens, base_pool_decimals)
alice_bp_balance_norm = base_pool_lp_token.balanceOf(alice) / 10**18
alice_mp_balance_norm = underlying_token.balanceOf(alice) / 10 ** underlying_token.decimals()

if alice_mp_balance_norm < alice_bp_balance_norm:
mint_for_testing(
alice, int(math.ceil(alice_bp_balance_norm) * 10 ** underlying_token.decimals()), underlying_token
)

with boa.env.prank(alice):
underlying_token.approve(meta_swap.address, 2**256 - 1)
base_pool_lp_token.approve(meta_swap.address, 2**256 - 1)
meta_swap.add_liquidity(deposit_amounts, 0)

add_base_pool_liquidity(bob, base_pool, base_pool_tokens, base_pool_decimals)
mint_for_testing(bob, initial_amounts[0], underlying_token, False)
assert underlying_token.balanceOf(bob) == pytest.approx(base_pool_lp_token.balanceOf(bob))

with boa.env.prank(bob):
for underlying_token in underlying_tokens:
underlying_token.approve(meta_swap.address, 2**256 - 1)


@pytest.fixture()
def initial_setup(meta_setup, basic_setup, pool_type):
if pool_type == 0:
return basic_setup
return meta_setup

0 comments on commit 3c72fbb

Please sign in to comment.