def db_session(config): """ (Integartion Testing) Instantiates a database session. :param config: The pyramid testing configuartion :returns: An instantiated sqalchemy database session """ from occams_datastore import models as datastore db_session = config.registry['db_sessionmaker']() # Pre-configure with a blame user blame = datastore.User(key=USERID) db_session.add(blame) db_session.flush() db_session.info['blame'] = blame # Other expected settings db_session.info['settings'] = config.registry.settings # Hardcoded workflow db_session.add_all([ datastore.State(name=u'pending-entry', title=u'Pending Entry'), datastore.State(name=u'pending-review', title=u'Pending Review'), datastore.State(name=u'pending-correction', title=u'Pending Correction'), datastore.State(name=u'complete', title=u'Complete') ]) return db_session
def test_state_unique_name(db_session): """ It should only allow states with unique names """ import sqlalchemy.exc from occams_datastore import models db_session.add(models.State(name=u'pending-entry', title=u'Pending Entry')) db_session.flush() db_session.add(models.State(name=u'pending-entry', title=u'Pending Entry')) with pytest.raises(sqlalchemy.exc.IntegrityError): db_session.flush()
def app(request, wsgi, db_session): """ (Functional Testing) Initiates a user request against a WSGI stack :param request: The pytest context :param wsgi: An initialized WSGI stack :param db_session: A database session for seting up pre-existing data :returns: a test app request against the WSGI instance """ import transaction from webtest import TestApp from zope.sqlalchemy import mark_changed from occams_datastore import models as datastore # Save all changes up tho this point (db_session does some configuration) with transaction.manager: blame = datastore.User(key='workflow@localhost') db_session.add(blame) db_session.flush() db_session.info['blame'] = blame db_session.add_all([ datastore.State(name=u'pending-entry', title=u'Pending Entry'), datastore.State(name=u'pending-review', title=u'Pending Review'), datastore.State(name=u'pending-correction', title=u'Pending Correction'), datastore.State(name=u'complete', title=u'Complete') ]) app = TestApp(wsgi) yield app with transaction.manager: # DELETE is dramatically faster than TRUNCATE # http://stackoverflow.com/a/11423886/148781 # We also have to do this as a raw query becuase SA does # not have a way to invoke server-side cascade db_session.execute('DELETE FROM "study" CASCADE') db_session.execute('DELETE FROM "patient" CASCADE') db_session.execute('DELETE FROM "site" CASCADE') db_session.execute('DELETE FROM "schema" CASCADE') db_session.execute('DELETE FROM "export" CASCADE') db_session.execute('DELETE FROM "state" CASCADE') db_session.execute('DELETE FROM "user" CASCADE') mark_changed(db_session)
def test_state_entity_relationship(db_session): """ It should implement state/entity relationship """ from datetime import date from occams_datastore import models schema = models.Schema(name=u'Foo', title=u'Foo', publish_date=date(2000, 1, 1)) pending_entry = models.State(name=u'pending-entry', title=u'Pending Entry') entity = models.Entity(schema=schema) db_session.add_all([pending_entry, entity]) db_session.flush() assert entity.state is None assert pending_entry.entities.count() == 0 entity.state = pending_entry db_session.flush() assert entity.state is not None assert pending_entry.entities.count() == 1