Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prove to your boss your database is sound - Unit Testing with tSQLt

Similar presentations


Presentation on theme: "Prove to your boss your database is sound - Unit Testing with tSQLt"— Presentation transcript:

1 Prove to your boss your database is sound - Unit Testing with tSQLt
For Your Database Dale Hirt

2 Who Am I? Service Engineer and DBA for Microsoft
10 Years in Developer Division Internal Engineering Systems services Database Development Production Administration Systems Architecture @dalehirt

3 Agenda Assumptions tSQLt Initial Setup AAA For Your Database Assemble
Act Assert This is our agenda for the day I’m going to discuss tSQLt, which is a unit-testing framework, and then we will talk about how to apply it using the AAA formula: Assemble, Act, and Assert

4 Assumptions Visual Studio 2013 or Newer
SQL Server Data Tools are installed DacPac Source Control TFS Git Even (shudder) CVS

5 What is a DacPac? A file consisting of multiple XML sections representing details of the DACPAC origin, the objects in the database, and other characteristics SQLPackage.exe

6 Is This Your Boss? Is this your boss every morning? Screaming that the database caused issues yet again with all the LOB applications overnight? That the database code was of such low quality that it is causing data integrity issues? That your stored procedures should be served with meat sauce and garlic bread because no one understands how they work, or even if they work?

7 What if you could show him this?

8 tSQLt is a database unit testing framework for Microsoft SQL Server
tSQLt is a database unit testing framework for Microsoft SQL Server. tSQLt is compatible with SQL Server 2005 (service pack 2 required) and above on all editions.

9 What Is Unit Testing and Why Do We Do It?
unit testing refers to the practice of testing certain functions and areas – or units – of our code, typically the smallest unit possible (function, stored procedures)  identify failures in our algorithms and/or logic a higher number of smaller, more focused functions that provide a single operation on a set of data prevent future changes from breaking functionality

10 What is tSQLt? Unit-Testing Framework Written Entirely in SQL
Transactions Grouped Together Fake Tables Fake Views Create Stored Procedure spies Each test is independent Reduces cleanup work You create a test schema Keeps your code organized Common setup methods

11 DEMO Set up Visual Studio & T-SQL
Show AssetExplorer Database Testing Solutions. VS: setting up solution add database reference Add tsqlt dacpac pre-setup post-setup. T-SQL: Schema steup Setup function DEMO

12

13 Default Stored Procedure

14 Assemble

15 Assemble - FakeTable (works on views too)
We want to keep our test cases focused and do not want to insert data which is irrelevant to the current test. However, table and column constraints can make this difficult. FakeTable allows tests to be written in isolation of the constraints on a table. FakeTable creates an empty version of the table without the constraints in place of the specified table. Therefore any statements which access the table during the execution of the test case are actually working against the fake table with no constraints. When the test case completes, the original table is put back in place because of the rollback which tSQLt performs at the end of each test case. FakeTable can be called on Tables, Views and Synonyms (A Synonym has to point to a table or view in the current database.) FakeTable cannot be used with temporary tables (tables whose names begin with # or ##). ] ‘preserve identity’ – Indicates if the identity properties of an identity column should be preserved. = 1, the identity properties will be preserved, otherwise the faked table will have the identity properties removed. The default = 0. ] ‘preserve computed columns’ – Indicates if the computations on computed columns should be preserved. = 1, the computations will be preserved, otherwise the faked table will have computations removed from computed columns. The default = 0. ] ‘preserve default constraints’ – Indicates if the default constraints on columns should be preserved. = 1, default constraints will be preserved for all columns on the faked table, otherwise the faked table will not contain default constraints. The default = 0. What does it do underneath? It renames the table, and recreates it with no constraints, unless you specify it. After the test is run, it removes the table and renames the original back.

16 Assemble - FakeFunction
Literally replace with a mock

17 Assemble - RemoveObject
Allows for stubs

18 Assemble - ApplyConstraint
ApplyConstraint in combination with FakeTable allows constraints to be tested in isolation of other constraints on a table. CHECK constraints FOREIGN KEY constraints UNIQUE constraints PRIMARY KEY constraints BUT NOT DEFAULT constraints Cascade properties of FOREIGN KEY constraints are not preserved. SQL Server automatically creates unique indexes for UNIQUE and PRIMARY KEY constraints. Those indexes for “applied” constraints do not preserve asc/desc properties of the original supporting indexes

19 Assemble - Setup

20 Act TestMSAssetArchiveOldRecordsMoveNone

21 ASSERT

22 Assert - Expectations Need to be added in before the Act stage

23 Assert - Fail Allows you to purposely fail.

24 Assert – AssertEquals (AssertNotEquals)

25 Assert - AssertEmptyTable

26 Assert – AssertEqualsTable

27 Time to Run

28 Show off the results!! Look, even the code that is tricky succeeded.

29 Caveats Cannot test deadlocks with this system No Concurrency testing
No Cross-database or –server testing

30 Publish and run unit tests
Demo

31 Is your boss happy now?

32 Questions? tSQLt http://tsqlt.org/ Github Code
Event:  Sessions:  @dalehirt


Download ppt "Prove to your boss your database is sound - Unit Testing with tSQLt"

Similar presentations


Ads by Google