Verify database

Verify the content of the database at the end of each test.

Use the dataset format to specify the tables, rows and column values to be verified.

Tables not included in the dataset are ignored in verification. However, once a table is included, all the rows in the dataset must match exactly, missing or unexpected rows fail the verification.

The order of rows in verification is irrelevant, the rows are matched by the number of matching column values.

Default @Verify annotation
When a test method or a test class is annotated with @Verify, the following files are located sequentially in the package of the class, the first file found wins:

  • <test class name>.<test method name>-verify.xml
  • <test method name>-verify.xml
  • <test class name>-verify.xml

Then the dataset is verified against the database.

Example:

@Verify
public class MyTest {
  @Test
  public void test() {
  }
}

Specify dataset file names in @Verify
When @Verify annotation contains file names of datasets, they are used to locate the datasets in the package of the test class and the dataset is verified against the database with all located datasets merged into one.

Example:

@Verify({ "verify-1.xml", "verify-2.xml" })
public class MyTest {
  @Test
  public void test() {
  }
}

Specify profile in @Verify
Use profile attribute of @Verify annotation to instruct Light Air to apply the datasets on the connection defined by the profile.
See the Profiles section in Configure Light Air for details on how to setup profiles.

Examples:

@Verify(value = { "verify-1.xml", "verify-2.xml" }, profile = "oracle")
public class MyTest {
  @Test
  public void test() {
  }
}

Specify multiple @Verify annotations
Use @Verify.List annotation to define multiple @Verify annotations for the same test class or test method.

Examples:

@Verify.List({
  @Verify("verify-h2.xml"),
  @Verify(value = "verify-hsql.xml", profile = "hsql"),
  @Verify(value = { "verify-ora-1.xml", "verify-ora-2.xml" }, profile = "oracle") })
public class MyTest {
  @Test
  public void test() {
  }
}

Empty row
Use "empty row", i.e. element with no attributes, to verify the DB table is empty (has no rows).

Example:

<dataset>
  <my_table/>
</dataset>

Variables
Use $name expressions as variables in verification datasets to verify that two (or more) column values are equal. This can be used for example to verify a foreign key referencing a generated primary key.

Example:

<dataset>
  <order id="$orderId" number="1234-567" />
  <line_item id="@any" order_id="$orderId" quantity="10" />
  <line_item id="@any" order_id="$orderId" quantity="30" />
</dataset>

The above dataset verifies there is one row in the order table with the given number, and there are two rows in the line_item table with the given quantities. What is important, it also verifies that the two line items are related to the order via foreign key order_id, no matter what the actual value of that foreign key is.

The expression $orderId is a variable, which on its first occurrence (in this case in order.id) takes the actual value from the database and defines a variable having this value. On any subsequent occurrence (in this case in both the line_item.order_id column values) it verifies that the actual database value is equal to the already defined value of the variable.

Token @any - any non-null value
Use @any to specify that the actual column should have any non-null value.

When you omit the column in the row, its value will be ignored. By using @any you can explicitly specify that the column value should be ignored, provided there is a value at all. (Use @null to verify the column has no value.)

Token @null - null value
Use @null to verify a column has null value.

Token @date - current date
Use @date to verify a column has current date midnight value.
The current date value includes yyyy-MM-dd as in 2012-12-31.
The time part is verified to be midnight (00:00:00.000).

Token @time - current time
Use @time to verify a column has current time value.
The current time value includes HH:mm:ss as in 23:59:58.
The date part is verified to be 1970-01-01. The milliseconds are verified to be 0 (zero).

Token @timestamp - current timestamp
Use @timestamp to verify a column has current timestamp value.
The current timestamp value includes yyyy-MM-ddTHH:mm:ss.SSS as in 2012-12-31T23:59:58.123.

Modify the temporal tokens with a duration
You can modify the temporal tokens @date, @time and @timestamp by appending a sign (+ or -) and a duration in ISO 8601 format. This will move the time instant represented by the temporal token in the direction of the sing by the duration specified.

Examples:

  • The bare token @date represents the last midnight.
  • This @date+P1D represents the first following midnight.
  • This @date-P1M represents the midnight one month before the last one.
  • This @date+PT12H represents noon today.
  • This @timestamp+PT1H represents one hour in the future.
  • This @timestamp-P2Y3M4DT5H6M7S represents 2 years, 3 months, 4 days, 5 hours, 6 minutes and 7 seconds in the past.

Token @auto - generate unique value
Use @auto as a column value to have the actual value generated automatically.
The value is guaranteed to be unique, with the obvious exception of boolean columns.

Example:

<dataset>
  <user id="@auto" email="@auto" name="@auto" password="@auto" version="@auto" />
  <user id="@auto" email="@auto" name="@auto" password="@auto" version="@auto" />
  <user id="@auto" email="@auto" name="@auto" password="@auto" version="@auto" />
</dataset>
Next: Await database >>