Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_do.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 }
7
8 # !!! do not replace this with done_testing - tests reside in the callbacks
9 # !!! number of calls is important
10 use Test::More tests => 13;
11 # !!!
12 use Test::Warn;
13 use Test::Exception;
14
15
16 use DBICTest;
17 require DBI;
18
19
20 my $schema = DBICTest->init_schema(
21     no_connect  => 1,
22     no_deploy   => 1,
23 );
24
25 ok $schema->connection(
26   DBICTest->_database,
27   {
28     on_connect_do => 'CREATE TABLE TEST_empty (id INTEGER)',
29   },
30 ), 'connection()';
31
32 is_deeply (
33   $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
34   [],
35   'string version on_connect_do() worked'
36 );
37
38 $schema->storage->disconnect;
39
40 ok $schema->connection(
41     sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 }) },
42     {
43         # DO NOT REMOVE - this seems like an unrelated piece of info,
44         # but is in fact a test for a bug where setting an accessor-via-option
45         # would trigger an early connect *bypassing* the on_connect_* pieces
46         cursor_class => 'DBIx::Class::Storage::Cursor',
47
48         on_connect_do       => [
49             'CREATE TABLE TEST_empty (id INTEGER)',
50             [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
51             \&insert_from_subref,
52         ],
53         on_disconnect_do    =>
54             [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
55     },
56 ), 'connection()';
57
58 warnings_exist {
59   $schema->storage->ensure_connected
60 } qr/The 'RaiseError' of the externally supplied DBI handle is set to false/,
61 'Warning on clobbered AutoCommit => 0 fired';
62
63 is_deeply (
64   $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
65   [ [ 2 ], [ 3 ], [ 7 ] ],
66   'on_connect_do() worked'
67 );
68 dies_ok {
69   $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent');
70 } 'Searching for nonexistent table dies';
71
72 $schema->storage->disconnect();
73
74 my($connected, $disconnected, @cb_args);
75 ok $schema->connection(
76     DBICTest->_database,
77     {
78         on_connect_do       => sub { $connected = 1; @cb_args = @_; },
79         on_disconnect_do    => sub { $disconnected = 1 },
80     },
81 ), 'second connection()';
82 $schema->storage->dbh->do('SELECT 1');
83 ok $connected, 'on_connect_do() called after connect()';
84 ok ! $disconnected, 'on_disconnect_do() not called after connect()';
85 $schema->storage->disconnect();
86 ok $disconnected, 'on_disconnect_do() called after disconnect()';
87
88 isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook');
89 @cb_args = ();
90
91 sub check_exists {
92     my $storage = shift;
93     ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
94     return;
95 }
96
97 sub check_dropped {
98     my $storage = shift;
99
100     dies_ok {
101       $storage->dbh->do('SELECT 1 FROM TEST_empty');
102     } 'Reading from dropped table fails';
103     return;
104 }
105
106 sub insert_from_subref {
107     my $storage = shift;
108     return [
109         [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ],
110         'INSERT INTO TEST_empty VALUES (7)',
111     ];
112 }