fix and regression test for RT #62642
[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
746c315f 13use Test::More tests => 16;
f602cd7b 14# !!!
5fc1107f 15
f602cd7b 16my $schema = DBICTest::Schema->clone;
17
18{
19 *DBIx::Class::Storage::DBI::connect_call_foo = sub {
20 isa_ok $_[0], 'DBIx::Class::Storage::DBI',
21 'got storage in connect_call method';
22 is $_[1], 'bar', 'got param in connect_call method';
23 };
24
25 *DBIx::Class::Storage::DBI::disconnect_call_foo = sub {
26 isa_ok $_[0], 'DBIx::Class::Storage::DBI',
27 'got storage in disconnect_call method';
28 };
5fc1107f 29
f602cd7b 30 ok $schema->connection(
31 DBICTest->_database,
32 {
33 on_connect_call => [
34 [ do_sql => 'create table test1 (id integer)' ],
35 [ do_sql => [ 'insert into test1 values (?)', {}, 1 ] ],
36 [ do_sql => sub { ['insert into test1 values (2)'] } ],
37 [ sub { $_[0]->dbh->do($_[1]) }, 'insert into test1 values (3)' ],
38 # this invokes $storage->connect_call_foo('bar') (above)
39 [ foo => 'bar' ],
40 ],
41 on_connect_do => 'insert into test1 values (4)',
42 on_disconnect_call => 'foo',
43 },
44 ), 'connection()';
5fc1107f 45
f602cd7b 46 ok (! $schema->storage->connected, 'start disconnected');
5fc1107f 47
f602cd7b 48 is_deeply (
49 $schema->storage->dbh->selectall_arrayref('select * from test1'),
50 [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ],
51 'on_connect_call/do actions worked'
52 );
5fc1107f 53
f602cd7b 54 $schema->storage->disconnect;
55}
5fc1107f 56
f602cd7b 57{
58 *DBIx::Class::Storage::DBI::connect_call_foo = sub {
59 isa_ok $_[0], 'DBIx::Class::Storage::DBI',
60 'got storage in connect_call method';
61 };
5fc1107f 62
f602cd7b 63 *DBIx::Class::Storage::DBI::connect_call_bar = sub {
64 isa_ok $_[0], 'DBIx::Class::Storage::DBI',
65 'got storage in connect_call method';
66 };
5fc1107f 67
5fc1107f 68
f602cd7b 69 ok $schema->connection(
70 DBICTest->_database,
71 {
72 # method list form
73 on_connect_call => [ 'foo', sub { ok 1, "coderef in list form" }, 'bar' ],
74 },
75 ), 'connection()';
5fc1107f 76
f602cd7b 77 ok (! $schema->storage->connected, 'start disconnected');
78 $schema->storage->ensure_connected;
79 $schema->storage->disconnect; # this should not fire any tests
80}
46bb50b7 81
82{
83 ok $schema->connection(
84 sub { DBI->connect(DBICTest->_database) },
85 {
86 # method list form
87 on_connect_call => [ sub { ok 1, "on_connect_call after DT parser" }, ],
88 on_disconnect_call => [ sub { ok 1, "on_disconnect_call after DT parser" }, ],
89 },
90 ), 'connection()';
91
92 ok (! $schema->storage->connected, 'start disconnected');
46bb50b7 93
746c315f 94 $schema->storage->_determine_driver; # this should connect due to the coderef
95
96 ok ($schema->storage->connected, 'determine driver connects');
46bb50b7 97 $schema->storage->disconnect;
98}