Skip to content

Fix command line parser for dump/import/export commands #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rethinkdb/_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def parse_options(argv, prog=None):
dest="db_tables",
metavar="DB|DB.TABLE",
default=[],
type='db_table',
help='limit dump to the given database or table (may be specified multiple times)',
action="append")

Expand Down
20 changes: 12 additions & 8 deletions rethinkdb/utils_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ def format_epilog(self, formatter):
def __init__(self, *args, **kwargs):
# -- Type Checkers

def check_tls_option(opt_str, value):
def check_tls_option(_, opt_str, value):
value = str(value)

if os.path.isfile(value):
return {'ca_certs': os.path.realpath(value)}
else:
raise optparse.OptionValueError('Option %s value is not a file: %r' % (opt_str, value))

def check_db_table_option(value):
def check_db_table_option(_, _opt_str, value):
res = _tableNameRegex.match(value)

if not res:
Expand All @@ -171,19 +171,23 @@ def check_db_table_option(value):

return DbTable(res.group('db'), res.group('table'))

def check_positive_int(opt_str, value):
if not isinstance(value, int) or value < 1:
raise optparse.OptionValueError('%s value must be an integer greater that 1: %s' % (opt_str, value))
def check_positive_int(_, opt_str, value):
try:
value = int(value)
if value < 1:
raise ValueError
except ValueError:
raise optparse.OptionValueError('%s value must be an integer greater than 1: %s' % (opt_str, value))

return int(value)
return value

def check_existing_file(_, opt_str, value):
if not os.path.isfile(value):
raise optparse.OptionValueError('%s value was not an existing file: %s' % (opt_str, value))

return os.path.realpath(value)

def check_new_file_location(opt_str, value):
def check_new_file_location(_, opt_str, value):
try:
real_value = os.path.realpath(value)
except Exception:
Expand All @@ -194,7 +198,7 @@ def check_new_file_location(opt_str, value):

return real_value

def file_contents(opt_str, value):
def file_contents(_, opt_str, value):
if not os.path.isfile(value):
raise optparse.OptionValueError('%s value is not an existing file: %r' % (opt_str, value))

Expand Down
73 changes: 73 additions & 0 deletions tests/test_utils_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest
from rethinkdb import utils_common


@pytest.fixture
def parser():
opt_parser = utils_common.CommonOptionsParser()
opt_parser.add_option(
"-e",
"--export",
dest="db_tables",
metavar="DB|DB.TABLE",
default=[],
type='db_table',
action="append")
opt_parser.add_option(
"--clients",
dest="clients",
metavar="NUM",
default=3,
type="pos_int")
return opt_parser


def test_option_parser_int_pos(parser):
options, args = parser.parse_args(['--clients', '4'], connect=False)

assert options.clients == 4


def test_option_parser_int_pos_equals(parser):
options, args = parser.parse_args(['--clients=4'], connect=False)

assert options.clients == 4


def test_option_parser_int_pos_default(parser):
options, args = parser.parse_args([], connect=False)

assert options.clients == 3


def test_option_parser_int_pos_fail(parser):
with pytest.raises(SystemExit):
parser.parse_args(['--clients=asdf'], connect=False)


def test_option_parser_int_pos_zero(parser):
with pytest.raises(SystemExit):
parser.parse_args(['--clients=0'], connect=False)


def test_option_parser_db_table(parser):
options, args = parser.parse_args(['--export=example.table'], connect=False)

assert options.db_tables == [('example', 'table')]


def test_option_parser_db_table_append(parser):
options, args = parser.parse_args(['--export=example.table', '--export=example.another'], connect=False)

assert options.db_tables == [('example', 'table'), ('example', 'another')]


def test_option_parser_db_table_only_db(parser):
options, args = parser.parse_args(['--export=example'], connect=False)

assert options.db_tables == [('example', None)]


def test_option_parser_db_table_fail(parser):
with pytest.raises(SystemExit):
parser.parse_args(['--export='], connect=False)