Database Development
Using TDD
Chris Oldwood
ACCU Conference 2012
@chrisoldwood / [email protected]
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)
Clean-up code
(refactor)
Done (done)
Small steps
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 Unit Testing
SQL Units
Procedure
 Function
 View
 Legacy (constraints/triggers)

Development Sandbox
Isolation
 Fast feedback
 Deterministic
 Tooling

SS-Unit Example Test
create procedure [email protected]@_Something
as
-- common arrangement
go
create procedure [email protected]@_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;
TDD By Example
Example Feature
Produce a report showing how
many bugs each developer has
fixed.
Continuous Integration
& Deployment
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
Unit Tests
Int. Tests
Deploy
Sys Tests
Build Server
Build DB
System Test Environments
Package
Done
Done
Database Refactoring
Refactoring
Encapsulation buys you freedom
Schema Changes
Object names
 Rationalising data types
 Remove dead objects
 Table splits/merges

Questions?
Want to Know More?
Blog
http://chrisoldwood.blogspot.com
SS-Unit / SS-Cop / sql2doxygen
http://www.cix.co.uk/~gort/sql.htm
@chrisoldwood / [email protected]
Descargar

Database Development Using TDD