Can DbUnit restore the database to a snapshot as it was before the test, and can DbUnit assert against an dataset file describing the changes since the snapshot ?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Can DbUnit restore the database to a snapshot as it was before the test, and can DbUnit assert against an dataset file describing the changes since the snapshot ?

Question 1:

Regarding restoring a "clean" database table, can DbUnit only
be used for either restoring a table by deleting all rows (DatabaseOperation.DELETE_ALL)
or by deleting those rows defined in the setup fixture dataset (DatabaseOperation.DELETE)

I would like a third option, which would extend the second option above
by also deleting all inserts that my test methods inserted (and also, I would
like all potential changes in the previous database table to be restored exactly
as it was before the test, i.e. there should be some snapshot taken in the
setup phase that can be restored in the teardown phase).

Question 2:

Is it possible to do DbUnit assertions with dataset files that do not define
entire table content but that only defines some few extra rows to be inserted
and expected (when you are using 'DatabaseOperation.DELETE' i.e. when you
have some previous data that you have not defined in dataset files to be compared)

In other words, I want to define only a small fixture file and a small
file with the expected data, while still being able to have lots of untouched
existing data in the tables.

(BTW, for those who now are wondering, the main
reason for not wanting to use CLEAN_INSERT/DELETE_ALL is that I am working
with a big existing application, and the DELETE_ALL will complain about lots
of foreign keys that will have to be removed or cascaded, and if too much
stuff is cascaded and deleted, then too much of the application will stop
working, and in fact, nobody really knows exactly what data that is needed
for what, and it is a generally messy web application with couplings between
just about everything and very difficult to succefully execute anything at
all from a main method or from a JUnit method, so as a new employee I now
want to be able to create some JUnit/DbUnit code that can be working as executable
working documentation, i.e. sample code that can make it a little easier for
future employees to understand how these kind of CRUD operations can be done)

I think my questions are difficult to write down in an understandable way,
so I will now try to explain them in more detail with an example below.

I would like to define only the NEW rows in the xml file used in the setup
fixture phase, and leave existing rows as they are.

In the comparison xml
file to be used after the test, I want to define only the changes compared
to the   setup fixture dataset and not having to define the entire content
of the tables (in the setup fixture and in the dataset with the expected data)
with the previously existing data.

My problem is that I do not know how
to avoid creating XML file with the full contents of the tables.

For example,
let us say I am using a database where I want to write a test that will affect
a table that has 1000 rows, and I want those rows to remain exactly the same
when my tests has finished (and no more, and no less rows than exactly those
1000 rows).

Further, let us say that my test class (i.e. my subclass to
DBTestCase) defines a method 'getDataSet()' that returns a dataset created
by an XML file that defines only one row to become inserted before each of
my three test methods as specified below. So, when the DbUnit setup has executed
and each one of my test methods starts, I want to have 1001 rows in the table,
and after each test method (i.e. after the teardown phase) I want the table
to have been restored with the original 1000 rows.

Let us say I want to
create three methods for doing these things:

(1) a method that tests DELETE
(which will delete only the one row I defined in the XML file, and I want
to be able to create an assertion that verifies successful deletion, and then
the teardown should not delete any more rows but only verify that the 1000
rows as the same data as before the test, or otherwise they should become
restored as in the snapshot)

(2) a method that tests UPDATE (which will update
only the one row I defined in the XML file, and after some DbUnit assertion
that the update was performed correctly, I want DbUnit to delete it for me
in the teardown, and as before leaving the original 1000 rows in the table
as in the snapshot before the test)

(3) a method that tests INSERT (and in
this case, the automatically inserted new row was actually unnecessary to
insert in the setup phase, since it will not be used in the insertion test,
but anyway, after some assertion that the insert was okay, I now want DbUnit
to delete both rows in the teardown phase, leaving the original 1000 rows
in the table, after the two new rows have been deleted, i.e. both the automatically
but unused inserted new row should be deleted, and also the new row that the
actual test inserted should also be deleted)

How can I let DbUnit delete
not only the rows defined in the dataset file (DatabaseOperation.DELETE) but
ALSO the rows inserted by my test methods (when I tested a method that created
an INSERT) ?

How can I define small files with the NEW fixture data and the
expected data changes, and letting DbUnit assert the result without having
to specify all the original data that existed before the setup fixture added
some more data ?

Code examples, please :-)

This email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
dbunit-user mailing list
[hidden email]