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;

In reply to DBD::Mock -- Giving back wrong data and wrong *amounts* of data by sirrobert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.