File: 008-rows.t

package info (click to toggle)
libtest-mockdbi-perl 0.70-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 372 kB
  • sloc: perl: 954; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use strict;
use warnings;

use Test::More;

use_ok('Test::MockDBI');

my $mock = Test::MockDBI::get_instance();
my $dbh = DBI->connect('DBI:mysql:somedb', 'user1', 'password1');

sub get_sth{
  my $sth = $dbh->prepare('SELECT something FROM somewhere WHERE location = ?');
  $sth->bind_param(1, 'anywhere');
  $sth->execute();
  return $sth;
}

{
  #Check that rows default returns -1
  my $sth = get_sth();
  cmp_ok($sth->rows(), '==', -1, '$sth->rows default returns -1');
}

{
  my $resultset = [ { A => 1 }, { B => 1 }];
  $mock->set_retval(
    method => 'fetchrow_hashref',
    retval => $resultset
  );
  
  #Check that we can get rows to return the right number of rows
  my $sth = get_sth();
  $mock->set_retval(
    sql => 'SELECT something FROM somewhere WHERE location = ?',
    retval => sub{
      return scalar( @{ $resultset } );
    },
    method => 'rows'
  );
  cmp_ok($sth->rows(), '==', 2, '$sth->rows returns 2');
}
done_testing();