Skip to content

Commit

Permalink
Only integration test with postgres when available
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 22, 2016
1 parent 9684156 commit 242f5c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
5 changes: 4 additions & 1 deletion memegen/stores/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def create(self, image):
if self.exists(image) and not self.regenerate_images:
return

ImageModel(image)
try:
ImageModel(image)
except ImportError:
log.warning("Unable to store models on this machine")

image.root = self.root
image.generate()
Expand Down
45 changes: 28 additions & 17 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,53 @@ def load(response, as_json=True, key=None):


@pytest.yield_fixture(scope='session')
def app(postgres):
def app():
app = create_app(get_config('test'))
app.config['SQLALCHEMY_DATABASE_URI'] = postgres.url()
yield app


@pytest.yield_fixture(scope='session')
def postgres():
with Postgresql() as pg:
yield pg
try:
import psycopg2
except ImportError:
yield None # PostgreSQL database adapter is unavailable on this system
else:
try:
with Postgresql() as pg:
yield pg
except FileNotFoundError:
yield None # PostgreSQL is unavailable on this system


@pytest.yield_fixture(scope='module')
def db_engine(app):
_db.app = app
def db_engine(app, postgres):
if postgres:
app.config['SQLALCHEMY_DATABASE_URI'] = postgres.url()
_db.app = app

with app.app_context():
_db.create_all()
with app.app_context():
_db.create_all()

yield _db
yield _db

# http://stackoverflow.com/a/6810165/1255482
_db.session.close() # pylint: disable=no-member
# http://stackoverflow.com/a/6810165/1255482
_db.session.close() # pylint: disable=no-member

try:
_db.drop_all()
except OperationalError:
# Allow tests to be killed cleanly
pass
try:
_db.drop_all()
except OperationalError:
pass # allow tests to be killed cleanly
else:
yield None


@pytest.yield_fixture(scope='function')
def db(db_engine):
yield db_engine
# Do a rollback after each test in case bad stuff happened
db_engine.session.rollback()
if db_engine:
db_engine.session.rollback()


@pytest.yield_fixture
Expand Down

0 comments on commit 242f5c6

Please sign in to comment.