Skip to content

Commit

Permalink
tests: addition of test_examples_app.py
Browse files Browse the repository at this point in the history
* Adds test_example_app.py. (closes #36)

Signed-off-by: Leonardo Rossi <leonardo.r@cern.ch>
  • Loading branch information
Leonardo Rossi committed Sep 22, 2016
1 parent 2685744 commit 3c1e1ff
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 16 deletions.
17 changes: 17 additions & 0 deletions examples/app-fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/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

# Load fixtures
flask fixtures records
flask index reindex --yes-i-know
flask index run
13 changes: 0 additions & 13 deletions examples/app-recreate.sh

This file was deleted.

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

# Create the database
flask db init
flask db create

# Create indices
flask index init
flask index queue init
12 changes: 12 additions & 0 deletions examples/app-teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

# Delete database
flask db destroy --yes-i-know

# Delete indices
flask index destroy --yes-i-know --force
14 changes: 11 additions & 3 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
$ pip install -e .[all]
$ cd examples
$ ./app-recreate.sh
$ ./app-setup.sh
$ ./app-fixtures.sh
Try to get some records:
Expand All @@ -40,6 +41,13 @@
$ curl -X GET localhost:9200/_cat/indices?v
$ curl -X GET localhost:9200/testrecords-testrecord-v1.0.0/_search | \
python -m json.tool
To be able to uninstall the example app:
.. code-block:: console
$ ./app-teardown.sh
"""

from __future__ import absolute_import, print_function
Expand All @@ -63,11 +71,11 @@
CELERY_CACHE_BACKEND='memory',
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
CELERY_RESULT_BACKEND='cache',
SQLALCHEMY_TRACK_MODIFICATIONS=True,
INDEXER_DEFAULT_INDEX=index_name,
INDEXER_DEFAULT_DOC_TYPE='testrecord-v1.0.0',
INDEXER_DEFAULT_INDEX=index_name,
SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI',
'sqlite:///app.db'),
SQLALCHEMY_TRACK_MODIFICATIONS=True,
)

FlaskCeleryExt(app)
Expand Down
70 changes: 70 additions & 0 deletions tests/test_examples_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015, 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
# setup example
cmd = './app-fixtures.sh'
exit_status = subprocess.call(cmd, shell=True)
assert exit_status == 0
time.sleep(10)
# return webapp
yield exit_status
# 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."""
cmd = 'curl -X GET localhost:9200/_cat/indices?v'
output = subprocess.check_output(cmd, shell=True).decode('utf-8')
assert 'testrecords-testrecord-v1.0.0' in output
cmd = 'curl -X GET localhost:9200/testrecords-testrecord-v1.0.0/_search'
output = json.loads(
subprocess.check_output(cmd, shell=True).decode('utf-8'))
assert len(output['hits']['hits']) == 10

0 comments on commit 3c1e1ff

Please sign in to comment.