Ayende @ Rahien

Refunds available at head office

NHProf: Alive! It is alive!

I just finished writing the final test for the basic functionality that I want for NHibernate Profiler:

        [Test]
public void SelectBlogById()
{
ExecuteScenarioInDifferentProcess<SelectBlogByIdUsingCriteria>();
StatementModel selectBlogById = observer.Model.Sessions.First()
.Statements.First();
const string expected = @"SELECT this_.Id as Id3_0_,
this_.Title as Title3_0_,
this_.Subtitle as Subtitle3_0_,
this_.AllowsComments as AllowsCo4_3_0_,
this_.CreatedAt as CreatedAt3_0_
FROM Blogs this_
WHERE this_.Id = @p0

";
Assert.AreEqual(expected, selectBlogById.Text);
}

I actually had to invest some thought about the architecture of testing this. This little test has a whole set of ideas behind it, about which I'll talk about at a later date. Suffice to say that this test creates a new process, start to listen to interesting things that are going on there (populating the observer model with data).

Another interesting tidbit is that the output is formatted for readability. By default, NHiberante's SQL output looks something like this:

SELECT this_.Id as Id3_0_, this_.Title as Title3_0_, this_.Subtitle as Subtitle3_0_, this_.AllowsComments as AllowsCo4_3_0_, this_.CreatedAt as CreatedAt3_0_ FROM Blogs this_ WHERE this_.Id = @p0

This is pretty hard to read the moment that you have any sort of complex conditions.

Comments

pb
10/13/2008 12:19 AM by
pb

Would you consider the test to be overspecified since formatting changes would break this test which tests functionality?

Cory Foy
10/13/2008 12:29 AM by
Cory Foy

Not even formatting changes. This test will fail on Mono/Linux unless the code is compiled because all of those newlines are \r\n and Linux would be \n. Trust me - that was one of the most difficult challenges we faced with getting NUnit running. System.Environment.NewLine is your friend:

www.cornetdesign.com/.../...nvironmentnewline.html

Krzysztof Koźmic
10/13/2008 07:10 AM by
Krzysztof Koźmic

Maybe it's time to introduce WhitespaceIgnoringStringComparer ? To NUnit, not Ayende's tool.

Ken Egozi
10/13/2008 07:19 AM by
Ken Egozi

Is the code available anywhere?

Ayende Rahien
10/13/2008 08:19 AM by
Ayende Rahien

Ken,

This is going to be commercial, so I don't think so

pb
10/15/2008 01:46 PM by
pb

I was thinking more along the lines of Query.HasColumns(new string[] { "Column1", "Column1").IsForTable("SomeTable"), etc.

Ayende Rahien
10/15/2008 01:51 PM by
Ayende Rahien

pb,

Can you explain more? I don't think that I follow you

pb
10/15/2008 07:20 PM by
pb

Instead of checking the SQL string, check what the SQL string is based on so it isn't dependent on formatting changes, those auto generated aliases, etc., Maybe something like:

Assert.IsTrue(selectBlogById.HasTablesExact(new string[] {

"Blogs"

}));

Assert.IsTrue(selectBlogById.HasColumnsExact(new string[] {

"Id",

"Title",

"Subtitle",

"AllowsComments",

"CreatedAt"

}));

Comments have been closed on this topic.