QuickTip

Using NHibernate Profiler with Unit Tests

July 5, 2010 NHibernate, QuickTip, UnitTesting No comments

Today, I’ve been writing few methods for my DAL classes, of course, I decided to have one or few integration unit tests that hits database and see actual SQLs with NHibernate Profiler.

So in couple of minetes I’ve got unit tests that had following line in the beggining of each:

 HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();

if you don’t know, this line attaches your execution code with NHibernate Profiler, so I’m able to see SQLs NHibernate generates.

When I run bunch of unit tests in my testing file, I’ve got strange picture with duplicating of queries for each test with arithmetic progression. And N+1 problem, but hold on, I’m sure that I did everything through joins.

Reason is that profiler appends to my code on each new test run and that is why it start thinking that I have multiple selects to get list of something.

Solution, which I would recommend as pattern for writing Unit Tests with NHibernate Profiler is following:

        [SetUp]
        public void SetUp()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }
        [TearDown]
        public void TearDown()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Stop();
        }


No comments


Quick & cheap way to rename colum in table – sp_rename

March 31, 2010 QuickTip, SQL No comments

Quick & cheap way to rename colum in table:

EXEC sp_rename
    @objname = ‘MY_TABLE.COULMN_NAME’,
    @newname = ‘COLUMN_NAME’,
    @objtype = ‘COLUMN’

Get fun!


No comments


Simple Web Page doesn’t work after Ctrl+F5 hit in VS under Windows 7

March 14, 2010 ASP.NET, Environment, QuickTip No comments

I’ve upgraded my system to Windows 7 couple of months ago, but I haven’t used web development.

The Problem
Few days ago, I created empty web application, added new page and added <p>bla-bla<p>, then I hit Ctrl+F5 and my default browser opened, but nothing happened. I was thinking that it is because of my browser, so I switched over 3 browsers and nothing helped, I also thought that it VS2010 broke something causing asp.net development server issues, so uninstalled it and installed back, but nothing helped.

I even had a thought to reinstall system, but decided to search over the internet more deeper and found that:

Reason
Reason is that when system uses IPv6 browsers could have issues with resolving localhost. 

Solution
Disabling IPv6 is solution for this problem.

You can disable IP 6 in your system by ensuring that you have this in your registry:
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpip6ParametersDisabledComponents set to 0xffffffff

Firefox settings change
Also when searching I found for myself that we can change settings of Firefox with writing about:config in address bar.


No comments


NHibernate: Projections.RowCount()

February 19, 2010 NHibernate, QuickTip No comments

 What do you see to be wrong with this code?

        public bool RuleExists(string ruleName, string documentName)
        {
            var result = Session.CreateCriteria(typeof(Rule)“r”)
                //some aliaces 
             
                .SetProjection(Projections.RowCount())

                .Add(Restrictions.Eq(“r.Name”, ruleName))
                //other restrictions
                .List();

            return result.Count > 0;
        }


At first I did not see any issues with it so I copied it and changed a bit for my another query. But current method is wrong. I discovered this with UTs.

First, Projections.RowCount(generates COUNT(*) in select statement.

This is query, which I got from my new Unit Tests: RuleExists_HitsDatabase_ThereIsNoRule.

SELECT count(* ) AS y0_
FROM   TBL_RULE this_
       INNER JOIN — some joins here
     
WHERE  this_.NAME = ‘absolultely_incorrect_rule_name’ /* @p0 */
       AND — other conditions 

Result of this query is number of rows like on picture below:

So, verification return result.Count > 0is absolutely incorrect.
I’ve chagned it to return (int)result[0] > 0;

Moral:
Do not be lazy to write Unit Tests both for success and failure sceneries.


No comments


Few NHibernate hints on Query Criteria

February 15, 2010 NHibernate, QuickTip No comments

ORDER BY


To add “order by” to your criteria you need this statement.
.AddOrder(Order.Asc(“Priority”))

TOP


To add “top 10” to your criteria you need this statement
.SetMaxResults(10)

Criteria

So code about which I’m talking could look like:

        public IList<Customer> FetchTopPriorityCustomers()
        {
            var result = Session.CreateCriteria(typeof(Customer))
                .SetFetchMode(“CustomerStatusType”FetchMode.Eager)
                .Add(Expression.Eq(“CustomerStatusType.CustomerStatusTypeID”, (Int32)CustomerStatusType.Created))
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .AddOrder(Order.Asc(“Priority”))
                .SetMaxResults(10)
                .List<Customer>();
            return result;
        }

SetResultTransformer or “Why did I get 5 results instead of 10?”

So you expect to have top 10 priority Customers with status Created.
In scope of my current task it was needed to add priority to this query, so I decided to unit test it of course.
In Debug I found that there are actually 5 results in resulting collection.

That is because generated SQL generates result which contains duplicated CUSTOMER_IDs, that is because I have join-s there. But then why did not I get 10 duplicated Customers? Because query has ResultTransformer which is applied after SQL has been ran. (That is 100% since I took a look at generated SQL via NHibernateProfiler).

So .SetResultTransformer(new DistinctRootEntityResultTransformer()) is removing of all duplicated entries of my root entity (Customer).

Good explanation to this you can find here.


No comments


Button in pressed/unpressed state in System.Windows.Forms

January 26, 2010 QuickTip, UI, WindowsForms No comments

Do you want to see your button to be in pressed or unpressed state when you hit it once?

Did you catch that this behavior is not really button’s behavior – it is more checkBox’s behavior.

So to accomplish your goal you need move checkBox to your form and set appearance from “Normal” to “Button”. Like here:

System.Windows.Forms.CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;

Kick me if you don’t want to see such posts on my blog. :)


No comments