File: 010-fetchrow_arrayref.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 (83 lines) | stat: -rw-r--r-- 2,105 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use strict;
use warnings;

use Test::More;

use_ok('Test::MockDBI');

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

{
  #Setting up a global resultset
  
  my @expected = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );
  
  $instance->set_retval( method => 'fetchrow_arrayref', retval => \@expected );
  
  my $sth = $dbh->prepare('SELECT * FROM sometable');
  
  $sth->execute();
  
  
  my @got = ();
  my $cnt = 0;
  
  while( my $row = $sth->fetchrow_arrayref()){
    push(@got, $row);
    $cnt++;
  }
  
  is_deeply(\@got, \@expected, "Got the expected resultset");
  cmp_ok($cnt, '==', scalar(@expected), "Executed the while loop the expected number of times");
  
}
{
  #Testing setting a resultset based on the sql
  my @expected = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );
  my $sql = "SELECT * FROM atable";
  
  
  $instance->set_retval( method => 'fetchrow_arrayref', retval => \@expected,  sql => $sql );
  
  my $sth = $dbh->prepare($sql);
  $sth->execute();
  
  my @got = ();
  my $cnt = 0;
  
  while( my $row = $sth->fetchrow_arrayref()){
    push(@got, $row);
    $cnt++;
  }
  
  is_deeply(\@got, \@expected, "Got the expected resultset");
  cmp_ok($cnt, '==', scalar(@expected), "Executed the while loop the expected number of times");  
}

{
  #Testing that a sql resultset should have precedence over a global resultset
  my @expected = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] );
  my @not_expected = ( ['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'J'] );
  my $sql = "SELECT * FROM atable";
  
  
  $instance->set_retval( method => 'fetchrow_arrayref', retval => \@expected, sql => $sql );
  $instance->set_retval( method => 'fetchrow_arrayref', retval => \@not_expected );
  
  my $sth = $dbh->prepare($sql);
  $sth->execute();
  
  my @got = ();
  my $cnt = 0;
  
  while( my $row = $sth->fetchrow_arrayref()){
    push(@got, $row);
    $cnt++;
  }
  
  is_deeply(\@got, \@expected, "Got the expected resultset");
  cmp_ok($cnt, '==', scalar(@expected), "Executed the while loop the expected number of times");  
}

done_testing();