示例#1
0
def create_engine(**kwargs):
    if 'TEST_DB_URL' in os.environ:
        TEST_DB_URL = os.environ['TEST_DB_URL']
    else:
        TEST_DB_URL = 'sqlite://'

    if TEST_DB_URL.startswith("sqlite:"):
        # per
        # http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#serializable-isolation-savepoints-transactional-ddl,
        # necessary to test savepoints in SQLite.

        sub_engine = sqlalchemy.create_engine(TEST_DB_URL, **kwargs)

        @listens_for(sub_engine, "connect")
        def do_connect(dbapi_connection, connection_record):
            # disable pysqlite's emitting of the BEGIN statement entirely.
            # also stops it from emitting COMMIT before any DDL.
            dbapi_connection.isolation_level = None

        @listens_for(sub_engine, "begin")
        def do_begin(conn):
            # emit our own BEGIN
            conn.execute("BEGIN")
        return wrap_engine(FakeThreadedReactor(), sub_engine, ImmediateWorker)

    engine = sqlalchemy.create_engine(
        TEST_DB_URL, strategy=TWISTED_STRATEGY,
        reactor=FakeThreadedReactor(), create_worker=ImmediateWorker,
        **kwargs)
    if TEST_DB_URL.startswith("postgresql"):
        tmpdb_name = "testdb"+hexlify(os.urandom(16)).decode()
        tmpdb_url = '/'.join(
            TEST_DB_URL.split('/')[:-1] + [tmpdb_name])
        conn = engine.connect().result
        conn.execute("commit")
        conn.execute("CREATE DATABASE {}".format(tmpdb_name))
        conn.close()
        engine = sqlalchemy.create_engine(
            tmpdb_url, strategy=TWISTED_STRATEGY,
            reactor=FakeThreadedReactor(), create_worker=ImmediateWorker,
            **kwargs)
    return engine
示例#2
0
                db.session.add_all(users_db)
                db.session.commit()

        # in case there is a session with this name
        else:
            self._verify_user_in_session(self.session)


if __name__ == '__main__':

    display_message('AMS', 'creating tables in database...')
    db.create_all()
    display_message('AMS', 'tables created in database.')

    ENGINE = create_engine('sqlite:///' + os.path.join(basedir, 'data.sqlite'))
    TWISTED_ENGINE = wrap_engine(reactor, ENGINE)
    TWISTED_ENGINE.run_callable = ENGINE.run_callable

    METADATA = MetaData()
    METADATA.bind = ENGINE
    AGENTS = Table('agents', METADATA, autoload=True, autoload_with=ENGINE)

    ams = AMS(port=int(sys.argv[4]))
    # instantiates AMS agent and calls listenTCP method
    # from Twisted to launch the agent
    ams_agent = AMS()  # TODO: precisa implementar a passagem de parametros
    ams.register_user(username=sys.argv[1],
                      email=sys.argv[2],
                      password=sys.argv[3])
    ams._initialize_database()
    reactor.callLater(0.1, display_message,
示例#3
0
 def test_wrap_engine(self):
     sub_engine = sqlalchemy.create_engine("sqlite://")
     twisted_engine = wrap_engine(FakeThreadedReactor(), sub_engine)
     assert isinstance(twisted_engine, TwistedEngine)