Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with manual transactions and layered transactions

Due to a few restrictions I can't use entity Framework and thus need to use SQL Connections, commands and Transactions manually.

While writing unit tests for the methods calling these data layer operations I stumbled upon a few problems.

For the unit tests I NEED to do them in a Transaction as most of the operations are changing data by their nature and thus doing them outside a Transaction is problematic as that would change the whole base data. Thus I need to put a Transaction around these (with no commit fired at the end).

Now I have 2 different variants of how These BL methods work. A few have Transactions themselves inside of them while others have no Transactions at all. Both of these variants cause problems.

  • Layered Transaction: Here I get errors that the DTC cancelled the distributed Transaction due to timeouts (although the timeout is being set to 15 minutes and it is running for only 2 minutes).

  • Only 1 Transaction: Here I get an error about the state of the Transaction when I come to the "new SQLCommand" line in the called method.

My question here is what can I do to correct this and get unit testing with manual normal and layered Transactions working?

Unit testing method example:

using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString))
{
    connection.Open();
    using (SqlTransaction transaction = connection.BeginTransaction())
    {
        MyBLMethod();
    }
}

Example for a Transaction using method (very simplified)

using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString))
{
    connection.Open();
    using (SqlTransaction transaction = connection.BeginTransaction())
    {
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.Transaction = transaction;
        command.CommandTimeout = 900;   // Wait 15 minutes before a timeout
        command.CommandText = "INSERT ......";
        command.ExecuteNonQuery();

        // Following commands
        ....

        Transaction.Commit();
    }
}

Example for a non Transaction using method

using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString))
{
    connection.Open();

    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandTimeout = 900;   // Wait 15 minutes before a timeout
    command.CommandText = "INSERT ......";
    command.ExecuteNonQuery();
}
like image 979
Thomas Avatar asked Jun 05 '15 08:06

Thomas


1 Answers

On the face of it, you have a few options, depending upon what you want to test and your ability to spend money / change your code base.

At the moment, you’re effectively writing integration tests. If the database isn’t available then your tests will fail. This means the tests can be slow, but on the plus side if they pass you’re pretty confident that you code can hit the database correctly.

If you don’t mind hitting the database, then the minimum impact to changing your code / spending money would be for you to allow the transactions to complete and verify them in the database. You can either do this by taking database snapshots and resetting the database each test run, or by having a dedicated test database and writing your tests in such a way that they can safely hit the database over and over again and then verified. So for example, you can insert a record with an incremented id, update the record, and then verify that it can be read. You may have more unwinding to do if there are errors, but if you’re not modifying the data access code or the database structure that often then this shouldn’t be too much of an issue.

If you’re able to spend some money and you want to actually turn your tests into unit tests, so that they don’t hit the database, then you should consider looking into TypeMock. It’s a very powerful mocking framework that can do some pretty scary stuff. I believe it using the profiling API to intercept calls, rather than using the approach used by frameworks like Moq. There's an example of using Typemock to mock a SQLConnection here.

If you don’t have money to spend / you’re able to change your code and don’t mind continuing to rely on the database then you need to look at some way to share your database connection between your test code and your dataaccess methods. Two approaches that spring to mind are to either inject the connection information into the class, or make it available by injecting a factory that gives access to the connection information (in which case you can inject a mock of the factory during testing that returns the connection you want).

If you go with the above approach, rather than directly injecting SqlConnection, consider injecting a wrapper class that is also responsible for the transaction. Something like:

public class MySqlWrapper : IDisposable {
    public SqlConnection Connection { get; set; }
    public SqlTransaction Transaction { get; set; }

    int _transactionCount = 0;

    public void BeginTransaction() {
        _transactionCount++;
        if (_transactionCount == 1) {
            Transaction = Connection.BeginTransaction();
        }
    }

    public void CommitTransaction() {
        _transactionCount--;
        if (_transactionCount == 0) {
            Transaction.Commit();
            Transaction = null;
        }
        if (_transactionCount < 0) {
            throw new InvalidOperationException("Commit without Begin");
        }
    }

    public void Rollback() {
        _transactionCount = 0;
        Transaction.Rollback();
        Transaction = null;
    }


    public void Dispose() {
        if (null != Transaction) {
            Transaction.Dispose();
            Transaction = null;
        }
        Connection.Dispose();
    }
}

This will stop nested transactions from being created + committed.

If you’re more willing to restructure your code, then you might want to wrap your dataaccess code in a more mockable way. So, for example you could push your core database access functionality into another class. Depending on what you’re doing you’ll need to expand on it, however you might end up with something like this:

public interface IMyQuery {
    string GetCommand();
}

public class MyInsert : IMyQuery{
    public string GetCommand() {
        return "INSERT ...";
    }
}

class DBNonQueryRunner {
    public void RunQuery(IMyQuery query) {
        using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) {
            connection.Open();
            using (SqlTransaction transaction = connection.BeginTransaction()) {
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.Transaction = transaction;
                command.CommandTimeout = 900;   // Wait 15 minutes before a timeout
                command.CommandText = query.GetCommand();

                command.ExecuteNonQuery();

                transaction.Commit();
            }
        }
    }
}

This allows you to unit test more of your logic, like the command generation code, without having to actually worry about hitting the database and you can test your core dataaccess code (the Runner) against the database once, rather than for every command you want to run against the database. I would still write integration tests for all dataaccess code, but I’d only tend to run them whilst actually working on that section of code (to ensure column names etc have been specified correctly).

like image 73
forsvarir Avatar answered Sep 28 '22 07:09

forsvarir