| 1 |
use 5.008; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok('DBD::Mock'); |
| 10 |
use_ok('DBI'); |
| 11 |
} |
| 12 |
|
| 13 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 14 |
|
| 15 |
$dbh->{mock_add_resultset} = [ |
| 16 |
[ 'id', 'type', 'inventory_id' ], |
| 17 |
[ '1', 'european', '42' ], |
| 18 |
[ '27', 'african', '2' ], |
| 19 |
]; |
| 20 |
|
| 21 |
my $sth = $dbh->prepare( 'SELECT id, type, inventory_id FROM Swallow' ); |
| 22 |
|
| 23 |
$sth->execute(); |
| 24 |
|
| 25 |
{ |
| 26 |
my ($id, $type, $inventory_id); |
| 27 |
|
| 28 |
$sth->bind_col( 1, \$id ); |
| 29 |
$sth->bind_col( 2, \$type ); |
| 30 |
$sth->bind_col( 3, \$inventory_id ); |
| 31 |
|
| 32 |
ok( $sth->fetch(), 'fetch() returned data' ); |
| 33 |
is( $id, 1, 'bind_col to $id == 1' ); |
| 34 |
is( $type, 'european', 'bind_col to $type == "european"' ); |
| 35 |
is( $inventory_id, 42, 'bind_col to $inventory_id == 42' ); |
| 36 |
} |
| 37 |
|
| 38 |
{ |
| 39 |
my %hash; |
| 40 |
|
| 41 |
$sth->bind_columns( \( @hash{ qw( id type inventory_id ) } ) ); |
| 42 |
|
| 43 |
ok( $sth->fetch(), 'fetch() returned data' ); |
| 44 |
is( $hash{id}, 27, 'bind_columns with hash, id == 1' ); |
| 45 |
is( $hash{type}, 'african', 'bind_columns with hash, type == "african"' ); |
| 46 |
is( $hash{inventory_id}, 2, 'bind_columns with hash, inventory_id == 2' ); |
| 47 |
} |
| 48 |
|
| 49 |
{ |
| 50 |
ok( ! $sth->fetchrow_arrayref(), |
| 51 |
'fetchrow_arrayref returns false after data is exhausted, even with bound columns' ); |
| 52 |
} |
| 53 |
|
| 54 |
{ |
| 55 |
$dbh->{mock_clear_history} = 1; |
| 56 |
|
| 57 |
my @rows = |
| 58 |
( [ '1', 'european', '42' ], |
| 59 |
[ '27', 'african', '2' ], |
| 60 |
); |
| 61 |
|
| 62 |
$dbh->{mock_add_resultset} = [ |
| 63 |
[ 'id', 'type', 'inventory_id' ], |
| 64 |
@rows, |
| 65 |
]; |
| 66 |
|
| 67 |
my $results = $dbh->selectall_arrayref( 'SELECT id, type, inventory_id FROM Swallow' ); |
| 68 |
|
| 69 |
is_deeply( $results, |
| 70 |
\@rows, |
| 71 |
'bind_col implementation does not break selectall_* methods' ); |
| 72 |
} |
| 73 |
|
| 74 |
done_testing(); |