30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Testing tables, views, and UDFs<br />

157<br />

Chapter 5: Testing Databases<br />

Tables, views, and UDFs are all tested in the same manner, namely by directly executing<br />

T-<strong>SQL</strong> queries against them. In each case, we are testing the validity of the returned data<br />

so, in that sense, this is pure black-box testing.<br />

We're basically testing that any changes made to these database objects, such as adding or<br />

removing joins to views, or changing some business logic in a UDF, haven't changed the<br />

expected result.<br />

These tests should be run under the same isolation level as that which the application will<br />

use, and we need to perform both read and write tests against these objects, since they<br />

can be used to modify data, as well as read it. Scalar UDFs fall into the read-test category,<br />

since we can't insert data through a scalar UDF.<br />

Included in the write-testing of tables, is the testing of triggers. A trigger cannot be fired<br />

directly; it is executed as part of a table modification procedure. Therefore, our write tests<br />

on the tables must consider the effects of any triggers that may be fired, and may modify<br />

other tables as a result. Each of these "secondary" tables is added to the unit that we must<br />

test. Overuse of triggers can greatly widen our testing scope and make the tests huge,<br />

bulky, and impractical.<br />

Listing 5-6 shows three test methods; a table read test, along with write tests for a table<br />

and a view, as shown here.<br />

• Table_single_line_read_from_Person_Address method – returns the first<br />

row from the Person.Address table, ordered by AddressId column, and checks its<br />

value against our known values.<br />

• Table_single_line_insert_into_Person_Address method – inserts a single<br />

new row into the Person.Address table and tests if the inserted values match our<br />

known values. For example, if we had a trigger on the Person.Address table that

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!