piker/tests/test_cli.py

223 lines
6.4 KiB
Python
Raw Normal View History

2018-03-27 20:28:44 +00:00
"""
CLI testing, dawg.
"""
import json
import subprocess
import pytest
2018-04-03 03:55:02 +00:00
import tempfile
2018-03-29 00:43:33 +00:00
import os.path
2018-03-27 20:28:44 +00:00
2018-04-03 03:55:02 +00:00
import piker.watchlists as wl
2018-03-27 20:28:44 +00:00
2018-04-03 03:55:02 +00:00
pytestmark = pytest.mark.skipif(
True,
reason="cli tests rely on quote api and questrade symbols"
)
2018-04-03 03:55:02 +00:00
def run(cmd, *args):
2018-03-27 20:28:44 +00:00
"""Run cmd and check for zero return code.
"""
2018-04-03 03:55:02 +00:00
cp = subprocess.run(cmd.split() + list(args))
2018-03-27 20:28:44 +00:00
cp.check_returncode()
return cp
def verify_keys(tickers, quotes):
2018-03-27 20:28:44 +00:00
"""Verify all ticker names are keys in ``quotes_dict``.
"""
for quote in quotes:
assert quote['key'] in tickers
2018-03-27 20:28:44 +00:00
@pytest.fixture
def nyse_tickers():
"""List of well known NYSE ticker symbols.
"""
return ('TD', 'CRON', 'TSLA', 'AAPL')
def test_known_quotes(capfd, nyse_tickers):
"""Verify quotes are dumped to the console as json.
"""
run(f"piker quote {' '.join(nyse_tickers)}")
# verify output can be parsed as json
out, err = capfd.readouterr()
quotes = json.loads(out)
verify_keys(nyse_tickers, quotes)
2018-03-27 20:28:44 +00:00
@pytest.mark.parametrize(
'multiple_tickers',
[True, False]
)
def test_quotes_ticker_not_found(
capfd, caplog, nyse_tickers, multiple_tickers
):
"""Verify that if a ticker can't be found it's quote value is
``None`` and a warning log message is emitted to the console.
"""
bad_ticker = ('doggy',)
tickers = bad_ticker + nyse_tickers if multiple_tickers else bad_ticker
run(f"piker quote {' '.join(tickers)}")
out, err = capfd.readouterr()
if out:
# verify output can be parsed as json
quotes = json.loads(out)
verify_keys(tickers, quotes)
2018-03-27 20:28:44 +00:00
# check for warning log message when some quotes are found
warnmsg = f'Could not find symbol {bad_ticker[0]}'
assert warnmsg in err
else:
# when no quotes are found we should get an error message
errmsg = f'No quotes could be found for {bad_ticker}'
assert errmsg in err
def test_api_method(nyse_tickers, capfd):
"""Ensure a low level api method can be called via CLI.
"""
2019-02-10 22:28:43 +00:00
run(f"piker api search prefix='WEED'")
2018-03-27 20:28:44 +00:00
out, err = capfd.readouterr()
quotes_dict = json.loads(out)
assert isinstance(quotes_dict, dict)
def test_api_method_not_found(nyse_tickers, capfd):
"""Ensure an error messages is printed when an API method isn't found.
"""
bad_meth = 'doggy'
run(f"piker api {bad_meth} names={' '.join(nyse_tickers)}")
out, err = capfd.readouterr()
assert 'null' in out
assert f'No api method `{bad_meth}` could be found?' in err
2018-04-03 03:55:02 +00:00
@pytest.fixture
def temp_dir():
"""Creates a path to a pretend config dir in a temporary directory for
testing.
"""
with tempfile.TemporaryDirectory() as tempdir:
yield os.path.join(tempdir, 'piker')
2018-04-03 03:55:02 +00:00
@pytest.fixture
2018-04-10 18:13:58 +00:00
def ex_watchlists():
"""Made up watchlists to use for expected outputs.
"""
watchlists = {
'dad': list(sorted(['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'])),
2018-04-03 03:55:02 +00:00
'pharma': ['ATE.VN'],
}
2018-04-10 18:13:58 +00:00
return watchlists
@pytest.fixture
def ex_watchlists_wbi(ex_watchlists):
"""Made up watchlists + built-in list(s) to use for expected outputs.
"""
with_builtins = ex_watchlists.copy()
with_builtins.update(wl._builtins)
return with_builtins
@pytest.fixture
def piker_dir(temp_dir, ex_watchlists):
wl.make_config_dir(temp_dir)
json_file_path = os.path.join(temp_dir, 'watchlists.json')
# push test watchlists to file without built-ins
to_write = ex_watchlists.copy()
wl.write_to_file(to_write, json_file_path)
2018-04-03 03:55:02 +00:00
yield json_file_path
2018-04-10 18:13:58 +00:00
def test_show_watchlists(capfd, piker_dir, ex_watchlists_wbi):
"""Ensure all watchlists are printed as json to stdout.
(Can't seem to get pretty formatting to work, pytest thing?)
2018-04-03 03:55:02 +00:00
"""
2018-04-10 18:13:58 +00:00
expected_out = json.dumps(ex_watchlists_wbi, indent=4, sort_keys=True)
2018-04-03 03:55:02 +00:00
run(f"piker watchlists -d {piker_dir} show")
out, err = capfd.readouterr()
assert out.strip() == expected_out
2018-04-10 18:13:58 +00:00
def test_dump_watchlists(capfd, piker_dir, ex_watchlists):
"""Ensure watchlist is dumped without built-in lists.
2018-04-03 03:55:02 +00:00
"""
2018-04-10 18:13:58 +00:00
expected_out = json.dumps(ex_watchlists)
2018-04-03 03:55:02 +00:00
run(f"piker watchlists -d {piker_dir} dump")
out, err = capfd.readouterr()
assert out.strip() == expected_out
2018-04-25 13:11:21 +00:00
@pytest.mark.parametrize(
'tickers', [('CRACK',), ('CRACK', 'SUIT',)]
)
def test_ticker_added_to_watchlists(capfd, piker_dir, ex_watchlists, tickers):
"""Verify that single or multi-ticker lists can be added.
"""
for ticker in tickers:
ex_watchlists['pharma'].append(ticker)
run(f"piker watchlists -d {piker_dir} add pharma {' '.join(tickers)}")
2018-04-03 03:55:02 +00:00
out = wl.ensure_watchlists(piker_dir)
2018-04-10 18:13:58 +00:00
assert out == ex_watchlists
2018-04-03 03:55:02 +00:00
2018-04-10 18:13:58 +00:00
def test_ticker_removed_from_watchlists(capfd, piker_dir, ex_watchlists):
expected_out = ex_watchlists.copy()
expected_out['dad'].remove('SPY')
run(f"piker watchlists -d {piker_dir} remove dad SPY")
2018-04-03 03:55:02 +00:00
out = wl.ensure_watchlists(piker_dir)
assert out == expected_out
2018-04-10 18:13:58 +00:00
# removing a non-entry should be a no-op
run(f"piker watchlists -d {piker_dir} remove dad SPY")
out = wl.ensure_watchlists(piker_dir)
2018-04-03 03:55:02 +00:00
2018-04-10 18:13:58 +00:00
def test_group_deleted_from_watchlists(capfd, piker_dir, ex_watchlists):
expected_out = ex_watchlists.copy()
expected_out.pop('pharma')
2018-04-03 03:55:02 +00:00
run(f"piker watchlists -d {piker_dir} delete pharma")
out = wl.ensure_watchlists(piker_dir)
assert out == expected_out
def test_watchlists_loaded(capfd, piker_dir):
expected_out = {
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'pharma': ['ATE.VN'],
}
expected_out_text = json.dumps(expected_out)
2018-04-03 03:55:02 +00:00
run(f"piker watchlists -d {piker_dir} load", expected_out_text)
out = wl.ensure_watchlists(piker_dir)
assert out == expected_out
def test_watchlists_are_merged(capfd, piker_dir):
2018-04-03 03:55:02 +00:00
orig_watchlist = {
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
'pharma': ['ATE.VN'],
}
list_to_merge = json.dumps({
'drugs': ['CRACK'],
'pharma': ['ATE.VN', 'MALI', 'PERCOCET']
2018-04-03 03:55:02 +00:00
})
expected_out = {
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
'pharma': ['ATE.VN', 'MALI', 'PERCOCET'],
2018-04-03 03:55:02 +00:00
'drugs': ['CRACK']
}
2018-04-06 19:07:47 +00:00
wl.write_to_file(orig_watchlist, piker_dir)
2018-04-03 03:55:02 +00:00
run(f"piker watchlists -d {piker_dir} merge", list_to_merge)
out = wl.ensure_watchlists(piker_dir)
assert out == expected_out