Skip to content
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
6 changes: 1 addition & 5 deletions mssqlcli/mssql_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,7 @@ def __init__(self, options):
self.multiline_mode = c['main'].get('multi_line_mode', 'tsql')
self.vi_mode = c['main'].as_bool('vi')
self.prompt_format = options.prompt or c['main'].get('prompt', self.default_prompt)
if options.row_limit is not None:
self.row_limit = options.row_limit
else:
self.row_limit = c['main'].as_int('row_limit')

self.row_limit = options.row_limit
self.min_num_menu_lines = c['main'].as_int('min_num_menu_lines')
self.multiline_continuation_char = c['main']['multiline_continuation_char']
self.syntax_style = c['main']['syntax_style']
Expand Down
28 changes: 26 additions & 2 deletions mssqlcli/mssqlclioptionsparser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import argparse
import os
import sys
import click
import mssqlcli

from .config import config_location
from .config import config_location, get_config

MSSQL_CLI_USER = u'MSSQL_CLI_USER'
MSSQL_CLI_PASSWORD = u'MSSQL_CLI_PASSWORD'
Expand Down Expand Up @@ -70,7 +72,9 @@ def create_parser():
args_parser.add_argument(
u'--row-limit',
dest=u'row_limit',
default=os.environ.get(MSSQL_CLI_ROW_LIMIT, None),
default=get_config()['main'].as_int('row_limit') or \
os.environ.get(MSSQL_CLI_ROW_LIMIT, None),
type=check_row_limit,
metavar=u'',
help=u'Set threshold for row limit prompt. Use 0 to disable prompt.')

Expand Down Expand Up @@ -187,3 +191,23 @@ def create_parser():
)

return args_parser

def check_row_limit(row_limit):
"""
Validates row_limit option has valid positive integer
"""

try:
row_limit_int = int(row_limit)
except ValueError:
click.secho(u'Error: row-limit has been set to an invalid value.\nPlease ' \
u'specify a positive integer using the --row-limit command-line ' \
u'argument or by setting row_limit in the config file.', fg='red')
sys.exit(1)
else:
if row_limit_int < 0:
click.secho(u'Error: row-limit cannot be negative.\nPlease ' \
u'specify a positive integer using the --row-limit command-line ' \
u'argument or by setting row_limit in the config file.', fg='red')
sys.exit(1)
return row_limit_int
9 changes: 9 additions & 0 deletions tests/test_interactive_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def mssqlcli():
yield mssqlcli
shutdown(mssqlcli)


class TestInteractiveModeQueries(TestInteractiveMode):
@staticmethod
@pytest.mark.parametrize("query_str, test_file", test_queries)
Expand All @@ -37,6 +38,7 @@ def test_query(query_str, test_file, mssqlcli):
output_query = '\n'.join(mssqlcli.execute_query(query_str)).replace('\r', '')
assert output_query == output_baseline


class TestInteractiveModeInvalidRuns:
@pytest.mark.timeout(60)
def test_noninteractive_run(self):
Expand Down Expand Up @@ -83,6 +85,7 @@ def invalid_run(**options):
if mssqlcli is not None:
shutdown(mssqlcli)


class TestInteractiveModePager(TestInteractiveMode):
"""
Test default pager setting.
Expand Down Expand Up @@ -120,6 +123,12 @@ def test_pager_config(mssqlcli):
config['main']['pager'] = config_value
assert mssqlcli.set_default_pager(config) == config_value


class TestInteractiveModeRun:
"""
Tests the executable.
"""

@staticmethod
@pytest.mark.timeout(60)
def test_valid_command():
Expand Down
34 changes: 34 additions & 0 deletions tests/test_rowlimit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pylint: disable=protected-access
import unittest
import pytest
from mssqlcli.mssql_cli import MssqlCli
from mssqlcli.mssqlclioptionsparser import check_row_limit
from mssqltestutils import create_mssql_cli_options

class RowLimitTests(unittest.TestCase):
Expand Down Expand Up @@ -43,3 +45,35 @@ def test_row_limit_on_non_select(self):
result = cli._should_show_limit_prompt(stmt, ['row']*self.over_default)
assert cli.row_limit == 0
assert not result


class TestRowLimitArgs:
"""
Tests for valid row-limit arguments.
"""
# pylint: disable=protected-access

test_data_valid = [5, 0]
test_data_invalid = ['string!', -3]

@staticmethod
@pytest.mark.parametrize("row_limit", test_data_valid)
def test_valid_row_limit(row_limit):
"""
Test valid value types for row limit argument
"""
assert check_row_limit(row_limit) == row_limit

@staticmethod
@pytest.mark.parametrize("row_limit", test_data_invalid)
def test_invalid_row_limit(row_limit):
"""
Test invalid value types for row limit argument
"""
try:
check_row_limit(row_limit)
except SystemExit:
# mssqlcli class calls sys.exit(1) on invalid value
assert True
else:
assert False