def test_connected(self): config = self.connect_kwargs.copy() cmy = MySQL() self.assertFalse(cmy.connected()) cmy.connect(**config) self.assertTrue(cmy.connected()) cmy.close() self.assertFalse(cmy.connected())
def test_thread_id(self): cmy = MySQL() self.assertRaises(MySQLInterfaceError, cmy.thread_id) cmy.connect(**self.connect_kwargs) self.assertIsInstance(cmy.thread_id(), int) self.assertGreater(cmy.thread_id(), 0) thread_id = cmy.thread_id() cmy.close() self.assertRaises(MySQLError, cmy.thread_id)
def test_thread_id(self): cmy = MySQL() self.assertRaises(MySQLInterfaceError, cmy.thread_id) cmy.connect(**self.connect_kwargs) if tests.PY2: self.assertIsInstance(cmy.thread_id(), long) else: self.assertIsInstance(cmy.thread_id(), int) self.assertGreater(cmy.thread_id(), 0) thread_id = cmy.thread_id() cmy.close() self.assertRaises(MySQLError, cmy.thread_id)
def test_autocommit(self): cmy1 = MySQL(buffered=True) cmy1.connect(**self.connect_kwargs) cmy2 = MySQL(buffered=True) cmy2.connect(**self.connect_kwargs) self.assertRaises(ValueError, cmy1.autocommit, 'ham') self.assertRaises(ValueError, cmy1.autocommit, 1) self.assertRaises(ValueError, cmy1.autocommit, None) table = "autocommit_test" # For the test we start off by making sure the autocommit is off # for both sessions cmy1.query("SELECT @@global.autocommit") if cmy1.fetch_row()[0] != 1: cmy1.query("SET @@session.autocommit = 0") cmy2.query("SET @@session.autocommit = 0") cmy1.query("DROP TABLE IF EXISTS {0}".format(table)) cmy1.query("CREATE TABLE {0} (c1 INT)".format(table)) # Turn AUTOCOMMIT on cmy1.autocommit(True) cmy1.query("INSERT INTO {0} (c1) VALUES " "(1), (2), (3)".format(table)) cmy2.query("SELECT * FROM {0}".format(table)) self.assertEqual(3, cmy2.num_rows()) rows = fetch_rows(cmy2) # Turn AUTOCOMMIT off cmy1.autocommit(False) cmy1.query("INSERT INTO {0} (c1) VALUES " "(4), (5), (6)".format(table)) cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table)) self.assertEqual([], fetch_rows(cmy2)) cmy1.commit() cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table)) self.assertEqual([(4, ), (5, ), (6, )], fetch_rows(cmy2)) cmy1.query("DROP TABLE IF EXISTS {0}".format(table)) cmy1.close() cmy2.close()
def test_autocommit(self): cmy1 = MySQL(buffered=True) cmy1.connect(**self.connect_kwargs) cmy2 = MySQL(buffered=True) cmy2.connect(**self.connect_kwargs) self.assertRaises(ValueError, cmy1.autocommit, 'ham') self.assertRaises(ValueError, cmy1.autocommit, 1) self.assertRaises(ValueError, cmy1.autocommit, None) table = "autocommit_test" # For the test we start off by making sure the autocommit is off # for both sessions cmy1.query("SELECT @@global.autocommit") if cmy1.fetch_row()[0] != 1: cmy1.query("SET @@session.autocommit = 0") cmy2.query("SET @@session.autocommit = 0") cmy1.query("DROP TABLE IF EXISTS {0}".format(table)) cmy1.query("CREATE TABLE {0} (c1 INT)".format(table)) # Turn AUTOCOMMIT on cmy1.autocommit(True) cmy1.query("INSERT INTO {0} (c1) VALUES " "(1), (2), (3)".format(table)) cmy2.query("SELECT * FROM {0}".format(table)) self.assertEqual(3, cmy2.num_rows()) rows = fetch_rows(cmy2) # Turn AUTOCOMMIT off cmy1.autocommit(False) cmy1.query("INSERT INTO {0} (c1) VALUES " "(4), (5), (6)".format(table)) cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table)) self.assertEqual([], fetch_rows(cmy2)) cmy1.commit() cmy2.query("SELECT * FROM {0} WHERE c1 > 3".format(table)) self.assertEqual([(4,), (5,), (6,)], fetch_rows(cmy2)) cmy1.query("DROP TABLE IF EXISTS {0}".format(table)) cmy1.close() cmy2.close()
def test_connect(self): config = self.connect_kwargs.copy() cmy = MySQL() self.assertFalse(cmy.ping()) # Using Unix Socket cmy.connect(**config) self.assertTrue(cmy.ping()) # Using TCP config['unix_socket'] = None cmy.connect(**config) self.assertTrue(cmy.ping()) self.assertEqual(None, cmy.close()) self.assertFalse(cmy.ping()) self.assertEqual(None, cmy.close())