0

I've been playing around with some ADO.NET extensions inspired by a PostgreSQL Pluralsight article by Rob Conery.

I'm new to unit testing and have been trying to learn some more about it by writing some tests around a couple of extension methods I've written for the NpgsqlDataReader class.

An example of a method I'm trying to test:

    public static T ToEntity<T>(this NpgsqlDataReader reader) where T : new()
    {
        var result = new T();
        var properties = typeof(T).GetProperties();

        foreach (var prop in properties)
        {
            for (var i = 0; i < reader.FieldCount; i++)
            {
                if (reader.GetName(i).Replace("_", "").Equals(prop.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    var val = reader.GetValue(i) != DBNull.Value ? reader.GetValue(i) : null;
                    prop.SetValue(result, val);
                }
            }
        }

        return result;
    }

I'm not sure how to even start mocking the database connection, as all I want to test is functionality of iterating over the returned result object and mapping the columns to the properties of a generic entity class.

How would I go about testing the entity mapping, without accessing the database?

Thanks

Update:

The code I've been using to call the extension above is another extension on the NpgsqlCommand class:

public static IEnumerable<T> ToList<T>(this NpgsqlCommand cmd) where T : new()
{
    var results = new List<T>();
    var reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        results.Add(reader.ToEntity<T>());
    }

    reader.Close();
    reader.Dispose();

    return results;
}

I'll be using the same techniques posted in the answer below to test this method as well.

1 Answer 1

1

First of all, the method implementation does not depend on the concrete implementation of NpgsqlDataReader. So you can change the signature to:

public static T ToEntity<T>(this IDataReader reader) where T : new()

Next, the signature is not logical. A datareader is used to loop over a result set and yields a list of rows (with 0, 1 or more elements), not a single result. So a more logical signature and implementation would be:

public static IEnumerable<T> ToEntity<T>(this IDataReader reader) where T : new()
{
    var properties = typeof(T).GetProperties();

    while (reader.Read())
    {
        var result = new T();
        foreach (var prop in properties)
        {
            for (var i = 0; i < reader.FieldCount; i++)
            {
                if (reader.GetName(i).Replace("_", "").Equals(prop.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    var val = reader.GetValue(i) != DBNull.Value ? reader.GetValue(i) : null;
                    prop.SetValue(result, val);
                }
            }
        }

        yield return result;
    }
}

Now you can provide a fake implementation of IDataReader. Either you can use a mocking framework such as NSubstitute, but if you're new to unit testing a simpler way is to manually implement one. For example, in this gist (incomplete implementation) I implemented IDataReader on an in-memory generic list (and an extension method to facilitate usage).

Now you can write a test method like this:

// note the anonymous type here
var expectedEntity = new {MY_PROPERTY = "SomeValue"};
var reader = new[]
{
    expectedEntity
}.AsDataReader();
var entity = reader.ToEntity<MyEntity>().First();
Assert.AreEqual("SomeValue", entity.MyProperty);

Finally, your solution using reflection works, but may end up rather slow. Keep in mind that this kind of code is a solved problem; have alook at light-weight ORM's such as PetaPoco, dapper.net a.o.

2
  • And now the extension is more widely applicable too. If the extension had really been specific to a given provider, it would be a case where I'd consider not mocking the database as something so specific to a database needs to specifically test that, much as the tests for providers themselves do.
    – Jon Hanna
    Jan 29, 2016 at 13:20
  • Thank you for a very comprehensive answer! I've been calling this extension from another method which loops over the readers results. I realise there are other solutions to this type of problem, but I like to have a good play around with things like that for my own understanding and learning. Thanks again. I'll update the original post with the calling code. Jan 29, 2016 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.