Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_do.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
579ca3f7 3use strict;
4use warnings;
5
8b60b921 6BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 }
7
f3d405dc 8# !!! do not replace this with done_testing - tests reside in the callbacks
9# !!! number of calls is important
6c925c72 10use Test::More tests => 13;
f3d405dc 11# !!!
6c925c72 12use Test::Warn;
f3d405dc 13use Test::Exception;
579ca3f7 14
c0329273 15
8d6b1478 16use DBICTest;
7c9aa29f 17require DBI;
579ca3f7 18
19
20my $schema = DBICTest->init_schema(
21 no_connect => 1,
22 no_deploy => 1,
23);
00f57441 24
25ok $schema->connection(
26 DBICTest->_database,
27 {
28 on_connect_do => 'CREATE TABLE TEST_empty (id INTEGER)',
29 },
30), 'connection()';
31
32is_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
579ca3f7 40ok $schema->connection(
6c925c72 41 sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 }) },
579ca3f7 42 {
1eb87dd7 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
1bd1640b 48 on_connect_do => [
49 'CREATE TABLE TEST_empty (id INTEGER)',
50 [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
1dafdb2a 51 \&insert_from_subref,
1bd1640b 52 ],
579ca3f7 53 on_disconnect_do =>
54 [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
55 },
56), 'connection()';
57
6c925c72 58warnings_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
00f57441 63is_deeply (
1bd1640b 64 $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
1dafdb2a 65 [ [ 2 ], [ 3 ], [ 7 ] ],
00f57441 66 'on_connect_do() worked'
67);
f3d405dc 68dies_ok {
69 $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent');
70} 'Searching for nonexistent table dies';
579ca3f7 71
72$schema->storage->disconnect();
73
abe38018 74my($connected, $disconnected, @cb_args);
6d2e7a96 75ok $schema->connection(
76 DBICTest->_database,
77 {
abe38018 78 on_connect_do => sub { $connected = 1; @cb_args = @_; },
6d2e7a96 79 on_disconnect_do => sub { $disconnected = 1 },
80 },
81), 'second connection()';
82$schema->storage->dbh->do('SELECT 1');
83ok $connected, 'on_connect_do() called after connect()';
84ok ! $disconnected, 'on_disconnect_do() not called after connect()';
85$schema->storage->disconnect();
86ok $disconnected, 'on_disconnect_do() called after disconnect()';
87
abe38018 88isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook');
6892eb09 89@cb_args = ();
6d2e7a96 90
579ca3f7 91sub check_exists {
92 my $storage = shift;
93 ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
1dafdb2a 94 return;
579ca3f7 95}
96
97sub check_dropped {
98 my $storage = shift;
f3d405dc 99
100 dies_ok {
101 $storage->dbh->do('SELECT 1 FROM TEST_empty');
102 } 'Reading from dropped table fails';
1dafdb2a 103 return;
104}
105
106sub insert_from_subref {
107 my $storage = shift;
108 return [
109 [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ],
110 'INSERT INTO TEST_empty VALUES (7)',
111 ];
579ca3f7 112}