File: 024_selcol_fetchhash.t

package info (click to toggle)
libdbd-mock-perl 1.43-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 416 kB
  • sloc: perl: 1,135; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download | duplicates (5)
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
use 5.006;

use strict;
use warnings;

use Test::More tests => 8;

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 worked correctly');

is_deeply(
	  $dbh->selectall_hashref($items_sql, 1, "Checking selectall_hashref with named key."), 
	  { 'coconuts' => $coco_hash,
	    'not coconuts' => $not_coco_hash,
	  },
	  '... selectall_hashref worked correctly');