I've been using DbUnit for a while and there's a couple of very common use cases that are a bit verbose to in dbunit, so I've written a couple of extra assertion methods that perform the whole thing in a single operation. It would be nice if that could be added to the Assertion class
/**
* Compare a table present in two datasets.
*
* @param dataset1 First dataset.
* @param dataset2 Second dataset.
* @param tableName Table name.
* @param ignoreCols Columns to ignore while comparing.
* @throws org.dbunit.DatabaseUnitException If an error occurs.
*/
public static void compareTable(final IDataSet dataset1, final IDataSet dataset2, final String tableName,
final String... ignoreCols) throws DatabaseUnitException
{
final ITable t1 = DefaultColumnFilter.excludedColumnsTable(dataset1.getTable(tableName), ignoreCols);
final ITable t2 = DefaultColumnFilter.excludedColumnsTable(dataset2.getTable(tableName), ignoreCols);
Assertion.assertEquals(t1, t2);
}
/**
* Compare a table from a dataset with a table generated from an sql query.
*
* @param dataset Dataset to retrieve the first table from.
* @param connection Connection to use for the SQL statement.
* @param sqlQuery SQL query that will build the data in returned second table rows.
* @param tableName Table name
* @param ignoreCols Columns to ignore while comparing.
* @throws DatabaseUnitException If an error occurs while performing the comparison.
* @throws java.sql.SQLException If an SQL error occurs.
*/
public static void compareTableUsingQuery(final IDataSet dataset, final IDatabaseConnection connection, final String sqlQuery,
final String tableName, final String... ignoreCols)
throws DatabaseUnitException, SQLException
{
final ITable t1 = DefaultColumnFilter.excludedColumnsTable(dataset.getTable(tableName), ignoreCols);
final ITable t2 = DefaultColumnFilter.excludedColumnsTable(connection.createQueryTable(tableName, sqlQuery), ignoreCols);
Assertion.assertEquals(t1, t2);
}
Logged In: YES
user_id=57511
Originator: NO
I think we can apply this on the upcoming 2.3.0 release. Gommma, can you handle this?
Logged In: YES
user_id=1803108
Originator: NO
Yeah, I will do that. Is it okay when I rename the methods so that they better fit into the API? For example instead of "compareTable" I would prefer the method name "assertEquals" as the other methods are named. Roberto, is it okay if I create a patch or would you like me to commit it to a branch?
Logged In: YES
user_id=1803108
Originator: NO
Ok. It is in the repository on the trunk. I took the liberty to rename the methods from "compareXX" to "assertEqualsXX" so that they fit better into the dbunit naming pattern.
Thanks for that little but helpful patch!
Logged In: YES
user_id=1803108
Originator: NO
Yeah, I will do that. Is it okay when I rename the methods so that they better fit into the API? For example instead of "compareTable" I would prefer the method name "assertEquals" as the other methods are named. Roberto, is it okay if I create a patch or would you like me to commit it to a branch?
Logged In: YES
user_id=57511
Originator: NO
Please commit your contribution directly into the trunk, if we'll ever need to maintain the 2.2 trunk we'll create a branch starting from the latest 2.2.* tag
Logged In: YES
user_id=1803108
Originator: NO
Hi Roberto,
it is already committed on the trunk since rev. 669. The new method signatures are the following:
1. assertEqualsIgnoreCols(final IDataSet expectedDataset, final IDataSet actualDataset,
final String tableName, final String[] ignoreCols)
2. assertEqualsIgnoreCols(final ITable expectedTable, final ITable actualTable,
final String[] ignoreCols)
3. assertEqualsByQuery(final IDataSet expectedDataset,
final IDatabaseConnection connection, final String sqlQuery,
final String tableName, final String[] ignoreCols)
4. assertEqualsByQuery(final ITable expectedTable,
final IDatabaseConnection connection, final String tableName, final String sqlQuery,
final String[] ignoreCols)
Regards,
mat