def test_evolution(): import logging logging.basicConfig(level=logging.DEBUG) import stucco_evolution import stucco_auth.tables import stucco_auth.evolve engine = sqlalchemy.create_engine('sqlite:///:memory:') connection = engine.connect() trans = connection.begin() try: stucco_evolution.initialize(connection) stucco_evolution.create_or_upgrade_packages(connection, 'stucco_auth') session = sqlalchemy.orm.sessionmaker()(bind=connection) versions = {} for row in session.query(stucco_evolution.SchemaVersion): versions[row.package] = row.version assert 'stucco_evolution' in versions, versions assert 'stucco_auth' in versions, versions assert versions['stucco_auth'] == stucco_auth.evolve.VERSION session.commit() # the automatically added admin user assert session.query(stucco_auth.tables.User).count() > 0 finally: connection.close()
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ engine = engine_from_config(settings, 'sqlalchemy.') tables.DBSession.configure(bind=engine) augment_settings(settings) with engine.begin() as connection: stucco_evolution.initialize(connection) stucco_evolution.create_or_upgrade_packages(connection, 'sharder') config = Configurator( settings=settings, request_factory=Request, root_factory=models.make(), ) config.add_static_view('static', 'sharder:static', cache_max_age=3600) config.add_static_view('deform', 'deform:static', cache_max_age=3600) config.include('pyramid_jinja2') config.add_view('.views.home', context=models.Sharder, renderer='home.jinja2') config.include('sharder') return config.make_wsgi_app()
def test_evolve_compat(): """Ensure we bring over any rows from the old table name 'ponzi_evolution'""" engine = sqlalchemy.create_engine("sqlite:///:memory:") Session = sqlalchemy.orm.sessionmaker(bind=engine) session = Session() session.execute( "CREATE TABLE ponzi_evolution (package STRING, version INTEGER)") session.execute( "INSERT INTO ponzi_evolution (package, version) VALUES ('ponzi_evolution', 1)" ) session.execute( "INSERT INTO ponzi_evolution (package, version) VALUES ('third_party', 2)" ) stucco_evolution.initialize(session.connection()) session.flush() session.execute( "UPDATE stucco_evolution SET version = 1 WHERE package = 'stucco_evolution'" ) stucco_evolution.upgrade_many( stucco_evolution.managers( session.connection(), stucco_evolution.dependencies("stucco_evolution"))) session.commit() rows = session.execute("SELECT COUNT(*) FROM stucco_evolution").scalar() assert rows == 3, rows
def demo_app(global_config, **settings): """Return the example application for stucco_auth.""" from stucco_auth.models import get_root engine = sqlalchemy.engine_from_config(settings) Session = sqlalchemy.orm.sessionmaker(bind=engine) settings[SESSION_FACTORY_KEY] = Session session = Session() try: import stucco_evolution stucco_evolution.initialize(session.connection()) stucco_evolution.create_or_upgrade_packages(session.connection(), 'stucco_auth') tkt_secret = auth_tkt_secret(session) authentication_policy = AuthTktAuthenticationPolicy( tkt_secret.value, callback=security.lookup_groups) authorization_policy = ACLAuthorizationPolicy() config = Configurator(root_factory=get_root, settings=settings, authentication_policy=authentication_policy, authorization_policy=authorization_policy) config.add_renderer('.jinja2', pyramid_jinja2.renderer_factory) # Configure beaker session: import pyramid_beaker session_factory = \ pyramid_beaker.session_factory_from_settings(settings) config.set_session_factory(session_factory) config.include('stucco_auth.includeme') config.add_static_view('static', 'stucco_auth:static') config.add_view(context=IAuthRoot, renderer='welcome.jinja2') # event handler will only work if stucco_auth.tm is being used config.add_subscriber(new_request_listener, NewRequest) app = config.make_wsgi_app() tm = TM(app, Session) # For pshell compatibility: tm.registry, tm.threadlocal_manager, tm.root_factory = \ app.registry, app.threadlocal_manager, app.root_factory # In case database work was done during init: session.commit() except: session.rollback() raise finally: session.close() return tm
def setUp(self): engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=False) self.Session = sqlalchemy.orm.sessionmaker(bind=engine) session = self.Session() import stucco_evolution stucco_evolution.initialize(session.connection()) stucco_evolution.create_or_upgrade_packages(session.connection(), 'stucco_auth') session.commit()
def test_unversioned(): engine = sqlalchemy.create_engine("sqlite:///:memory:") Session = sqlalchemy.orm.sessionmaker(bind=engine) session = Session() stucco_evolution.initialize(session.connection()) manager = stucco_evolution.SQLAlchemyEvolutionManager( session.connection(), "testing_testing", 4) assert manager.get_db_version() is None assert manager.get_sw_version() is 4 assert isinstance(repr(manager), basestring)
def test_create_or_upgrade_many(): """The recommended schema management strategy.""" from stucco_evolution import dependencies, managers from stucco_evolution import create_or_upgrade_many engine = sqlalchemy.create_engine("sqlite:///:memory:") connection = engine.connect() dependencies = dependencies("stucco_openid") managers = managers(connection, dependencies) stucco_evolution.initialize(connection) create_or_upgrade_many(managers)
def test_initialize(): import stucco_evolution.evolve engine = sqlalchemy.create_engine("sqlite:///:memory:") connection = engine.connect() assert stucco_evolution.is_initialized(connection) is False stucco_evolution.initialize(connection) assert stucco_evolution.is_initialized(connection) db_version = stucco_evolution.manager(connection, "stucco_evolution").get_db_version() assert db_version == stucco_evolution.evolve.VERSION
def test_authenticate(self): import stucco_evolution import stucco_auth.security # Very accurate mock session: Session = sqlalchemy.orm.sessionmaker( sqlalchemy.create_engine('sqlite:///:memory:')) session = Session() connection = session.connection() stucco_evolution.initialize(connection) stucco_evolution.manager(connection, 'stucco_auth').create() user = stucco_auth.security.authenticate(session, u'foo', 'bar') assert user is None, user
def test_transactional_ddl(): """Test transaction ddl. Requires a patched sqlite3 module.""" from sqlalchemy import Column, Integer from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class BoringTable(Base): __tablename__ = "boring" id = Column(Integer, primary_key=True) class TestException(Exception): pass class TestEvolutionManager(stucco_evolution.SQLAlchemyEvolutionManager): def create(self): Base.metadata.create_all(self.connection) def evolve_to(self, version): self.connection.execute("INSERT INTO boring (id) VALUES (1)") raise TestException("It wasn't meant to be.") engine = sqlalchemy.create_engine("sqlite:///:memory:") connection = engine.connect() trans = connection.begin() stucco_evolution.initialize(connection) managers = [TestEvolutionManager(connection, "test", 0)] stucco_evolution.create_or_upgrade_many(managers) managers[0].sw_version = 1 @raises(TestException) def go(): try: managers[0].evolve_to(1) except: trans.rollback() raise go() assert not engine.has_table("boring") connection.close()