1
0
Fork 0

Started working on integration tests.

This commit is contained in:
Iryna Cherniavska 2015-06-21 15:00:18 -07:00 committed by Amjith Ramanujam
parent b292a52180
commit 5b8afaafd6
6 changed files with 211 additions and 0 deletions

5
behave.ini Normal file
View File

@ -0,0 +1,5 @@
[behave.userdata]
pg_test_user = behave_user
pg_test_pass =
pg_test_host = localhost
pg_test_db = pgcli_behave_tests

81
features/db_utils.py Normal file
View File

@ -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))

71
features/environment.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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']))

5
requirements-dev.txt Normal file
View File

@ -0,0 +1,5 @@
pytest>=2.7.0
mock>=1.0.1
tox>=1.9.2
behave>=1.2.4
pexpect>=3.3