A better nUnit assertion to verify content of a database Row

Sometimes you need to check that the content of various fields in a database row match some constraints, in this situation a little helper could make it possible to write more elegant assertion

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[Test]
public void TestInsertACustomerDataFluent()
{
    NorthwindCustomerDao sut = new NorthwindCustomerDao();
    sut.CreateACustomer("RICCI", "DotNetMarche", "contact", "Loc piano frassineta 31");
    DbAssert.OnQuery(
        @"SELECT * from Customers where CustomerId = 'RICCI'")
       .That("CustomerID", Is.EqualTo("RICCI"))
       .That("CompanyName", Is.EqualTo("DotNetMarche"))
       .That("ContactName", Is.EqualTo("contact"))
       .That("Address", Text.Contains("31")).ExecuteAssert();
}

The assertion can be read very quicly, assert that on query “Select … ” CustomerID is equal to Ricci, etc etc. The advantage is that you can easily use constraint for each field, in this way the whole assertion is really clear. The class to obtain this result is the following

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    public class DbAssert
    {
        public static DbAssert OnQuery(String query)
        {
            return new DbAssert(query);
        }

        public DbAssert That(String field, Constraint constraint)
        {
            constraints.Add(field, constraint);
            return this;
        }

        private DbAssert(string query)
        {
            this.query = query;
        }

        private String query;
        private Dictionary<String, Constraint> constraints = new Dictionary<String, Constraint>();

        public void ExecuteAssert()
        {
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.NorthWindTestConnectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = query;
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        NUnit.Framework.Assert.That(dr.Read(), "La query " + query + " non ha ritornato dati");
                        foreach(KeyValuePair<String, Constraint> kvp in constraints)
                        {
                            NUnit.Framework.Assert.That(dr[kvp.Key], kvp.Value, String.Format("field {0} reason:", kvp.Key));
                        }
                    }
                }
            }
        }

    }

In this version I simply use a Sql connection and a connection string stored in Settings, but it is not difficult to adapt this class to your project, or extend to make it possible to specify from external code the database to use.

alk.

Tags: xUnit Testing