Commit 6b1d4af2 authored by Theophile GENTILHOMME's avatar Theophile GENTILHOMME
Browse files

[test][test_config] Fit tests to Click framework

Unitests à la Click.
parent d38b7dac
......@@ -29,16 +29,18 @@
# Basic tests for the command line beat program: config
import os
import click
from click.testing import CliRunner
import nose.tools
from nose.tools import assert_raises
import simplejson
from . import tmp_prefix, temp_cwd
from . import tmp_prefix
from ..scripts.beat import main
from beat.core.test.utils import cleanup
from .. import config
from .. import common
from beat.cmdline.scripts import main_cli
def call(*args, **kwargs):
......@@ -53,7 +55,10 @@ def call(*args, **kwargs):
def test_config_list():
nose.tools.eq_(call('config', 'show'), 0)
runner = CliRunner()
result = runner.invoke(main_cli.main, ['config', 'show'])
assert result.exit_code == 0, (result.exit_code, result.output)
def test_config_cache():
......@@ -69,10 +74,13 @@ def test_config_cache():
@nose.tools.with_setup(teardown=cleanup)
def test_set_local_token():
token_value = '123456abcdef'
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', 'token', token_value), 0)
config = os.path.join(d, '.beatrc')
token_value = '123456abcdefffff'
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'set', '--local',
'token', token_value])
assert result.exit_code == 0, (result.exit_code, result.output)
config = '.beatrc'
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents['token'] == token_value
......@@ -81,17 +89,21 @@ def test_set_local_token():
@nose.tools.with_setup(teardown=cleanup)
def test_set_local_multiple():
token_value = '123456abcde123456abcde123456abcdefff123456abcdef'
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', 'token', token_value), 0)
config = os.path.join(d, '.beatrc')
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'set', '--local',
'token', token_value])
assert result.exit_code == 0, (result.exit_code, result.output)
config = '.beatrc'
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents['token'] == token_value
# then we reduce the token size and see if the written file gets messed-up
token_value = '123456'
nose.tools.eq_(call('config', 'set', '--local', 'token', token_value), 0)
config = os.path.join(d, '.beatrc')
result = runner.invoke(main_cli.main, ['config', 'set', '--local',
'token', token_value])
assert result.exit_code == 0, (result.exit_code, result.output)
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents['token'] == token_value
......@@ -101,9 +113,12 @@ def test_set_local_multiple():
def test_set_local_atnt_db():
db_config = 'database/atnt'
db_path = './atnt_db'
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', db_config, db_path), 0)
config = os.path.join(d, '.beatrc')
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'set', '--local',
db_config, db_path])
assert result.exit_code == 0, (result.exit_code, result.output)
config = '.beatrc'
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents[db_config] == db_path
......@@ -113,31 +128,41 @@ def test_set_local_atnt_db():
def test_set_get_local_atnt_db():
db_config = 'database/atnt'
db_path = './atnt_db'
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', db_config, db_path), 0)
nose.tools.eq_(call('config', 'get', db_config), 0)
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'set', '--local',
db_config, db_path])
assert result.exit_code == 0, (result.exit_code, result.output)
result = runner.invoke(main_cli.main, ['config', 'get', db_config])
assert result.exit_code == 0, (result.exit_code, result.output)
assert db_path in result.output
@nose.tools.with_setup(teardown=cleanup)
def test_set_bad_config_key():
db_config = 'fail'
with assert_raises(SystemExit) as c, temp_cwd() as d:
call('config', 'set', '--local', db_config, db_config)
assert c.exception.code == 1
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'set', '--local',
db_config, db_config])
assert result.exit_code == 1, (result.exit_code, result.output)
@nose.tools.with_setup(teardown=cleanup)
@nose.tools.raises(KeyError)
def test_get_bad_config_key():
db_config = 'fail'
nose.tools.eq_(call('config', 'get', db_config), 1)
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'get', db_config])
assert result.exit_code == -1, (result.exit_code, result.output)
@nose.tools.with_setup(teardown=cleanup)
def test_get_token():
nose.tools.eq_(call('config', 'get', 'token'), 0)
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(main_cli.main, ['config', 'get', 'token'])
assert result.exit_code == 0, (result.exit_code, result.output)
@nose.tools.with_setup(teardown=cleanup)
def test_get_editor():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment