use sqlalchemy hooks rather than pyscopg2

seems to play nicer with docker?
This commit is contained in:
Leo Hemsted
2017-08-24 13:54:16 +01:00
parent 3d4dbaa632
commit 2fefe8a957
2 changed files with 17 additions and 13 deletions

View File

@@ -30,6 +30,6 @@ fi
pycodestyle . pycodestyle .
display_result $? 1 "Code style check" display_result $? 1 "Code style check"
# run with six concurrent threads # run with four concurrent threads
py.test --cov=app --cov-report=term-missing tests/ --junitxml=test_results.xml -n 6 py.test --cov=app --cov-report=term-missing tests/ --junitxml=test_results.xml -n 4
display_result $? 2 "Unit tests" display_result $? 2 "Unit tests"

View File

@@ -5,10 +5,9 @@ from alembic.command import upgrade
from alembic.config import Config from alembic.config import Config
from flask_migrate import Migrate, MigrateCommand from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager from flask_script import Manager
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import boto3 import boto3
import psycopg2
import pytest import pytest
import sqlalchemy
from app import create_app, db from app import create_app, db
@@ -48,19 +47,24 @@ def client(notify_api):
def create_test_db(database_uri): def create_test_db(database_uri):
database_name = database_uri.split('/')[-1] # get the
system_db = psycopg2.connect(dbname='postgres') db_uri_parts = database_uri.split('/')
system_db.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) postgres_db_uri = '/'.join(db_uri_parts[:-1] + ['postgres'])
cursor = system_db.cursor()
postgres_db = sqlalchemy.create_engine(
postgres_db_uri,
echo=False,
isolation_level='AUTOCOMMIT',
client_encoding='utf8'
)
try: try:
cursor.execute('CREATE DATABASE {}'.format(database_name)) result = postgres_db.execute(sqlalchemy.sql.text('CREATE DATABASE {}'.format(db_uri_parts[-1])))
except psycopg2.ProgrammingError: result.close()
# database "test_notification_api_x..." already exists except sqlalchemy.exc.ProgrammingError:
# database "test_notification_api_master" already exists
pass pass
finally: finally:
cursor.close() postgres_db.dispose()
system_db.close()
@pytest.fixture(scope='session') @pytest.fixture(scope='session')