3

Do you test your SQL or SQL generated by your database framework?

There are frameworks like DbUnit that allow you to create real in-memory database and execute real SQL. But its very hard to use(not developer-friendly so to speak), because you need to first prepare test data(and it should not be shared between tests).

P.S. I don't mean mocking database or framework's database methods, but tests that make you 99% sure that your SQL is working even after some hardcore refactoring.

2
  • This is worded as a poll rather than an actual question. What is your purpose in asking this? Presumably you want to know if you should be testing yours; if so, be specific. Explain your circumstances and your goals.
    – Aaronaught
    Jan 2, 2011 at 17:55
  • you are right, Im wondering if I should do it.
    – IAdapter
    Jan 3, 2011 at 4:41

5 Answers 5

1
  1. Always test your DAO or Repository for the most basic case, just to ensure that you've hooked everything up properly
  2. Test your DAO or Repository methods normally if it performs any logic beyond invoking the Hibernate api
  3. Do the least possible work to test the persistence layer!

You can spend way too much time and energy testing a persistence layer and getting very little value out of it if you're too concerned about the perfect test setup, this is somewhat mitigated if your company standardizes to the testing method so it is easy to setup a second or third time. But in general you can test the most important aspects just by using SQL in the setup and teardown methods, as well using custom sql assertions.

1
  • +1 - don't duplicate your ORM framework. Jan 8, 2011 at 7:10
0

I never tried DbUnit. What I do with nHibernate is creating test suites that create the database & data at setup (using both SchemaExport and batch inserts), then run tests that check both entities and queries.

I use local development databases such as SQL Server Express of Oracle Express Edition depending on the project.

In one very large project I was part of, we used the Rollback technique. Each test was creating a transaction which was rolled back at the end. This is described here.

But there is a better way of testing. Yesterday, I read a book called nHibernate 3.0 Cookbook from Jason Dentler in which the author propose a method called Fast testing with SQLite in-memory database.

I didn't test it yet, but that recipe is fully detailed and looks promising.

I'm sure you can download the sample code somewhere (wasn't able to find it quickly), but I highly suggest to buy the book. It contains various other test techniques such as Fluent nHibernate Persistance Tester and Ghostbusters test.

That technique is also explained in this blog post.

0

Yes A function or any data alteration can be tested the same as any other language. If you can test adding, deleting or updating values in an array, is working with a table that different?

If I'm trying to optimize a select statement, it will get tested against the same dataset before and after for two reasons: 1) it must return the same data 2) it's the best way to see if it is faster (compare apples to apples). Since database do so many things behind the scenes where you have less control (statistics, index fragmentation, query optimization & caching), you have to be careful when comparing performance from environment or set of data to the next. Then you can change the criteria or even the test database (Other company or different backup/version).

0

This is the way I set up my NHibernate integration tests:

I first create a fresh db before running my integration tests. Each query and entity mapping is tested in its own fixture.

I run each test in a transaction that is rolled back on completion. Each test is responsible for setting up test data.

This approach has worked fairly well for me especially testing queries and whether or not they are returning expected results.

0

In my current project I first attempted to test my SQL by checking that it matched the expected values. Then I made a change to my SQL generation that caused every query to change (in a way that made no difference to all but the newest test case) thus causing all my tests to fail.

Ugh...

Now, I test against a in memory SQLite database and the framework I use (SQLalchemy) takes care of handling the differences between that an my target database. I encapsulate the process of obtaining a database so that I can switch my tests to run against my actual database server (Firebird) occasionally, which takes considerably longer to run.

Your Answer

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

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