Press "Enter" to skip to content

Misusing tSQLt: test that table/view metadata is correct

The following tSQLt test checks that the basic schema of a table (or view) to ensure that it has not been changed unexpectedly as part of the data warehouse update:

CREATE PROCEDURE testSchema.[test that TableName metadata is correct]
AS
BEGIN
    IF OBJECT_ID('expected') IS NOT NULL DROP TABLE expected;

    -- Create a table with the expected schema to test against the actual data warehouse table
    CREATE TABLE expected (
        Id INT NOT NULL
        ,DateCreated DATETIME2(2) NOT NULL
        ,DateLastUpdated DATETIME2(2) NOT NULL
        ,Text1 VARCHAR(64) NOT NULL
        ,Text2 CHAR(4) NULL
        ,IsActive BIT NOT NULL
    );

    -- Update DatabaseName, SchemaName & TableName to the appropriate names for the object that is to be tested
    EXEC tSQLt.AssertResultSetsHaveSameMetaData
        @expectedCommand = 'SELECT Id, DateCreated, DateLastUpdated, Text1, Text2, IsActive FROM expected',
        @actualCommand = 'SELECT TOP 1 Id, DateCreated, DateLastUpdated, Text1, Text2, IsActive FROM DatabaseName.SchemaName.TableName';

END

See Misusing tSQLt for more info.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *