Debian Codegraph @

t/034_custom_attributes.t

raw
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
14 {
15 my $dbh = DBI->connect('dbi:Mock:', '', '');
16 isa_ok($dbh, 'DBI::db');
17
18 $dbh->{mock_add_resultset} = {
19 sql => 'SELECT baz FROM qux',
20 prepare_attributes => {
21 foo => 'bar'
22 },
23 results => [[ 'baz' ], [ 10 ]]
24 };
25
26 my $sth = $dbh->prepare('SELECT baz FROM qux');
27 isa_ok($sth, 'DBI::st');
28
29 is( $sth->{foo}, 'bar', "our custom prepare_attribute should be set after the prepare" );
30
31 my $rows = $sth->execute();
32 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
33
34 is( $sth->{foo}, 'bar', "our custom prepare_attribute should persist after the execute if nothing's changed it" );
35 }
36
37 {
38 my $dbh = DBI->connect('dbi:Mock:', '', '');
39 isa_ok($dbh, 'DBI::db');
40
41 $dbh->{mock_add_resultset} = {
42 sql => 'SELECT baz FROM qux',
43 execute_attributes => {
44 foo => 'bar'
45 },
46 results => [[ 'baz' ], [ 10 ]]
47 };
48
49 my $sth = $dbh->prepare('SELECT baz FROM qux');
50 isa_ok($sth, 'DBI::st');
51
52 is( $sth->{foo}, undef, "our custom execute_attribute should be undefined until we execute the statement" );
53
54 my $rows = $sth->execute();
55 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
56
57 is( $sth->{foo}, 'bar', "our custom execute_attribute should be present after the statements executed" );
58 }
59
60 {
61 my $dbh = DBI->connect('dbi:Mock:', '', '');
62 isa_ok($dbh, 'DBI::db');
63
64 $dbh->{mock_add_resultset} = {
65 sql => 'SELECT baz FROM qux',
66 execute_attributes => {
67 foo => 'should be overwritten',
68 },
69 callback => sub {
70 my @bound_params = @_;
71
72 my %result = (
73 fields => [ 'baz'],
74 rows => [],
75 last_insert_id => 99,
76 execute_attributes => {
77 foo => 'bar'
78 },
79 );
80
81 return %result;
82 },
83 results => [[ 'baz' ], [ 10 ]]
84 };
85
86 my $sth = $dbh->prepare('SELECT baz FROM qux');
87 isa_ok($sth, 'DBI::st');
88
89 is( $sth->{foo}, undef, "our custom execute_attribute should be undefined until we execute the statement" );
90
91 my $rows = $sth->execute();
92 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
93
94 is( $sth->{foo}, 'bar', "our custom execute_attribute should be present after the statements executed" );
95 }
96
97
98 {
99 my $dbh = DBI->connect('dbi:Mock:', '', '');
100 isa_ok($dbh, 'DBI::db');
101
102 $dbh->{mock_add_resultset} = {
103 sql => 'SELECT baz FROM qux',
104 prepare_attributes => {
105 foo => 'prepare_bar',
106 qux => 'prepare_quz',
107 },
108 execute_attributes => {
109 foo => 'execute_bar'
110 },
111 results => [[ 'baz' ], [ 10 ]]
112 };
113
114 my $sth = $dbh->prepare('SELECT baz FROM qux');
115 isa_ok($sth, 'DBI::st');
116
117 is( $sth->{foo}, 'prepare_bar', "our custom prepare_attribute should be present after the statement has been defined" );
118 is( $sth->{qux}, 'prepare_quz', "our custom prepare_attribute should be present after the statement has been defined" );
119
120 my $rows = $sth->execute();
121 is($rows, '0E0', '... got back 0E0 for rows with a SELECT statement');
122
123 is( $sth->{foo}, 'execute_bar', "our custom execute_attribute should take precedence over the the prepare one after the statements executed" );
124 is( $sth->{qux}, 'prepare_quz', "our custom prepare_attribute should still be present after the statement has been executed if there's no matching execute_attributes entry" );
125 }
126
127 done_testing();