def test_basic(self): try: # start mysql server mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None}) self.assertIsNotNone(mysqld) self.assertEqual( mysqld.dsn(), dict(unix_socket=mysqld.base_dir + '/tmp/mysql.sock', user='******', db='test')) # connect to mysql (w/ pymysql) conn = pymysql.connect(**mysqld.dsn()) self.assertIsNotNone(conn) self.assertRegexpMatches(mysqld.read_bootlog(), 'ready for connections') # connect to mysql (w/ sqlalchemy) engine = sqlalchemy.create_engine(mysqld.url()) self.assertIsNotNone(engine) # shutting down pid = mysqld.server_pid self.assertTrue(os.path.exists(mysqld.base_dir + '/tmp/mysql.sock')) self.assertTrue(mysqld.is_alive()) finally: mysqld.stop() sleep(1) self.assertFalse( os.path.exists(mysqld.base_dir + '/tmp/mysql.sock')) self.assertFalse(mysqld.is_alive()) with self.assertRaises(OSError): os.kill(pid, 0) # process is down
def test_dsn_and_url(self): mysqld = testing.mysqld.Mysqld(auto_start=0) self.assertEqual( { 'db': 'test', 'unix_socket': mysqld.my_cnf['socket'], 'user': '******' }, mysqld.dsn()) self.assertEqual( "mysql+pymysql://root@localhost/test?unix_socket=%s" % mysqld.my_cnf['socket'], mysqld.url()) self.assertEqual( "mysql+pymysql://root@localhost/test?unix_socket=%s&charset=utf8" % mysqld.my_cnf['socket'], mysqld.url(charset='utf8')) self.assertEqual( "mysql+mysqldb://root@localhost/test?unix_socket=%s" % mysqld.my_cnf['socket'], mysqld.url(driver='mysqldb')) mysqld = testing.mysqld.Mysqld(my_cnf={'port': 12345}, auto_start=0) self.assertEqual( { 'db': 'test', 'host': '127.0.0.1', 'port': 12345, 'user': '******' }, mysqld.dsn()) self.assertEqual("mysql+pymysql://[email protected]:12345/test", mysqld.url()) self.assertEqual( "mysql+pymysql://[email protected]:12345/test?charset=utf8", mysqld.url(charset='utf8')) self.assertEqual("mysql+mysqldb://[email protected]:12345/test", mysqld.url(driver='mysqldb'))
def test_copy_data_from_with_passwd(self): try: tmpdir = tempfile.mkdtemp() # create new database with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, base_dir=tmpdir) as mysqld: conn = pymysql.connect(**mysqld.dsn()) cursor = conn.cursor() cursor.execute( "CREATE TABLE hello(id int, value varchar(256))") cursor.execute( "INSERT INTO hello values(1, 'hello'), (2, 'ciao')") cursor.execute( "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secret'); FLUSH PRIVILEGES;" ) conn.commit() # create another database from first one data_dir = os.path.join(tmpdir, 'var') with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, copy_data_from=data_dir, passwd="secret") as mysqld: conn = pymysql.connect(**mysqld.dsn()) cursor = conn.cursor() cursor.execute('SELECT * FROM test.hello ORDER BY id') self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao'))) finally: rmtree(tmpdir)
def test_basic(self): try: # start mysql server mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None}) self.assertIsNotNone(mysqld) self.assertEqual(mysqld.dsn(), dict(unix_socket=mysqld.base_dir + '/tmp/mysql.sock', user='******', db='test')) # connect to mysql (w/ pymysql) conn = pymysql.connect(**mysqld.dsn()) self.assertIsNotNone(conn) self.assertRegexpMatches(mysqld.read_bootlog(), 'ready for connections') # connect to mysql (w/ sqlalchemy) engine = sqlalchemy.create_engine(mysqld.url()) self.assertIsNotNone(engine) # shutting down pid = mysqld.server_pid self.assertTrue(os.path.exists(mysqld.base_dir + '/tmp/mysql.sock')) self.assertTrue(mysqld.is_alive()) finally: mysqld.stop() sleep(1) self.assertFalse(os.path.exists(mysqld.base_dir + '/tmp/mysql.sock')) self.assertFalse(mysqld.is_alive()) with self.assertRaises(OSError): os.kill(pid, 0) # process is down
def test_execute_bad_query(): q = "SELECT blarg" with testing.mysqld.Mysqld() as mysqld: dsn = mysqld.dsn() conn = pymysql.connect(database=dsn['db'], user=dsn['user'], password='', host=dsn['host'], port=dsn['port']) cur = conn.cursor() try: cur.execute(q) except Exception: pass subsegment = xray_recorder.current_segment().subsegments[-1] assert subsegment.name == "execute" sql = subsegment.sql assert sql['database_type'] == 'MySQL' assert sql['user'] == dsn['user'] assert sql['driver_version'] == 'PyMySQL' assert sql['database_version'] exception = subsegment.cause['exceptions'][0] assert exception.type == 'InternalError'
def handler(mysqld): conn = pymysql.connect(**mysqld.dsn()) with closing(conn.cursor()) as cursor: cursor.execute("CREATE TABLE hello(id int, value varchar(256))") cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')") conn.commit() conn.close()
def test_dsn_and_url(self): mysqld = testing.mysqld.Mysqld(auto_start=0) self.assertEqual({'db': 'test', 'unix_socket': mysqld.my_cnf['socket'], 'user': '******'}, mysqld.dsn()) self.assertEqual("mysql+pymysql://root@localhost/test?unix_socket=%s" % mysqld.my_cnf['socket'], mysqld.url()) self.assertEqual("mysql+pymysql://root@localhost/test?unix_socket=%s&charset=utf8" % mysqld.my_cnf['socket'], mysqld.url(charset='utf8')) self.assertEqual("mysql+mysqldb://root@localhost/test?unix_socket=%s" % mysqld.my_cnf['socket'], mysqld.url(driver='mysqldb')) mysqld = testing.mysqld.Mysqld(my_cnf={'port': 12345}, auto_start=0) self.assertEqual({'db': 'test', 'host': '127.0.0.1', 'port': 12345, 'user': '******'}, mysqld.dsn()) self.assertEqual("mysql+pymysql://[email protected]:12345/test", mysqld.url()) self.assertEqual("mysql+pymysql://[email protected]:12345/test?charset=utf8", mysqld.url(charset='utf8')) self.assertEqual("mysql+mysqldb://[email protected]:12345/test", mysqld.url(driver='mysqldb'))
def handler(mysqld): conn = pymysql.connect(**mysqld.dsn()) with closing(conn.cursor()) as cursor: cursor.execute( "CREATE TABLE hello(id int, value varchar(256))") cursor.execute( "INSERT INTO hello values(1, 'hello'), (2, 'ciao')") conn.commit() conn.close()
def handler(mysqld): conn = MySQLdb.connect(**mysqld.dsn()) cursor = conn.cursor() with open(MYSQL_INIT, 'r') as sql_init_file: for line in sql_init_file.read().split(';'): if not line.strip(): continue cursor.execute(line.strip()) cursor.close() conn.commit() conn.close()
def test_with_mysql(self): with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}) as mysqld: self.assertIsNotNone(mysqld) # connect to mysql conn = pymysql.connect(**mysqld.dsn()) self.assertIsNotNone(conn) self.assertTrue(mysqld.is_alive()) self.assertFalse(mysqld.is_alive())
def test_copy_data_from(self): try: tmpdir = tempfile.mkdtemp() # create new database with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, base_dir=tmpdir) as mysqld: conn = pymysql.connect(**mysqld.dsn()) cursor = conn.cursor() cursor.execute("CREATE TABLE hello(id int, value varchar(256))") cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')") conn.commit() # create another database from first one data_dir = os.path.join(tmpdir, 'var') with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, copy_data_from=data_dir) as mysqld: conn = pymysql.connect(**mysqld.dsn()) cursor = conn.cursor() cursor.execute('SELECT * FROM test.hello ORDER BY id') self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao'))) finally: rmtree(tmpdir)
def db_connection_fx(sessionmonkey): """ Patch mysql connector to return a test database connection. Yield that database connection. """ with testing.mysqld.Mysqld() as mysqld: conn = mysql.connector.connect(**mysqld.dsn()) def _connection_factory(**kwargs): # pylint: disable=unused-argument return conn sessionmonkey.setattr(mysql.connector, "connect", _connection_factory) yield conn
def test_execute_dsn_kwargs(): q = 'SELECT 1' with testing.mysqld.Mysqld() as mysqld: dsn = mysqld.dsn() conn = pymysql.connect(database=dsn['db'], user=dsn['user'], password='', host=dsn['host'], port=dsn['port']) cur = conn.cursor() cur.execute(q) subsegment = xray_recorder.current_segment().subsegments[-1] assert subsegment.name == 'execute' sql = subsegment.sql assert sql['database_type'] == 'MySQL' assert sql['user'] == dsn['user'] assert sql['driver_version'] == 'PyMySQL' assert sql['database_version']
def test_MysqldFactory_with_initialized_handler(self): def handler(mysqld): conn = pymysql.connect(**mysqld.dsn()) with closing(conn.cursor()) as cursor: cursor.execute("CREATE TABLE hello(id int, value varchar(256))") cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')") conn.commit() conn.close() Mysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True, on_initialized=handler) try: with Mysqld() as mysqld: conn = pymysql.connect(**mysqld.dsn()) with closing(conn.cursor()) as cursor: cursor.execute('SELECT * FROM hello ORDER BY id') self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao'))) conn.close() finally: Mysqld.clear_cache()
def test_MysqldFactory_with_initialized_handler(self): def handler(mysqld): conn = pymysql.connect(**mysqld.dsn()) with closing(conn.cursor()) as cursor: cursor.execute( "CREATE TABLE hello(id int, value varchar(256))") cursor.execute( "INSERT INTO hello values(1, 'hello'), (2, 'ciao')") conn.commit() conn.close() Mysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True, on_initialized=handler) try: with Mysqld() as mysqld: conn = pymysql.connect(**mysqld.dsn()) with closing(conn.cursor()) as cursor: cursor.execute('SELECT * FROM hello ORDER BY id') self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao'))) conn.close() finally: Mysqld.clear_cache()
def handler(mysqld): conn = mysql.connector.connect(**mysqld.dsn()) cursor = conn.cursor(buffered=True) cursor.execute(""" CREATE TABLE `message_entry` ( `id` int(11) NOT NULL AUTO_INCREMENT, `message_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `file_id` int(11) DEFAULT NULL, `content` text NOT NULL, `created_at` datetime DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_user_id` (`user_id`), KEY `index_message_id` (`message_id`) ) DEFAULT CHARSET=utf8""") cursor.execute(""" CREATE TABLE `user_message` ( `message_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `is_originator` tinyint(4) DEFAULT NULL, `last_viewed` datetime DEFAULT NULL, `created_at` datetime DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, PRIMARY KEY (`message_id`,`user_id`), KEY `index_last_viewed` (`last_viewed`) ) DEFAULT CHARSET=utf8;""") cursor.execute(""" CREATE TABLE `group` ( `id` int(11) NOT NULL AUTO_INCREMENT, `space_id` int(10) DEFAULT NULL, `name` varchar(45) DEFAULT NULL, `description` text, `created_at` datetime DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, `ldap_dn` varchar(255) DEFAULT NULL, `is_admin_group` tinyint(1) NOT NULL DEFAULT '0', `show_at_registration` tinyint(1) NOT NULL DEFAULT '1', `show_at_directory` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;""") cursor.execute(""" CREATE TABLE `group_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `group_id` int(11) NOT NULL, `is_group_manager` tinyint(1) NOT NULL DEFAULT '0', `created_at` datetime DEFAULT NULL, `created_by` int(11) DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `updated_by` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;""") cursor.executemany( """INSERT INTO `message_entry` (`message_id`, `user_id`, `content`, `created_at`, `created_by`, `updated_at`, `updated_by`) VALUES (%s, %s, %s, %s, %s, %s, %s)""", [(1, 1, 'Foo', '2017-09-26 11:59:04', 1, '2017-09-26 11:59:04', 1), (1, 1, '!bot Bar', '2017-09-26 11:59:09', 1, '2017-09-26 11:59:09', 1), (1, 1, '!bot Foo', '2017-09-26 12:09:46', 1, '2017-09-26 12:09:46', 1), (1, 1, '!bot Foobar', '2017-09-26 12:11:10', 1, '2017-09-26 12:11:10', 1), (1, 1, 'Lorem', '2017-09-27 11:32:15', 1, '2017-09-27 11:32:15', 1), (1, 1, '!bot Lipsum', '2017-09-28 12:58:20', 1, '2017-09-28 12:58:20', 1), (1, 1, 'Lorem Lipsum', '2017-10-01 10:23:26', 1, '2017-10-01 10:23:26', 1)]) cursor.execute(""" INSERT INTO `user_message` (`message_id`, `user_id`, `is_originator`, `last_viewed`, `created_at`, `created_by`, `updated_at`, `updated_by`) VALUES (1, 1, 1, '2017-10-01 10:23:26', '2017-09-26 11:59:04', 1, '2017-10-01 10:23:26', 1);""" ) cursor.execute(""" INSERT INTO `group` (`id`, `space_id`, `name`, `description`, `created_at`, `created_by`, `updated_at`, `updated_by`, `ldap_dn`, `is_admin_group`, `show_at_registration`, `show_at_directory`) VALUES (1, NULL, 'Administrator', 'Administrator Group', '2017-05-28 14:09:29', NULL, NULL, NULL, NULL, 1, 0, 0), (2, NULL, 'Users', 'Example Group by Installer', '2017-05-28 14:09:32', NULL, '2017-05-28 14:09:32', NULL, NULL, 0, 1, 1), (3, NULL, 'Bots', 'User Group for Bots', '2017-11-30 13:48:26', 1, '2017-11-30 13:48:26', 1, NULL, 0, 0, 1);""" ) cursor.execute(""" INSERT INTO `group_user` (`id`, `user_id`, `group_id`, `is_group_manager`, `created_at`, `created_by`, `updated_at`, `updated_by`) VALUES (1, 1, 1, 0, '2017-05-28 14:10:59', NULL, '2017-05-28 14:10:59', NULL), (2, 4, 2, 0, '2017-09-07 09:46:13', 1, '2017-09-07 09:46:13', 1), (3, 5, 2, 0, '2017-09-19 12:48:11', 1, '2017-09-19 12:48:11', 1), (4, 6, 2, 0, '2017-11-22 18:25:47', 1, '2017-11-22 18:25:47', 1), (5, 6, 1, 0, '2017-11-22 18:26:12', 1, '2017-11-22 18:26:12', 1), (6, 5, 3, 0, '2017-11-30 13:48:39', 1, '2017-11-30 13:48:39', 1);""") cursor.close() conn.commit() conn.close()