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

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

Dec 23, 2015

Download

Documents

Samantha Willis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Database Database Development Using Development Using

TDDTDD

Chris OldwoodChris Oldwood

ACCU Conference 2012ACCU Conference 2012

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

Page 2: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@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

Page 3: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

@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

Page 4: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

EnvironmentsEnvironments

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

Page 5: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Principles of TDDPrinciples of TDD

Test DrivenTest Driven

(Development|Design)(Development|Design)

Page 6: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

The TDD CycleThe TDD Cycle

Write a failing test(red)

Write production code(green)

Clean-up code(refactor)

Small steps

Done (done)

Page 7: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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

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

Page 8: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Top-Down Design & Top-Down Design & ImplementationImplementation

Client

Services

Database

Design

Implementation

Design & Implementation

Page 9: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Executable SpecificationExecutable Specification

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

Page 10: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

The Public InterfaceThe Public Interface

Encapsulation buys you Encapsulation buys you freedomfreedom

Page 11: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Public ObjectsPublic Objects

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

Page 12: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Implementation DetailsImplementation Details

TablesTables Constraints Constraints

(triggers)(triggers) IndexesIndexes

Page 13: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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

Page 14: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Code StructureCode Structure

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

Page 15: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

PerformancePerformance

Encapsulation buys you Encapsulation buys you freedomfreedom

Page 16: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

SQL Unit TestingSQL Unit Testing

Page 17: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

SQL UnitsSQL Units

ProcedureProcedure FunctionFunction ViewView Legacy Legacy

(constraints/triggers)(constraints/triggers)

Page 18: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Development SandboxDevelopment Sandbox

IsolationIsolation Fast feedbackFast feedback DeterministicDeterministic ToolingTooling

Page 19: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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;

Page 20: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

TDD By ExampleTDD By Example

Page 21: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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.

Page 22: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Continuous Continuous Integration & Integration & DeploymentDeployment

Page 23: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Continuous (SQL) Continuous (SQL) IntegrationIntegration

Build Database

Run Test Suite

Run Static Analysis

Page 24: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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

Page 25: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Continuous DeploymentContinuous Deployment

PackageDatabase

Run End-to-EndTests

Deploy Database

PackageServices

PackageClient

Deploy Services Deploy Client

Page 26: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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

Page 27: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Database RefactoringDatabase Refactoring

Page 28: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

RefactoringRefactoring

Encapsulation buys you Encapsulation buys you freedomfreedom

Page 29: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Schema ChangesSchema Changes

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

Page 30: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

Questions?Questions?

Page 31: Database Development Using TDD Chris Oldwood ACCU Conference 2012 @chrisoldwood / gort@cix.co.uk.

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