Thursday, March 24, 2011

Approach for Oracle view unit testing with Rspec

A View in Oracle and in other database systems is simply the representation of a SQL statement (select query). It provides access to a subset of columns from one or more tables.

In a rails migration, when you incorporate a view definition which represents data from multiple tables with multiple joins and multiple conditions, how will you ensure that the view is fetching the data that it is supposed to fetch? How will you test the view logic?

And here, Unit Testing plays a very important role. Today, I am going to tell you the simple approach that we can follow for unit testing the view definition with Rspec.

First, with rails convention, you will create the model class (which maps to the view name) extending ActiveRecord::Base.
You will then set up the test data as you generally set for testing other model methods. (You can use factory_girl or simple model methods)
The purpose is to unit test each and every column data of the view according to the joins and the conditions. It all depends on the view logic that you have written according to your business requirement. The view definition can include decode statements, group by/ order by clauses, oracle aggregator functions e.g. sum, max, etc., function calls from the view sql, if-else logic to name a few.

With this, you have all the unit level requirement specs ready for your view which you can validate by running your Rspecs. Simple but succinct ! Isn't it ?

4 comments:

  1. Great ! You have explained an another important usage of unit testing which is very useful. The approach that you have suggested is really simple and is of great help to test all the views in short time. Thanks for posting.
    oracle ebs

    ReplyDelete
    Replies
    1. Thanks ! It is really good to know that the simple approach that I tried works for others as well ! Thanks for reading !

      Delete