diff options
author | Irina Truong <i.chernyavska@gmail.com> | 2018-09-21 13:36:23 -0700 |
---|---|---|
committer | Irina Truong <i.chernyavska@gmail.com> | 2018-09-22 04:03:11 +0000 |
commit | 683487a873ac0b162442b2c2fbfe89fc5335acc9 (patch) | |
tree | bf56ce3b84a2ba01d4be05bab5007f7cdcfe609c /tests | |
parent | 5834259535e1cf5a75204305b0533ac5559ff344 (diff) |
Attempt to fix failing tests. Add some debug info. Make imports py3-compatible.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/conftest.py | 6 | ||||
-rw-r--r-- | tests/features/environment.py | 42 | ||||
-rw-r--r-- | tests/features/fixture_utils.py | 5 | ||||
-rw-r--r-- | tests/features/steps/auto_vertical.py | 7 | ||||
-rw-r--r-- | tests/features/steps/basic_commands.py | 21 | ||||
-rw-r--r-- | tests/features/steps/crud_database.py | 4 | ||||
-rw-r--r-- | tests/features/steps/crud_table.py | 4 | ||||
-rw-r--r-- | tests/features/steps/expanded.py | 4 | ||||
-rw-r--r-- | tests/features/steps/iocommands.py | 4 | ||||
-rw-r--r-- | tests/features/steps/named_queries.py | 4 | ||||
-rw-r--r-- | tests/features/steps/specials.py | 4 | ||||
-rw-r--r-- | tests/features/steps/wrappers.py | 2 | ||||
-rw-r--r-- | tests/test_main.py | 7 | ||||
-rw-r--r-- | tests/test_pgexecute.py | 3 | ||||
-rw-r--r-- | tests/test_prompt_utils.py | 7 | ||||
-rw-r--r-- | tests/test_smart_completion_multiple_schemata.py | 5 | ||||
-rw-r--r-- | tests/test_smart_completion_public_schema_only.py | 5 |
18 files changed, 81 insertions, 53 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/tests/__init__.py diff --git a/tests/conftest.py b/tests/conftest.py index 5eceb462..c0b666f2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,9 @@ +from __future__ import print_function, absolute_import + import os import pytest -from utils import (POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD, create_db, db_connection, - drop_tables) +from tests.utils import (POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD, create_db, db_connection, + drop_tables) import pgcli.pgexecute diff --git a/tests/features/environment.py b/tests/features/environment.py index e8271623..f5dc3e93 100644 --- a/tests/features/environment.py +++ b/tests/features/environment.py @@ -1,22 +1,24 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals -from __future__ import print_function +from __future__ import unicode_literals, print_function, absolute_import +import copy import os import sys -import db_utils as dbutils -import fixture_utils as fixutils +import tests.features.db_utils as dbutils +import tests.features.fixture_utils as fixutils import pexpect import tempfile import shutil -from steps.wrappers import run_cli, wait_prompt + +from tests.features.steps import wrappers def before_all(context): """ Set env parameters. """ + env_old = copy.deepcopy(dict(os.environ)) os.environ['LINES'] = "100" os.environ['COLUMNS'] = "100" os.environ['PAGER'] = 'cat' @@ -24,6 +26,10 @@ def before_all(context): context.package_root = os.path.abspath( os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) + fixture_dir = os.path.join(context.package_root, 'tests/features/fixture_data') + + print('package root:', context.package_root) + print('fixture dir:', fixture_dir) os.environ["COVERAGE_PROCESS_START"] = os.path.join(context.package_root, '.coveragerc') @@ -97,11 +103,24 @@ def before_all(context): context.conf['pass'], context.conf['dbname'], context.conf['port']) - context.fixture_data = fixutils.read_fixture_files() + context.fixture_data = fixutils.read_fixture_files(fixture_dir) # use temporary directory as config home context.env_config_home = tempfile.mkdtemp(prefix='pgcli_home_') os.environ['XDG_CONFIG_HOME'] = context.env_config_home + show_env_changes(env_old, dict(os.environ)) + + +def show_env_changes(env_old, env_new): + """Print out all test-specific env values.""" + print('--- os.environ changed values: ---') + all_keys = set(list(env_old.keys()) + list(env_new.keys())) + for k in sorted(all_keys): + old_value = env_old.get(k, '') + new_value = env_new.get(k, '') + if new_value and old_value != new_value: + print('{}="{}"'.format(k, new_value)) + print('-' * 20) def after_all(context): @@ -129,13 +148,12 @@ def before_step(context, _): def before_scenario(context, _): - run_cli(context) - wait_prompt(context) + wrappers.run_cli(context) + wrappers.wait_prompt(context) def after_scenario(context, _): - """Cleans up after each test complete.""" - + """Cleans up after each scenario completes.""" if hasattr(context, 'cli') and not context.exit_sent: # Quit nicely. if not context.atprompt: @@ -147,6 +165,10 @@ def after_scenario(context, _): context.cli.sendcontrol('c') context.cli.sendcontrol('d') context.cli.expect_exact(pexpect.EOF, timeout=10) + if hasattr(context, 'tmpfile_sql_help') and context.tmpfile_sql_help: + context.tmpfile_sql_help.close() + context.tmpfile_sql_help = None + # TODO: uncomment to debug a failure # def after_step(context, step): diff --git a/tests/features/fixture_utils.py b/tests/features/fixture_utils.py index 917baeb3..dcc09db4 100644 --- a/tests/features/fixture_utils.py +++ b/tests/features/fixture_utils.py @@ -18,14 +18,11 @@ def read_fixture_lines(filename): return lines -def read_fixture_files(): +def read_fixture_files(fixture_dir): """ Read all files inside fixture_data directory. """ fixture_dict = {} - - current_dir = os.path.dirname(__file__) - fixture_dir = os.path.join(current_dir, 'fixture_data/') for filename in os.listdir(fixture_dir): if filename not in ['.', '..']: fullname = os.path.join(fixture_dir, filename) diff --git a/tests/features/steps/auto_vertical.py b/tests/features/steps/auto_vertical.py index 215a1a2b..77487536 100644 --- a/tests/features/steps/auto_vertical.py +++ b/tests/features/steps/auto_vertical.py @@ -1,10 +1,9 @@ # -*- coding: utf-8 -from __future__ import unicode_literals -from textwrap import dedent +from __future__ import unicode_literals, print_function, absolute_import +from textwrap import dedent from behave import then, when - -import wrappers +from tests.features.steps import wrappers @when('we run dbcli with {arg}') diff --git a/tests/features/steps/basic_commands.py b/tests/features/steps/basic_commands.py index 83e05831..154881fb 100644 --- a/tests/features/steps/basic_commands.py +++ b/tests/features/steps/basic_commands.py @@ -4,13 +4,13 @@ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import import tempfile -from behave import when -import wrappers +from behave import when, then from textwrap import dedent +from tests.features.steps import wrappers @when('we run dbcli') @@ -28,6 +28,9 @@ def step_ctrl_d(context): """ Send Ctrl + D to hopefully exit. """ + # turn off pager before exiting + context.cli.sendline('\pset pager off') + wrappers.wait_prompt(context) context.cli.sendcontrol('d') context.exit_sent = True @@ -42,12 +45,12 @@ def step_send_help(context): @when(u'we send source command') def step_send_source_command(context): - with tempfile.NamedTemporaryFile() as f: - f.write(b'\?') - f.flush() - context.cli.sendline('\i {0}'.format(f.name)) - wrappers.expect_exact( - context, context.conf['pager_boundary'] + '\r\n', timeout=5) + context.tmpfile_sql_help = tempfile.NamedTemporaryFile(prefix='pgcli_') + context.tmpfile_sql_help.write(b'\?') + context.tmpfile_sql_help.flush() + context.cli.sendline('\i {0}'.format(context.tmpfile_sql_help.name)) + wrappers.expect_exact( + context, context.conf['pager_boundary'] + '\r\n', timeout=5) @when(u'we run query to check application_name') diff --git a/tests/features/steps/crud_database.py b/tests/features/steps/crud_database.py index f049ee7e..a46f53bc 100644 --- a/tests/features/steps/crud_database.py +++ b/tests/features/steps/crud_database.py @@ -4,12 +4,12 @@ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import import pexpect -import wrappers from behave import when, then +from tests.features.steps import wrappers @when('we create database') diff --git a/tests/features/steps/crud_table.py b/tests/features/steps/crud_table.py index 360f78f9..4dad0703 100644 --- a/tests/features/steps/crud_table.py +++ b/tests/features/steps/crud_table.py @@ -4,11 +4,11 @@ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import -import wrappers from behave import when, then from textwrap import dedent +from tests.features.steps import wrappers @when('we create table') diff --git a/tests/features/steps/expanded.py b/tests/features/steps/expanded.py index b1970a34..6e1af6c0 100644 --- a/tests/features/steps/expanded.py +++ b/tests/features/steps/expanded.py @@ -5,11 +5,11 @@ Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import -import wrappers from behave import when, then from textwrap import dedent +from tests.features.steps import wrappers @when('we prepare the test data') diff --git a/tests/features/steps/iocommands.py b/tests/features/steps/iocommands.py index afedc91c..b3954a83 100644 --- a/tests/features/steps/iocommands.py +++ b/tests/features/steps/iocommands.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import import os import os.path -import wrappers from behave import when, then +from tests.features.steps import wrappers @when('we start external editor providing a file name') diff --git a/tests/features/steps/named_queries.py b/tests/features/steps/named_queries.py index 8f3c6d6d..57be08b0 100644 --- a/tests/features/steps/named_queries.py +++ b/tests/features/steps/named_queries.py @@ -4,10 +4,10 @@ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import -import wrappers from behave import when, then +from tests.features.steps import wrappers @when('we save a named query') diff --git a/tests/features/steps/specials.py b/tests/features/steps/specials.py index 851adeb7..268c531e 100644 --- a/tests/features/steps/specials.py +++ b/tests/features/steps/specials.py @@ -4,10 +4,10 @@ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import -import wrappers from behave import when, then +from tests.features.steps import wrappers @when('we refresh completions') diff --git a/tests/features/steps/wrappers.py b/tests/features/steps/wrappers.py index 080167d7..de49cede 100644 --- a/tests/features/steps/wrappers.py +++ b/tests/features/steps/wrappers.py @@ -30,6 +30,8 @@ def run_cli(context, run_args=None): context.cli = pexpect.spawnu(cmd, cwd=context.package_root) context.exit_sent = False context.currentdb = context.conf['dbname'] + context.cli.sendline('\pset pager always') + wait_prompt(context) def wait_prompt(context): diff --git a/tests/test_main.py b/tests/test_main.py index 8651a07a..7d6d773e 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,9 +1,8 @@ # coding=utf-8 -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import import os import platform import mock -from decimal import Decimal import pytest try: @@ -14,7 +13,9 @@ except ImportError: from pgcli.main import ( obfuscate_process_password, format_output, PGCli, OutputSettings ) -from utils import dbtest, run +from pgspecial.main import (PAGER_OFF, PAGER_LONG_OUTPUT, PAGER_ALWAYS) +from tests.utils import dbtest, run +from collections import namedtuple @pytest.mark.skipif(platform.system() == 'Windows', diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index da055fb6..3644a020 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -1,11 +1,12 @@ # coding=UTF-8 +from __future__ import print_function, absolute_import import pytest import psycopg2 from mock import patch from pgcli.packages.parseutils.meta import FunctionMetadata from textwrap import dedent -from utils import run, dbtest, requires_json, requires_jsonb +from tests.utils import run, dbtest, requires_json, requires_jsonb from pgcli.main import PGCli diff --git a/tests/test_prompt_utils.py b/tests/test_prompt_utils.py index de676366..7fb7482e 100644 --- a/tests/test_prompt_utils.py +++ b/tests/test_prompt_utils.py @@ -8,7 +8,6 @@ from pgcli.packages.prompt_utils import confirm_destructive_query def test_confirm_destructive_query_notty(): stdin = click.get_text_stream('stdin') - assert stdin.isatty() is False - - sql = 'drop database foo;' - assert confirm_destructive_query(sql) is None + if not stdin.isatty(): + sql = 'drop database foo;' + assert confirm_destructive_query(sql) is None diff --git a/tests/test_smart_completion_multiple_schemata.py b/tests/test_smart_completion_multiple_schemata.py index 5935af99..49aee7cf 100644 --- a/tests/test_smart_completion_multiple_schemata.py +++ b/tests/test_smart_completion_multiple_schemata.py @@ -1,6 +1,7 @@ -from __future__ import unicode_literals +from __future__ import unicode_literals, print_function, absolute_import + import itertools -from metadata import (MetaData, alias, name_join, fk_join, join, +from tests.metadata import (MetaData, alias, name_join, fk_join, join, schema, table, function, wildcard_expansion, column, get_result, result_set, qual, no_qual, parametrize) diff --git a/tests/test_smart_completion_public_schema_only.py b/tests/test_smart_completion_public_schema_only.py index 0e676065..93a55a95 100644 --- a/tests/test_smart_completion_public_schema_only.py +++ b/tests/test_smart_completion_public_schema_only.py @@ -1,5 +1,6 @@ -from __future__ import unicode_literals -from metadata import (MetaData, alias, name_join, fk_join, join, keyword, +from __future__ import unicode_literals, print_function, absolute_import + +from tests.metadata import (MetaData, alias, name_join, fk_join, join, keyword, schema, table, view, function, column, wildcard_expansion, get_result, result_set, qual, no_qual, parametrize) from prompt_toolkit.completion import Completion |