Slide 1

Slide 1 text

ADDING UNIT TESTS WITH TSQLT TO THE DATABASE DEPLOYMENT PIPELINE Eduardo Piairo @EdPiairo #PortoData

Slide 2

Slide 2 text

ABOUT ME Adding unit tests with tSQLt to the database pipeline @EdPiairo, #PortoData @EdPiairo https://pt.linkedin.com/in/jesuspiairo [email protected] http://www.eduardopiairo.com/ Eduardo Piairo Operations Engineer DevOps Porto Founder

Slide 3

Slide 3 text

TSQLT Adding unit tests with tSQLt to the database pipeline • Database unit testing framework for Microsoft SQL Server • Allow to write T-SQL code as tests • Tests are automatically run within transactions • Provides a way to isolate code and tables using mocking • Output can be plain text or XML @EdPiairo, #PortoData

Slide 4

Slide 4 text

TSQLT INSTALL Adding unit tests with tSQLt to the database pipeline • tSQLt.class.sql • CLR • clr enabled • clr strict security • Should be installed in the development database @EdPiairo, #PortoData

Slide 5

Slide 5 text

WHAT’S A TSQLT TEST? Adding unit tests with tSQLt to the database pipeline • Stored Procedure • Starts with the word test • Must be in a schema that contains the extended property tSQLt.TestClass = 1 • tSQLt.NewTestClass • Each test is wrapped in a transaction: modifications are rolled back and the results saved • Benefits • Requirements documentation • Ability to refactor • Isolation - unrelated changes do not affect other parts of the system • Help structure code into distinct components @EdPiairo, #PortoData

Slide 6

Slide 6 text

WHAT’S CAN BE TESTED? Adding unit tests with tSQLt to the database pipeline • Stored Procedures • Functions • Views • Tables • Tables constrains that are critical @EdPiairo, #PortoData

Slide 7

Slide 7 text

TESTING WHITOUT DATA Adding unit tests with tSQLt to the database pipeline • Unit tests is about testing code • You do not need a database full of data, you need the opposite • Data setup • Only the necessary data for making the test work is needed • Mocking • tSQLt.FakeTable • tSQLt.FakeFunction • tSQLt.SpyProcedure @EdPiairo, #PortoData

Slide 8

Slide 8 text

ASSERTS Adding unit tests with tSQLt to the database pipeline • tSQLt.AsserEquals • tSQLr.AssertEqualsTable • tSQLt.AssertEmptyTable • tSQLt.AssertEqualsString • tSQLt.AssertEqualsTableS chema • tSQLt.AssertLike • tSQLt.AssertNotEquals • AssertObjectDoesNotExist • AssertObjectExists • AssertResultSetsHaveSam eMetaData • Fail @EdPiairo, #PortoData

Slide 9

Slide 9 text

DEMO Adding unit tests with tSQLt to the database pipeline @EdPiairo, #PortoData

Slide 10

Slide 10 text

REFERENCES Adding unit tests with tSQLt to the database pipeline • http://tsqlt.org/user-guide/tSQLr.AssertEqualsTable • https://courses.agilesql.club/ @EdPiairo, #PortoData

Slide 11

Slide 11 text

Q&A Operations for databases @EdPiairo https://pt.linkedin.com/in/jesuspiairo [email protected] http://www.eduardopiairo.com/ @EdPiairo, #PortoData