Update tests
parent
577ca43c02
commit
2070f292b1
|
@ -6,11 +6,8 @@ import subprocess
|
||||||
import pytest
|
import pytest
|
||||||
import tempfile
|
import tempfile
|
||||||
import os.path
|
import os.path
|
||||||
import logging
|
|
||||||
|
|
||||||
import piker.watchlists as wl
|
import piker.watchlists as wl
|
||||||
import piker.cli as cli
|
|
||||||
from piker.log import colorize_json
|
|
||||||
|
|
||||||
|
|
||||||
def run(cmd, *args):
|
def run(cmd, *args):
|
||||||
|
@ -104,71 +101,77 @@ def temp_dir():
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def piker_dir(temp_dir):
|
def ex_watchlists():
|
||||||
|
"""Made up watchlists to use for expected outputs.
|
||||||
|
"""
|
||||||
|
watchlists = {
|
||||||
|
'dad': list(sorted(['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'])),
|
||||||
|
'pharma': ['ATE.VN'],
|
||||||
|
}
|
||||||
|
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)
|
wl.make_config_dir(temp_dir)
|
||||||
json_file_path = os.path.join(temp_dir, 'watchlists.json')
|
json_file_path = os.path.join(temp_dir, 'watchlists.json')
|
||||||
watchlist = {
|
# push test watchlists to file without built-ins
|
||||||
'dad': ['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'],
|
to_write = ex_watchlists.copy()
|
||||||
'pharma': ['ATE.VN'],
|
wl.write_to_file(to_write, json_file_path)
|
||||||
'indexes': ['SPY', 'DAX', 'QQQ', 'DIA'],
|
|
||||||
}
|
|
||||||
wl.write_to_file(watchlist, json_file_path)
|
|
||||||
yield json_file_path
|
yield json_file_path
|
||||||
|
|
||||||
|
|
||||||
def test_show_watchlists(capfd, piker_dir):
|
def test_show_watchlists(capfd, piker_dir, ex_watchlists_wbi):
|
||||||
"""Ensure a watchlist is printed.
|
"""Ensure all watchlists are printed as json to stdout.
|
||||||
|
|
||||||
|
(Can't seem to get pretty formatting to work, pytest thing?)
|
||||||
"""
|
"""
|
||||||
expected_out = json.dumps({
|
expected_out = json.dumps(ex_watchlists_wbi, indent=4, sort_keys=True)
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
|
||||||
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
|
||||||
'pharma': ['ATE.VN'],
|
|
||||||
}, indent=4)
|
|
||||||
run(f"piker watchlists -d {piker_dir} show")
|
run(f"piker watchlists -d {piker_dir} show")
|
||||||
out, err = capfd.readouterr()
|
out, err = capfd.readouterr()
|
||||||
assert out.strip() == expected_out
|
assert out.strip() == expected_out
|
||||||
|
|
||||||
|
|
||||||
def test_dump_watchlists(capfd, piker_dir):
|
def test_dump_watchlists(capfd, piker_dir, ex_watchlists):
|
||||||
"""Ensure watchlist is dumped.
|
"""Ensure watchlist is dumped without built-in lists.
|
||||||
"""
|
"""
|
||||||
expected_out = json.dumps({
|
expected_out = json.dumps(ex_watchlists)
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
|
||||||
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
|
||||||
'pharma': ['ATE.VN'],
|
|
||||||
})
|
|
||||||
run(f"piker watchlists -d {piker_dir} dump")
|
run(f"piker watchlists -d {piker_dir} dump")
|
||||||
out, err = capfd.readouterr()
|
out, err = capfd.readouterr()
|
||||||
assert out.strip() == expected_out
|
assert out.strip() == expected_out
|
||||||
|
|
||||||
|
|
||||||
def test_ticker_added_to_watchlists(capfd, piker_dir):
|
def test_ticker_added_to_watchlists(capfd, piker_dir, ex_watchlists):
|
||||||
expected_out = {
|
ex_watchlists['pharma'].append('CRACK')
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
|
||||||
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
|
||||||
'pharma': ['ATE.VN', 'CRACK'],
|
|
||||||
}
|
|
||||||
run(f"piker watchlists -d {piker_dir} add pharma CRACK")
|
run(f"piker watchlists -d {piker_dir} add pharma CRACK")
|
||||||
out = wl.ensure_watchlists(piker_dir)
|
out = wl.ensure_watchlists(piker_dir)
|
||||||
assert out == expected_out
|
assert out == ex_watchlists
|
||||||
|
|
||||||
|
|
||||||
def test_ticker_removed_from_watchlists(capfd, piker_dir):
|
def test_ticker_removed_from_watchlists(capfd, piker_dir, ex_watchlists):
|
||||||
expected_out = {
|
expected_out = ex_watchlists.copy()
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
expected_out['dad'].remove('SPY')
|
||||||
'indexes': ['DAX', 'DIA', 'SPY'],
|
run(f"piker watchlists -d {piker_dir} remove dad SPY")
|
||||||
'pharma': ['ATE.VN'],
|
|
||||||
}
|
|
||||||
run(f"piker watchlists -d {piker_dir} remove indexes QQQ")
|
|
||||||
out = wl.ensure_watchlists(piker_dir)
|
out = wl.ensure_watchlists(piker_dir)
|
||||||
assert out == expected_out
|
assert out == expected_out
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
def test_group_deleted_from_watchlists(capfd, piker_dir):
|
|
||||||
expected_out = {
|
def test_group_deleted_from_watchlists(capfd, piker_dir, ex_watchlists):
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
expected_out = ex_watchlists.copy()
|
||||||
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
expected_out.pop('pharma')
|
||||||
}
|
|
||||||
run(f"piker watchlists -d {piker_dir} delete pharma")
|
run(f"piker watchlists -d {piker_dir} delete pharma")
|
||||||
out = wl.ensure_watchlists(piker_dir)
|
out = wl.ensure_watchlists(piker_dir)
|
||||||
assert out == expected_out
|
assert out == expected_out
|
||||||
|
|
|
@ -83,6 +83,14 @@ def test_ticker_is_removed():
|
||||||
assert wl_temp == {'test': ['TEST2.CN']}
|
assert wl_temp == {'test': ['TEST2.CN']}
|
||||||
assert not wl_temp.get('test2')
|
assert not wl_temp.get('test2')
|
||||||
|
|
||||||
|
# verify trying to remove from nonexistant list
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
wl.remove_ticker('doggy', 'TEST.CN', wl_temp)
|
||||||
|
|
||||||
|
# verify trying to remove non-existing ticker
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
wl.remove_ticker('test', 'TEST.CN', wl_temp)
|
||||||
|
|
||||||
|
|
||||||
def test_group_is_deleted():
|
def test_group_is_deleted():
|
||||||
"""Check that watchlist group is removed.
|
"""Check that watchlist group is removed.
|
||||||
|
|
Loading…
Reference in New Issue