Beispiel #1
0
    def setUp(self):
        from pyClanSphere.api import get_application
        from pyClanSphere.database import db, init_database
        self.app = get_application()
        self.db = db

        # just in case the table(s) for the test haven't been created
        init_database(self.app.database_engine)
Beispiel #2
0
    def start_setup(self, request):
        """
        This is called when all the form data is validated
        and pyClanSphere is ready to install the data. In theory
        this can also be called if no form validation is done and
        someone faked the request. But because that's the fault of
        the administrator we don't care about that.
        """
        value = request.values.get
        error = None
        database_uri = value('database_uri', '').strip()

        try:
            from pyClanSphere.database import init_database

            # create database and all tables
            e = db.create_engine(database_uri, self.instance_folder)
            init_database(e)
        except Exception, error:
            error = str(error).decode('utf-8', 'ignore')
Beispiel #3
0
def create_temporary_instance():
    """Create a sqlite based test instance in a temporary directory"""
    dbname = 'sqlite://database.db'
    instance_folder = mkdtemp(prefix='pyclanspheretest')

    # create database and all tables
    from pyClanSphere.database import db, init_database
    e = db.create_engine(dbname, instance_folder)
    from pyClanSphere.schema import users, user_privileges, privileges
    init_database(e)

    # create admin account
    from pyClanSphere.privileges import CLAN_ADMIN
    user_id = e.execute(users.insert(),
        username=u'TestAdmin',
        pw_hash=gen_pwhash('TestPassWord'),
        email=u'*****@*****.**',
        real_name=u'',
        description=u'',
        extra={},
        display_name='$username'
    ).inserted_primary_key[0]

    # insert a privilege for the user
    privilege_id = e.execute(privileges.insert(),
        name=CLAN_ADMIN.name
    ).inserted_primary_key[0]
    e.execute(user_privileges.insert(),
        user_id=user_id,
        privilege_id=privilege_id
    )

    # set up the initial config
    from pyClanSphere.config import Configuration
    config_filename = join(instance_folder, 'pyClanSphere.ini')
    cfg = Configuration(config_filename)
    t = cfg.edit()
    t.update(
        maintenance_mode=False,
        site_url='http://localtest',
        secret_key=gen_secret_key(),
        database_uri=dbname,
        iid=new_iid()
    )
    t.commit()
    
    from pyClanSphere import setup
    from pyClanSphere.upgrades.webapp import WebUpgrades
    instance = setup(instance_folder)
    
    if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>":
        # Fast Migration
        from pyClanSphere.upgrades import ManageDatabase
        manage = ManageDatabase(instance)
        upgrade = manage.cmd_upgrade()
        while True:
            try:
                upgrade.next()
            except StopIteration:
                break
        from pyClanSphere._core import _unload_pyClanSphere
        _unload_pyClanSphere()

        instance = setup(instance_folder)
        if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>":
            sys.stderr.write('Automatic db migration failed, check your scripts!\n')
            sys.exit(1)
    return instance, instance_folder