class BaseTestCase(AsyncTestCase): PARAMS = dict( host=os.getenv("MYSQL_HOST", "127.0.0.1"), port=int(os.getenv("MYSQL_PORT", "3306")), user=os.getenv("MYSQL_USER", "root"), passwd=os.getenv("MYSQL_PASSWD", ""), db=os.getenv("MYSQL_DB", "test"), charset=os.getenv("MYSQL_CHARSET", "utf8"), no_delay=True, sql_mode="REAL_AS_FLOAT", init_command="SET max_join_size=DEFAULT" ) def setUp(self): super(BaseTestCase, self).setUp() self.pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", 5)), idle_seconds=7200, **self.PARAMS ) def tearDown(self): super(BaseTestCase, self).tearDown() if not self.pool.closed: self.pool.close() def get_new_ioloop(self): return IOLoop.current()
def setUp(self): super(BaseTestCase, self).setUp() self.pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", 5)), idle_seconds=7200, **self.PARAMS )
class CRUD(object): def __init__(self, app, db, schema, table, colnames, host="127.0.0.1", user="******", passwd="",): self.db = ConnectionPool( max_connections = 1000, # Max open connections idle_seconds = 7200, # Connection idle timeout time, 0 is not timeout wait_connection_timeout = 3, # Wait connection timeout host = host, user = user, passwd = passwd, db = db, charset = "utf8" ) self.app = app self.schema = schema self.table = table self.colnames = colnames @coroutine def get_user(self, name): with (yield self.db.Connection()) as conn: with conn.cursor() as cursor: yield cursor.execute( "SELECT * FROM %s WHERE %s = '%s'" % ( self.table, self.colnames['username'], name ) ) user = cursor.fetchone() if user: response = dict(zip(self.schema, user)) raise Return(response) raise Return(None) @coroutine def switch_ban_user(self, name, ban, until='', comment=''): if not self.app.moderation: raise Return() temp = "UPDATE %s SET %s=%s, %s='%s', %s='%s' WHERE %s='%s'" query = temp % ( self.table self.app.fields['isban'], ban, self.app.fields['banunt'], until, self.app.fields['bancom'], comment, self.app.fields['username'], name ) with (yield self.db.Connection()) as conn: try: with conn.cursor() as cursor: yield cursor.execute(query) except Exception as e: logging.error("Something went wrong on mysql operation!") logging.error(str(e)) yield conn.rollback() else: yield conn.commit()
def init_params(self): self.PARAMS = dict(host=os.getenv("MYSQL_HOST", "127.0.0.1"), user=os.getenv("MYSQL_USER", "root"), passwd=os.getenv("MYSQL_PASSWD", ""), db=os.getenv("MYSQL_DB", "test"), charset=os.getenv("MYSQL_CHARSET", "utf8"), sql_mode="REAL_AS_FLOAT", init_command="SET max_join_size=DEFAULT", cursorclass=SSDictCursor) self.pool = ConnectionPool(max_connections=int( os.getenv("MYSQL_POOL", 5)), idle_seconds=7200, **self.PARAMS)
def test_remote_closing(self): pool = ConnectionPool(max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS) try: self.proxy.stop() yield pool.Connection() except OperationalError: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close()
def test_pool_closing(self): pool = ConnectionPool(max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS) try: with (yield pool.Connection()) as connect: with connect.cursor() as cursor: self._close_proxy_sessions() yield cursor.execute("SELECT 1 as test") except (OperationalError, ConnectionNotFoundError) as e: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close()
def __init__(self, app, db, schema, table, colnames, host="127.0.0.1", user="******", passwd="",): self.db = ConnectionPool( max_connections = 1000, # Max open connections idle_seconds = 7200, # Connection idle timeout time, 0 is not timeout wait_connection_timeout = 3, # Wait connection timeout host = host, user = user, passwd = passwd, db = db, charset = "utf8" ) self.app = app self.schema = schema self.table = table self.colnames = colnames
def test_remote_closing(self): pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS ) try: self.proxy.stop() yield pool.Connection() except OperationalError: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close()
def test_pool_closing(self): pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS ) try: with (yield pool.Connection()) as connect: with connect.cursor() as cursor: self._close_proxy_sessions() yield cursor.execute("SELECT 1 as test") except (OperationalError, ConnectionNotFoundError) as e: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close(10)
def __init__(self): self.db = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", 10)), idle_seconds=0, # conntion idle timeout time, 0 is not timeout # cursorclass=DictCursor, **MYSQL_DATABASE_CONFIG) logging.info("init database success") self.adb_trans_sql = []
def _execute_test_remote_closing(self): self.init_proxy() pool = ConnectionPool(max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS) try: conn = yield pool.Connection() yield conn.do_close() self.proxy_server.close() yield pool.Connection() except OperationalError: pass else: raise AssertionError("Unexpected normal situation") finally: yield pool.close()
def _execute_test_remote_closing(self): self.init_proxy() pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS ) try: conn = yield pool.Connection() yield conn.do_close() self.proxy_server.close() yield pool.Connection() except OperationalError: pass else: raise AssertionError("Unexpected normal situation") finally: yield pool.close()
class BaseTestCase(AsyncTestCase): PARAMS = dict( host=os.getenv("MYSQL_HOST", "127.0.0.1"), port=int(os.getenv("MYSQL_PORT", "3306")), user=os.getenv("MYSQL_USER", "root"), passwd=os.getenv("MYSQL_PASSWD", ""), db=os.getenv("MYSQL_DB", "test"), charset=os.getenv("MYSQL_CHARSET", "utf8"), sql_mode="REAL_AS_FLOAT", init_command="SET max_join_size=DEFAULT" ) def setUp(self): super(BaseTestCase, self).setUp() self.pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", 5)), idle_seconds=7200, **self.PARAMS ) def tearDown(self): if not self.pool.closed: self.pool.close() super(BaseTestCase, self).tearDown()
class TestWithWith(BaseTestCase): @gen.coroutine def _execute_test1(self): sql = "select * from test limit 1" with (yield self.pool.Connection()) as connection: with connection.cursor() as cursor: yield cursor.execute(sql) datas = cursor.fetchall() self.assertTrue(bool(datas)) yield self.pool.close() def init_params(self): self.PARAMS = dict(host=os.getenv("MYSQL_HOST", "127.0.0.1"), user=os.getenv("MYSQL_USER", "root"), passwd=os.getenv("MYSQL_PASSWD", ""), db=os.getenv("MYSQL_DB", "test"), charset=os.getenv("MYSQL_CHARSET", "utf8"), no_delay=True, sql_mode="REAL_AS_FLOAT", init_command="SET max_join_size=DEFAULT", cursorclass=SSDictCursor) self.pool = ConnectionPool(max_connections=int( os.getenv("MYSQL_POOL", 5)), idle_seconds=7200, **self.PARAMS) @gen.coroutine def _execute_test2(self): self.init_params() sql = "select 1 as test" with (yield self.pool.Connection()) as connection: cursor = connection.cursor() yield cursor.execute(sql) result = yield cursor.fetchone() yield cursor.close() self.assertTrue('test' in result) self.assertEqual(result['test'], 1) @gen_test def test(self): yield self._execute_test1() yield self._execute_test2()