Skip to content

Commit

Permalink
Pass a config tmp_dir: Path to the runtime when testing
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed Mar 9, 2023
1 parent 5b9905c commit 56940bd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
1 change: 0 additions & 1 deletion piker/testing/__init__.py

This file was deleted.

47 changes: 28 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
from functools import partial
import os
from pathlib import Path
from shutil import rmtree

import pytest
import tractor
from piker import (
# log,
config,
)
from piker.service import (
Expand Down Expand Up @@ -71,6 +69,7 @@ def ci_env() -> bool:

@acm
async def _open_test_pikerd(
tmpconfdir: str,
reg_addr: tuple[str, int] | None = None,
loglevel: str = 'warning',
**kwargs,
Expand All @@ -97,6 +96,10 @@ async def _open_test_pikerd(
maybe_open_pikerd(
registry_addr=reg_addr,
loglevel=loglevel,

tractor_runtime_overrides={
'piker_test_dir': tmpconfdir,
},
**kwargs,
) as service_manager,
):
Expand All @@ -119,18 +122,40 @@ async def _open_test_pikerd(

@pytest.fixture
def open_test_pikerd(
request,
request: pytest.FixtureRequest,
tmp_path: Path,
loglevel: str,
):
tmpconfdir: Path = tmp_path / '_testing'
tmpconfdir.mkdir()
tmpconfdir_str: str = str(tmpconfdir)

# NOTE: on linux the tmp config dir is generally located at:
# /tmp/pytest-of-<username>/pytest-<run#>/test_<current_test_name>/
# the default `pytest` config ensures that only the last 4 test
# suite run's dirs will be persisted, otherwise they are removed:
# https://docs.pytest.org/en/6.2.x/tmpdir.html#the-default-base-temporary-directory
print(f'CURRENT TEST CONF DIR: {tmpconfdir}')

yield partial(
_open_test_pikerd,

# pass in a unique temp dir for this test request
# so that we can have multiple tests running (maybe in parallel)
# bwitout clobbering each other's config state.
tmpconfdir=tmpconfdir_str,

# bind in level from fixture, which is itself set by
# `--ll <value>` cli flag.
loglevel=loglevel,
)

# NOTE: the `tmp_dir` fixture will wipe any files older then 3 test
# sessions by default:
# https://docs.pytest.org/en/6.2.x/tmpdir.html#the-default-base-temporary-directory
# BUT, if we wanted to always wipe conf dir and all contained files,
# rmtree(str(tmp_path))

# TODO: teardown checks such as,
# - no leaked subprocs or shm buffers
# - all requested container service are torn down
Expand Down Expand Up @@ -169,19 +194,3 @@ def open_test_pikerd_and_ems(
loglevel,
open_test_pikerd
)


@pytest.fixture(scope='module')
def delete_testing_dir():
'''
This fixture removes the temp directory
used for storing all config/ledger/pp data
created during testing sessions. During test runs
this file can be found in .config/piker/_testing
'''
yield
app_dir = Path(config.get_app_dir('piker')).resolve()
if app_dir.is_dir():
rmtree(str(app_dir))
assert not app_dir.is_dir()
20 changes: 9 additions & 11 deletions tests/test_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from piker.log import get_logger
from piker.clearing._messages import Order
from piker.pp import (
open_trade_ledger,
open_pps,
)

Expand All @@ -42,18 +41,19 @@ async def _async_main(
price: int = 30000,
executions: int = 1,
size: float = 0.01,

# Assert options
assert_entries: bool = False,
assert_pps: bool = False,
assert_zeroed_pps: bool = False,
assert_msg: bool = False,

) -> None:
'''
Start piker, place a trade and assert data in
pps stream, ledger and position table.
'''

oid: str = ''
last_msg = {}

Expand Down Expand Up @@ -136,7 +136,7 @@ def _assert(


def _run_test_and_check(fn):
'''
'''
Close position and assert empty position in pps
'''
Expand All @@ -150,8 +150,7 @@ def _run_test_and_check(fn):


def test_buy(
open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
open_test_pikerd_and_ems: AsyncContextManager,
):
'''
Enter a trade and assert entries are made in pps and ledger files.
Expand All @@ -177,8 +176,7 @@ def test_buy(


def test_sell(
open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
open_test_pikerd_and_ems: AsyncContextManager,
):
'''
Sell position and ensure pps are zeroed.
Expand All @@ -201,13 +199,13 @@ def test_sell(
),
)


def test_multi_sell(
open_test_pikerd_and_ems: AsyncContextManager,
delete_testing_dir
open_test_pikerd_and_ems: AsyncContextManager,
):
'''
Make 5 market limit buy orders and
then sell 5 slots at the same price.
Make 5 market limit buy orders and
then sell 5 slots at the same price.
Finally, assert cleared positions.
'''
Expand Down

0 comments on commit 56940bd

Please sign in to comment.