A test is only as good as the person writing it

I recently introduced the NHibernate.Caches.SysCache into my application to cache some very common queries. I created a unit test like the following to ensure it was properly working.

[Test]
public void DoesNotLoadEntityFromSysCache()
{
Foo foo = new Foo();
foo.Name = "Craig";

using (ISession s = OpenSession())
{
s.Save(foo);
}

using (ISession s = OpenSession())
{
Foo result = (Foo)s.CreateCriteria(typeof(Foo))
.Add(Property.ForName("Name").Eq("Craig"))
.SetCacheable(true)
.UniqueResult();

Assert.IsNotNull(result);
}

using (ISession s = OpenSession())
{
using (IDbCommand command = s.Connection.CreateCommand())
{
command.CommandText = "DELETE FROM Foos";
command.ExecuteNonQuery();
}
}

using (ISession s = OpenSession())
{
Foo result = (Foo)s.CreateCriteria(typeof(Foo))
.Add(Property.ForName("Name").Eq("Craig"))
.SetCacheable(true)
.UniqueResult();

Assert.IsNotNull(result);
}

using (ISession s = OpenSession())
{
s.Delete("from Foo");
s.Flush();
}
}

Too my surprise, it didn't work :-(

In a nutshell, I was using NHibernate to Save the Foo entity. The first error I made, was to not put this in a transaction. NHibernate requires it to be in a transaction to propertly work. Without the transaction, the Foo entities in the Second Level Cache were polluted and not candidates for query cache. However, applying the Save in a transaction made the unit test pass, but this prevented testing what I actually wanted to do since the committing of the transaction put my Foo in the second level cache and thus put the cache in a populated state before my query ran. The simple solution was to insert the Foo explicitly and then run the queries. Here is the correct test

[Test]
public void CanQueryFromSecondLevelCache()
{
using (ISession s = OpenSession())
{
using (IDbCommand command = s.Connection.CreateCommand())
{
command.CommandText = "INSERT INTO Foos VALUES('Craig')";
command.ExecuteNonQuery();
}
}

using (ISession s = OpenSession())
{
Foo result = (Foo)s.CreateCriteria(typeof(Foo))
.Add(Property.ForName("Name").Eq("Craig"))
.SetCacheable(true)
.UniqueResult();

Assert.IsNotNull(result);
}

using (ISession s = OpenSession())
{
using (IDbCommand command = s.Connection.CreateCommand())
{
command.CommandText = "DELETE FROM Foos";
command.ExecuteNonQuery();
}
}

using (ISession s = OpenSession())
{
Foo result = (Foo)s.CreateCriteria(typeof(Foo))
.Add(Property.ForName("Name").Eq("Craig"))
.SetCacheable(true)
.UniqueResult();

Assert.IsNotNull(result);
}
}

Much thanks to James Kovacs and Ayende for setting me straight on the often mysterious Second Level Cache

Print | posted on Tuesday, September 09, 2008 10:45 AM

Feedback

# re: A test is only as good as the person writing it

Left by Roelof at 9/12/2008 3:00 AM
Gravatar Cool, very helpful.

Now how about blogging a bit about the fluent MicroKernel registration stuff, or the Wcf facility? It's really great stuff but it lacks exposure and that's a shame.

Cheers,
Roelof.

# Viagra.

Left by Compare stay erect and viagra. at 7/8/2009 1:12 AM
Gravatar Free viagra sample.

# Re viagra cello.

Left by Best price on viagra. at 7/8/2009 5:30 PM
Gravatar Viagra. Generic viagra mastercard. Viagra vs cialis. Viagra samples. Recreational viagra. Buy viagra. Viagra 6 free samples.

# Vicodin.

Left by Buy vicodin without a prescription. at 7/9/2009 5:06 PM
Gravatar Vicodin.

# Effects of soma.

Left by Soma. at 7/10/2009 5:09 PM
Gravatar Prescription drug called soma. Soma. Pornstar soma.

# Tramadol.

Left by Tramadol. at 7/11/2009 5:04 PM
Gravatar Tramadol.

# Vicodin interaction with metropolol.

Left by Vicodin. at 7/12/2009 5:23 PM
Gravatar Buy vicodin online.

# Phentermine.

Left by Discount phentermine. at 7/13/2009 5:34 PM
Gravatar Low cost phentermine. Phentermine. Phentermine cheap. Phentermine no prescription. Phentermine forum. Buy no phentermine prescription.

# Phentermine forum.

Left by Phentermine. at 7/14/2009 5:31 PM
Gravatar Phentermine blue. Online phentermine. Discount phentermine.

# Percocet.

Left by Percocet. at 7/17/2009 6:57 PM
Gravatar Side effects of percocet. Percocet. Buy percocet online.

# Tramadol.

Left by Comparative potencies of opioids tramadol. at 7/18/2009 5:44 PM
Gravatar Tramadol 180. Tramadol.

# Soma to florida.

Left by Soma prozac. at 7/19/2009 6:19 PM
Gravatar Soma sonic music. Effects of soma. Soma next day delievery. Soma.

# re: A test is only as good as the person writing it

Left by software development company at 8/19/2009 12:50 PM
Gravatar That was inspiring,
Although testing is my weekness, I'm happy to learn the new testing pattern
Thanks

# re: A test is only as good as the person writing it

Left by New Homes Ft. Lee at 2/16/2010 6:05 AM
Gravatar Thanks so much for posting these codes, hope these are useful for the concerned users. :)

# re: A test is only as good as the person writing it

Left by Childrens Outdoor Play at 2/18/2010 10:48 PM
Gravatar These codes will be helpful to many working professionals in more than one ways. Thanks for sharing this article!

# re: A test is only as good as the person writing it

Left by Pool Fencing at 2/19/2010 7:54 AM
Gravatar This is very important article for many people as there are many who ignore the importance of testing. Thanks for sharing! :)

# re: A test is only as good as the person writing it

Left by cheap picture frames at 2/23/2010 12:19 AM

# re: A test is only as good as the person writing it

Left by free recipes at 2/24/2010 1:38 AM
Gravatar Gravatar Side effects of percocet. Percocet. Buy percocet online.

# re: A test is only as good as the person writing it

Left by Teach Your Child To Read at 2/27/2010 12:52 AM
Gravatar On a related topic I recently found out that Fluent NHibernate is a statically compiled alternative to NHibernate's standard hbm xml mapping.

# re: A test is only as good as the person writing it

Left by acekard 2 at 3/8/2010 8:56 PM

# re: A test is only as good as the person writing it

Left by rolex at 3/11/2010 8:12 PM
Gravatar $75 Replica Rolex Watches sale, Our site provides Rolex replica, replica Rolex Watches, replica breitling, replica Cartier, replica Omega, Tag Heuer Watches. . .more than 100 famous rolex brands"

# re: A test is only as good as the person writing it

Left by Depression Treatment at 3/12/2010 8:04 AM
Gravatar Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Your comment:





 
Please add 6 and 3 and type the answer here:

Copyright © Craig

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski