testing database applications with quickcheck

52
Testing database applications with QuickCheck — Tutorial — Laura M. Castro Universidade da Coruña (Spain) Stockholm, 15th November 2010 Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 1 / 23

Upload: laura-m-castro

Post on 12-May-2015

607 views

Category:

Technology


3 download

DESCRIPTION

Tutorials day.Erlang User Conference, 2010.

TRANSCRIPT

Page 1: Testing database applications with QuickCheck

Testing database applications with QuickCheck— Tutorial —

Laura M. Castro

Universidade da Coruña (Spain)

Stockholm, 15th November 2010

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 1 / 23

Page 2: Testing database applications with QuickCheck

Outline

1 What is a database application?

2 Why do DB applications require special testing?

3 The theory: how to test a database application with QuickCheck

4 The practise: testing a simple e-shop

5 Summing up

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 2 / 23

Page 3: Testing database applications with QuickCheck

What is a database application?

A database or data-intensive application is a software system which:

makes intensive use of great amounts of data,

relies on external storage sources for persistence (e.g., a database).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 3 / 23

Page 4: Testing database applications with QuickCheck

What is a database application?

A database or data-intensive application is a software system which:

makes intensive use of great amounts of data,

relies on external storage sources for persistence (e.g., a database).

onlineshop

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 3 / 23

Page 5: Testing database applications with QuickCheck

What is a database application?

A database or data-intensive application is a software system which:

makes intensive use of great amounts of data,

relies on external storage sources for persistence (e.g., a database).

onlineshop

taskflow

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 3 / 23

Page 6: Testing database applications with QuickCheck

What is a database application?

A database or data-intensive application is a software system which:

makes intensive use of great amounts of data,

relies on external storage sources for persistence (e.g., a database).

onlineshop

taskflow

bankapp

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 3 / 23

Page 7: Testing database applications with QuickCheck

Why do DB applications require special testing?

Database or data-intensive applications are software systems which:

impose complex constraints on the data they handle,

their correct operation depends on their enforcement.

These constraints are usually referred to as business rules.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 4 / 23

Page 8: Testing database applications with QuickCheck

Why do DB applications require special testing?

Database or data-intensive applications are software systems which:

impose complex constraints on the data they handle,

their correct operation depends on their enforcement.

These constraints are usually referred to as business rules.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 4 / 23

Page 9: Testing database applications with QuickCheck

Why do DB applications require special testing?

Database or data-intensive applications are software systems which:

impose complex constraints on the data they handle,

their correct operation depends on their enforcement.

These constraints are usually referred to as business rules.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 4 / 23

Page 10: Testing database applications with QuickCheck

Business RulesWhat are they?

“Statements that define or constrain some aspect of a business (. . . )intended to assert business structure or to control or influencebehavior”(B.R. Group, Defining Business Rules – What are they really?)

“Definitions of how the business should be carried out andconstraints on the business” (I. Sommerville, Software Engineering)

“Software is the realization of business rules”(R.S. Pressman, Software Engineering – A practitioner’s approach)

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 5 / 23

Page 11: Testing database applications with QuickCheck

Business RulesWhere are they?

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 6 / 23

Page 12: Testing database applications with QuickCheck

Business RulesWhere are they?

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

only basicconstraints

(E/R)

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 6 / 23

Page 13: Testing database applications with QuickCheck

Business RulesWhere are they?

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

only basicconstraints

(E/R)

noconstraints

at all

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 6 / 23

Page 14: Testing database applications with QuickCheck

Business RulesWhere are they?

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 6 / 23

Page 15: Testing database applications with QuickCheck

Business RulesWhen do we test them?

Since business rules are not located in a specific unit or component,

they are not covered by unit testing.

Since business rules dictate data-related constraints,

they are not the scope of integration testing.

Since business rules need to be respected at all times,

they are not considered when testing the GUI.

Business rules must be tested as part of system testing.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 7 / 23

Page 16: Testing database applications with QuickCheck

Business RulesWhen do we test them?

Since business rules are not located in a specific unit or component,

they are not covered by unit testing.

Since business rules dictate data-related constraints,

they are not the scope of integration testing.

Since business rules need to be respected at all times,

they are not considered when testing the GUI.

Business rules must be tested as part of system testing.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 7 / 23

Page 17: Testing database applications with QuickCheck

Business RulesWhen do we test them?

Since business rules are not located in a specific unit or component,

they are not covered by unit testing.

Since business rules dictate data-related constraints,

they are not the scope of integration testing.

Since business rules need to be respected at all times,

they are not considered when testing the GUI.

Business rules must be tested as part of system testing.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 7 / 23

Page 18: Testing database applications with QuickCheck

Business RulesWhen do we test them?

Since business rules are not located in a specific unit or component,

they are not covered by unit testing.

Since business rules dictate data-related constraints,

they are not the scope of integration testing.

Since business rules need to be respected at all times,

they are not considered when testing the GUI.

Business rules must be tested as part of system testing.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 7 / 23

Page 19: Testing database applications with QuickCheck

Why do DB applications require special testing?Because of Business Rules

Therefore, database or data-intensive applications:

include business rules that put constraints on the data they handle,

business rules must be enforced by the system at all times,

location of the business rules is unclear.

In this tutorial, we will present a methodology to

test business rules at system testing level.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 8 / 23

Page 20: Testing database applications with QuickCheck

Why do DB applications require special testing?Because of Business Rules

Therefore, database or data-intensive applications:

include business rules that put constraints on the data they handle,

business rules must be enforced by the system at all times,

location of the business rules is unclear.

In this tutorial, we will present a methodology to

test business rules at system testing level.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 8 / 23

Page 21: Testing database applications with QuickCheck

The theoryHow to test business rules with QuickCheck

To test that a data-intensive application complies with the data constraints

imposed by its business rules at all times, we use QuickCheck:

an automatic testing tool,

generates and runs random sequences of test cases,

when an error is found, test sequence is shrunk to return a minimal

test case.

In the rest of the tutorial we assume familiarity with Quviq QuickCheck testingtool. We will present the basics of how QuickCheck state machine libraryworks, but explaining these concepts is not the purpose of this specific tutorial.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 9 / 23

Page 22: Testing database applications with QuickCheck

The theoryHow to test business rules with QuickCheck

To test that a data-intensive application complies with the data constraints

imposed by its business rules at all times, we use QuickCheck:

an automatic testing tool,

generates and runs random sequences of test cases,

when an error is found, test sequence is shrunk to return a minimal

test case.

In the rest of the tutorial we assume familiarity with Quviq QuickCheck testingtool. We will present the basics of how QuickCheck state machine libraryworks, but explaining these concepts is not the purpose of this specific tutorial.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 9 / 23

Page 23: Testing database applications with QuickCheck

The theoryHow to test business rules with QuickCheck

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 10 / 23

Page 24: Testing database applications with QuickCheck

The theoryHow to test business rules with QuickCheck

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

BRenforcedhere

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 10 / 23

Page 25: Testing database applications with QuickCheck

The theoryHow to test business rules with QuickCheck

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

APPLICATION INTERFACE

or heredata will be"corrupted"

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 10 / 23

Page 26: Testing database applications with QuickCheck

The theoryHow to test business rules with QuickCheck

STORAGE ACCESS

APPLICATION BUSINESS LOGIC

QUICKCHECK

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 10 / 23

Page 27: Testing database applications with QuickCheck

The theoryQuickCheck state machine library

In particular, we use QuickCheck state machine library:

mechanism to easily implement a testing state machine

(library callbacks),

the testing state machine generates and runs test sequences,

tests are sequences of calls to the functionalities under test.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 11 / 23

Page 28: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 29: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 30: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 31: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 32: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 33: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 34: Testing database applications with QuickCheck

The theoryHow are test sequences generated?

test sequencelength reached?

SUCCESS

YES

NO

selectoperation

preconditions hold?FALSE

TRUE

TRUE FALSE

ERROR

postconditions hold?

executeoperation

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 12 / 23

Page 35: Testing database applications with QuickCheck

The theoryQuickCheck statem machine skeleton

-module(test_eqc).

-include_lib("eqc/include/eqc.hrl").-include_lib("eqc/include/eqc_statem.hrl").

-compile(export_all).

-record(state,{useful_info}).

%% Initialize the stateinitial_state() ->

#state{useful_info = []}.

%% Command generator, S is the statecommand(S) ->

oneof([ PUBLIC API OPERATIONS ]).

%% Next state transformation, S is the current statenext_state(S,_V,{call,_,_,_}) ->

S.

%% Precondition, checked before command is added to the command sequenceprecondition(_S,{call,_,_,_}) ->

true.

%% Postcondition, checked after command has been evaluated%% OBS: S is the state before next_state(S,_,<command>)postcondition(_S,{call,_,_,_},_Res) ->

true.

prop_statem() ->?FORALL(Cmds,commands(?MODULE),

begin{H,S,Res} = run_commands(?MODULE,Cmds),?WHENFAIL(

io:format("History: ~p~nState: ~p~nRes: ~p~n",[H,S,Res]),Res == ok)

end).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 13 / 23

Page 36: Testing database applications with QuickCheck

The practiseTesting a simple e-shop

Very simple online shop application:

Register new customer

Add new product to shop

Add product to cart

Remove product from cart

Place order

Cancel order

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 14 / 23

Page 37: Testing database applications with QuickCheck

The practiseUML model: main components

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 15 / 23

Page 38: Testing database applications with QuickCheck

The practiseE/R model: basic data constraints

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 16 / 23

Page 39: Testing database applications with QuickCheck

The practiseGolden business rule

Example of business rule (complex data constraint).

Business rules may be implemented in different ways. . .. . . but we only care they actually are.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 17 / 23

Page 40: Testing database applications with QuickCheck

The practiseGolden business rule

Example of business rule (complex data constraint).

Business ruleOnly golden customers maypurchase golden products.

Business rules may be implemented in different ways. . .. . . but we only care they actually are.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 17 / 23

Page 41: Testing database applications with QuickCheck

The practiseGolden business rule

Example of business rule (complex data constraint).

Business ruleOnly golden customers maypurchase golden products.

Business rules may be implemented in different ways. . .

. . . but we only care they actually are.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 17 / 23

Page 42: Testing database applications with QuickCheck

The practiseGolden business rule

Example of business rule (complex data constraint).

Business ruleOnly golden customers maypurchase golden products.

Business rules may be implemented in different ways. . .. . . but we only care they actually are.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 17 / 23

Page 43: Testing database applications with QuickCheck

The practiseHands-on time!

1 Explore the simple e-shop implementation given,

2 inspect the simpleshop_eqc module stub

1 and fill-in the gaps,

3 find out if business rule is respected (and if not, fix it!).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 18 / 23

Page 44: Testing database applications with QuickCheck

The practiseHands-on time!

1 Explore the simple e-shop implementation given,

2 inspect the simpleshop_eqc module stub

1 and fill-in the gaps,

3 find out if business rule is respected (and if not, fix it!).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 18 / 23

Page 45: Testing database applications with QuickCheck

The practiseHands-on time!

1 Explore the simple e-shop implementation given,

2 inspect the simpleshop_eqc module stub

1 and fill-in the gaps,

3 find out if business rule is respected (and if not, fix it!).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 18 / 23

Page 46: Testing database applications with QuickCheck

The practiseHands-on time!

1 Explore the simple e-shop implementation given,

2 inspect the simpleshop_eqc module stub

1 and fill-in the gaps,

3 find out if business rule is respected

(and if not, fix it!).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 18 / 23

Page 47: Testing database applications with QuickCheck

The practiseHands-on time!

1 Explore the simple e-shop implementation given,

2 inspect the simpleshop_eqc module stub

1 and fill-in the gaps,

3 find out if business rule is respected (and if not, fix it!).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 18 / 23

Page 48: Testing database applications with QuickCheck

The practiseOutcome: QuickCheck statem machine skeleton for BR testing (I)

-module(testbr_eqc).

-include_lib("eqc/include/eqc.hrl").-include_lib("eqc/include/eqc_statem.hrl").

-compile(export_all).

-record(state, {useful_info}).

initial_state() ->#state{useful_info = []}.

command(S) ->oneof([ PUBLIC API OPERATIONS (LOCAL WRAPPERS) ]).

next_state(S,_V,{call,_,_,_}) ->S.

precondition(_S,{call,_,_,_}) ->true.

postcondition(_S,{call,_,_,_},_Res) ->true.

prop_brstatem() ->?FORALL(Cmds, commands(?MODULE),

begintrue = check_data_invariant(),{H, S, Res} = run_commands(?MODULE, Cmds),Invariant = check_data_invariant(),clean_up(S),?WHENFAIL(io:format("H ~p~nS ~p~nRes ~p~n", [H, S, Res]),

conjunction([{test_execution, Res == ok},{business_rules, Invariant}]))

end).

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 19 / 23

Page 49: Testing database applications with QuickCheck

The practiseOutcome: QuickCheck statem machine skeleton for BR testing (& II)

<command>_local(Args) ->Expected = expected_result(<command>, Args),Obtained = <command>(Args),match(Expected, Obtained).

check_data_invariant() ->IMPLEMENTATION OF BUSINESS RULES AS STORAGE QUERIES.

expected_result(<command>, Args) ->QUERY STORAGE TO GUESS RESULT.

clean_up(S) ->EMPTY STATE BETWEEN TEST SEQUENCES.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 20 / 23

Page 50: Testing database applications with QuickCheck

Summing upTesting of data-intensive applications

When testing database or data-intensive applications,

special attention must be paid to data-consistency business rules,

data-consistency constraints cannot always be trusted to the data

storage and can never be trusted to the user interface,

business rules implementation may be spread over the system,

system testing is the most adequate level to test for business rules

compliance.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 21 / 23

Page 51: Testing database applications with QuickCheck

Summing upMethodology to test BR using QuickCheck

1 Use a QuickCheck state machine,

2 keep state minimum,

3 add public API operations as commands/transitions,

I use local wrappers to predict the result according to existing data,

I and then match with the result actually obtained

4 specify pre- and postconditions as true,

5 formulate business rules (invariants) as queries to data storage,

6 write property checking invariants after each test sequence.

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 22 / 23

Page 52: Testing database applications with QuickCheck

Summing upI hope this tutorial has been useful!

Attendants ! thanks

Get help subscribing to: [email protected] for images came from: openclipart.org, kde-look.org

Erlang User Conference (2010) Tutorial Workshop Testing DB apps with QC 23 / 23