File: 022_DBD_Mock_Session_bound_params.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 (194 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (4)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use strict;

use Test::More tests => 29;

BEGIN {
    use_ok('DBD::Mock');
    use_ok('DBI');
}

{
    my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
    isa_ok($dbh, 'DBI::db');
    
    my $session = DBD::Mock::Session->new((
        {
            statement    => 'SELECT foo FROM bar WHERE baz = ?',
            bound_params => [ 100 ],
            results      => [[ 'foo' ], [ 10 ]]
        },
        {
            statement    => 'SELECT bar FROM foo WHERE baz = ?',
            bound_params => [ 125 ],
            results      => [[ 'bar' ], [ 15 ]]
        },
    ));
    isa_ok($session, 'DBD::Mock::Session');
    
    $dbh->{mock_session} = $session;
    
    eval {
        my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
        $sth->execute(100);
        my ($result) = $sth->fetchrow_array();
        is($result, 10, '... got the right value');        
    };
    ok(!$@, '... everything worked as planned');
    
    eval {
        my $sth = $dbh->prepare('SELECT bar FROM foo WHERE baz = ?');
        $sth->execute(125);
        my ($result) = $sth->fetchrow_array();
        is($result, 15, '... got the right value');
    };
    ok(!$@, '... everything worked as planned');

    # Shuts up warning when object is destroyed
    undef $dbh->{mock_session};
}

{
    my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
    isa_ok($dbh, 'DBI::db');
    
    my $session = DBD::Mock::Session->new((
        {
            statement    => 'SELECT foo FROM bar WHERE baz = ?',
            bound_params => [ 100 ],
            results      => [[ 'foo' ], [ 10 ]]
        },
        {
            statement => 'SELECT bar FROM foo WHERE baz = 125',
            results   => [[ 'bar' ], [ 15 ]]
        },        
        {
            statement    => 'DELETE FROM bar WHERE baz = ?',
            results      => [[], [], []],
            bound_params => [ 100 ]            
        }
    ));
    isa_ok($session, 'DBD::Mock::Session');
    
    $dbh->{mock_session} = $session;
    
    eval {
        my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
        $sth->execute(100);
        my ($result) = $sth->fetchrow_array();
        is($result, 10, '... got the right value');        
    };
    ok(!$@, '... first state worked as planned');
    
    eval {
        my $sth = $dbh->prepare('SELECT bar FROM foo WHERE baz = 125');
        $sth->execute();
        my ($result) = $sth->fetchrow_array();
        is($result, 15, '... got the right value');
    };
    ok(!$@, '... second state worked as planned');
        
    eval {
        my $sth = $dbh->prepare('DELETE FROM bar WHERE baz = ?');
        $sth->execute(100);
        is($sth->rows(), 2, '... got the right number of affected rows');
    };
    ok(!$@, '... third state worked as planned');

    # Shuts up warning when object is destroyed
    undef $dbh->{mock_session};
}

# check some errors

{
    my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
    isa_ok($dbh, 'DBI::db');
    
    my $session = DBD::Mock::Session->new((
        {
            statement    => 'SELECT foo FROM bar WHERE baz = ?',
            bound_params => [ 100 ],
            results      => [[ 'foo' ], [ 10 ]]
        }
    ));
    isa_ok($session, 'DBD::Mock::Session');
    
    $dbh->{mock_session} = $session;
    
    eval {
        my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
        $sth->execute(100, 200);
        my ($result) = $sth->fetchrow_array();
    };
    ok($@, '... everything failed as planned');
    like($@, 
        qr/Session Error\: Not the same number of bound params in current state in DBD\:\:Mock\:\:Session/, 
        '... everything failed as planned');    

    # Shuts up warning when object is destroyed
    undef $dbh->{mock_session};
}

{
    my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
    isa_ok($dbh, 'DBI::db');
    
    my $session = DBD::Mock::Session->new((
        {
            statement    => 'SELECT foo FROM bar WHERE baz = ?',
            bound_params => [ 100 ],
            results      => [[ 'foo' ], [ 10 ]]
        }
    ));
    isa_ok($session, 'DBD::Mock::Session');
    
    $dbh->{mock_session} = $session;
    
    eval {
        my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?');
        $sth->execute(200);
        my ($result) = $sth->fetchrow_array();
    };
    ok($@, '... everything failed as planned');
    like($@, 
        qr/Session Error\: Bound param 0 do not match in current state in DBD\:\:Mock\:\:Session/, 
        '... everything failed as planned');    

    # Shuts up warning when object is destroyed
    undef $dbh->{mock_session};
}

{ 
    my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1,  PrintError => 0 }); 
    isa_ok($dbh, 'DBI::db'); 
 
    my $session = DBD::Mock::Session->new(( 
        { 
            statement    => 'SELECT foo FROM bar WHERE baz = ?', 
            bound_params => [ 100 ], 
            results      => [[ 'foo' ], [ 10 ]] 
        }, 
        { 
            statement    => 'SELECT foo FROM bar WHERE baz = ?', 
            bound_params => [ 125 ], 
            results      => [[ 'foo' ], [ 15 ]] 
        }, 
    )); 
    isa_ok($session, 'DBD::Mock::Session'); 
 
    $dbh->{mock_session} = $session; 
 
    eval { 
        my $sth = $dbh->prepare('SELECT foo FROM bar WHERE baz = ?'); 
        $sth->execute(100); 
        my ($result) = $sth->fetchrow_array(); 
        is($result, 10, '... first execute got the right  value'); 
        $sth->execute(125); 
        ($result) = $sth->fetchrow_array(); 
        is($result, 15, '... second execute got the right value'); 
    }; 
    ok(!$@, '... everything worked as planned'); 

    # Shuts up warning when object is destroyed
    undef $dbh->{mock_session};
}