Malvin
Malvin

Reputation: 859

Compare 2 datasets with dbunit?

Currently I need to create tests for my application. I used "dbunit" to achieve that and now need to compare 2 datasets:

1) The records from the database I get with QueryDataSet
2) The expected results are written in the appropriate FlatXML in a file which I read in as a dataset as well

Basically 2 datasets can be compared this way.

Now the problem are columns with a Timestamp. They will never fit together with the expected dataset. I really would like to ignore them when comparing them, but it doesn't work the way I want it.

It does work, when I compare each table for its own with adding a column filter and ignoreColumns. However, this approch is very cumbersome, as many tables are used in that comparison, and forces one to add so much code, it eventually gets bloated. The same applies for fields which have null-values

A probable solution would also be, if I had the chance to only compare the very first column of all tables - and not by naming it with its column name, but only with its column index. But there's nothing I can find.

Maybe I am missing something, or maybe it just doesn't work any other way than comparing each table for its own?

Upvotes: 1

Views: 7326

Answers (2)

user2520181
user2520181

Reputation: 1

You can also use this format:

// Assert actual database table match expected table
String[] columnsToIgnore = {"CONTACT_TITLE","POSTAL_CODE"}; 
Assertion.assertEqualsIgnoreCols(expectedTable, actualTable, columnsToIgnore);

Upvotes: 0

Malvin
Malvin

Reputation: 859

For the sake of completion some additional information must be posted. Actually my previously posted solution will not work at all as the process reading data from the database got me trapped.

The process using "QueryDataset" did read the data from the database and save it as a dataset, but the data couldn't be accessed from this dataset anymore (although I could see the data in debug mode)! Instead the whole operation failed with an UnsupportedOperationException at org.dbunit.database.ForwardOnlyResultSetTable.getRowCount(ForwardOnlyResultSetTable.java:73)

Example code to produce failure:

QueryDataSet qds = new QueryDataSet(connection);
qds.addTable(“specificTable”);
qds.getTable(„specificTable“).getRowCount(); 

Even if you try it this way it fails:

IDataSet tmpDataset = connection.createDataSet(tablenames);
tmpDataset.getTable("specificTable").getRowCount();

In order to make extraction work you need to add this line (the second one):
IDataSet tmpDataset = connection.createDataSet(tablenames);
IDataSet actualDataset = new CachedDataSet(tmpDataset);

Great, that this was nowhere documented...

But that is not all: now you'd certainly think that one could add this line after doing a "QueryDataSet" as well... but no! This still doesn't work! It will still throw the same Exception! It doesn't make any sense to me and I wasted so much time with it...

It should be noted that extracting data from a dataset which was read in from an xml file does work without any problem. This annoyance just happens when trying to get a dataset directly from the database.

If you have done the above you can then continue as below which compares only the columns you got in the expected xml file:

    // put in here some code to read in the dataset from the xml file...
    // and name it "expectedDataset"
    // then get the tablenames from it...
    String[] tablenames = expectedDataset.getTableNames();

    // read dataset from database table using the same tables as from the xml
    IDataSet tmpDataset = connection.createDataSet(tablenames);
    IDataSet actualDataset = new CachedDataSet(tmpDataset);

    for(int i=0;i<tablenames.length;i++)
    {
        ITable expectedTable = expectedDataset.getTable(tablenames[i]);
        ITable actualTable = actualDataset.getTable(tablenames[i]);         
        ITable filteredActualTable = DefaultColumnFilter.includedColumnsTable(actualTable, expectedTable.getTableMetaData().getColumns());
        Assertion.assertEquals(expectedTable,filteredActualTable);
    }

Upvotes: 3

Related Questions