2023-01-09 23:16:09 +00:00
|
|
|
from contextlib import asynccontextmanager as acm
|
2019-02-26 01:23:20 +00:00
|
|
|
import os
|
|
|
|
|
2018-11-12 02:05:44 +00:00
|
|
|
import pytest
|
2018-11-30 13:18:13 +00:00
|
|
|
import tractor
|
2023-01-09 23:16:09 +00:00
|
|
|
from piker import (
|
|
|
|
# log,
|
|
|
|
config,
|
|
|
|
)
|
2018-11-30 13:18:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--ll", action="store", dest='loglevel',
|
|
|
|
default=None, help="logging level to set when testing")
|
2019-02-26 01:23:20 +00:00
|
|
|
parser.addoption("--confdir", default=None,
|
|
|
|
help="Use a practice API account")
|
2018-11-30 13:18:13 +00:00
|
|
|
|
|
|
|
|
2019-02-26 01:23:20 +00:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def test_config():
|
|
|
|
dirname = os.path.dirname
|
|
|
|
dirpath = os.path.abspath(
|
|
|
|
os.path.join(
|
|
|
|
dirname(os.path.realpath(__file__)),
|
|
|
|
'data'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return dirpath
|
|
|
|
|
|
|
|
|
2019-03-18 03:04:04 +00:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def confdir(request, test_config):
|
2023-01-09 23:16:09 +00:00
|
|
|
'''
|
|
|
|
If the `--confdir` flag is not passed use the
|
2019-03-18 03:04:04 +00:00
|
|
|
broker config file found in that dir.
|
2023-01-09 23:16:09 +00:00
|
|
|
|
|
|
|
'''
|
2019-03-18 03:04:04 +00:00
|
|
|
confdir = request.config.option.confdir
|
|
|
|
if confdir is not None:
|
|
|
|
config._override_config_dir(confdir)
|
|
|
|
|
|
|
|
return confdir
|
|
|
|
|
|
|
|
|
2023-01-09 23:16:09 +00:00
|
|
|
# @pytest.fixture(scope='session', autouse=True)
|
|
|
|
# def travis(confdir):
|
|
|
|
# is_travis = os.environ.get('TRAVIS', False)
|
|
|
|
# if is_travis:
|
|
|
|
# # this directory is cached, see .travis.yaml
|
|
|
|
# conf_file = config.get_broker_conf_path()
|
|
|
|
# refresh_token = os.environ['QT_REFRESH_TOKEN']
|
|
|
|
|
|
|
|
# def write_with_token(token):
|
|
|
|
# # XXX don't pass the dir path here since may be
|
|
|
|
# # written behind the scenes in the `confdir fixture`
|
|
|
|
# if not os.path.isfile(conf_file):
|
|
|
|
# open(conf_file, 'w').close()
|
|
|
|
# conf, path = config.load()
|
|
|
|
# conf.setdefault('questrade', {}).update(
|
|
|
|
# {'refresh_token': token,
|
|
|
|
# 'is_practice': 'True'}
|
|
|
|
# )
|
|
|
|
# config.write(conf, path)
|
|
|
|
|
|
|
|
# async def ensure_config():
|
|
|
|
# # try to refresh current token using cached brokers config
|
|
|
|
# # if it fails fail try using the refresh token provided by the
|
|
|
|
# # env var and if that fails stop the test run here.
|
|
|
|
# try:
|
|
|
|
# async with questrade.get_client(ask_user=False):
|
|
|
|
# pass
|
|
|
|
# except (
|
|
|
|
# FileNotFoundError, ValueError,
|
|
|
|
# questrade.BrokerError, questrade.QuestradeError,
|
|
|
|
# trio.MultiError,
|
|
|
|
# ):
|
|
|
|
# # 3 cases:
|
|
|
|
# # - config doesn't have a ``refresh_token`` k/v
|
|
|
|
# # - cache dir does not exist yet
|
|
|
|
# # - current token is expired; take it form env var
|
|
|
|
# write_with_token(refresh_token)
|
|
|
|
|
|
|
|
# async with questrade.get_client(ask_user=False):
|
|
|
|
# pass
|
|
|
|
|
|
|
|
# # XXX ``pytest_trio`` doesn't support scope or autouse
|
|
|
|
# trio.run(ensure_config)
|
2019-02-26 01:23:20 +00:00
|
|
|
|
|
|
|
|
2023-01-09 20:30:26 +00:00
|
|
|
_ci_env: bool = os.environ.get('CI', False)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def ci_env() -> bool:
|
2023-01-09 23:16:09 +00:00
|
|
|
'''
|
|
|
|
Detect CI envoirment.
|
|
|
|
|
|
|
|
'''
|
2023-01-09 20:30:26 +00:00
|
|
|
return _ci_env
|
|
|
|
|
2023-01-09 23:16:09 +00:00
|
|
|
|
2018-11-12 02:05:44 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def us_symbols():
|
|
|
|
return ['TSLA', 'AAPL', 'CGC', 'CRON']
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tmx_symbols():
|
|
|
|
return ['APHA.TO', 'WEED.TO', 'ACB.TO']
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def cse_symbols():
|
|
|
|
return ['TRUL.CN', 'CWEB.CN', 'SNN.CN']
|
2023-01-09 20:30:26 +00:00
|
|
|
|
2023-01-09 23:16:09 +00:00
|
|
|
|
|
|
|
@acm
|
|
|
|
async def _open_test_pikerd(
|
|
|
|
reg_addr: tuple[str, int] | None = None,
|
|
|
|
**kwargs,
|
|
|
|
|
|
|
|
) -> tuple[
|
|
|
|
str,
|
|
|
|
int,
|
|
|
|
tractor.Portal
|
|
|
|
]:
|
|
|
|
'''
|
|
|
|
Testing helper to startup the service tree and runtime on
|
|
|
|
a different port then the default to allow testing alongside
|
|
|
|
a running stack.
|
|
|
|
|
|
|
|
'''
|
|
|
|
import random
|
|
|
|
from piker._daemon import maybe_open_pikerd
|
|
|
|
|
|
|
|
if reg_addr is None:
|
|
|
|
port = random.randint(6e3, 7e3)
|
|
|
|
reg_addr = ('127.0.0.1', port)
|
|
|
|
|
2023-01-10 20:40:45 +00:00
|
|
|
# try:
|
2023-01-09 23:16:09 +00:00
|
|
|
async with (
|
|
|
|
maybe_open_pikerd(
|
|
|
|
registry_addr=reg_addr,
|
|
|
|
**kwargs,
|
2023-01-10 20:40:45 +00:00
|
|
|
) as pikerd_portal,
|
2023-01-09 23:16:09 +00:00
|
|
|
):
|
2023-01-10 20:40:45 +00:00
|
|
|
# this proc/actor is the pikerd
|
|
|
|
assert pikerd_portal is None
|
|
|
|
|
2023-01-09 23:16:09 +00:00
|
|
|
async with tractor.wait_for_actor(
|
|
|
|
'pikerd',
|
|
|
|
arbiter_sockaddr=reg_addr,
|
|
|
|
) as portal:
|
|
|
|
raddr = portal.channel.raddr
|
|
|
|
assert raddr == reg_addr
|
|
|
|
yield (
|
|
|
|
raddr[0],
|
|
|
|
raddr[1],
|
|
|
|
portal,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def open_test_pikerd():
|
|
|
|
|
|
|
|
yield _open_test_pikerd
|
|
|
|
|
|
|
|
# TODO: teardown checks such as,
|
|
|
|
# - no leaked subprocs or shm buffers
|
|
|
|
# - all requested container service are torn down
|
|
|
|
# - certain ``tractor`` runtime state?
|