blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Claypole's World - The SQL Server Side

Concerning development of bespoke applications and database administration.

NOT FOR REPLICATION Database Unit Tests

I recently put up a post with a unit test for check constraints validating all were defined with the NOT FOR REPLICATION option.  I thought it might be helpful to put up a post with tests for all the other object types that can have this option set.

select object_id,name,is_not_for_replication
from sys.foreign_keys where is_not_for_replication <> 1

select object_id,name,is_not_for_replication
from sys.triggers where is_not_for_replication <> 1

select object_id,name,is_not_for_replication 
from sys.check_constraints  where is_not_for_replication <> 1

select object_id,name,is_not_for_replication
from sys.identity_columns where is_not_for_replication <> 1

I guess you could put a UNION ALL in between all these tests and create one test but I would advise against this as it's harder to verify with any degree of accuracy. Strictly speaking I would say that it also wouldn't be a unit test as it is testing multiple object types.

To verify the results of these tests you could again use the Code assertions in Team Edition for database professionals (DataDude) or use SQL assertions.

If you decide to use sql assertions it might be a good idea to come up with a nice pattern for implementing them.

Here's a basic suggestion:

declare  @DbName sysname
           ,@ObjectName sysname
           ,@TestType nvarchar(100)
           ,@TestResult nvarchar(100)
           ,@Message nvarchar(MAX)

select   object_id
           ,name
           ,is_not_for_replication
from  sys.foreign_keys
where  is_not_for_replication <> 1

If @@error <> 0
or @@rowcount <> 0
BEGIN
 select   @DbName  = db_name()
           ,@ObjectName  = N'All Foreign Keys'
           ,@TestType  = N'Schema Test'
           ,@TestResult  = N'FAILED'
           ,@Message  = N'NOT FOR REPLICATION Foreign Keys Test'

 raiserror(N'%s : %s : %s : %s : %s',16,1,@DbName,@ObjectName,@TestType,@TestResult,@Message)
END

It would be nice if the raiserror severity was respected and you could set it to say 10 or 15 for info or warning messages without it failing the test.  However, this doesn't seem to be the case.

Cheers, James


Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About James.Rowland-Jones

James is a Senior Technical Consultant for Conchango. He works primarily with SQL Server technologies in architecture, development and administration capacities. He also co-ordinates Conchango's SQL Server Community.

View James Rowland-Jones's profile on LinkedIn

Powered by Community Server (Personal Edition), by Telligent Systems