''' Test package for ordr2. ''' import pytest import transaction from pyramid import testing APP_SETTINGS = { 'sqlalchemy.url': 'sqlite:///:memory:', 'auth.secret': 'not-very-secure', 'session.secret': 'not-very-secure', 'session.auto_csrf': True } # helpers def create_users(db): ''' set up some well known example users ''' from ordr2.models import Role, User stubs = [ ('Graham', 'Chapman', Role.UNVALIDATED), ('John', 'Cleese', Role.NEW), ('Terry', 'Gilliam', Role.USER), ('Eric', 'Idle', Role.PURCHASER), ('Terry', 'Jones', Role.ADMIN), ('Michael', 'Palin', Role.INACTIVE) ] for i, stub in enumerate(stubs): first_name, last_name, role = stub user = User( id=i+1, username=first_name + last_name, first_name = first_name, last_name = last_name, email = last_name.lower() + '@example.com', role=role, password_hash = first_name.lower() ) db.add(user) # fixtures @pytest.fixture(scope='session') def app_config(): ''' fixture for tests requiring a pyramid.testing setup ''' with testing.testConfig(settings=APP_SETTINGS) as config: config.include('pyramid_jinja2') #config.include('pyramid_mailer.testing') yield config @pytest.fixture(scope='function') def dbsession(app_config): ''' fixture for testing with database connection ''' from ordr2.models.meta import Base from ordr2.models import ( get_engine, get_session_factory, get_tm_session ) settings = app_config.get_settings() engine = get_engine(settings) session_factory = get_session_factory(engine) session = get_tm_session(session_factory, transaction.manager) Base.metadata.create_all(engine) yield session transaction.abort() Base.metadata.drop_all(engine)