Press "Enter" to skip to content

Misusing tSQLt: test that table data is correct (dims or reference data)

The following tSQLt test checks the unchanging (or very slowly-changing) data in a dimension or reference table to ensure that it has not been changed unexpectedly as part of the data warehouse update:

CREATE PROCEDURE testSchema.[test that TableName data is correct]
AS
BEGIN

    IF OBJECT_ID('actual') IS NOT NULL DROP TABLE actual;
    IF OBJECT_ID('expected') IS NOT NULL DROP TABLE expected;

    -- Update DatabaseName, SchemaName & TableName to the appropriate names for the object that is to be tested
    SELECT Id, Text1, Text2, IsActive INTO actual
    FROM DatabaseName.SchemaName.TableName;

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

    INSERT INTO expected (Id, Text1, Text2, IsActive)
    VALUES (0, 'Unknown', NULL, 1)
        ,(1, 'The first text', '0001', 1) 
        ,(2, 'Some other text', '1234', 1);

    EXEC tSQLt.AssertEqualsTable 
        @Expected = 'expected',
        @Actual = 'actual';

END

See Misusing tSQLt for more info.

One Comment

Leave a Reply

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