Exam >
On this page

db-set

Creates DbUnit dataset for specified table and applies it to database

Overview

Usage

1. Configure a connection to database via DbPlugin.

2. Use e:db-set tag in specification:

src/test/resources/specs/Specs.html
<e:db-set caption="optional caption" operation="clean_insert" table="ANDROIDS_TABLE" cols="id, name" separator=",">
    <e:row>1, Adam</e:row>
    <e:row>2, Bob</e:row>
</e:db-set>  

Attributes

attributedescexample
table Table name Required. Default: -
table="androids_table"
cols List of comma-separated column names with optionally provided values to include in dataset Optional. Default: -
cols="id=1..10, name, height=170, manufactured={{now}}"
operation DbUnit operation to apply Optional. Default: clean_insert
operation="update"
other options: insert, update, refresh, delete, delete_all, truncate_table, truncate_insert
separator Value separator in e:row Optional. Default: ,
separator="|"
ds Datasource to which dataset will be applied. By default only 1 datasource with name default exists, other may be added by DbPlugin configuration. Optional. Default: default
ds="myOtherDatasource"
caption Caption to display instead of table name in report Optional. Default: -
caption="Androids"

Each dataset record will be zipped from cols and rows declarations (hence order of cols and order of values inside rows should match).

Dummy example

Given


Empty table:
ANDROIDS_TABLE
EMPTY

When


The following markup:
<e:db-set table="ANDROIDS_TABLE" cols="id, name, height, manufactured">
  <e:row>1, Adam, 170, {{now}}</e:row>
  <e:row>2, Bob, 200, {{now}}</e:row>
</e:db-set>
will be rendered as:
ANDROIDS_TABLE
idnameheightmanufactured
1Adam1702022-03-12 15:26:17.732
2Bob2002022-03-12 15:26:17.733

Then


Table will have the following records after applying the following dataset:
ANDROIDS_TABLE
IDNAMEHEIGHTWEIGHTMANUFACTURED
1Adam1.7E+2(null)2022-03-12 15:26:17.721
2Bob2E+2(null)2022-03-12 15:26:17.729

If all rows should have the same value declaration for specific column, then value assignment may be inlined with column declaration. Values declaration for such columns should be omitted in e:row tag.

The next example produces the same result as the previous one:

Declaration inlining

Given


Empty table:
ANDROIDS_TABLE
EMPTY

When


The following markup:
<e:db-set table="ANDROIDS_TABLE" cols="id=1..10, name, height, manufactured={{now}}">
  <e:row>Adam, 170</e:row>
  <e:row> Bob, 200</e:row>
</e:db-set>
will be rendered as:
ANDROIDS_TABLE
idnameheightmanufactured
1Adam1702022-03-12 15:26:17.755
2Bob2002022-03-12 15:26:17.756

Then


Table will have the following records after applying the following dataset:
ANDROIDS_TABLE
IDNAMEHEIGHTWEIGHTMANUFACTURED
1Adam1.7E+2(null)2022-03-12 15:26:17.752
2Bob2E+2(null)2022-03-12 15:26:17.754

Examples

Value declaration

Value can be declared as simple text, Handlebar helpers invocation or ranges.

Value declaration

Given


Empty table:
ANDROIDS_TABLE
EMPTY

When


  • id value is declared as range and will be assigned from 1 to 10 in circle
  • name and weight values are declared as simple text values (value will be casted to column type be DbUnit)
  • manufactured value is declared as Handlebar helpers invocation and will be assigned to result of invocation (which will be casted to column type by DbUnit)
The following markup:
<e:db-set table="ANDROIDS_TABLE" cols="id=1..10, name, weight, height=180, manufactured={{now minus='1 d'}}">
  <e:row>Adam, 70</e:row>
  <e:row>Bob , 90</e:row>
</e:db-set>
will be rendered as:
ANDROIDS_TABLE
idnameweightheightmanufactured
1Adam701802022-03-11 15:26:17.766
2Bob901802022-03-11 15:26:17.767

Then


Table will have the following records after applying the following dataset:
ANDROIDS_TABLE
IDNAMEHEIGHTWEIGHTMANUFACTURED
1Adam1.8E+27E+12022-03-11 15:26:17.763
2Bob1.8E+29E+12022-03-11 15:26:17.765

Trimmed spaces

Values declarations a trimmed by default, if there is a need to preserve spaces, then declaration should be surrounded by '

Trimmed spaces

Given


Empty table:
ANDROIDS_TABLE
EMPTY

When


First name equals to A (surrounded by spaces).
Second name equals to just A.
The following markup:
<e:db-set table="ANDROIDS_TABLE" cols="id=1..10, name, weight">
  <e:row> ' A ' , 70</e:row>
  <e:row> A , 90</e:row>
</e:db-set>
will be rendered as:
ANDROIDS_TABLE
idnameweight
1 A 70
2A90

Then


Table will have the following records after applying the following dataset:
ANDROIDS_TABLE
IDNAMEHEIGHTWEIGHTMANUFACTURED
1 A (null)7E+1(null)
2A(null)9E+1(null)

Values with commas

Comma is used as default values separator, so using it as part as the value declaration will break parsing. To workaround this custom value separator may be set by separator attribute.

Values with commas

Given


Empty table:
ANDROIDS_TABLE
EMPTY

When


name equals to a , b:
The following markup:
<e:db-set table="ANDROIDS_TABLE" cols="id=1..10, name, weight" separator="|">
  <e:row> a , b | 70</e:row>
</e:db-set>
will be rendered as:
ANDROIDS_TABLE
idnameweight
1a , b70

Then


Table will have the following records after applying the following dataset:
ANDROIDS_TABLE
IDNAMEHEIGHTWEIGHTMANUFACTURED
1a , b(null)7E+1(null)