Exam >
On this page

db-execute

Applies specified DbUnit datasets to database

Overview

Usage

1. Configure a connection to database via DbPlugin.

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

src/test/resources/specs/Specs.html
<e:db-execute datasets='/db/adam.xml, /db/bob.json'/>

Attributes

attributedescexample
datasets Comma-separated paths in src/test/resources to datasets to apply. Supported formats: xml, json, csv Required. Default: -
datasets="/db/adam.xml, /db/bob.json, /db/carl/person.csv"
dir Set directory in src/test/resources as working directory so that all datasets from datasets attribute will be relative to it: src/test/resources[dir][item in datasets] Optional. Default: -
dir="/db/"
operation DbUnit operation to apply Optional. Default: clean_insert
operation="update"
other options: insert, update, refresh, delete, delete_all, truncate_table, truncate_insert
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"
vars Comma-separated list of name=value pairs to set as variables Optional. Default: -
vars="var1=1, var2={{now}}, var3=' preserved spaces string '"
varsSeparator name=value pairs separator to use in vars attribute Optional. Default: ,
varsSeparator="|"

Examples

Basic example

Basic example

Given


Empty table:
PERSON
EMPTY
And following dataset files:
/home/runner/work/Exam/Exam/example/build/resources/test/specs/db/data
filecontent
adam.xml
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <PERSON ID="1" NAME="Adam" AGE="10" BIRTHDAY="{{today}}"/>
  <PERSON_FIELDS ID="11..19" NAME="a1" VAL="{{a1}}" PERSON_ID="1"/>
  <PERSON_FIELDS ID="11..19" NAME="a2" VAL="{{a2}}" PERSON_ID="1"/>
</dataset>
bob.json
{
  "PERSON": [
    {"id": 10, "name": "Bob", "age": 20, "BIRTHDAY": "2000-10-10 00:00:00"}
  ],
  "PERSON_FIELDS": [
    {"id": "21..30", "name": "b1", "val": "1", "person_id": 10},
    {"id": "21..30", "name": "b2", "val": "2", "person_id": 10}
  ]
}
carl/table-ordering.txt
person_fields
person
carl/person.csv
ID, NAME, AGE, BIRTHDAY
2, "Carl", 30, {{date "2003-03-03"}}
carl/person_fields.csv
ID, NAME, VAL, PERSON_ID
31..40, c1, "value with comma >> , << and quotes \"_'", 2

When


The following markup:
<e:db-execute dir="/specs/db/data/" datasets="adam.xml, bob.json, carl/person.csv" vars="a1=1, a2=2" varsSeparator=","/>
will be rendered as:
PERSON
IDNAMEAGEBIRTHDAY
1Adam102022-03-12 00:00:00.000
10Bob202000-10-10 00:00:00
2Carl302003-03-03 00:00:00.000
PERSON_FIELDS
IDNAMEVALPERSON_ID
11a111
12a221
21b1110
22b2210
31c1value with comma >> , << and quotes "_'2

Then


Datasets were applied to database:
PERSON
IDNAMEAGEIQBIRTHDAY
1Adam1E+1(null)2022-03-12 00:00:00.000
2Carl3E+1(null)2003-03-03 00:00:00.000
10Bob2E+1(null)2000-10-10 00:00:00.000