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_examples_app.py. (closes #37)

Signed-off-by: Leonardo Rossi <leonardo.r@cern.ch>
  • Loading branch information
Leonardo Rossi committed Oct 12, 2016
1 parent 54b7252 commit 53e71ab
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 9 deletions.
7 changes: 2 additions & 5 deletions MANIFEST.in
Expand Up @@ -34,11 +34,8 @@ include MAINTAINERS
include babel.ini
include docs/requirements.txt
include pytest.ini
recursive-include docs *.bat
recursive-include docs *.py
recursive-include docs *.rst
recursive-include docs Makefile
recursive-include examples *.py
recursive-include docs *.bat *.py *.rst Makefile
recursive-include examples *.py *.sh *.html
recursive-include invenio_pages *.html
recursive-include invenio_pages *.po *.pot *.mo
recursive-include tests *.py
15 changes: 15 additions & 0 deletions examples/app-fixtures.sh
@@ -0,0 +1,15 @@
#!/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 pages
16 changes: 16 additions & 0 deletions examples/app-setup.sh
@@ -0,0 +1,16 @@
#!/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
9 changes: 9 additions & 0 deletions examples/app-teardown.sh
@@ -0,0 +1,9 @@
#!/bin/sh

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

# Clean the database
flask db drop --yes-i-know
49 changes: 45 additions & 4 deletions examples/app.py
Expand Up @@ -35,15 +35,56 @@

from __future__ import absolute_import, print_function

import os

from flask import Flask
from flask_babelex import Babel
from invenio_db import InvenioDB, db

from invenio_pages import InvenioPages
from invenio_pages.models import Page
from invenio_pages.views import blueprint

# Create Flask application
app = Flask(__name__)
Babel(app)

app.config.update(
PAGES_TEMPLATES=[
('invenio_pages/default.html', 'Default'),
('app/mytemplate.html', 'App'),
],
SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI',
'sqlite:///app.db'),
)

InvenioDB(app)
InvenioPages(app)

if __name__ == "__main__":
app.run()
app.register_blueprint(blueprint)


@app.cli.group()
def fixtures():
"""Command for working with test data."""


@fixtures.command()
def pages():
"""Load pages."""
p1 = Page(
url='/example1',
title='My page with default template',
description='my description',
content='hello default page',
template_name='invenio_pages/default.html',
)
p2 = Page(
url='/example2',
title='My page with my template',
description='my description',
content='hello my page',
template_name='app/mytemplate.html',
)
with db.session.begin_nested():
db.session.add(p1)
db.session.add(p2)
db.session.commit()
31 changes: 31 additions & 0 deletions examples/templates/app/default.html
@@ -0,0 +1,31 @@
{#
# This file is part of Invenio.
# Copyright (C) 2014, 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.
#}
{% extends config.PAGES_BASE_TEMPLATE %}

{% set title = page.title %}

{% block page_body %}
<div class="container">
<div class="row">
<div class="col-xs-12">
{% block content %}{{ page.content|safe }}{% endblock %}
</div>
</div>
</div>
{% endblock %}
1 change: 1 addition & 0 deletions invenio_pages/ext.py
Expand Up @@ -27,6 +27,7 @@
from __future__ import absolute_import, print_function

from distutils.version import StrictVersion

from flask import __version__ as flask_version
from flask import url_for
from jinja2.sandbox import SandboxedEnvironment
Expand Down
76 changes: 76 additions & 0 deletions tests/test_examples_app.py
@@ -0,0 +1,76 @@
# -*- 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 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 -h 0.0.0.0'
webapp = subprocess.Popen(cmd, stdout=subprocess.PIPE,
preexec_fn=os.setsid, shell=True)
time.sleep(10)
# 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(10)
# open page
cmd = 'curl http://0.0.0.0:5000/example1'
output = subprocess.check_output(cmd, shell=True).decode('utf-8')
assert 'hello default page' in output
cmd = 'curl http://0.0.0.0:5000/example2'
output = subprocess.check_output(cmd, shell=True).decode('utf-8')
assert 'hello my page' in output

0 comments on commit 53e71ab

Please sign in to comment.