Simplifying Unit Testing Main Stored Procedure Which Also Calls a Utility Procedure

Simplifying Unit Testing Main Stored Procedure Which Also Calls a Utility Procedure
3.3 (66.67%) 3 votes

This article provides a walkthrough of database unit testing a stored procedure which contains a utility procedure within it.

In this article, I am going to discuss a database unit testing scenario when a main stored procedure depends on a utility procedure and the main procedure needs to be unit tested in order to make sure that the requirements are met. The key is to ensure that a unit test can only be written for a single unit of code which means we need one unit test for the main procedure and another unit test for the utility procedure.

Unit testing a single stored procedure is easier as compared to unit testing a procedure which calls a utility procedure inside its code.

It is very important to understand the utility procedure scenario and why it is different than unit testing a normal stored procedure.

Scenario: Utility Procedure within Main Procedure

In order to understand utility procedure scenario let us begin by the definition and example of utility procedure:

What is Utility Procedure

A utility procedure is generally a small procedure which is used by the main procedure(s) to do some specific task such as getting something for the main procedure or adding something to the main procedure.

Another definition of utility procedure is a small stored procedure written for maintenance purposes which may involve system tables or views to be called by any number of procedures or even directly.

Examples of Utility Procedure

Think of a customer-order-product scenario where a customer places an order for a particular product. If we create the main procedure to get us all the orders placed by a particular customer then a utility procedure can be used to help us understand whether each order was placed by the customer on weekday or weekend.
In this way, a small utility procedure can be written to return “Weekday” or “Weekend” based on the date the product was ordered by the customer.

Another example can be system stored procedures such as “sp_server_info” in the master database which gives SQL Server installed version information:

Why Unit Testing Utility Procedure is Different

As discussed earlier, unit testing a utility procedure which is called inside the main procedure is slightly complicated than unit testing a simple stored procedure.

Considering customer-order-product example mentioned above, we need to write a unit test to check utility procedure is working fine and also a unit test has to be written to check the main procedure which calls utility procedure is also functioning properly as well as meeting the business requirement(s).

This is illustrated as follows:

Isolating from Utility/Main Procedure Challenge

The main challenge in writing a unit test(s) for the procedure which involves a utility procedure is to make sure that we should not worry about the functioning of utility procedure when writing a unit test for the main procedure and same is true for utility procedure. This is a challenging task which must be kept in mind while writing unit tests for such a scenario.
Isolating from the utility or main procedure is a must, depending on which procedure is under test. We have to keep the following things in mind in the context of isolating while unit testing:

  1. Isolating from utility procedure when unit testing the main procedure.
  2. Isolating from the main procedure when unit testing utility procedure.

Please remember this article is focused on unit testing the main procedure by isolating it from its utility procedure.

Creating Main Procedure and its Utility Procedure

In order to write a unit test for a scenario where utility procedure is in use by the main procedure we first need to have the following pre-requisites:

  1. Sample Database
  2. Business Requirement(s)

Setup Sample Database (SQLBookShop)

We are creating a simple two-table sample database called “SQLBookShop” which contains the records of all the books ordered as shown below:

Create SQLBookShop sample database as follows:

Create and populate database objects (tables) as follows:

Quick Check – Sample Database

Do a quick database check by running the OrderedBooks view using the following code:

Please note that I am using dbForge Studio for SQL Server so the output look may differ if you run the same code in SSMS (SQL Server Management Studio). However, there is no difference between scripts and their results.

Business Requirement to see the latest order with additional information

A business requirement has been sent to the dev team which states that “The end user wants to know about the most recent order placed for a particular book along with the information whether the order was placed on a weekday or weekend”

A Word about TDDD

We are not strictly following test-driven database development (TDDD) in this article but I strongly recommend to use test-driven database development (TDDD) to create both main and utility procedures which begins by creating a unit test to check if object exists which fails at first, followed by creating the object and rerunning the unit test which must pass.
For a detailed walk-through, please, refer to the first part of this article.

Identifying Utility Procedure

Seeing the business requirement one thing is for sure we need a utility procedure which can tell us whether a particular date is a weekday or a weekend.

Creating Utility Procedure (GetDayType)

Create a utility procedure and call it “GetDayType” as follows:

Quick Check – Utility Procedure

Write the following lines of code to quickly check utility procedure:

Creating Main Procedure (GetLatestOrderByBookId)

Create the main procedure to see the most recent order placed for a particular book and also whether the order was placed on a weekday or weekend and call it “GetLatestOrderByBookId” which contains the call for the utility procedure as follows:

Quick Check – Main Procedure

Run the following code to see if the procedure is working fine or not:

Unit Testing Main Procedure Calling Utility Procedure

The key here is to understand the difference between unit testing the main procedure and the utility procedure.

We are currently focused on unit testing the main procedure so this means utility procedure needs to be gracefully isolated from this unit test.

Use of Spy Procedure

In order to make sure that main procedure unit test remains focussed on testing the functionality of main procedure, we have to use spy procedure provided by tSQLt which is going to act as a stub (placeholder) for utility procedure.

According to tsqlt.org, please remember if you are spying a procedure you are not actually unit testing that procedure rather you are making it easier for the other procedure related to the procedure you are spying to be unit tested.

For example, in our case, if we want to unit test main procedure then we have to mock the utility procedure by the use of spy procedure which is going to make easier for us to unit test the main procedure.

Creating Unit Test for Main Procedure Spying Utility Procedure

Create a database unit test to check the main procedure functions properly.

This article works for dbForge Studio for SQL Server (or only dbForge Unit Test) and SSMS (SQL Server Management Studio). However, please note that when using SSMS (SQL Server Management Studio) I assume you have already installed tSQLt Framework and ready to write the unit tests.

To create the first database unit test, right-click the SQLBookShop database. On the shortcut menu, click Unit Test and then Add New Test as follows:

Write the unit test code:

Running Unit Test for Main Procedure

Run the unit test:

Congratulations, you have successfully unit tested a stored procedure by isolating it from its utility procedure after using spy procedure.

For more information about unit testing please go through the following parts of my previous article about test-driven database development (TDDD):

Things to Do

You can now create database unit tests for slightly complex scenarios where stored procedures call utility procedures.

  1. Please try changing spy procedure @CommandToExecute argument (value) as @CommandToExecute = ‘set @DayType = ”Nothing” ‘ and see the test is going to fail now
  2. Please try meeting the business requirement in this article by using test-driven database development (TDDD)
  3. Please try to meet another business requirement to see the most recent order placed by any customer using test-driven development (TDDD) involving the same utility procedure
  4. Please try creating a unit test for utility procedure by isolating the main procedure
  5. Please give yourself a try to create a unit test for a procedure which calls two utility procedures

 

Useful products:

dbForge Unit Test – an intuitive and convenient GUI for implementing automated unit testing in SQL Server Management Studio.

Haroon Ashraf

Haroon Ashraf

Haroon's deep interest in logic and reasoning at an early age of his academic career paved his path to become a data professional.

He began his professional life as a computer programmer more than 10 years ago working on his first data venture to migrate and rewrite a public sector database driven examination system from IBM AS400 (DB2) to SQL Server 2000 using VB 6.0 and Classic ASP along with developing reports and archiving many years of data.

His work and interest revolves around Database-Centric Architectures and his expertise include database and reports design, development, testing, implementation and migration along with Database Life Cycle Management (DLM).
Haroon Ashraf