DBIC now warns on explicit false AutoCommit, and when altering external $dbh's
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_call.t
1 use strict;
2 use warnings;
3 no warnings qw/once redefine/;
4
5 use lib qw(t/lib);
6 use DBI;
7 use DBICTest;
8 use DBICTest::Schema;
9 use DBIx::Class::Storage::DBI;
10
11 # !!! do not replace this with done_testing - tests reside in the callbacks
12 # !!! number of calls is important
13 use Test::More tests => 17;
14 # !!!
15 use Test::Warn;
16
17 my $schema = DBICTest::Schema->clone;
18
19 {
20   *DBIx::Class::Storage::DBI::connect_call_foo = sub {
21     isa_ok $_[0], 'DBIx::Class::Storage::DBI',
22       'got storage in connect_call method';
23     is $_[1], 'bar', 'got param in connect_call method';
24   };
25
26   *DBIx::Class::Storage::DBI::disconnect_call_foo = sub {
27     isa_ok $_[0], 'DBIx::Class::Storage::DBI',
28       'got storage in disconnect_call method';
29   };
30
31   ok $schema->connection(
32       DBICTest->_database,
33     {
34       on_connect_call => [
35           [ do_sql => 'create table test1 (id integer)' ],
36           [ do_sql => [ 'insert into test1 values (?)', {}, 1 ] ],
37           [ do_sql => sub { ['insert into test1 values (2)'] } ],
38           [ sub { $_[0]->dbh->do($_[1]) }, 'insert into test1 values (3)' ],
39           # this invokes $storage->connect_call_foo('bar') (above)
40           [ foo => 'bar' ],
41       ],
42       on_connect_do => 'insert into test1 values (4)',
43       on_disconnect_call => 'foo',
44     },
45   ), 'connection()';
46
47   ok (! $schema->storage->connected, 'start disconnected');
48
49   is_deeply (
50     $schema->storage->dbh->selectall_arrayref('select * from test1'),
51     [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ],
52     'on_connect_call/do actions worked'
53   );
54
55   $schema->storage->disconnect;
56 }
57
58 {
59   *DBIx::Class::Storage::DBI::connect_call_foo = sub {
60     isa_ok $_[0], 'DBIx::Class::Storage::DBI',
61       'got storage in connect_call method';
62   };
63
64   *DBIx::Class::Storage::DBI::connect_call_bar = sub {
65     isa_ok $_[0], 'DBIx::Class::Storage::DBI',
66       'got storage in connect_call method';
67   };
68
69
70   ok $schema->connection(
71     DBICTest->_database,
72     {
73       # method list form
74       on_connect_call => [ 'foo', sub { ok 1, "coderef in list form" }, 'bar' ],
75     },
76   ), 'connection()';
77
78   ok (! $schema->storage->connected, 'start disconnected');
79   $schema->storage->ensure_connected;
80   $schema->storage->disconnect; # this should not fire any tests
81 }
82
83 {
84   ok $schema->connection(
85     sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 } ) },
86     {
87       # method list form
88       on_connect_call => [ sub { ok 1, "on_connect_call after DT parser" }, ],
89       on_disconnect_call => [ sub { ok 1, "on_disconnect_call after DT parser" }, ],
90     },
91   ), 'connection()';
92
93   ok (! $schema->storage->connected, 'start disconnected');
94
95   # this should connect due to the coderef, and also warn due to the false autocommit above
96   warnings_exist {
97     $schema->storage->_determine_driver
98   } qr/The 'RaiseError' of the externally supplied DBI handle is set to false/, 'Warning on clobbered AutoCommit => 0 fired';
99
100   ok ($schema->storage->connected, 'determine driver connects');
101   $schema->storage->disconnect;
102 }