diff options
author | Irina Truong <i.chernyavska@gmail.com> | 2017-09-05 14:12:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-05 14:12:41 -0700 |
commit | e733b0ea914a6744f7ffd96569b75437244aa4de (patch) | |
tree | c2c26e6544c6690dbc9d7a55ac406706849f4371 | |
parent | 8c8b6816ca9bea1d4611e23ad73a22018605371a (diff) | |
parent | ca88e62a174d94ddbcb7042c39abff7370e62a9a (diff) |
Merge pull request #782 from dbcli/feature/auto_vertical
Port auto_vertical feature test from mycli to pgcli
-rw-r--r-- | changelog.rst | 1 | ||||
-rwxr-xr-x | pgcli/main.py | 13 | ||||
-rw-r--r-- | tests/features/auto_vertical.feature | 12 | ||||
-rw-r--r-- | tests/features/steps/auto_vertical.py | 92 | ||||
-rw-r--r-- | tests/features/steps/wrappers.py | 7 |
5 files changed, 119 insertions, 6 deletions
diff --git a/changelog.rst b/changelog.rst index a38d1318..2bfe3f75 100644 --- a/changelog.rst +++ b/changelog.rst @@ -15,6 +15,7 @@ Internal changes: * Preliminary work for a future change in outputting results that uses less memory. (Thanks: `Dick Marinus`_) * Remove import workaround for OrderedDict, required for python < 2.7. (Thanks: `Andrew Speed`_) * Use less memory when formatting results for display (Thanks: `Dick Marinus`_). +* Port auto_vertical feature test from mycli to pgcli. (Thanks: `Dick Marinus`_) Bug Fixes: ---------- diff --git a/pgcli/main.py b/pgcli/main.py index f3aa0d7f..6742ad50 100755 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -119,7 +119,8 @@ class PGCli(object): def __init__(self, force_passwd_prompt=False, never_passwd_prompt=False, pgexecute=None, pgclirc_file=None, row_limit=None, - single_connection=False, less_chatty=None, prompt=None): + single_connection=False, less_chatty=None, prompt=None, + auto_vertical_output=False): self.force_passwd_prompt = force_passwd_prompt self.never_passwd_prompt = never_passwd_prompt @@ -138,7 +139,8 @@ class PGCli(object): self.multi_line = c['main'].as_bool('multi_line') self.multiline_mode = c['main'].get('multi_line_mode', 'psql') self.vi_mode = c['main'].as_bool('vi') - self.auto_expand = c['main'].as_bool('auto_expand') + self.auto_expand = auto_vertical_output or c['main'].as_bool( + 'auto_expand') self.expanded_output = c['main'].as_bool('expand') self.pgspecial.timing_enabled = c['main'].as_bool('timing') if row_limit is not None: @@ -820,11 +822,13 @@ class PGCli(object): @click.option('--prompt', help='Prompt format (Default: "\\u@\\h:\\d> ").') @click.option('-l', '--list', 'list_databases', is_flag=True, help='list ' 'available databases, then exit.') +@click.option('--auto-vertical-output', is_flag=True, + help='Automatically switch to vertical output mode if the result is wider than the terminal width.') @click.argument('database', default=lambda: None, envvar='PGDATABASE', nargs=1) @click.argument('username', default=lambda: None, envvar='PGUSER', nargs=1) def cli(database, username_opt, host, port, prompt_passwd, never_prompt, single_connection, dbname, username, version, pgclirc, dsn, row_limit, - less_chatty, prompt, list_databases): + less_chatty, prompt, list_databases, auto_vertical_output): if version: print('Version:', __version__) @@ -848,7 +852,8 @@ def cli(database, username_opt, host, port, prompt_passwd, never_prompt, pgcli = PGCli(prompt_passwd, never_prompt, pgclirc_file=pgclirc, row_limit=row_limit, single_connection=single_connection, - less_chatty=less_chatty, prompt=prompt) + less_chatty=less_chatty, prompt=prompt, + auto_vertical_output=auto_vertical_output) # Choose which ever one has a valid value. database = database or dbname diff --git a/tests/features/auto_vertical.feature b/tests/features/auto_vertical.feature new file mode 100644 index 00000000..aa957186 --- /dev/null +++ b/tests/features/auto_vertical.feature @@ -0,0 +1,12 @@ +Feature: auto_vertical mode: + on, off + + Scenario: auto_vertical on with small query + When we run dbcli with --auto-vertical-output + and we execute a small query + then we see small results in horizontal format + + Scenario: auto_vertical on with large query + When we run dbcli with --auto-vertical-output + and we execute a large query + then we see large results in vertical format diff --git a/tests/features/steps/auto_vertical.py b/tests/features/steps/auto_vertical.py new file mode 100644 index 00000000..215a1a2b --- /dev/null +++ b/tests/features/steps/auto_vertical.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 +from __future__ import unicode_literals +from textwrap import dedent + +from behave import then, when + +import wrappers + + +@when('we run dbcli with {arg}') +def step_run_cli_with_arg(context, arg): + wrappers.run_cli(context, run_args=arg.split('=')) + + +@when('we execute a small query') +def step_execute_small_query(context): + context.cli.sendline('select 1') + + +@when('we execute a large query') +def step_execute_large_query(context): + context.cli.sendline( + 'select {}'.format(','.join([str(n) for n in range(1, 50)]))) + + +@then('we see small results in horizontal format') +def step_see_small_results(context): + wrappers.expect_pager(context, dedent("""\ + +------------+\r + | ?column? |\r + |------------|\r + | 1 |\r + +------------+\r + SELECT 1\r + """), timeout=5) + + +@then('we see large results in vertical format') +def step_see_large_results(context): + wrappers.expect_pager(context, dedent("""\ + -[ RECORD 1 ]-------------------------\r + ?column? | 1\r + ?column? | 2\r + ?column? | 3\r + ?column? | 4\r + ?column? | 5\r + ?column? | 6\r + ?column? | 7\r + ?column? | 8\r + ?column? | 9\r + ?column? | 10\r + ?column? | 11\r + ?column? | 12\r + ?column? | 13\r + ?column? | 14\r + ?column? | 15\r + ?column? | 16\r + ?column? | 17\r + ?column? | 18\r + ?column? | 19\r + ?column? | 20\r + ?column? | 21\r + ?column? | 22\r + ?column? | 23\r + ?column? | 24\r + ?column? | 25\r + ?column? | 26\r + ?column? | 27\r + ?column? | 28\r + ?column? | 29\r + ?column? | 30\r + ?column? | 31\r + ?column? | 32\r + ?column? | 33\r + ?column? | 34\r + ?column? | 35\r + ?column? | 36\r + ?column? | 37\r + ?column? | 38\r + ?column? | 39\r + ?column? | 40\r + ?column? | 41\r + ?column? | 42\r + ?column? | 43\r + ?column? | 44\r + ?column? | 45\r + ?column? | 46\r + ?column? | 47\r + ?column? | 48\r + ?column? | 49\r + SELECT 1\r + """), timeout=5) diff --git a/tests/features/steps/wrappers.py b/tests/features/steps/wrappers.py index 2444ef0d..080167d7 100644 --- a/tests/features/steps/wrappers.py +++ b/tests/features/steps/wrappers.py @@ -21,10 +21,13 @@ def expect_pager(context, expected, timeout): context.conf['pager_boundary'], expected), timeout=timeout) -def run_cli(context): +def run_cli(context, run_args=None): """Run the process using pexpect.""" + run_args = run_args or [] cli_cmd = context.conf.get('cli_command') - context.cli = pexpect.spawnu(cli_cmd, cwd=context.package_root) + cmd_parts = [cli_cmd] + run_args + cmd = ' '.join(cmd_parts) + context.cli = pexpect.spawnu(cmd, cwd=context.package_root) context.exit_sent = False context.currentdb = context.conf['dbname'] |