tenkan/tests/cli_test.py

82 lines
2.5 KiB
Python
Raw Permalink Normal View History

2022-01-12 21:31:56 +01:00
# -*- coding: utf-8 -*-
import configparser
from pathlib import Path
import pytest
from tenkan.cli import load_args, load_config, run
from tenkan.feedsfile import add_feed, read
def test_config_loaded():
config_file = Path('./tests/data/tenkan.conf')
res = load_config(config_file)
assert isinstance(res, configparser.ConfigParser)
def test_config_tenkan_section_missing():
config_file = Path('./tests/data/tenkan.conf_fail')
with pytest.raises(SystemExit) as pytest_wrapped_e:
load_config(config_file)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
def test_arg_feedsfile_missing():
args = load_args(['--feedsfile', '/tmp/toto.json', 'list'])
config = Path('./tests/data/tenkan.conf')
with pytest.raises(SystemExit) as pytest_wrapped_e:
run(args, config)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
# def test_stupid_command():
# args = load_args(['bla'])
# config = Path('./tests/data/tenkan.conf')
# with pytest.raises(SystemExit) as pytest_wrapped_e:
# load_args(args)
# assert pytest_wrapped_e.type == SystemExit
# assert pytest_wrapped_e.value.code == 2
def test_add_cmd_feedsfile_missing(tmp_path):
feeds = tmp_path / 'toto.json'
args = load_args(['--feedsfile', str(feeds), 'add', 'blabla', 'blibli'])
config = Path('./tests/data/tenkan.conf')
run(args, config)
assert Path(f'{feeds}').is_file()
def test_add_bad_feedsfile_folder():
args = load_args(
['--feedsfile', '/tmp/tmp/tmp/titi.json', 'add', 'blabla', 'blibli']
)
config = Path('./tests/data/tenkan.conf')
with pytest.raises(SystemExit) as pytest_wrapped_e:
run(args, config)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1
def test_del_cmd():
feeds = Path('./tests/data/feeds.json')
args = load_args(['--feedsfile', str(feeds), 'delete', 'tutu'])
config = Path('./tests/data/tenkan.conf')
add_feed(file=feeds, feed_name='tutu', feed_url='tata')
run(args, config)
data = read(file=feeds)
assert not data['feeds'].get('tutu')
def test_update_cmd():
feeds = Path('./tests/data/feeds.json')
args = load_args(['--feedsfile', str(feeds), 'update'])
config = load_config(str(Path('./tests/data/tenkan.conf')))
data1 = read(file=feeds)['last_run']
run(args, config)
data2 = read(file=feeds)['last_run']
assert data1 != data2