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