mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Add tests for argparse
This commit is contained in:
parent
7944e0cd98
commit
9b709b7f98
@ -245,8 +245,7 @@ def run_set_replicas(args):
|
|||||||
except r.ReqlOpFailedError as e:
|
except r.ReqlOpFailedError as e:
|
||||||
logger.warn(e)
|
logger.warn(e)
|
||||||
|
|
||||||
|
def create_parser():
|
||||||
def main():
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Control your BigchainDB node.',
|
description='Control your BigchainDB node.',
|
||||||
parents=[utils.base_parser])
|
parents=[utils.base_parser])
|
||||||
@ -325,8 +324,8 @@ def main():
|
|||||||
'is set, the count is distributed equally to all the '
|
'is set, the count is distributed equally to all the '
|
||||||
'processes')
|
'processes')
|
||||||
|
|
||||||
utils.start(parser, globals())
|
return parser
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def main():
|
||||||
main()
|
utils.start(create_parser(), sys.argv[1:], globals())
|
||||||
|
@ -59,7 +59,7 @@ def start_rethinkdb():
|
|||||||
raise StartupError(line)
|
raise StartupError(line)
|
||||||
|
|
||||||
|
|
||||||
def start(parser, scope):
|
def start(parser, argv, scope):
|
||||||
"""Utility function to execute a subcommand.
|
"""Utility function to execute a subcommand.
|
||||||
|
|
||||||
The function will look up in the ``scope``
|
The function will look up in the ``scope``
|
||||||
@ -74,11 +74,11 @@ def start(parser, scope):
|
|||||||
NotImplementedError: if ``scope`` doesn't contain a function called
|
NotImplementedError: if ``scope`` doesn't contain a function called
|
||||||
``run_<parser.args.command>``.
|
``run_<parser.args.command>``.
|
||||||
"""
|
"""
|
||||||
args = parser.parse_args()
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
if not args.command:
|
if not args.command:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
return
|
raise SystemExit()
|
||||||
|
|
||||||
# look up in the current scope for a function called 'run_<command>'
|
# look up in the current scope for a function called 'run_<command>'
|
||||||
# replacing all the dashes '-' with the lowercase character '_'
|
# replacing all the dashes '-' with the lowercase character '_'
|
||||||
@ -96,7 +96,7 @@ def start(parser, scope):
|
|||||||
elif args.multiprocess is None:
|
elif args.multiprocess is None:
|
||||||
args.multiprocess = mp.cpu_count()
|
args.multiprocess = mp.cpu_count()
|
||||||
|
|
||||||
func(args)
|
return func(args)
|
||||||
|
|
||||||
|
|
||||||
base_parser = argparse.ArgumentParser(add_help=False, prog='bigchaindb')
|
base_parser = argparse.ArgumentParser(add_help=False, prog='bigchaindb')
|
||||||
|
@ -62,6 +62,34 @@ def mock_bigchaindb_backup_config(monkeypatch):
|
|||||||
monkeypatch.setattr('bigchaindb._config', config)
|
monkeypatch.setattr('bigchaindb._config', config)
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_sure_we_dont_remove_any_command():
|
||||||
|
# thanks to: http://stackoverflow.com/a/18161115/597097
|
||||||
|
from bigchaindb.commands.bigchain import utils
|
||||||
|
from bigchaindb.commands.bigchain import create_parser
|
||||||
|
parser = create_parser()
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
utils.start(parser, [], {})
|
||||||
|
|
||||||
|
assert parser.parse_args(['configure']).command
|
||||||
|
assert parser.parse_args(['show-config']).command
|
||||||
|
assert parser.parse_args(['export-my-pubkey']).command
|
||||||
|
assert parser.parse_args(['init']).command
|
||||||
|
assert parser.parse_args(['drop']).command
|
||||||
|
assert parser.parse_args(['start']).command
|
||||||
|
assert parser.parse_args(['set-shards', '1']).command
|
||||||
|
assert parser.parse_args(['set-replicas', '1']).command
|
||||||
|
assert parser.parse_args(['load']).command
|
||||||
|
|
||||||
|
|
||||||
|
@patch('bigchaindb.commands.utils.start')
|
||||||
|
def test_main_entrypoint(mock_start):
|
||||||
|
from bigchaindb.commands.bigchain import main
|
||||||
|
main()
|
||||||
|
|
||||||
|
assert mock_start.called
|
||||||
|
|
||||||
|
|
||||||
def test_bigchain_run_start(mock_run_configure, mock_processes_start, mock_db_init_with_existing_db):
|
def test_bigchain_run_start(mock_run_configure, mock_processes_start, mock_db_init_with_existing_db):
|
||||||
from bigchaindb.commands.bigchain import run_start
|
from bigchaindb.commands.bigchain import run_start
|
||||||
args = Namespace(start_rethinkdb=False, allow_temp_keypair=False, config=None, yes=True)
|
args = Namespace(start_rethinkdb=False, allow_temp_keypair=False, config=None, yes=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user