Skip to content

Commit

Permalink
tests: full cli coverage
Browse files Browse the repository at this point in the history
* BETTER Adds posibility to disable automatic entry points loading or
  specify custom entry point group name.

* Enables matrix for testing SQLite, MySQL, Postgre on Travis CI.

* Improves code style and test coverage.

Signed-off-by: Jiri Kuncar <jiri.kuncar@cern.ch>
  • Loading branch information
jirikuncar committed Oct 14, 2015
1 parent fd636b0 commit 5b1ae49
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 24 deletions.
16 changes: 13 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ language: python
cache:
- pip

services:
- mysql
- postgresql

env:
- REQUIREMENTS=lowest
- REQUIREMENTS=release
- REQUIREMENTS=devel
- REQUIREMENTS=lowest SQLALCHEMY_DATABASE_URI="sqlite:///test.db"
- REQUIREMENTS=lowest SQLALCHEMY_DATABASE_URI="mysql+pymysql://travis@localhost:3306/travis"
- REQUIREMENTS=lowest SQLALCHEMY_DATABASE_URI="postgresql+psycopg2://postgres@localhost:5432/travis"
- REQUIREMENTS=release SQLALCHEMY_DATABASE_URI="sqlite:///test.db"
- REQUIREMENTS=release SQLALCHEMY_DATABASE_URI="mysql+pymysql://travis@localhost:3306/travis"
- REQUIREMENTS=release SQLALCHEMY_DATABASE_URI="postgresql+psycopg2://postgres@localhost:5432/travis"
- REQUIREMENTS=devel SQLALCHEMY_DATABASE_URI="sqlite:///test.db"
- REQUIREMENTS=devel SQLALCHEMY_DATABASE_URI="mysql+pymysql://travis@localhost:3306/travis"
- REQUIREMENTS=devel SQLALCHEMY_DATABASE_URI="postgresql+psycopg2://postgres@localhost:5432/travis"

python:
- "2.7"
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ recursive-include docs *.py
recursive-include docs *.rst
recursive-include docs Makefile
recursive-include tests *.py

prune example
14 changes: 14 additions & 0 deletions example/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os

from flask import Flask
from flask_cli import FlaskCLI
from flask_sqlalchemy import SQLAlchemy

from invenio_db import InvenioDB

app = Flask('demo')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'
)
FlaskCLI(app)
InvenioDB(app)
22 changes: 12 additions & 10 deletions invenio_db/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

# Fix Python 3 compatibility issue in click
if sys.version_info > (3,):
_termui_impl.long = int
_termui_impl.long = int # pragma: no cover


def abort_if_false(ctx, param, value):
Expand All @@ -59,13 +59,13 @@ def db():
@with_appcontext
def create(verbose):
"""Create tables."""
click.secho('Creating all tables!', bg='yellow', bold=True)
click.secho('Creating all tables!', fg='yellow', bold=True)
with click.progressbar(reversed(_db.metadata.sorted_tables)) as bar:
for table in bar:
if verbose:
click.echo(" Creating table {0}".format(table))
click.echo(' Creating table {0}'.format(table))
table.create(bind=_db.engine, checkfirst=True)
click.secho('Created all tables!', bg='green')
click.secho('Created all tables!', fg='green')


@db.command()
Expand All @@ -76,21 +76,22 @@ def create(verbose):
@with_appcontext
def drop(verbose):
"""Drop tables."""
click.secho('Dropping all tables!', bg='red', bold=True)
click.secho('Dropping all tables!', fg='red', bold=True)
with click.progressbar(reversed(_db.metadata.sorted_tables)) as bar:
for table in bar:
if verbose:
click.echo(" Dropping table {0}".format(table))
click.echo(' Dropping table {0}'.format(table))
table.drop(bind=_db.engine, checkfirst=True)
click.secho('Dropped all tables!', bg='green')
click.secho('Dropped all tables!', fg='green')


@db.command()
@with_appcontext
def init():
"""Create database."""
click.echo("Creating database {0}".format(_db.engine.url))
create_database(_db.engine.url)
click.secho('Creating database {0}'.format(_db.engine.url),
fg='green')
create_database(str(_db.engine.url))


@db.command()
Expand All @@ -100,5 +101,6 @@ def init():
@with_appcontext
def destroy():
"""Drop database."""
click.echo("Destroying database {0}".format(_db.engine.url))
click.secho('Destroying database {0}'.format(_db.engine.url),
fg='red', bold=True)
drop_database(_db.engine.url)
9 changes: 5 additions & 4 deletions invenio_db/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def __init__(self, app=None, **kwargs):

def init_app(self, app, **kwargs):
"""Initialize application object."""
self.init_db(app)
self.init_db(app, **kwargs)
app.extensions['invenio-db'] = self
app.cli.add_command(db_cmd)

def init_db(self, app):
def init_db(self, app, entrypoint_name='invenio_db.models', **kwargs):
"""Initialize Flask-SQLAlchemy extension."""
# Setup SQLAlchemy
app.config.setdefault(
Expand All @@ -61,5 +61,6 @@ def init_db(self, app):
db.init_app(app)

# Initialize model bases
for base_entry in pkg_resources.iter_entry_points('invenio_db.models'):
base_entry.load()
if entrypoint_name:
for base_entry in pkg_resources.iter_entry_points(entrypoint_name):
base_entry.load()
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@

extras_require = {
'docs': [
"Sphinx>=1.3",
'Sphinx>=1.3',
],
'mysql': [
'pymysql>=0.6.7',
],
'postgresql': [
'psycopg2>=2.6.1',
],
'tests': tests_require,
}
Expand Down
31 changes: 25 additions & 6 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Test BowerBundle."""
"""Test database integration layer."""

from __future__ import absolute_import, print_function

import importlib
import os

import sqlalchemy as sa
from click.testing import CliRunner
from flask import Flask
from flask_cli import FlaskCLI, ScriptInfo
from mock import patch
from pkg_resources import EntryPoint
from sqlalchemy_utils.functions import create_database, drop_database

from invenio_db import InvenioDB, db
from invenio_db.cli import db as db_cmd
Expand All @@ -60,9 +62,11 @@ def _mock_entry_points(name):

def test_init():
app = Flask('demo')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'
)
FlaskCLI(app)
InvenioDB(app)
InvenioDB(app, entrypoint_name=False)

class Demo(db.Model):
__tablename__ = 'demo'
Expand All @@ -73,9 +77,11 @@ class Demo2(db.Model):
pk = sa.Column(sa.Integer, primary_key=True)

with app.app_context():
create_database(db.engine.url)
db.create_all()
assert len(db.metadata.tables) == 2
db.drop_all()
drop_database(db.engine.url)


@patch('pkg_resources.iter_entry_points', _mock_entry_points)
Expand All @@ -87,7 +93,9 @@ def test_entry_points(script_info):
db = invenio_db.db = shared.db = SQLAlchemy()

app = Flask('demo')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'
)
FlaskCLI(app)
InvenioDB(app)

Expand All @@ -101,21 +109,32 @@ def test_entry_points(script_info):
result = runner.invoke(db_cmd, [], obj=script_info)
assert result.exit_code == 0

result = runner.invoke(db_cmd, ['init'], obj=script_info)
assert result.exit_code == 0

result = runner.invoke(db_cmd, ['create'], obj=script_info)
assert result.exit_code == 0

result = runner.invoke(db_cmd, ['drop'],
result = runner.invoke(db_cmd, ['drop', '-v'],
obj=script_info)
assert result.exit_code == 1

result = runner.invoke(db_cmd, ['drop', '--yes-i-know'],
obj=script_info)
assert result.exit_code == 0

result = runner.invoke(db_cmd, ['drop', 'create'],
result = runner.invoke(db_cmd, ['drop', 'create', '-v'],
obj=script_info)
assert result.exit_code == 1

result = runner.invoke(db_cmd, ['drop', '--yes-i-know', 'create'],
obj=script_info)
assert result.exit_code == 0

result = runner.invoke(db_cmd, ['destroy'],
obj=script_info)
assert result.exit_code == 1

result = runner.invoke(db_cmd, ['destroy', '--yes-i-know'],
obj=script_info)
assert result.exit_code == 0

0 comments on commit 5b1ae49

Please sign in to comment.