public void CUBRIDCommand_Constructor_SQLAndConnAndTran_Test() { CUBRIDConnection conn = new CUBRIDConnection(); conn.ConnectionString = DBHelper.connString; conn.Open(); conn.SetAutoCommit(false); string sql = "drop table if exists t"; CUBRIDTransaction transaction = new CUBRIDTransaction(conn, CUBRIDIsolationLevel.TRAN_DEFAULT_ISOLATION); CUBRIDCommand cmd = new CUBRIDCommand(sql, conn, transaction); conn.BeginTransaction(); cmd.ExecuteNonQuery(); cmd.CommandText = "create table t (id int, name varchar(50))"; cmd.ExecuteNonQuery(); cmd.CommandText = "insert into t values(1, 'Nancy')"; cmd.ExecuteNonQuery(); conn.Commit(); cmd.CommandText = "select * from t"; CUBRIDDataReader reader = (CUBRIDDataReader)cmd.ExecuteReader(); reader.Read(); Assert.AreEqual(2, reader.FieldCount); Assert.AreEqual("1", reader.GetString(0)); Assert.AreEqual("Nancy", reader.GetString(1)); cmd.Close(); reader.Close(); conn.Close(); }
public void CUBRIDTransaction_CUBRIDIsolationLevel_Test() { LogTestStep("Test CUBRIDTransaction.CUBRIDConnection"); CUBRIDConnection conn = new CUBRIDConnection(DBHelper.connString); conn.Open(); CUBRIDTransaction transaction = conn.BeginTransaction(CUBRIDIsolationLevel.TRAN_DEFAULT_ISOLATION); Console.WriteLine(transaction.CUBRIDIsolationLevel); Assert.AreEqual(CUBRIDIsolationLevel.TRAN_DEFAULT_ISOLATION, transaction.CUBRIDIsolationLevel); LogStepPass(); LogTestResult(); }
public void CUBRIDTransaction_Commit_Rollback_Negtive2_Test() { using (CUBRIDConnection conn = new CUBRIDConnection(DBHelper.connString)) { conn.Open(); DBHelper.ExecuteSQL("drop table if exists t", conn); LogTestStep("Begin a transaction, commit after close the connection"); CUBRIDTransaction transaction = conn.BeginTransaction(); DBHelper.ExecuteSQL("create table t(idx integer)", conn); try { conn.Close(); transaction.Commit(); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("Connection must be valid and open to commit transaction!", ex.Message); LogStepPass(); } conn.Open(); LogTestStep("Begin a transaction, rollback after close the connection"); CUBRIDTransaction transaction2 = conn.BeginTransaction(); DBHelper.ExecuteSQL("drop table if exists t", conn); try { conn.Close(); transaction2.Rollback(); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("Connection must be valid and open to rollback transaction!", ex.Message); LogStepPass(); } LogTestResult(); } }
public void CUBRIDTransaction_Commit_Rollback_Test() { using (CUBRIDConnection conn = new CUBRIDConnection()) { conn.ConnectionString = DBHelper.connString; conn.Open(); DBHelper.ExecuteSQL("drop table if exists t", conn); LogTestStep("Begin a transaction, then rollback"); CUBRIDTransaction transaction = conn.BeginTransaction(); DBHelper.ExecuteSQL("create table t(idx integer)", conn); int tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); transaction.Rollback(); Log("Verify the table does not exist"); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(0, tablesCount); LogStepPass(); LogTestStep("Begin a transaction, then commit"); transaction = conn.BeginTransaction(); DBHelper.ExecuteSQL("create table t(idx integer)", conn); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); transaction.Commit(); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); LogStepPass(); //revert the test db DBHelper.ExecuteSQL("drop table t", conn); LogTestResult(); } }
public void CUBRIDTransaction_CUBRIDConnection_Test() { LogTestStep("Test CUBRIDTransaction.CUBRIDConnection"); CUBRIDConnection conn = new CUBRIDConnection(DBHelper.connString); conn.Open(); CUBRIDTransaction transaction = conn.BeginTransaction(); transaction.Connection.Close(); try { DBHelper.ExecuteSQL("drop table if exists t", conn); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("The connection is not open!", ex.Message); LogStepPass(); } LogTestResult(); }
public void CUBRIDTransaction_Commit_Rollback_Negtive1_Test() { using (CUBRIDConnection conn = new CUBRIDConnection(DBHelper.connString)) { conn.Open(); DBHelper.ExecuteSQL("drop table if exists t", conn); LogTestStep("Begin a transaction, commit twice"); CUBRIDTransaction transaction = conn.BeginTransaction(); DBHelper.ExecuteSQL("create table t(idx integer)", conn); transaction.Commit(); int tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); try { transaction.Commit(); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("Transaction has already been committed or is not pending!", ex.Message); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); LogStepPass(); } LogTestStep("Begin a transaction, rollback twice"); CUBRIDTransaction transaction2 = conn.BeginTransaction(); DBHelper.ExecuteSQL("drop table if exists t", conn); transaction2.Rollback(); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); try { transaction.Rollback(); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("Transaction has already been committed or is not pending!", ex.Message); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(1, tablesCount); LogStepPass(); } LogTestStep("Begin a transaction, commit, then rollback"); CUBRIDTransaction transaction3 = conn.BeginTransaction(); DBHelper.ExecuteSQL("drop table if exists t", conn); transaction3.Commit(); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(0, tablesCount); try { transaction3.Rollback(); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("Transaction has already been committed or is not pending!", ex.Message); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(0, tablesCount); LogStepPass(); } LogTestStep("Begin a transaction, rollback, then commit"); CUBRIDTransaction transaction4 = conn.BeginTransaction(); DBHelper.ExecuteSQL("create table t(idx integer)", conn); transaction4.Rollback(); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(0, tablesCount); try { transaction4.Commit(); LogStepFail(); } catch (Exception ex) { Assert.AreEqual("Transaction has already been committed or is not pending!", ex.Message); tablesCount = DBHelper.GetTablesCount("t", conn); Assert.AreEqual(0, tablesCount); LogStepPass(); } LogTestResult(); } }