H
hellosticky
I am very confused why this test is not working. I create a fake table,
then insert a row without completing the scope, and just in case,
throwing an exception, but when the test finishes, there is still a row
in the database (beyond the Assert, I can go to the database table and
see the row)!
[Test]
public void SimpleTransaction()
{
string tableName = "test" + new
Random(unchecked((int)DateTime.UtcNow.Ticks)).Next(1, 1000);
try
{
// Create a test database
using (IDbConnection conn = RDBMSLayer.DBConnect())
{
RDBMSLayer.ExecuteNonQuery(conn, "create table " +
tableName + " (id int not null primary key)");
}
using (IDbConnection conn2 = RDBMSLayer.DBConnect())
{
using (TransactionScope scope = new
TransactionScope())
{
int rowsInserted =
RDBMSLayer.ExecuteNonQuery(conn2, "insert into " + tableName + " (id)
values (1)");
Assert.AreEqual(1, rowsInserted);
// Now throw a random exception
if (rowsInserted == 1)
{
throw new Exception("Transaction
Exception");
}
}
}
}
catch (Exception)
{
using (IDbConnection conn = RDBMSLayer.DBConnect())
{
Assert.AreEqual(0, RDBMSLayer.GetRowCount(conn,
tableName));
}
}
}
then insert a row without completing the scope, and just in case,
throwing an exception, but when the test finishes, there is still a row
in the database (beyond the Assert, I can go to the database table and
see the row)!
[Test]
public void SimpleTransaction()
{
string tableName = "test" + new
Random(unchecked((int)DateTime.UtcNow.Ticks)).Next(1, 1000);
try
{
// Create a test database
using (IDbConnection conn = RDBMSLayer.DBConnect())
{
RDBMSLayer.ExecuteNonQuery(conn, "create table " +
tableName + " (id int not null primary key)");
}
using (IDbConnection conn2 = RDBMSLayer.DBConnect())
{
using (TransactionScope scope = new
TransactionScope())
{
int rowsInserted =
RDBMSLayer.ExecuteNonQuery(conn2, "insert into " + tableName + " (id)
values (1)");
Assert.AreEqual(1, rowsInserted);
// Now throw a random exception
if (rowsInserted == 1)
{
throw new Exception("Transaction
Exception");
}
}
}
}
catch (Exception)
{
using (IDbConnection conn = RDBMSLayer.DBConnect())
{
Assert.AreEqual(0, RDBMSLayer.GetRowCount(conn,
tableName));
}
}
}