Skip to content

Commit

Permalink
tests: use pytest-invenio
Browse files Browse the repository at this point in the history
  • Loading branch information
egabancho authored and lnielsen committed Jan 11, 2019
1 parent 6f4c2b2 commit e45a146
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 75 deletions.
4 changes: 2 additions & 2 deletions invenio_sequencegenerator/__init__.py
Expand Up @@ -147,10 +147,10 @@
>>> videos.next()
'VIDEOS: File 002'
>>> Sequence('invalid')
>>> Sequence('invalid') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SequenceNotFound
invenio_sequencegenerator.errors.SequenceNotFound
>>> invalid = Sequence(cat, invalid='PHOTOS')
>>> invalid.next()
Expand Down
1 change: 1 addition & 0 deletions requirements-devel.txt
Expand Up @@ -7,3 +7,4 @@
# under the terms of the MIT License; see LICENSE file for more details.

-e git+git://github.com/inveniosoftware/invenio-db.git#egg=invenio-db
-e git+git://github.com/inveniosoftware/pytest-invenio.git#egg=pytest-invenio
8 changes: 3 additions & 5 deletions setup.py
Expand Up @@ -22,7 +22,7 @@
'pydocstyle>=2.0.0',
'pytest-cov>=2.5.1',
'pytest-pep8>=1.0.6',
'pytest>=2.8.0',
'pytest-invenio>=1.0.6',
]

extras_require = {
Expand Down Expand Up @@ -56,8 +56,7 @@
]

install_requires = [
'Flask>=0.11.1',
'Flask-BabelEx>=0.9.2',
'Flask-BabelEx>=0.9.3',
'sqlalchemy-utils>=0.31',
]

Expand All @@ -66,8 +65,7 @@

# Get the version string. Cannot be done with import!
g = {}
with open(os.path.join('invenio_sequencegenerator', 'version.py'),
'rt') as fp:
with open(os.path.join('invenio_sequencegenerator', 'version.py'), 'rt') as fp:
exec(fp.read(), g)
version = g['__version__']

Expand Down
54 changes: 10 additions & 44 deletions tests/conftest.py
Expand Up @@ -5,61 +5,27 @@
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.


"""Pytest configuration."""

from __future__ import absolute_import, print_function

import os
import shutil
import tempfile

import pytest
from flask import Flask
from flask.cli import ScriptInfo
from invenio_db import InvenioDB
from invenio_db import db as db_
from sqlalchemy_utils.functions import create_database, database_exists

from invenio_sequencegenerator.ext import InvenioSequenceGenerator


@pytest.yield_fixture()
def app(request):
"""Flask application fixture."""
instance_path = tempfile.mkdtemp()
app = Flask(__name__, instance_path=instance_path)
app.config.update(
SECRET_KEY="CHANGE_ME",
SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO",
SQLALCHEMY_DATABASE_URI=os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'),
SQLALCHEMY_TRACK_MODIFICATIONS=True,
TESTING=True,
)
InvenioDB(app)
InvenioSequenceGenerator(app)

with app.app_context():
yield app

shutil.rmtree(instance_path)

@pytest.fixture(scope='module')
def create_app():
"""Application factory fixture."""
def factory(**config):
app = Flask('testapp')
app.config.update(**config)

@pytest.yield_fixture()
def db(app):
"""Database fixture."""
if not database_exists(str(db_.engine.url)):
create_database(str(db_.engine.url))
db_.create_all()
yield db_
db_.session.remove()
db_.drop_all()
InvenioDB(app)
InvenioSequenceGenerator(app)

return app

@pytest.yield_fixture()
def script_info(db, app):
"""Get ScriptInfo object for testing CLI."""
with app.app_context():
yield ScriptInfo(create_app=lambda info: app)
return factory
41 changes: 20 additions & 21 deletions tests/test_admin.py
Expand Up @@ -11,15 +11,15 @@

from __future__ import absolute_import, print_function

from flask import url_for
from flask import current_app, url_for
from flask_admin import Admin

from invenio_sequencegenerator.admin import counter_adminview as ca
from invenio_sequencegenerator.admin import templatedefinition_adminview as ta
from invenio_sequencegenerator.api import Sequence, Template


def test_admin(app, db):
def test_admin(db):
"""Test admin interface."""
assert isinstance(ca, dict)
assert isinstance(ta, dict)
Expand All @@ -30,27 +30,26 @@ def test_admin(app, db):
assert 'modelview' in ta

# Create admin
admin = Admin(app, name='Example: Sequence Generator')
admin = Admin(current_app, name='Example: Sequence Generator')

# Add views
admin.add_view(ta['modelview'](ta['model'], db.session))
admin.add_view(ca['modelview'](ca['model'], db.session))

with app.app_context():
# Create test data
seq = Sequence(Template.create('ID', 'File {counter}'))
assert seq.next() == 'File 0'
assert seq.next() == 'File 1'
assert seq.next() == 'File 2'
db.session.commit()

with app.test_request_context():
request_url = url_for('counter.reset_view')
with app.test_client() as client:
# Reset counter
client.post(request_url,
data={'start': 0, 'rowid': 'File {counter}'},
follow_redirects=False)

# Assert that reset was successful
assert seq.next() == 'File 0'
# Create test data
seq = Sequence(Template.create('ID', 'File {counter}'))
assert seq.next() == 'File 0'
assert seq.next() == 'File 1'
assert seq.next() == 'File 2'
db.session.commit()

with current_app.test_request_context():
request_url = url_for('counter.reset_view')
with current_app.test_client() as client:
# Reset counter
client.post(request_url,
data={'start': 0, 'rowid': 'File {counter}'},
follow_redirects=False)

# Assert that reset was successful
assert seq.next() == 'File 0'
4 changes: 2 additions & 2 deletions tests/test_cli.py
Expand Up @@ -24,7 +24,7 @@ def assert_success(res, expected):
assert expected in res.output


def test_cli_authors(script_info):
def test_cli_authors(script_info, db):
"""Test CLI for the use case of author identifiers."""
runner = CliRunner()
run = partial(runner.invoke, sequences, obj=script_info)
Expand All @@ -46,7 +46,7 @@ def test_cli_authors(script_info):
assert_success(author_2(), 'Tom.Phake.5')


def test_cli_playlists(script_info):
def test_cli_playlists(script_info, db):
"""Test CLI for the use case of video playlist identifiers."""
runner = CliRunner()
run = partial(runner.invoke, sequences, obj=script_info)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_version.py
Expand Up @@ -12,7 +12,7 @@
from __future__ import absolute_import, print_function


def test_version(db):
def test_version():
"""Test version import."""
from invenio_sequencegenerator import __version__
assert __version__

0 comments on commit e45a146

Please sign in to comment.