How to compare table before and after test in the right way?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to compare table before and after test in the right way?

maly.velky
Hi all,

Does anybody of you bright people know what would be the correct and least-effort way to compare the content of a table before executing a method and after its execution?

I did the following:

------------------------------------
final ITable preTestData = getConnection()
                .createQueryTable("pre-test table", "select * from " + TABLE);

myTestedObject.executeTestedMethod();
       
// Data shouldn't be changed
final ITable postTestData = getConnection()
                .createQueryTable("post-test table", "select * from " + TABLE);
Assertion.assertEquals(preTestData, postTestData);
------------------------------------
However this leads to the failure because creating the first ITable locks the target table and thus the attempt to create the postTestData fails because of a timeout.

I understand now that createQueryTable(...) doesn't fetch the data as I thought but only opens a cursor to the table. Is there a simple way to get the actual data without manually reading them row by row, column by column and storing them into some data structure?

BTW I know I could do things like simply comparing the number of rows but this solution however ineffective it may seem is robust and simple (provided there is a way to compare the table content before and after a change).

Thank you!

PS: I'm using Derby 2.4.7 with Derby embedded

Jakub Holy, the author of DbUnit Test Skeleton a.k.a. dbunit-embeddedderby-parenttest
[hidden email]

------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to compare table before and after test in the right way?

Zdenek Vrablik
Hi Jakub,

you could use an xml file or you could define the table in java code.

Load the xml file and than create IDataSet dataSet = new
FlatXmlDataSet( inputRefresh, true );

or you could create the dataset on the fly:

Column[] columns = new Column[]
    {
      new Column( "ID", DataType.NUMERIC , Column.NO_NULLS),
      new Column( "NAME", DataType.VARCHAR, Column.NO_NULLS),
      new Column( "DESCRIPTION", DataType.VARCHAR, Column.NULLABLE)
    };

   table.addRow( new Object[] { new BigDecimal( 10 ) , "user1",
"user1Description"} );
    table.addRow( new Object[] { new BigDecimal( 11 ) , "user2",
"user2Description"} );

    DefaultTable table = new DefaultTable("PEOPLE", columns);

    DefaultDataSet ds = new DefaultDataSet();
    ds.addTable( table );


Regards,
Zdenek

On Mon, Apr 19, 2010 at 9:52 AM,  <[hidden email]> wrote:

> Hi all,
>
> Does anybody of you bright people know what would be the correct and
> least-effort way to compare the content of a table before executing a method
> and after its execution?
>
> I did the following:
>
> ------------------------------------
> final ITable preTestData = getConnection()
>                 .createQueryTable("pre-test table", "select * from " +
> TABLE);
>
> myTestedObject.executeTestedMethod();
>
> // Data shouldn't be changed
> final ITable postTestData = getConnection()
>                 .createQueryTable("post-test table", "select * from " +
> TABLE);
> Assertion.assertEquals(preTestData, postTestData);
> ------------------------------------
> However this leads to the failure because creating the first ITable locks
> the target table and thus the attempt to create the postTestData fails
> because of a timeout.
>
> I understand now that createQueryTable(...) doesn't fetch the data as I
> thought but only opens a cursor to the table. Is there a simple way to get
> the actual data without manually reading them row by row, column by column
> and storing them into some data structure?
>
> BTW I know I could do things like simply comparing the number of rows but
> this solution however ineffective it may seem is robust and simple (provided
> there is a way to compare the table content before and after a change).
>
> Thank you!
>
> PS: I'm using Derby 2.4.7 with Derby embedded
>
> Jakub Holy, the author of DbUnit Test Skeleton a.k.a.
> dbunit-embeddedderby-parenttest
> [hidden email]
>
> ------------------------------------------------------------------------------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> dbunit-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: How to compare table before and after test in the right way? [SOLVED]

maly.velky
I was mistaken, DbUnit actually does what I need, namely it does load the results of a query into the memory releasing the connection immediately (via the CachedResultSetTable). So executing

ITable t1 = getConnection().createQueryTable("1", "select ...")
ITable t2 = getConnection().createQueryTable("2", "select ...")
Assertion.assertEquals(t1, t2);

is perfectly legal. My problem and misunderstanding were cause by the tested call that set auto-commit to false and finished without a commit or rollback, thus leaving the table locked.

@ Zdenek: Thanks for your answer. But it's a bit too much of a code and it does something a bit different than I wanted (you manually create a table while I wanted to assert that table content in the DB hasn't changed without the need to replicate it in the code). Thanks anyway.

> ------------ Původní zpráva ------------
> Od: Zdeněk Vráblík <[hidden email]>
> Předmět: Re: [dbunit-user] How to compare table before and after test in > the right way?
> Datum: 19.4.2010 13:14:35
> ---------------------------------------------
> Hi Jakub,

you could use an xml file or you could define the table > in java code.

Load the xml file and than create IDataSet dataSet = > new
FlatXmlDataSet( inputRefresh, true );

or you could create > the dataset on the fly:

Column[] columns = new Column[]
> {
new Column( "ID", DataType.NUMERIC , Column.NO_NULLS),
> new Column( "NAME", DataType.VARCHAR, Column.NO_NULLS),
new Column( > "DESCRIPTION", DataType.VARCHAR, Column.NULLABLE)
};

> table.addRow( new Object[] { new BigDecimal( 10 ) , "user1",
/>"user1Description"} );
table.addRow( new Object[] { new BigDecimal( > 11 ) , "user2",
"user2Description"} );

DefaultTable table > = new DefaultTable("PEOPLE", columns);

DefaultDataSet ds = new > DefaultDataSet();
ds.addTable( table );


/>Regards,
Zdenek

On Mon, Apr 19, 2010 at 9:52 AM, > <[hidden email]> wrote:

> Hi all,
>
> Does anybody of > you bright people know what would be the correct and
> least-effort way to > compare the content of a table before executing a method
> and after its > execution?
>
> I did the following:
>
> > ------------------------------------
> final ITable preTestData = > getConnection()
>                 .createQueryTable("pre-test table", > "select * from " +
> TABLE);
>
> > myTestedObject.executeTestedMethod();
>
> // Data shouldn't be > changed
> final ITable postTestData = getConnection()
>             >     .createQueryTable("post-test table", "select * from " +
> TABLE);
/>> Assertion.assertEquals(preTestData, postTestData);
> > ------------------------------------
> However this leads to the failure > because creating the first ITable locks
> the target table and thus the > attempt to create the postTestData fails
> because of a timeout.
/>>
> I understand now that createQueryTable(...) doesn't fetch the data > as I
> thought but only opens a cursor to the table. Is there a simple way > to get
> the actual data without manually reading them row by row, column > by column
> and storing them into some data structure?
>
> BTW > I know I could do things like simply comparing the number of rows but
> > this solution however ineffective it may seem is robust and simple (provided
/>> there is a way to compare the table content before and after a change).
/>>
> Thank you!
>
> PS: I'm using Derby 2.4.7 with Derby > embedded
>
> Jakub Holy, the author of DbUnit Test Skeleton > a.k.a.
> dbunit-embeddedderby-parenttest
> [hidden email]
/>>
> > ------------------------------------------------------------------------------
/>> Download Intel® Parallel Studio Eval

> Try the new software tools > for yourself. Speed compiling, find bugs
> proactively, and fine-tune > applications for parallel performance.
> See why Intel Parallel Studio got > high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> > _______________________________________________
> dbunit-user mailing > list
> [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/dbunit-user
>
>
>

Jakub Holy
[hidden email]

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user