Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adding unit tests with tSQLt to the database de...

Adding unit tests with tSQLt to the database deployment pipeline

Talk presented @ PortoData (30-01-2018)

Eduardo Piairo

January 30, 2018
Tweet

More Decks by Eduardo Piairo

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. REFERENCES Adding unit tests with tSQLt to the database pipeline

    • http://tsqlt.org/user-guide/tSQLr.AssertEqualsTable • https://courses.agilesql.club/ @EdPiairo, #PortoData