diff options
-rw-r--r-- | behave.ini | 5 | ||||
-rw-r--r-- | features/db_utils.py | 81 | ||||
-rw-r--r-- | features/environment.py | 71 | ||||
-rw-r--r-- | features/simple_cli_commands.feature | 15 | ||||
-rw-r--r-- | features/steps/step_definitions.py | 34 | ||||
-rw-r--r-- | requirements-dev.txt | 5 |
6 files changed, 211 insertions, 0 deletions
diff --git a/behave.ini b/behave.ini new file mode 100644 index 00000000..e7b7b725 --- /dev/null +++ b/behave.ini @@ -0,0 +1,5 @@ +[behave.userdata] +pg_test_user = behave_user +pg_test_pass = +pg_test_host = localhost +pg_test_db = pgcli_behave_tests diff --git a/features/db_utils.py b/features/db_utils.py new file mode 100644 index 00000000..83bbbfb2 --- /dev/null +++ b/features/db_utils.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from __future__ import print_function + +from psycopg2 import connect +from psycopg2.extensions import AsIs + + +def create_db(hostname='localhost', username=None, password=None, + dbname=None): + """ + Create test database. + :param hostname: string + :param username: string + :param password: string + :param dbname: string + :return: + """ + cn = create_cn(hostname, password, username, 'postgres') + + # ISOLATION_LEVEL_AUTOCOMMIT = 0 + # Needed for DB creation. + cn.set_isolation_level(0) + + with cn.cursor() as cr: + cr.execute('create database %s', (AsIs(dbname),)) + + cn.close() + + cn = create_cn(hostname, password, username, dbname) + return cn + + +def create_cn(hostname, password, username, dbname): + """ + Open connection to database. + :param hostname: + :param password: + :param username: + :param dbname: string + :return: psycopg2.connection + """ + if password: + cn = connect(host=hostname, user=username, database=dbname, + password=password) + else: + cn = connect(host=hostname, user=username, database=dbname) + + print('Created connection: {0}.'.format(cn.dsn)) + return cn + + +def drop_db(hostname='localhost', username=None, password=None, + dbname=None): + """ + Drop database. + :param hostname: string + :param username: string + :param password: string + :param dbname: string + """ + cn = create_cn(hostname, password, username, 'postgres') + + # ISOLATION_LEVEL_AUTOCOMMIT = 0 + # Needed for DB drop. + cn.set_isolation_level(0) + + with cn.cursor() as cr: + cr.execute('drop database %s', (AsIs(dbname),)) + + cn.close() + + +def close_cn(cn=None): + """ + Close connection. + :param connection: psycopg2.connection + """ + if cn: + cn.close() + print('Closed connection: {0}.'.format(cn.dsn)) diff --git a/features/environment.py b/features/environment.py new file mode 100644 index 00000000..62711263 --- /dev/null +++ b/features/environment.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from __future__ import print_function + +import os +import pexpect +import db_utils as dbutils + + +def before_all(context): + """ + Set env parameters. + """ + os.environ['LINES'] = "50" + os.environ['COLUMNS'] = "120" + context.exit_sent = False + + # Store get params from config. + context.conf = { + 'host': context.config.userdata.get('pg_test_host', 'localhost'), + 'user': context.config.userdata.get('pg_test_user', 'root'), + 'pass': context.config.userdata.get('pg_test_pass', None), + 'dbname': context.config.userdata.get('pg_test_db', None), + } + + # Store old env vars. + context.pgenv = { + 'PGDATABASE': os.environ.get('PGDATABASE', None), + 'PGUSER': os.environ.get('PGUSER', None), + 'PGHOST': os.environ.get('PGHOST', None), + 'PGPASS': os.environ.get('PGPASS', None), + } + + # Set new env vars. + os.environ['PGDATABASE'] = context.conf['dbname'] + os.environ['PGUSER'] = context.conf['user'] + os.environ['PGHOST'] = context.conf['host'] + if context.conf['pass']: + os.environ['PGPASS'] = context.conf['pass'] + elif 'PGPASS' in os.environ: + del os.environ['PGPASS'] + + context.cn = dbutils.create_db(context.conf['host'], context.conf['user'], + context.conf['pass'], context.conf['dbname']) + + +def after_all(context): + """ + Unset env parameters. + """ + dbutils.close_cn(context.cn) + dbutils.drop_db(context.conf['host'], context.conf['user'], + context.conf['pass'], context.conf['dbname']) + + # Restore env vars. + for k, v in context.pgenv.items(): + if k in os.environ and v is None: + del os.environ[k] + elif v: + os.environ[k] = v + + +def after_scenario(context, _): + """ + Cleans up after each test complete. + """ + + if hasattr(context, 'cli') and not context.exit_sent: + # Send Ctrl + D into cli + context.cli.sendcontrol('d') + context.cli.expect(pexpect.EOF) diff --git a/features/simple_cli_commands.feature b/features/simple_cli_commands.feature new file mode 100644 index 00000000..c60f14b0 --- /dev/null +++ b/features/simple_cli_commands.feature @@ -0,0 +1,15 @@ +Feature: run the cli, + call the help command, + exit the cli + + Scenario: run the cli + Given we have pgcli installed + when we run pgcli + then we see pgcli prompt + + Scenario: run the cli and exit + Given we have pgcli installed + when we run pgcli + and we wait for prompt + and we send "ctrl + d" + then pgcli exits diff --git a/features/steps/step_definitions.py b/features/steps/step_definitions.py new file mode 100644 index 00000000..e8ecb4ae --- /dev/null +++ b/features/steps/step_definitions.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import pip +import pexpect + +from behave import given, when, then + +@given('we have pgcli installed') +def step_impl(context): + ds = set([di.key for di in pip.get_installed_distributions()]) + assert 'pgcli' in ds + +@when('we run pgcli') +def step_impl(context): + context.cli = pexpect.spawnu('pgcli') + + +@when('we wait for prompt') +def step_impl(context): + context.cli.expect('{0}> '.format(context.conf['dbname'])) + +@when('we send "ctrl + d"') +def step_impl(context): + context.cli.sendcontrol('d') + context.exit_sent = True + +@then('pgcli exits') +def step_impl(context): + context.cli.expect(pexpect.EOF) + +@then('we see pgcli prompt') +def step_impl(context): + context.cli.expect('{0}> '.format(context.conf['dbname'])) diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 00000000..d6e3e92e --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,5 @@ +pytest>=2.7.0 +mock>=1.0.1 +tox>=1.9.2 +behave>=1.2.4 +pexpect>=3.3 |