Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

tests: addition of test_examples_app.py #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/app-fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

# quit on errors:
set -o errexit

# quit on unbound symbols:
set -o nounset

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

# Create a user
flask users create [email protected] -a --password 123456

# Load some test data
flask fixtures files
20 changes: 20 additions & 0 deletions examples/app-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# quit on errors:
set -o errexit

# quit on unbound symbols:
set -o nounset

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py
mkdir $DIR/instance

# Install specific dependencies
pip install -r requirements.txt

# Create the database
flask db init
flask db create
9 changes: 9 additions & 0 deletions examples/app-teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

# clean environment
[ -e "$DIR/instance" ] && rm $DIR/instance -Rf
34 changes: 16 additions & 18 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,22 @@

"""Minimal Flask application example for development.

Start the Redis server.

Initialize database

.. code-block:: console

$ pip install -e .[all]
$ cd examples
$ flask -a app.py db init
$ flask -a app.py db create

Create a user (for accessing admin):

$ flask -a app.py users create [email protected] -a

Load some test data:

$ flask -a app.py fixtures files
$ ./app-setup.sh
$ ./app-fixtures.sh

Run example development server:

.. code-block:: console

$ flask -a app.py --debug run
$ FLASK_APP=app.py flask run --debugger -p 5000

Run example worker:

Expand All @@ -55,11 +50,17 @@

Administration interface is available on::

http://localhost:5000/admin/
$ open http://localhost:5000/admin/

REST API is available on::

http://localhost:5000/files/
$ open http://localhost:5000/files/

To be able to uninstall the example app:

.. code-block:: console

$ ./app-teardown.sh
"""

from __future__ import absolute_import, print_function
Expand All @@ -70,7 +71,6 @@

from flask import Flask, current_app
from flask_babelex import Babel
from flask_cli import FlaskCLI
from flask_menu import Menu
from invenio_access import InvenioAccess
from invenio_accounts import InvenioAccounts
Expand All @@ -81,6 +81,7 @@
from invenio_files_rest import InvenioFilesREST
from invenio_files_rest.models import Bucket, FileInstance, Location, \
ObjectVersion
from invenio_files_rest.views import blueprint as files_rest_bp
from invenio_rest import InvenioREST

from invenio_memento import InvenioMemento
Expand All @@ -95,14 +96,11 @@
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
CELERY_RESULT_BACKEND='cache',
SQLALCHEMY_TRACK_MODIFICATIONS=True,
SQLALCHEMY_DATABASE_URI=environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'),
REST_ENABLE_CORS=True,
SECRET_KEY='CHANGEME',
DATADIR=join(dirname(__file__), 'data')
))

FlaskCLI(app)
Babel(app)
Menu(app)
InvenioDB(app)
Expand All @@ -116,7 +114,7 @@
app.url_map.converters['archived'] = ArchivedConverter
app.register_blueprint(accounts_bp)
app.register_blueprint(memento_bp)

# app.register_blueprint(files_rest_bp)

@app.cli.group()
def fixtures():
Expand Down
28 changes: 28 additions & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

flask-menu>=0.5.0
invenio-access>=1.0.0a9
invenio-admin>=1.0.0a3
invenio-celery>=1.0.0b1
75 changes: 75 additions & 0 deletions tests/test_examples_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Test example app."""

import json
import os
import signal
import subprocess
import time

import pytest


@pytest.yield_fixture
def example_app():
"""Example app fixture."""
current_dir = os.getcwd()
# go to example directory
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
exampleappdir = os.path.join(project_dir, 'examples')
os.chdir(exampleappdir)
# setup example
cmd = './app-setup.sh'
exit_status = subprocess.call(cmd, shell=True)
assert exit_status == 0
# Starting example web app
cmd = 'FLASK_APP=app.py flask run --debugger -p 5000'
webapp = subprocess.Popen(cmd, stdout=subprocess.PIPE,
preexec_fn=os.setsid, shell=True)
time.sleep(20)
# return webapp
yield webapp
# stop server
os.killpg(webapp.pid, signal.SIGTERM)
# tear down example app
cmd = './app-teardown.sh'
subprocess.call(cmd, shell=True)
# return to the original directory
os.chdir(current_dir)


def test_example_app(example_app):
"""Test example app."""
# load fixtures
cmd = './app-fixtures.sh'
exit_status = subprocess.call(cmd, shell=True)
assert exit_status == 0
time.sleep(7)
# Testing record retrieval via web
cmd = 'curl http://127.0.0.1:5000/files/'
output = json.loads(
subprocess.check_output(cmd, shell=True).decode("utf-8")
)