sirrobert has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I've started the task of implementing a test suite for a largish code project. It's a CGI based project that uses a DB backend (mysql; in a LAMP environment). I've gotten a good start on the unit tests, but am now running into some trouble trying to deal with testing the database side of things. I've read quite a lot of material covering DBD::Mock and have successfully gotten things hooked up... but it seems to be returning data that's not helpful at all. Am I missing something obvious? I've rtfm and a number of other online resources, but I just don't get it! I've simplified the case to a simple case scenario. Looking at the "Test Execution" block below, obviously the data that's being returned in the 'SELECT' isn't what I asked for; I just wanted the "name" and "description." Also, they are not from the correct record (they are from the record where id=1 instead of id=3). Is DBD::Mock only good for getting the entire row's data or ...? Please help =)########## FILES ########## ./t/mock_db_test.t ./t/sample_db_data_test.pm
############## Test execution ############## $ prove -v t/mock_db_test.t t/mock_db_test....ok 1 - baseline test # $VAR1 = [ # 1, # 'name1', # 'description1', # 'created_on1', # 'created_by1', # 'deleted1', # 'deleted_by1', # 'public1' # ]; 1..1 ok All tests successful. Files=1, Tests=1, 0 wallclock secs ( 0.03 cusr + 0.01 csys = 0.04 C +PU) $
################## ./t/mock_db_test.t ################## use Test::More qw(no_plan); use strict; use warnings; ### Just a blank test so this sample runs. ok(1,'baseline test'); ### Load DBI and create a handle. use DBI; my $dbh = DBI->connect('DBI:Mock:', '', ''); ### Load the sample data set into memory and add it as a resultset to +the ### mock db driver. use t::sample_db_data_test; $dbh->{'mock_add_resultset'} = \@t::sample_db_data_test::Accounts; ### Try to get just the name and description. my $sth = $dbh->prepare('SELECT name, description FROM Accounts WHERE +id=?'); if ($sth->execute(3)) { ### Successful query. my @results = $sth->fetchrow_array; use Data::Dumper; diag(Dumper(\@results)); } else { ### Failure... diag('Some weird error: ' . $sth->errstr); }
########################## ./t/sample_db_data_test.pm ########################## package t::sample_db_data_test; our @Accounts = ( ['id', 'name', 'description', 'created_on', 'created_by','deleted' +, 'deleted_by', 'public'], [ 1, 'name1','description1','created_on1','created_by1','deleted +1','deleted_by1','public1'], [ 2, 'name2','description2','created_on2','created_by2','deleted +2','deleted_by2','public2'], [ 3, 'name3','description3','created_on3','created_by3','deleted +3','deleted_by3','public3'], ); 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBD::Mock -- Giving back wrong data and wrong *amounts* of data
by andreas1234567 (Vicar) on Aug 20, 2009 at 18:53 UTC | |
by sirrobert (Acolyte) on Aug 20, 2009 at 21:08 UTC | |
by andreas1234567 (Vicar) on Aug 21, 2009 at 09:22 UTC |