fix and regression test for RT #62642
[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 => 16;
14 # !!!
15
16 my $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   };
29
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()';
45
46   ok (! $schema->storage->connected, 'start disconnected');
47
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   );
53
54   $schema->storage->disconnect;
55 }
56
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   };
62
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   };
67
68
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()';
76
77   ok (! $schema->storage->connected, 'start disconnected');
78   $schema->storage->ensure_connected;
79   $schema->storage->disconnect; # this should not fire any tests
80 }
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');
93
94   $schema->storage->_determine_driver;  # this should connect due to the coderef
95
96   ok ($schema->storage->connected, 'determine driver connects');
97   $schema->storage->disconnect;
98 }