Test::DatabaseRow

2shortplanks on 2003-02-05T18:38:03

Today's been a really odd day. I've been running some stuff that takes more than a couple of hours to do it's thing, so I've been doing odds and ends while waiting for it to return the correct (or incorrect) data. Odds and ends like listing to Colin Powel and releasing modules to CPAN. As I said, an odd day.

The second module Profero has released to CPAN today is Test::DatabaseRow - a module that is used to simply test things in are in your database in a Test::Builder compatible way. In the simplest form it runs some SQL for you and checks that the fields in the first row back meet the criteria of the test.

For example, a simple test for my Buffy database:

  row_ok( sql   => "SELECT * FROM chars WHERE name = 'Buffy'",
          tests => [ mother => "Joyce" ],
          label => "Buffy's mother is Joyce" );
There's a SQL generation routine too (though it's also designed to work well with SQL::Abstract if that's your bag)
  row_ok( table => "chars",
          where => [ name     => "Willow" ],
          tests => [ fullname => "Willow Rosenberg"],
                     addr     => qr/Sunnydale/ ],
          label => "Check Willow's name & she lives in Sunnydale" );
The where and tests when used with an arrayref like in the above examples are designed to dtrt and do the right comparisons (which essentially means '=' tests for where and ==, eq or =~ tests depending on the data for the tests.) You can be explicit if you want though:
 row_ok( table => "chars",

         where => { 'like' => { name     => 'Angel%'   },},

         tests => { '=='   => { height   => 6*12+1,
                                born     => 1727           }
                    'eq'   => { fullname => "Liam"         },
                    '=~'   => { addr     => qr/California/ },},

         label => "angel entered okay" );
Hope it's useful. I'm off home to crowd round the electric heater and shoot nasty glances and the knackered boiler.


How about returning empty?

petdance on 2003-02-05T19:49:00

My immediate need is to find rows that don't match. I want to perform queries that should return 0 rows, because if they don't, then I've got bad data in the database....

Re:How about returning empty?

2shortplanks on 2003-02-06T13:07:56

I'll add it to the todo list.