Move find_co_root into DBICTest::Util
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_call.t
CommitLineData
5fc1107f 1use strict;
2use warnings;
3no warnings qw/once redefine/;
4
5use lib qw(t/lib);
f602cd7b 6use DBI;
9900b569 7use DBICTest;
f602cd7b 8use DBICTest::Schema;
9use DBIx::Class::Storage::DBI;
5fc1107f 10
f602cd7b 11# !!! do not replace this with done_testing - tests reside in the callbacks
12# !!! number of calls is important
6c925c72 13use Test::More tests => 17;
f602cd7b 14# !!!
6c925c72 15use Test::Warn;
5fc1107f 16
f602cd7b 17my $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 };
5fc1107f 30
f602cd7b 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()';
5fc1107f 46
f602cd7b 47 ok (! $schema->storage->connected, 'start disconnected');
5fc1107f 48
f602cd7b 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 );
5fc1107f 54
f602cd7b 55 $schema->storage->disconnect;
56}
5fc1107f 57
f602cd7b 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 };
5fc1107f 63
f602cd7b 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 };
5fc1107f 68
5fc1107f 69
f602cd7b 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()';
5fc1107f 77
f602cd7b 78 ok (! $schema->storage->connected, 'start disconnected');
79 $schema->storage->ensure_connected;
80 $schema->storage->disconnect; # this should not fire any tests
81}
46bb50b7 82
83{
84 ok $schema->connection(
6c925c72 85 sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 } ) },
46bb50b7 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');
46bb50b7 94
6c925c72 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';
746c315f 99
100 ok ($schema->storage->connected, 'determine driver connects');
46bb50b7 101 $schema->storage->disconnect;
102}