Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
tests: moved code to standard py.test
Browse files Browse the repository at this point in the history
* Uses py.test style. (closes #20)

* Does a rollback of all the data inserted in the database.

Signed-off-by: Jose Benito Gonzalez Lopez <jose.benito.gonzalez@cern.ch>
  • Loading branch information
jbenito3 committed Sep 11, 2015
1 parent 4e57584 commit 5325fb3
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 233 deletions.
19 changes: 18 additions & 1 deletion claimstore/modules/claims/fixtures/claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
from flask import current_app
from webtest import TestApp

from claimstore.modules.claims.models import Claim


@pytest.fixture
def load_all_claims(test_app=None, config_path=None):
"""Fixture that loads all test claims."""
if test_app is None:
Expand Down Expand Up @@ -82,3 +83,19 @@ def dummy_claim():
"created": "2015-03-25T11:00:00Z"
}
""")


def _remove_all_claims():
"""Remove all claims from the DB.
FIXME: this should never be used before testing. Improve tests in order
to avoid using this.
"""
Claim.query.delete()


@pytest.fixture
def all_claims(db):
"""Fixture that loads all claims."""
_remove_all_claims()
load_all_claims()
8 changes: 6 additions & 2 deletions claimstore/modules/claims/fixtures/claimant.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from claimstore.modules.claims.models import Claimant


@pytest.fixture
def create_claimant(claimant_json):
"""Insert a claimant in the database."""
if not Claimant.query.filter_by(name=claimant_json['name']).first():
Expand All @@ -40,7 +39,6 @@ def create_claimant(claimant_json):
db.session.commit()


@pytest.fixture
def load_all_claimants(config_path=None):
"""Fixture that loads all test claimants."""
if config_path:
Expand Down Expand Up @@ -86,3 +84,9 @@ def dummy_claimant():
]
}
""")


@pytest.fixture
def all_claimants(db):
"""Fixture that loads all claimants."""
load_all_claimants()
8 changes: 6 additions & 2 deletions claimstore/modules/claims/fixtures/pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from claimstore.modules.claims.models import IdentifierType


@pytest.fixture
def create_pid(pid_json):
"""Insert an identifier in the database."""
pid_json['name'] = pid_json.pop('type')
Expand All @@ -41,7 +40,6 @@ def create_pid(pid_json):
db.session.commit()


@pytest.fixture
def load_all_pids(config_path=None):
"""Populate all persistent identifiers."""
if config_path:
Expand All @@ -60,3 +58,9 @@ def load_all_pids(config_path=None):
for pid_fp in glob.glob("{}/*.json".format(pids_filepath)):
with open(pid_fp) as f:
create_pid(json.loads(f.read()))


@pytest.fixture
def all_pids(db):
"""Fixture that loads all PIDs."""
load_all_pids()
10 changes: 7 additions & 3 deletions claimstore/modules/claims/fixtures/predicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from claimstore.modules.claims.models import Predicate


@pytest.fixture
def create_predicate(pred_json):
"""Insert a predicate in the database."""
if not Predicate.query.filter_by(name=pred_json['name']).first():
Expand All @@ -40,7 +39,6 @@ def create_predicate(pred_json):
db.session.commit()


@pytest.fixture
def load_all_predicates(config_path=None):
"""Populate all predicates."""
if config_path:
Expand All @@ -58,4 +56,10 @@ def load_all_predicates(config_path=None):
)
for predicate_fp in glob.glob("{}/*.json".format(predicates_filepath)):
with open(predicate_fp) as f:
create_predicate(json.loads(f.read()))
create_predicate(json.load(f))


@pytest.fixture
def all_predicates(db):
"""Fixture that loads all predicates."""
load_all_predicates()
33 changes: 16 additions & 17 deletions tests/base.py → claimstore/testing/fixtures/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,26 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
# USA.

"""Base test class."""
"""App fixtures."""

from unittest import TestCase
import pytest
from webtest import TestApp

from claimstore.app import create_app
from claimstore.ext.sqlalchemy import db


class ClaimStoreTestCase(TestCase):
@pytest.yield_fixture(scope='session', autouse=True)
def app():
"""Create the flask app."""
app_ = create_app()
with app_.app_context():
yield app_

"""Testing claimstore.core.json."""

def setUp(self):
"""Set up."""
self.app = create_app()
with self.app.app_context():
db.create_all()

def tearDown(self):
"""Tear down."""
with self.app.app_context():
db.session.remove()
db.drop_all()
self.app = None
@pytest.fixture(scope='session')
def webtest_app(app):
"""Create webtest TestApp."""
return TestApp(
app,
extra_environ=dict(REMOTE_ADDR='127.0.0.1')
)
44 changes: 44 additions & 0 deletions claimstore/testing/fixtures/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
#
# This file is part of ClaimStore.
# Copyright (C) 2015 CERN.
#
# ClaimStore 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 3 of the License, or
# (at your option) any later version.
#
# ClaimStore 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 ClaimStore; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
# USA.

"""Database fixtures."""

import pytest

from claimstore.ext.sqlalchemy import db as db_


@pytest.yield_fixture(scope='session')
def database(app):
"""Ensure that the database schema is created."""
db_.create_all()
yield db_
db_.session.remove()


@pytest.yield_fixture
def db(database, monkeypatch):
"""Provide database access and ensure changes do not persist."""
# Prevent database/session modifications
monkeypatch.setattr(database.session, 'commit', database.session.flush)
monkeypatch.setattr(database.session, 'remove', lambda: None)
yield database
database.session.rollback()
database.session.remove()
14 changes: 13 additions & 1 deletion tests/__init__.py → claimstore/testing/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
# USA.

"""Test package."""
"""py.test configuration."""

import sys

pytest_plugins = (
'claimstore.testing.fixtures.app',
'claimstore.testing.fixtures.database'
)


def pytest_configure(config):
"""Load all the plugins defined in pytest_plugins."""
config.pluginmanager.consider_module(sys.modules[__name__])
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def run_tests(self):
entry_points={
'console_scripts': [
'claimstore = claimstore.cli:cli'
],
'pytest11': [
'claimstore = claimstore.testing.pytest_plugin'
]
},
install_requires=[
Expand Down
25 changes: 9 additions & 16 deletions tests/test_core_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@

from claimstore.core.json import get_json_schema

from .base import ClaimStoreTestCase


class FlaskTestCase(ClaimStoreTestCase):

"""Testing claimstore.core.json."""

def test_get_json_schema(self):
"""Testing `get_json_schema()`."""
with self.app.app_context():
assert '"title": "Service Name",' in \
get_json_schema('claims.claimant')
assert 'HTTP URL addressing the service home page' in \
get_json_schema('claims.claimant')
assert '"required": ["type", "description", "url", ' + \
'"example_value", "example_url"],' in \
get_json_schema('claims.persistent_id')
def test_get_json_schema(app):
"""Testing `get_json_schema()`."""
assert '"title": "Service Name",' in \
get_json_schema('claims.claimant')
assert 'HTTP URL addressing the service home page' in \
get_json_schema('claims.claimant')
assert '"required": ["type", "description", "url", ' + \
'"example_value", "example_url"],' in \
get_json_schema('claims.persistent_id')
Loading

0 comments on commit 5325fb3

Please sign in to comment.