Add travisCI specific test suite integration
Questrade is the default broker backend (for now) so the CI can run using a practice account token handed down through an env variable. If we add a cached directory to the build then the token should remain persistent in the brokers config and will only need to be updated if something goes wrong. Also, add a `--confdir` flag for pytest much in the same way as for the `piker` cli.kivy_mainline_and_py3.8
parent
d3fae00e74
commit
414734f803
|
@ -1,11 +1,17 @@
|
|||
import os
|
||||
|
||||
import pytest
|
||||
import tractor
|
||||
import trio
|
||||
from piker import log
|
||||
from piker.brokers import questrade, config
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--ll", action="store", dest='loglevel',
|
||||
default=None, help="logging level to set when testing")
|
||||
parser.addoption("--confdir", default=None,
|
||||
help="Use a practice API account")
|
||||
|
||||
|
||||
@pytest.fixture(scope='session', autouse=True)
|
||||
|
@ -17,9 +23,65 @@ def loglevel(request):
|
|||
tractor.log._default_loglevel = orig
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def brokerconf():
|
||||
from piker.brokers import config
|
||||
@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
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def travis():
|
||||
is_travis = os.environ.get('TRAVIS', False)
|
||||
if is_travis:
|
||||
# this directory is cached, see .travis.yaml
|
||||
cache_dir = config.get_broker_conf_path()
|
||||
refresh_token = os.environ['QT_REFRESH_TOKEN']
|
||||
|
||||
def write_with_token(token):
|
||||
conf, path = config.load(cache_dir)
|
||||
conf.setdefault('questrade', {}).update({'refresh_token': token})
|
||||
config.write(conf, path)
|
||||
|
||||
# if not os.path.isdir(cache_dir):
|
||||
# write_with_token(refresh_token)
|
||||
|
||||
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 (KeyError, ValueError, questrade.BrokerError):
|
||||
# 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)
|
||||
|
||||
|
||||
@pytest.fixture(scope='session', autouse=True)
|
||||
def brokerconf(request, test_config, travis):
|
||||
"""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]
|
||||
|
||||
|
||||
|
|
|
@ -17,14 +17,6 @@ from piker.brokers.data import DataFeed
|
|||
log = tractor.get_logger('tests')
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def check_qt_conf_section(brokerconf):
|
||||
"""Skip this module's tests if we have not quetrade API creds.
|
||||
"""
|
||||
if not brokerconf.has_section('questrade'):
|
||||
pytest.skip("No questrade API credentials available")
|
||||
|
||||
|
||||
# stock quote
|
||||
_ex_quotes = {
|
||||
'stock': {
|
||||
|
@ -137,7 +129,8 @@ async def test_concurrent_tokens_refresh(us_symbols, loglevel):
|
|||
async def intermittently_refresh_tokens(client):
|
||||
while True:
|
||||
try:
|
||||
await client.ensure_access(force_refresh=True)
|
||||
await client.ensure_access(
|
||||
force_refresh=True, ask_user=False)
|
||||
log.info(f"last token data is {client.access_data}")
|
||||
await trio.sleep(1)
|
||||
except Exception:
|
||||
|
|
Loading…
Reference in New Issue