310 likes | 494 Views
Database Development Using TDD. Chris Oldwood ACCU Conference 2012. @chrisoldwood / gort@cix.co.uk. SELECT * FROM Scope. Prologue Principles of TDD The Public Interface SQL Unit Testing TDD by Example Continuous Integration & Deployment Database Refactoring Questions.
E N D
Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk
SELECT * FROM Scope • Prologue • Principles of TDD • The Public Interface • SQL Unit Testing • TDD by Example • Continuous Integration & Deployment • Database Refactoring • Questions
@chrisoldwood is the only person I know with a convincing Agile SQL story. @allankelly
Environments • SQL based RDBMS • Applicable to OLTP & OLAP • Distributed systems
Principles of TDD Test Driven (Development|Design)
The TDD Cycle Write a failing test (red) Write production code (green) Small steps Clean-up code (refactor) Done (done)
Test-First vs Test-Later Test-first promotes a client-side perspective
Top-Down Design & Implementation Design & Implementation Design Client Services Database Implementation
Executable Specification • Helps ensure correctness first • Aids continued correctness after • Acts as documentation
The Public Interface Encapsulation buys you freedom
Public Objects • Stored procedures • User-defined functions • Views • User-defined types
Implementation Details • Tables • Constraints (triggers) • Indexes
Observable Behaviour Tests should verify the publicly observable behaviour not the choice of implementation
Code Structure • Use schemas for partitioning • Embrace composition • Single Responsibly Principle
Performance Encapsulation buys you freedom
SQL Units • Procedure • Function • View • Legacy (constraints/triggers)
Development Sandbox • Isolation • Fast feedback • Deterministic • Tooling
SS-Unit Example Test create procedure test._@TestSetUp@_Something as -- common arrangement go create procedure test._@Test@_Something_ShouldDoAnotherThing as declare @arrangement varchar(100) = 'arrangement'; declare @expected int = 42; declare @actual int = public.ActOnArrangement(); exec ssunit.AssertIntegerEqualTo @expected, @actual; go exec ssunit.RunTests;
Example Feature Produce a report showing how many bugs each developer has fixed.
Continuous (SQL) Integration Build Database Run Test Suite Run Static Analysis
Continuous (System) Integration Build Database Build Services Build Client Run Unit Tests Run Unit Tests Run Unit Tests Run Integration Tests Run Integration Tests
Continuous Deployment Package Database Package Services Package Client Deploy Database Deploy Services Deploy Client Run End-to-End Tests
Development Cycle Developer’s Workstation Feature Write test Write code Refactor Build Server Build DB Unit Tests Int. Tests System Test Environments Package Deploy Sys Tests Done Done
Refactoring Encapsulation buys you freedom
Schema Changes • Object names • Rationalising data types • Remove dead objects • Table splits/merges
Want to Know More? Blog http://chrisoldwood.blogspot.com SS-Unit / SS-Cop / sql2doxygen http://www.cix.co.uk/~gort/sql.htm @chrisoldwood / gort@cix.co.uk