Hi perl monks

I'm trying to mock an sql call (for practice purposes) using DBD::Mock and i'm getting a wrong result.

Code.t

#!use/local/bin/perl use Test::More qw (no_plan); use Data::Dumper; use DIR::DBTeams; use constant DB => "dbi::Mock:"; use constant DB_USER => "mock"; use constant DB_PWD => "mock"; my $db; my $query; my $columns; my @boundparams; my @results; my $test_teams; $query = q(select decode(substr(te.TEAM_ID,1,2),'MU','Manchester Unite +d', 'RM', 'Real Madrid')) from Teams t inner join TEAMS_EUROPE.TEAM_REF te + on t.TEAM_REF = te.TEAM_REF and t.TEAM_REF=:ref); $boundparams=('123'); #123 is reference for MU $columns=('TEAM_ID'); @results=('Manchester United'); $db = &connectDB(DB,DB_USER,DB_PWD); my $session = DBD::Mock::Session->new('my_sesion' => ( { statement => $query, bound_params = [@boundparams], results => [[$columns],[@results]] } ) ); $db->{mock_session}=$session; $test_teams = new DBTeams($db, $boundparams[0]); is($test_teams, "Manchester United", "Test Teams"); sub connectDB($$$) { my $dbh = DBI->connect($_[0],$_[1],$_[2]); return $dbh; }

Code : DBTeams

package DIR::DBTeams; use DBI; sub new { my $class = shift; my $self = {}; my ($dbh,$ref) = @_; $self->{dbh} = $dbh; &getTeam($ref); } sub getTeam { my $me = shift; my $ref = shift; my $ret; my $cur = $me->{dbh}->prepare( q(select decode(substr(te.TEAM_ID,1,2),'MU','Manchester United', 'RM', 'Real Madrid')) from Teams t inner join TEAMS_EUROPE.TEAM_REF te + on t.TEAM_REF = te.TEAM_REF and t.TEAM_REF=:ref)); $cur->bind_param(':ref',$ref); $cur->execute() or croak $cur->errstr; $ret = $cur->fetchrow_array(); $cur->finish; return $ret; }

The code seems ok, but when it runs, instead of getting 'Manchester United', the method returns 1.

I tried debugging and several alternatives and none seem to work.

Finaly i tried changing the original code:

Code : DBTeams

... ($ret) = $cur->fetchrow_array(); ...

By placing $ret in ( ), the code now returns what i want. However, i want to know if there is an alternative that allows me to get the result i want without changing the original code.

Can someone help me? Thanks


In reply to Mocked query returns wrong result by Anonymous Monk

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.