| 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 $swallow_sql = "SELECT id, type, inventory_id, species FROM birds WHERE species='swallow'"; |
| 14 |
my $items_sql = "SELECT id, name, weight FROM items"; |
| 15 |
my @resultList = |
| 16 |
( |
| 17 |
{ |
| 18 |
sql => $swallow_sql, |
| 19 |
results => [ |
| 20 |
[ 'id', 'type', 'inventory_id' ], |
| 21 |
[ '1', 'european', '42' ], |
| 22 |
[ '27', 'african', '2' ], |
| 23 |
], |
| 24 |
}, |
| 25 |
{ |
| 26 |
sql => $items_sql, |
| 27 |
results => [ |
| 28 |
[ 'id', 'name', 'weight' ], |
| 29 |
[ '2', 'coconuts', 'fairly hefty' ], |
| 30 |
[ '42', 'not coconuts', 'pretty light' ], |
| 31 |
], |
| 32 |
}, |
| 33 |
); |
| 34 |
|
| 35 |
my $coco_hash = { |
| 36 |
'id' => '2', |
| 37 |
'name' => 'coconuts', |
| 38 |
'weight' => 'fairly hefty', |
| 39 |
}; |
| 40 |
|
| 41 |
my $not_coco_hash = { |
| 42 |
'id' => '42', |
| 43 |
'name' => 'not coconuts', |
| 44 |
'weight' => 'pretty light', |
| 45 |
}; |
| 46 |
|
| 47 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ); |
| 48 |
|
| 49 |
{ |
| 50 |
my $res; |
| 51 |
|
| 52 |
foreach $res (@resultList) { |
| 53 |
$dbh->{mock_add_resultset} = $res; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
{ |
| 58 |
my $res; |
| 59 |
|
| 60 |
my @expected = ('1','27'); |
| 61 |
|
| 62 |
eval { |
| 63 |
$res = $dbh->selectcol_arrayref($swallow_sql); |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
isa_ok(\$res, "REF"); |
| 68 |
isa_ok($res, "ARRAY"); |
| 69 |
is_deeply($res, \@expected, "Checking if selectcol_arrayref works."); |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
my %expected = (1 => 'european', 27 => 'african'); |
| 74 |
|
| 75 |
my $res = eval { $dbh->selectcol_arrayref($swallow_sql, {Columns=>[1, 2]}) }; |
| 76 |
|
| 77 |
is_deeply( |
| 78 |
{ @{$res || []} }, \%expected, |
| 79 |
'Checking if selectcol_arrayref works with Columns attribute' |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
is_deeply( |
| 84 |
$dbh->selectall_hashref($items_sql, 'id', "Checking selectall_hashref with named key."), |
| 85 |
{ '2' => $coco_hash, |
| 86 |
'42' => $not_coco_hash, |
| 87 |
}, |
| 88 |
'... selectall_hashref with named key'); |
| 89 |
|
| 90 |
is_deeply( |
| 91 |
$dbh->selectall_hashref($items_sql, 2, "Checking selectall_hashref with numeric key."), |
| 92 |
{ 'coconuts' => $coco_hash, |
| 93 |
'not coconuts' => $not_coco_hash, |
| 94 |
}, |
| 95 |
'... selectall_hashref with numeric key'); |
| 96 |
|
| 97 |
is_deeply( |
| 98 |
$dbh->selectall_hashref($items_sql, ['id', 'name'], "Checking selectall_hashref with array of named keys."), |
| 99 |
{ 2 => { 'coconuts' => $coco_hash, }, |
| 100 |
42 => { 'not coconuts' => $not_coco_hash }, |
| 101 |
}, |
| 102 |
'... selectall_hashref with array of named keys'); |
| 103 |
|
| 104 |
is_deeply( |
| 105 |
$dbh->selectall_hashref($items_sql, [1, 2], "Checking selectall_hashref with array of numeric keys."), |
| 106 |
{ 2 => { 'coconuts' => $coco_hash, }, |
| 107 |
42 => { 'not coconuts' => $not_coco_hash }, |
| 108 |
}, |
| 109 |
'... selectall_hashref with array of numeric keys'); |
| 110 |
|
| 111 |
is_deeply( |
| 112 |
$dbh->selectall_hashref($items_sql, [], "Checking selectall_hashref with empty array of keys."), |
| 113 |
{ %{$not_coco_hash} }, |
| 114 |
'... selectall_hashref with empty array of keys'); |
| 115 |
|
| 116 |
done_testing(); |