Elements of the database

Here the elements on which it is possible to make assertions.

Note that, there are only 3 root elements : Table, Request and Changes.

That means that the other elements are components or sub components of a root element.

A root element is an element on which the assertion start (in practice, the parameter of a assertThat(…​) method).

Table

A Table represents a table in the database.

A Table can be constructed with the builder method table(String name) from AssertDbConnection.

// Prepare the connection
AssertDbConnection connection = ...
// Declare the "members" table by using a AssertDbConnection table builder
Table table1 = connection.table("members").build();

For more information about table construction, see the Table.Builder javadoc.

Table 1. Representation of "table1"
ID NAME FIRSTNAME SURNAME BIRTHDATE SIZE

1

'Hewson'

'Paul David'

'Bono'

05-10-60

1.75

2

'Evans'

'David Howell'

'The Edge'

08-08-61

1.77

3

'Clayton'

'Adam'

03-13-60

1.78

4

'Mullen'

'Larry'

10-31-61

1.70

For a Table, it is possible to choose the columns to include and to exclude in the assertions.

// Get the data of the "id" and "name" columns of the "members" table
Table table2 = connection.table("members").columnsToCheck(new String[] { "id", "name" }).build();
// Get the data of the "members" table but not of the "birthdate" column
Table table3 = connection.table("members").columnsToExclude(new String[] { "birthdate" }).build();
// Get the data of the "name" column of the "members" table (because "id" is included and excluded)
Table table4 = connection.table("members").columnsToCheck(new String[] { "id", "name" }).columnsToExclude(new String[] { "id" }).build();
Table 2. Representation of "table2"
ID NAME

1

'Hewson'

2

'Evans'

3

'Clayton'

4

'Mullen'

Table 3. Representation of "table3"
ID NAME FIRSTNAME SURNAME SIZE

1

'Hewson'

'Paul David'

'Bono'

1.75

2

'Evans'

'David Howell'

'The Edge'

1.77

3

'Clayton'

'Adam'

1.78

4

'Mullen'

'Larry'

1.70

Table 4. Representation of "table4"
NAME

'Hewson'

'Evans'

'Clayton'

'Mullen'

Since version 1.2.0, there are the possibility to indicate delimiters (start delimiter and end delimiter) and Order.

The delimiters are useful when the table name or column name is a reserved word or contains special characters (like space or '%'). Order allows to choose the order of the Row.

// The line code below throws SQLException because "group" is SQL reserved word
Table table5 = connection.table("group").build();
// Get the data of the "group" table by using "`" delimiter
// That generates a request
Table table6 = connection.table("group").delimiters('`', '`').build();

// Get the data from "members" table and order on "name" column in ascending order
Table table7 = connection.table("members").columnsToOrder(new Order[] { Order.asc("name") }).build();
Table 5. Representation of "table6"
ID NAME

1

'U2'

2

'Colplay'

Table 6. Representation of "table7"
ID NAME FIRSTNAME SURNAME BIRTHDATE SIZE

3

'Clayton'

'Adam'

03-13-60

1.78

2

'Evans'

'David Howell'

'The Edge'

08-08-61

1.77

1

'Hewson'

'Paul David'

'Bono'

05-10-60

1.75

4

'Mullen'

'Larry'

10-31-61

1.70

Request

A Request represents a SQL request on the database.

Like a Table, a Request can be constructed with the builder method request(String sql) from AssertDbConnection.

// Prepare the connection
AssertDbConnection connection = ...
// Declare a request which gets the name and the firstname of the corresponding members
Request request1 = connection.request("select name, firstname from members where id = 2 or id = 3").build();

For more information about request construction, see the Request.Builder javadoc.

Table 7. Representation of "request1"
NAME FIRSTNAME SURNAME

'Evans'

'David Howell'

'The Edge'

'Clayton'

'Adam'

For a Request, it is possible to use a simple SQL request or a SQL request with one or many parameters.

// Declare a request which gets the name and the firstname of the members
// and use "%e%" as a parameter
Request request2 = connection.request(
                               "select name, firstname from members " +
                               "where name like ?;")
                               .parameters("%e%")
                               .build();
// Declare a request which gets the name and the firstname of the members
// and use "%e%" and "%Paul%" as parameters
Request request3 = connection.request(
                               "select name, firstname from members " +
                               "where name like ? and firstname like ?;")
                               .parameters("%e%", "%Paul%")
                               .build();
Table 8. Representation of "request2"
NAME FIRSTNAME SURNAME

'Hewson'

'Paul David'

'Bono'

'Evans'

'David Howell'

'The Edge'

'Mullen'

'Larry'

Table 9. Representation of "request3"
NAME FIRSTNAME SURNAME

'Hewson'

'Paul David'

'Bono'

Changes

The Changes are the differences of states in database between a start point and a end point.

db changes concept

Assume that there are these SQL statements between the start point and the end point.

DELETE FROM ALBUMS WHERE ID = 15;
INSERT INTO MEMBERS(ID, NAME, FIRSTNAME) VALUES(5, 'McGuiness', 'Paul');
UPDATE MEMBERS SET SURNAME = 'Bono Vox' WHERE ID = 1;
UPDATE ALBUMS SET NAME = 'Rattle & Hum', LIVE = true WHERE ID = 8;
// Prepare the connection
AssertDbConnection connection = ...
// The changes can be on a DataSource or on a Source
Changes changes1 = connection.changes().build();
// The changes can also be on a Table or on a Request
Changes changes2 = connection.changes().table(table3).build();
Changes changes3 = connection.changes().request(request2).build();
// The names of the columns used for the primary key are found in the metadata for a table
// but for a request it can be important to set the primary key
Changes changes4 = connection.changes().request("select name, firstname from members", r -> r.pksName("name")).build();

For more information about changes construction, see the Changes.Builder javadoc.

The changes are ordered :

  • First by the type of the change : creation, modification and after deletion

  • After if it is a change on a table by the name of the table

  • To finish by the values of the primary key and if there are no primary key by the values of the row (for a modification)

As indicated above, the primary key is used to order the changes. But more important, the primary key is used to determinate which rows at the same with modifications.

In Representation of "changes3" the modification of first row of the table become a creation and deletion.

Table 10. Representation of "changes1"

Creation

"MEMBERS" table

5 as PK

ID NAME FIRSTNAME SURNAME BIRTHDATE SIZE

At start point

At end point

5

'McGuiness'

'Paul'

Modification

"ALBUMS" table

8 as PK

ID RELEASE TITLE NUMBEROFSONGS DURATION LIVE

At start point

8

10-10-88

'Rattle and Hum'

17

72:27

At end point

8

10-10-88

'Rattle & Hum'

17

72:27

true

Modification

"MEMBERS" table

1 as PK

ID NAME FIRSTNAME SURNAME BIRTHDATE SIZE

At start point

1

'Hewson'

'Paul David'

'Bono'

05-10-60

1.75

At end point

1

'Hewson'

'Paul David'

'Bono Vox'

05-10-60

1.75

Deletion

"ALBUMS" table

15 as PK

ID RELEASE TITLE NUMBEROFSONGS DURATION LIVE

At start point

15

09-09-14

'Songs of Innocence'

11

48:11

At end point

Table 11. Representation of "changes2"

Creation

"MEMBERS" table

5 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

At end point

5

'McGuiness'

'Paul'

Modification

"MEMBERS" table

1 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

1

'Hewson'

'Paul David'

'Bono'

1.75

At end point

1

'Hewson'

'Paul David'

'Bono Vox'

1.75

Table 12. Representation of "changes3"

Creation

No PK

NAME FIRSTNAME SURNAME

At start point

At end point

'Hewson'

'Paul David'

'Bono Vox'

Creation

No PK

NAME FIRSTNAME SURNAME

At start point

At end point

'McGuiness'

'Paul'

Deletion

No PK

NAME FIRSTNAME SURNAME

At start point

At end point

'Hewson'

'Paul David'

'Bono Vox'

Table 13. Representation of "changes4"

Creation

'McGuiness' as PK

NAME FIRSTNAME SURNAME

At start point

At end point

'McGuiness'

'Paul'

Modification

'Hewson' as PK

NAME FIRSTNAME SURNAME

At start point

'Hewson'

'Paul David'

'Bono'

At end point

'Hewson'

'Paul David'

'Bono Vox'

Change

A Change is an element of the Changes.

Below framed in red the first Change of "changes2".

Table 14. Representation of "changes2"

Creation

"MEMBERS" table

5 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

At end point

5

'McGuiness'

'Paul'

Modification

"MEMBERS" table

1 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

1

'Hewson'

'Paul David'

'Bono'

1.75

At end point

1

'Hewson'

'Paul David'

'Bono Vox'

1.75

Row

A Row can represent a row of a Table , of a Request or of a Change.

Below framed in red the third Row of "table3".

Table 15. Representation of "table3"
ID NAME FIRSTNAME SURNAME SIZE

1

'Hewson'

'Paul David'

'Bono'

1.75

2

'Evans'

'David Howell'

'The Edge'

1.77

3

'Clayton'

'Adam'

1.78

4

'Mullen'

'Larry'

1.70

Below framed in red the second Row of "request2".

Table 16. Representation of "request2"
NAME FIRSTNAME SURNAME

'Hewson'

'Paul David'

'Bono'

'Evans'

'David Howell'

'The Edge'

'Mullen'

'Larry'

Below framed in red the Row at end point of the second Change of "changes3".

Table 17. Representation of "changes2"

Creation

"MEMBERS" table

5 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

At end point

5

'McGuiness'

'Paul'

Modification

"MEMBERS" table

1 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

1

'Hewson'

'Paul David'

'Bono'

1.75

At end point

1

'Hewson'

'Paul David'

'Bono Vox'

1.75

Column

A Column can represent a column of a Table , of a Request or of a Change.

Below framed in red the second Column of "table3".

Table 18. Representation of "table3"
ID NAME FIRSTNAME SURNAME SIZE

1

'Hewson'

'Paul David'

'Bono'

1.75

2

'Evans'

'David Howell'

'The Edge'

1.77

3

'Clayton'

'Adam'

1.78

4

'Mullen'

'Larry'

1.70

Below framed in red the second Column of "request2".

Table 19. Representation of "request2"
NAME FIRSTNAME SURNAME

'Hewson'

'Paul David'

'Bono'

'Evans'

'David Howell'

'The Edge'

'Mullen'

'Larry'

Below framed in red the fourth Column of the second Change of "changes3".

Table 20. Representation of "changes2"

Creation

"MEMBERS" table

5 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

At end point

5

'McGuiness'

'Paul'

Modification

"MEMBERS" table

1 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

1

'Hewson'

'Paul David'

'Bono'

1.75

At end point

1

'Hewson'

'Paul David'

'Bono Vox'

1.75

Value

A value can be in a Row or in a Column.

Below framed in red (depending of the path) :

  • the second value of the third Row of "table3"

  • the third value of the second Column of "table3"

Table 21. Representation of "table3"
ID NAME FIRSTNAME SURNAME SIZE

1

'Hewson'

'Paul David'

'Bono'

1.75

2

'Evans'

'David Howell'

'The Edge'

1.77

3

'Clayton'

'Adam'

1.78

4

'Mullen'

'Larry'

1.70

Below framed in red (depending of the path) :

  • the second value of the second Row of "request2"

  • the second value of the second Column of "request2"

Table 22. Representation of "request2"
NAME FIRSTNAME SURNAME

'Hewson'

'Paul David'

'Bono'

'Evans'

'David Howell'

'The Edge'

'Mullen'

'Larry'

Below framed in red (depending of the path) :

  • the fourth value of the Row at end point of the second Change of "changes2"

  • the value at end point of the fourth Column of the second Change of "changes2"

Table 23. Representation of "changes2"

Creation

"MEMBERS" table

5 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

At end point

5

'McGuiness'

'Paul'

Modification

"MEMBERS" table

1 as PK

ID NAME FIRSTNAME SURNAME SIZE

At start point

1

'Hewson'

'Paul David'

'Bono'

1.75

At end point

1

'Hewson'

'Paul David'

'Bono Vox'

1.75