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
|
2019-02-26 01:23:20 +00:00
|
|
|
import trio
|
2018-11-30 13:18:13 +00:00
|
|
|
from piker import log
|
2019-02-26 01:23:20 +00:00
|
|
|
from piker.brokers import questrade, 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
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def loglevel(request):
|
|
|
|
orig = tractor.log._default_loglevel
|
|
|
|
level = tractor.log._default_loglevel = request.config.option.loglevel
|
|
|
|
log.get_console_log(level)
|
|
|
|
yield level
|
|
|
|
tractor.log._default_loglevel = orig
|
2018-11-12 02:05:44 +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):
|
|
|
|
"""If the `--confdir` flag is not passed use the
|
|
|
|
broker config file found in that dir.
|
|
|
|
"""
|
|
|
|
confdir = request.config.option.confdir
|
|
|
|
if confdir is not None:
|
|
|
|
config._override_config_dir(confdir)
|
|
|
|
|
|
|
|
# return config.load()[0]
|
|
|
|
return confdir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def travis(confdir):
|
2019-02-26 01:23:20 +00:00
|
|
|
is_travis = os.environ.get('TRAVIS', False)
|
|
|
|
if is_travis:
|
|
|
|
# this directory is cached, see .travis.yaml
|
2019-03-18 03:34:48 +00:00
|
|
|
conf_file = config.get_broker_conf_path()
|
2019-02-26 01:23:20 +00:00
|
|
|
refresh_token = os.environ['QT_REFRESH_TOKEN']
|
|
|
|
|
|
|
|
def write_with_token(token):
|
2019-03-18 03:04:04 +00:00
|
|
|
# XXX don't pass the dir path here since may be
|
|
|
|
# written behind the scenes in the `confdir fixture`
|
2019-03-18 03:34:48 +00:00
|
|
|
if not os.path.isfile(conf_file):
|
|
|
|
open(conf_file, 'w').close()
|
2019-03-18 03:04:04 +00:00
|
|
|
conf, path = config.load()
|
2019-02-26 03:12:33 +00:00
|
|
|
conf.setdefault('questrade', {}).update(
|
|
|
|
{'refresh_token': token,
|
|
|
|
'is_practice': 'True'}
|
|
|
|
)
|
2019-02-26 01:23:20 +00:00
|
|
|
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
|
2019-02-27 00:15:53 +00:00
|
|
|
except (
|
2019-03-18 03:22:00 +00:00
|
|
|
FileNotFoundError, ValueError,
|
2019-03-03 16:01:34 +00:00
|
|
|
questrade.BrokerError, questrade.QuestradeError,
|
|
|
|
trio.MultiError,
|
2019-02-27 00:15:53 +00:00
|
|
|
):
|
2019-02-26 01:23:20 +00:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
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']
|