Debian Codegraph @

t/013_st_execute_bound_params.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use DBI qw( :sql_types );
8
9 BEGIN {
10 use_ok('DBD::Mock');
11 }
12
13 my $sql = 'SELECT * FROM foo WHERE bar = ? AND baz = ?';
14
15 {
16 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
17 my $sth = eval { $dbh->prepare( $sql ) };
18 eval {
19 $sth->bind_param( 2, 'bar' );
20 $sth->bind_param( 1, 'baz' );
21 };
22 ok( ! $@, 'Parameters bound to statement handle with bind_param()' );
23
24 eval { $sth->execute() };
25
26 ok( ! $@, 'Called execute() ok (empty, after bind_param calls)' );
27
28 my $t_params = $sth->{mock_my_history}->bound_params;
29 is( scalar @{ $t_params }, 2,
30 'Correct number of parameters bound (method on tracker)' );
31 is( $t_params->[0], 'baz',
32 'Statement handle stored bound parameter from bind_param() (method on tracker)' );
33 is( $t_params->[1], 'bar',
34 'Statement handle stored bound parameter from bind_param() (method on tracker)' );
35
36 my $param_attrs = $sth->{mock_my_history}->bound_param_attrs;
37
38 is( scalar @{ $param_attrs }, 2,
39 'bound_param_types length should match the number of bound parameters' );
40
41 is( $param_attrs->[0], undef,
42 "as we didn't specify any attributes/types for the first bound parameter then it should be undefined");
43 is( $param_attrs->[1], undef,
44 "as we didn't specify any attributes/types for the second bound parameter then it should be undefined");
45
46 my $a_params = $sth->{mock_params};
47 is( scalar @{ $a_params }, 2, 'Correct number of parameters bound (attribute)' );
48 is( $a_params->[0], 'baz',
49 'Statement handle stored bound parameter from bind_param() (attribute)' );
50 is( $a_params->[1], 'bar',
51 'Statement handle stored bound parameter from bind_param() (attribute)' );
52
53 my $a_param_attrs = $sth->{mock_param_attrs};
54
55 is( scalar @{ $a_param_attrs }, 2,
56 'bound_param_types length should match the number of bound parameters' );
57
58 is( $a_param_attrs->[0], undef,
59 "as we didn't specify any attributes/types for the first bound parameter then it should be undefined");
60 is( $a_param_attrs->[1], undef,
61 "as we didn't specify any attributes/types for the second bound parameter then it should be undefined");
62 }
63
64 {
65 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
66 my $sth = eval { $dbh->prepare( $sql ) };
67 eval {
68 $sth->bind_param( 2, 'bar', SQL_VARCHAR );
69 $sth->bind_param( 1, 'baz', { TYPE => SQL_VARCHAR } );
70 };
71 ok( ! $@, 'Parameters bound to statement handle with bind_param()' );
72 eval { $sth->execute() };
73 ok( ! $@, 'Called execute() ok (empty, after bind_param calls)' );
74
75 my $t_params = $sth->{mock_my_history}->bound_params;
76 is( scalar @{ $t_params }, 2,
77 'Correct number of parameters bound (method on tracker)' );
78 is( $t_params->[0], 'baz',
79 'Statement handle stored bound parameter from bind_param() (method on tracker)' );
80 is( $t_params->[1], 'bar',
81 'Statement handle stored bound parameter from bind_param() (method on tracker)' );
82
83 my $param_attrs = $sth->{mock_my_history}->bound_param_attrs;
84
85 is( scalar @{ $param_attrs }, 2,
86 'bound_param_types length should match the number of bound parameters' );
87
88 is_deeply( $param_attrs->[0], { TYPE => SQL_VARCHAR },
89 "the second bound parameter attribute should match our hashref");
90 is( $param_attrs->[1], SQL_VARCHAR,
91 "the first bound parameter attribute should match what we bound");
92
93 my $a_params = $sth->{mock_params};
94 is( scalar @{ $a_params }, 2, 'Correct number of parameters bound (attribute)' );
95 is( $a_params->[0], 'baz',
96 'Statement handle stored bound parameter from bind_param() (attribute)' );
97 is( $a_params->[1], 'bar',
98 'Statement handle stored bound parameter from bind_param() (attribute)' );
99
100 my $a_param_attrs = $sth->{mock_param_attrs};
101
102 is( scalar @{ $a_param_attrs }, 2,
103 'bound_param_types length should match the number of bound parameters' );
104
105 is_deeply( $a_param_attrs->[0], { TYPE => SQL_VARCHAR },
106 "the second bound parameter attribute should match our hashref");
107 is( $a_param_attrs->[1], SQL_VARCHAR,
108 "the first bound parameter attribute should match what we bound");
109 }
110
111 {
112 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
113 my $sth = eval { $dbh->prepare( $sql ) };
114 eval { $sth->execute( 'baz', 'bar' ) };
115 ok( ! $@, 'Called execute() ok (empty, after bind_param calls)' );
116
117 my $t_params = $sth->{mock_my_history}->bound_params;
118 is( scalar @{ $t_params }, 2,
119 'Correct number of parameters bound (method on tracker)' );
120 is( $t_params->[0], 'baz',
121 'Statement handle stored bound parameter from bind_param() (method on tracker)' );
122 is( $t_params->[1], 'bar',
123 'Statement handle stored bound parameter from bind_param() (method on tracker)' );
124
125 my $param_attrs = $sth->{mock_my_history}->bound_param_attrs;
126
127 is( scalar @{ $param_attrs }, 2,
128 'bound_param_types length should match the number of bound parameters' );
129
130 is( $param_attrs->[0], undef,
131 "the first bound parameter attribute should be undef as the value was bound in the execute() call");
132 is( $param_attrs->[1], undef,
133 "the second bound parameter attribute should be undef as the value was bound in the execute() call");
134
135 my $a_params = $sth->{mock_params};
136 is( scalar @{ $a_params }, 2, 'Correct number of parameters bound (attribute)' );
137 is( $a_params->[0], 'baz',
138 'Statement handle stored bound parameter from bind_param() (attribute)' );
139 is( $a_params->[1], 'bar',
140 'Statement handle stored bound parameter from bind_param() (attribute)' );
141
142 my $a_param_attrs = $sth->{mock_param_attrs};
143
144 is( scalar @{ $a_param_attrs }, 2,
145 'bound_param_types length should match the number of bound parameters' );
146
147 is( $a_param_attrs->[0], undef,
148 "the first bound parameter attribute should be undef as the value was bound in the execute() call");
149 is( $a_param_attrs->[1], undef,
150 "the second bound parameter attribute should be undef as the value was bound in the execute() call");
151 }
152 {
153 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
154 my $sth = eval { $dbh->prepare( 'begin dbms_output.get_line(?,?); end;' ) };
155 my ($bar, $baz) = ('bar', 'baz');
156 eval {
157 $sth->bind_param_inout( 2, \$bar, 10 );
158 $sth->bind_param_inout( 1, \$baz, 100 );
159 };
160 diag $@ if $@;
161 ok(!$@, 'Parameters bound to statement handle with bind_param_inout()' );
162 eval { $sth->execute() };
163 ok( ! $@, 'Called execute() ok (empty, after bind_param_inout calls)' );
164 my $t_params = $sth->{mock_my_history}->bound_params;
165 is( scalar @{ $t_params }, 2, 'Correct number of parameters bound (method on tracker)' );
166 is( $t_params->[0], \$baz,
167 'Statement handle stored bound parameter from bind_param_inout() (method on tracker)' );
168 is( $t_params->[1], \$bar,
169 'Statement handle stored bound parameter from bind_param_inout() (method on tracker)' );
170 my $a_params = $sth->{mock_params};
171 is( scalar @{ $a_params }, 2,
172 'Correct number of parameters bound (attribute)' );
173 is( $a_params->[0], \$baz,
174 'Statement handle stored bound parameter from bind_param_inout() (attribute)' );
175 is( $a_params->[1], \$bar,
176 'Statement handle stored bound parameter from bind_param_inout() (attribute)' );
177 }
178
179 {
180 my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
181 my $sth = eval { $dbh->prepare( $sql ) };
182 eval {
183 $sth->bind_param( 2, 'bar' );
184 $sth->bind_param( 1, 'baz', SQL_VARCHAR );
185 $sth->execute();
186 };
187 ok( ! $@, 'Parameters bound to statement handle with bind_param() and executed' );
188
189 eval {
190 $sth->bind_param( 2, 'foo', { TYPE => SQL_VARCHAR } );
191 $sth->bind_param( 1, 'qux' );
192 $sth->execute();
193 };
194 ok( ! $@, 'Parameters bound to statement handle with bind_param() and executed' );
195
196 my $executionHistory = $sth->{mock_execution_history};
197 is_deeply(
198 $executionHistory,
199 [
200 {
201 params => [ 'baz', 'bar' ],
202 attrs => [ SQL_VARCHAR, undef ],
203 }, {
204 params => [ 'qux', 'foo' ],
205 attrs => [ undef, { TYPE => SQL_VARCHAR } ],
206 }
207 ],
208 "mock_execution_history should list the parameters and their attributes for each execution"
209 );
210 }
211
212 done_testing();