Skip to content

Commit

Permalink
Refactor bootstrapped fixtures (serum-community#28)
Browse files Browse the repository at this point in the history
* Add wallet fixture

* Make format

* Extra verbose logging for int tests

* Update make scripts

* Refactor bootstrapped fixtures
  • Loading branch information
michaelhly authored Sep 13, 2020
1 parent 1cc4b7b commit 43cfe1b
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 35 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ clean:
rm -rf dist build _build __pycache__ *.egg-info

format:
isort setup.py src tests
black --line-length=120 setup.py src tests
pipenv run isort setup.py src tests
pipenv run black --line-length=120 setup.py src tests

lint:
flake8 setup.py src tests
mypy src
pylint --rcfile=.pylintrc setup.py src tests
pipenv run flake8 setup.py src tests
pipenv run mypy src
pipenv run pylint --rcfile=.pylintrc setup.py src tests

.PHONY: notebook
notebook:
cd notebooks && PYTHONPATH=../ jupyter notebook

unit-tests:
pytest -v -m "not integration"
pipenv run pytest -v -m "not integration"

int-tests:
sh scripts/run_int_tests.sh
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_int_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ echo "dex_program_id: $DEX_PROGRAM_ID" >> crank.log
cp crank.log ../../tests
cd ../..
cat tests/crank.log
pipenv run pytest -v -m integration
pipenv run pytest -vv -m integration
rm -rf tests/crank.log
docker kill serum-dex_localnet_1
4 changes: 2 additions & 2 deletions src/_layouts/instructions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Layouts for dex instructions data."""
from enum import IntEnum

from construct import Bytes, Const, Int8ul, Int16ul, Int32ul, Int64ul, Pass # type: ignore
from construct import Switch # type: ignore
from construct import Bytes, Const, Int8ul, Int16ul, Int32ul, Int64ul, Pass
from construct import Struct as cStruct
from construct import Switch

from .slab import KEY

Expand Down
4 changes: 2 additions & 2 deletions src/_layouts/slab.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from enum import IntEnum
from typing import Iterable, List, NamedTuple, Optional

from construct import Bytes, Int8ul, Int32ul, Int64ul, Padding # type: ignore
from construct import Switch # type: ignore
from construct import Bytes, Int8ul, Int32ul, Int64ul, Padding
from construct import Struct as cStruct
from construct import Switch
from solana.publickey import PublicKey

from .account_flags import ACCOUNT_FLAGS_LAYOUT
Expand Down
129 changes: 122 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,134 @@
import pytest
from typing import Dict

import pytest
from solana.account import Account
from solana.publickey import PublicKey

__dex_fixtures = {}
__cached_params = {}


@pytest.mark.integration
@pytest.fixture(scope="session")
def serum_dex():
if not __dex_fixtures:
def __bs_params() -> Dict[str, str]:
if not __cached_params:
with open("tests/crank.log") as crank_log:
for line in crank_log.readlines():
if ":" not in line:
continue
key, val = line.strip().replace(",", "").split(": ")
assert val is not None
__dex_fixtures[key] = PublicKey(val)
return __dex_fixtures
assert key, "key must not be None"
assert val, "val must not be None"
__cached_params[key] = val
return __cached_params


def __bootstrap_account(pubkey: str, secret: str) -> Account:
secret = [int(b) for b in secret[1:-1].split(" ")]
account = Account(secret)
assert str(account.public_key()) == pubkey, "account must map to provided public key"
return account


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_payer(__bs_params) -> Account:
"""Bootstrapped payer account."""
return __bootstrap_account(__bs_params["payer"], __bs_params["payer_secret"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_base_mint(__bs_params) -> Account:
"""Bootstrapped base mint account."""
return __bootstrap_account(__bs_params["coin_mint"], __bs_params["coin_mint_secret"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_quote_mint(__bs_params) -> Account:
"""Bootstrapped quote mint account."""
return __bootstrap_account(__bs_params["pc_mint"], __bs_params["pc_mint_secret"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_base_wallet(__bs_params) -> Account:
"""Bootstrapped base mint account."""
return __bootstrap_account(__bs_params["coin_wallet"], __bs_params["coin_wallet_secret"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_quote_wallet(__bs_params) -> Account:
"""Bootstrapped quote mint account."""
return __bootstrap_account(__bs_params["pc_wallet"], __bs_params["pc_wallet_secret"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_market_pk(__bs_params) -> PublicKey:
"""Public key of the boostrapped market."""
return PublicKey(__bs_params["market"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_req_q_pk(__bs_params) -> PublicKey:
"""Public key of the bootstrapped request queue."""
return PublicKey(__bs_params["req_q"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_event_q_pk(__bs_params) -> PublicKey:
"""Public key of the bootstrapped request queue."""
return PublicKey(__bs_params["event_q"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_bids_pk(__bs_params) -> PublicKey:
"""Public key of the bootstrapped bids book."""
return PublicKey(__bs_params["bids"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_asks_pk(__bs_params) -> PublicKey:
"""Public key of the bootstrapped asks book."""
return PublicKey(__bs_params["asks"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_base_vault_pk(__bs_params) -> PublicKey:
"""Public key of the base vault account."""
return PublicKey(__bs_params["coin_vault"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_quote_vault_pk(__bs_params) -> PublicKey:
"""Public key of the quote vault account."""
return PublicKey(__bs_params["pc_vault"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_vault_signer_pk(__bs_params) -> PublicKey:
"""Public key of the bootstrapped vault signer."""
return PublicKey(__bs_params["vault_signer_key"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_bid_account_pk(__bs_params) -> PublicKey:
"""Public key of the initial bid order account."""
return PublicKey(__bs_params["bid_account"])


@pytest.mark.integration
@pytest.fixture(scope="session")
def stubbed_ask_account_pk(__bs_params) -> PublicKey:
"""Public key of the initial ask order account."""
return PublicKey(__bs_params["ask_account"])
85 changes: 68 additions & 17 deletions tests/integration/test_bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,73 @@
import pytest

from solana.account import Account
from solana.publickey import PublicKey


@pytest.mark.integration
def test_serum_dex(serum_dex):
"""Make sure serum_dex fixture is populated with public keys."""
assert isinstance(serum_dex["coin_mint"], PublicKey)
assert isinstance(serum_dex["pc_mint"], PublicKey)
assert isinstance(serum_dex["market"], PublicKey)
assert isinstance(serum_dex["req_q"], PublicKey)
assert isinstance(serum_dex["event_q"], PublicKey)
assert isinstance(serum_dex["bids"], PublicKey)
assert isinstance(serum_dex["asks"], PublicKey)
assert isinstance(serum_dex["coin_vault"], PublicKey)
assert isinstance(serum_dex["pc_vault"], PublicKey)
assert isinstance(serum_dex["vault_signer_key"], PublicKey)
assert isinstance(serum_dex["wallet"], PublicKey)
assert isinstance(serum_dex["bid_account"], PublicKey)
assert isinstance(serum_dex["ask_account"], PublicKey)
assert isinstance(serum_dex["dex_program_id"], PublicKey)
def test_payer(stubbed_payer):
assert isinstance(stubbed_payer, Account)


@pytest.mark.integration
def test_base_mint(stubbed_base_mint):
assert isinstance(stubbed_base_mint, Account)


@pytest.mark.integration
def test_base_wallet(stubbed_base_wallet):
assert isinstance(stubbed_base_wallet, Account)


@pytest.mark.integration
def test_base_vault_pk(stubbed_base_vault_pk):
assert isinstance(stubbed_base_vault_pk, PublicKey)


@pytest.mark.integration
def test_quote_mint(stubbed_quote_mint):
assert isinstance(stubbed_quote_mint, Account)


@pytest.mark.integration
def test_quote_wallet(stubbed_quote_wallet):
assert isinstance(stubbed_quote_wallet, Account)


@pytest.mark.integration
def test_quote_vault_pk(stubbed_quote_vault_pk):
assert isinstance(stubbed_quote_vault_pk, PublicKey)


@pytest.mark.integration
def test_market_pk(stubbed_market_pk):
assert isinstance(stubbed_market_pk, PublicKey)


@pytest.mark.integration
def test_event_q_pk(stubbed_event_q_pk):
assert isinstance(stubbed_event_q_pk, PublicKey)


@pytest.mark.integration
def test_req_q_pk(stubbed_req_q_pk):
assert isinstance(stubbed_req_q_pk, PublicKey)


@pytest.mark.integration
def test_bids_pk(stubbed_bids_pk):
assert isinstance(stubbed_bids_pk, PublicKey)


@pytest.mark.integration
def test_asks_pk(stubbed_asks_pk):
assert isinstance(stubbed_asks_pk, PublicKey)


@pytest.mark.integration
def test_bid_account_pk(stubbed_bid_account_pk):
assert isinstance(stubbed_bid_account_pk, PublicKey)


@pytest.mark.integration
def test_ask_account_pk(stubbed_ask_account_pk):
assert isinstance(stubbed_ask_account_pk, PublicKey)

0 comments on commit 43cfe1b

Please sign in to comment.