From 3c72fbbb2b6ae91e888f948763f31fe3e2a6d706 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 16 Jan 2024 14:33:08 +0100 Subject: [PATCH] Less test cases --- tests/conftest.py | 12 ++-- tests/constants.py | 2 +- tests/factory/test_factory_meta.py | 8 +-- tests/factory/test_factory_plain.py | 7 +-- tests/fixtures/accounts.py | 91 +++++++++++++++-------------- 5 files changed, 62 insertions(+), 58 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 0323a59d..3fce4ff5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ -import itertools import os +from itertools import combinations_with_replacement +from random import sample import boa import pytest @@ -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: diff --git a/tests/constants.py b/tests/constants.py index 06718302..073cdd09 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -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? diff --git a/tests/factory/test_factory_meta.py b/tests/factory/test_factory_meta.py index 0820f375..1243e964 100644 --- a/tests/factory/test_factory_meta.py +++ b/tests/factory/test_factory_meta.py @@ -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 diff --git a/tests/factory/test_factory_plain.py b/tests/factory/test_factory_plain.py index f4056ce1..be4bb690 100644 --- a/tests/factory/test_factory_plain.py +++ b/tests/factory/test_factory_plain.py @@ -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): diff --git a/tests/fixtures/accounts.py b/tests/fixtures/accounts.py index 8e5bba2b..e9f679e0 100644 --- a/tests/fixtures/accounts.py +++ b/tests/fixtures/accounts.py @@ -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