Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / [email protected].

31
Database Database Development Using Development Using TDD TDD Chris Oldwood Chris Oldwood ACCU Conference 2012 ACCU Conference 2012 @chrisoldwood / @chrisoldwood / [email protected] [email protected]

Transcript of Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / [email protected].

Database Database Development Using Development Using

TDDTDD

Chris OldwoodChris Oldwood

ACCU Conference 2012ACCU Conference 2012

@chrisoldwood / @chrisoldwood / [email protected]@cix.co.uk

SELECT * FROM ScopeSELECT * FROM Scope ProloguePrologue Principles of TDDPrinciples of TDD The Public InterfaceThe Public Interface SQL Unit TestingSQL Unit Testing TDD by ExampleTDD by Example Continuous Integration & Continuous Integration &

DeploymentDeployment Database RefactoringDatabase Refactoring QuestionsQuestions

@chrisoldwood is the only @chrisoldwood is the only person I know with a person I know with a convincing Agile SQL story.convincing Agile SQL story.

@allankelly@allankelly

EnvironmentsEnvironments

SQL based RDBMSSQL based RDBMS Applicable to OLTP & OLAPApplicable to OLTP & OLAP Distributed systemsDistributed systems

Principles of TDDPrinciples of TDD

Test DrivenTest Driven

(Development|Design)(Development|Design)

The TDD CycleThe TDD Cycle

Write a failing test(red)

Write production code(green)

Clean-up code(refactor)

Small steps

Done (done)

Test-First vs Test-LaterTest-First vs Test-Later

Test-first promotes a client-Test-first promotes a client-side perspectiveside perspective

Top-Down Design & Top-Down Design & ImplementationImplementation

Client

Services

Database

Design

Implementation

Design & Implementation

Executable SpecificationExecutable Specification

Helps ensure correctness firstHelps ensure correctness first Aids continued correctness afterAids continued correctness after Acts as documentationActs as documentation

The Public InterfaceThe Public Interface

Encapsulation buys you Encapsulation buys you freedomfreedom

Public ObjectsPublic Objects

Stored proceduresStored procedures User-defined functionsUser-defined functions ViewsViews User-defined typesUser-defined types

Implementation DetailsImplementation Details

TablesTables Constraints Constraints

(triggers)(triggers) IndexesIndexes

Observable BehaviourObservable Behaviour

Tests should verify the Tests should verify the publicly observable behaviour publicly observable behaviour

not the choice of not the choice of implementationimplementation

Code StructureCode Structure

Use schemas for partitioningUse schemas for partitioning Embrace compositionEmbrace composition Single Responsibly PrincipleSingle Responsibly Principle

PerformancePerformance

Encapsulation buys you Encapsulation buys you freedomfreedom

SQL Unit TestingSQL Unit Testing

SQL UnitsSQL Units

ProcedureProcedure FunctionFunction ViewView Legacy Legacy

(constraints/triggers)(constraints/triggers)

Development SandboxDevelopment Sandbox

IsolationIsolation Fast feedbackFast feedback DeterministicDeterministic ToolingTooling

SS-Unit Example TestSS-Unit Example Test

create procedure test._@TestSetUp@_Somethingas -- common arrangementgo

create procedure test._@Test@_Something_ShouldDoAnotherThingas 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 ExampleTDD By Example

Example FeatureExample Feature

Produce a report showing how Produce a report showing how many bugs each developer many bugs each developer has fixed.has fixed.

Continuous Continuous Integration & Integration & DeploymentDeployment

Continuous (SQL) Continuous (SQL) IntegrationIntegration

Build Database

Run Test Suite

Run Static Analysis

Continuous (System) Continuous (System) IntegrationIntegration

Build Database

Run Unit Tests

Build Client

Run Unit Tests

Build Services

Run Unit Tests

Run Integration Tests Run Integration Tests

Continuous DeploymentContinuous Deployment

PackageDatabase

Run End-to-EndTests

Deploy Database

PackageServices

PackageClient

Deploy Services Deploy Client

Developer’s Workstation

Development CycleDevelopment Cycle

Feature Write test Write code Refactor

Build Server

Build DB Unit Tests Int. Tests

System Test Environments

Package Deploy Sys TestsDoneDone

Database RefactoringDatabase Refactoring

RefactoringRefactoring

Encapsulation buys you Encapsulation buys you freedomfreedom

Schema ChangesSchema Changes

Object namesObject names Rationalising data typesRationalising data types Remove dead objectsRemove dead objects Table splits/mergesTable splits/merges

Questions?Questions?

Want to Know More?Want to Know More?

BlogBlog

http://chrisoldwood.blogspot.comhttp://chrisoldwood.blogspot.com

SS-Unit / SS-Cop / sql2doxygenSS-Unit / SS-Cop / sql2doxygen

http://www.cix.co.uk/~gort/sql.htmhttp://www.cix.co.uk/~gort/sql.htm

@chrisoldwood / @chrisoldwood / [email protected]@cix.co.uk