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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
use 5.008;
use strict;
use warnings;
use Test::More;
BEGIN {
use_ok('DBD::Mock');
use_ok('DBI');
}
my $swallow_sql = "SELECT id, type, inventory_id, species FROM birds WHERE species='swallow'";
my $items_sql = "SELECT id, name, weight FROM items";
my @resultList =
(
{
sql => $swallow_sql,
results => [
[ 'id', 'type', 'inventory_id' ],
[ '1', 'european', '42' ],
[ '27', 'african', '2' ],
],
},
{
sql => $items_sql,
results => [
[ 'id', 'name', 'weight' ],
[ '2', 'coconuts', 'fairly hefty' ],
[ '42', 'not coconuts', 'pretty light' ],
],
},
);
my $coco_hash = {
'id' => '2',
'name' => 'coconuts',
'weight' => 'fairly hefty',
};
my $not_coco_hash = {
'id' => '42',
'name' => 'not coconuts',
'weight' => 'pretty light',
};
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
{
my $res;
foreach $res (@resultList) {
$dbh->{mock_add_resultset} = $res;
}
}
{
my $res;
my @expected = ('1','27');
eval {
$res = $dbh->selectcol_arrayref($swallow_sql);
};
isa_ok(\$res, "REF");
isa_ok($res, "ARRAY");
is_deeply($res, \@expected, "Checking if selectcol_arrayref works.");
}
{
my %expected = (1 => 'european', 27 => 'african');
my $res = eval { $dbh->selectcol_arrayref($swallow_sql, {Columns=>[1, 2]}) };
is_deeply(
{ @{$res || []} }, \%expected,
'Checking if selectcol_arrayref works with Columns attribute'
);
}
is_deeply(
$dbh->selectall_hashref($items_sql, 'id', "Checking selectall_hashref with named key."),
{ '2' => $coco_hash,
'42' => $not_coco_hash,
},
'... selectall_hashref with named key');
is_deeply(
$dbh->selectall_hashref($items_sql, 2, "Checking selectall_hashref with numeric key."),
{ 'coconuts' => $coco_hash,
'not coconuts' => $not_coco_hash,
},
'... selectall_hashref with numeric key');
is_deeply(
$dbh->selectall_hashref($items_sql, ['id', 'name'], "Checking selectall_hashref with array of named keys."),
{ 2 => { 'coconuts' => $coco_hash, },
42 => { 'not coconuts' => $not_coco_hash },
},
'... selectall_hashref with array of named keys');
is_deeply(
$dbh->selectall_hashref($items_sql, [1, 2], "Checking selectall_hashref with array of numeric keys."),
{ 2 => { 'coconuts' => $coco_hash, },
42 => { 'not coconuts' => $not_coco_hash },
},
'... selectall_hashref with array of numeric keys');
is_deeply(
$dbh->selectall_hashref($items_sql, [], "Checking selectall_hashref with empty array of keys."),
{ %{$not_coco_hash} },
'... selectall_hashref with empty array of keys');
done_testing();
|