| 1 |
use 5.008; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More; |
| 7 |
|
| 8 |
use DBD::Mock; |
| 9 |
use DBI; |
| 10 |
|
| 11 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 12 |
|
| 13 |
{ |
| 14 |
my $rows = [ |
| 15 |
[ '1', 'european', '42' ], |
| 16 |
[ '27', 'african', '2' ], |
| 17 |
]; |
| 18 |
|
| 19 |
$dbh->{mock_add_resultset} = { |
| 20 |
sql => 'SELECT id, type, inventory_id FROM Swallow', |
| 21 |
results => [ |
| 22 |
[ 'id', 'type', 'inventory_id' ], |
| 23 |
@{ $rows }, |
| 24 |
] |
| 25 |
}; |
| 26 |
|
| 27 |
my $results = $dbh->selectall_arrayref( 'SELECT id, type, inventory_id FROM Swallow' ); |
| 28 |
|
| 29 |
is_deeply( $results, $rows, 'SELECTALL_ARRAYREF ref by default returns the rows from the result set' ); |
| 30 |
|
| 31 |
|
| 32 |
my $expectedResults = [ |
| 33 |
{ |
| 34 |
id => 1, |
| 35 |
type => 'european', |
| 36 |
inventory_id => 42, |
| 37 |
|
| 38 |
}, { |
| 39 |
id => 27, |
| 40 |
type => 'african', |
| 41 |
inventory_id => 2, |
| 42 |
}, |
| 43 |
]; |
| 44 |
|
| 45 |
$results = $dbh->selectall_arrayref( 'SELECT id, type, inventory_id FROM Swallow', { Slice => {} } ); |
| 46 |
|
| 47 |
is_deeply( $results, $expectedResults, 'SELECTALL_ARRAYREF ref with a slice defined should return each row as a hashref' ); |
| 48 |
|
| 49 |
|
| 50 |
$results = $dbh->selectall_arrayref( 'SELECT id, type, inventory_id FROM Swallow', { Slice => { 'id' => 1 } } ); |
| 51 |
|
| 52 |
$expectedResults = [ |
| 53 |
{ |
| 54 |
id => 1, |
| 55 |
}, { |
| 56 |
id => 27, |
| 57 |
}, |
| 58 |
]; |
| 59 |
|
| 60 |
is_deeply( $results, $expectedResults, 'SELECTALL_ARRAYREF ref with a slice defining column names should return each row as a hashref which only contains those columns' ); |
| 61 |
|
| 62 |
|
| 63 |
$expectedResults = [ |
| 64 |
[ 'european', 42 ], |
| 65 |
[ 'african', 2], |
| 66 |
]; |
| 67 |
|
| 68 |
$results = $dbh->selectall_arrayref( 'SELECT id, type, inventory_id FROM Swallow', { Columns => [2,3] } ); |
| 69 |
|
| 70 |
is_deeply( $results, $expectedResults, 'SELECTALL_ARRAYREF ref with Columns defined should return just those columns' ); |
| 71 |
} |
| 72 |
|
| 73 |
done_testing(); |