Let on_connect_do() and on_disconnect_do() take code references.
[dbsrgits/DBIx-Class.git] / t / 92storage_on_connect_do.t
CommitLineData
579ca3f7 1use strict;
2use warnings;
3
6d2e7a96 4use Test::More tests => 9;
579ca3f7 5
6use lib qw(t/lib);
7use base 'DBICTest';
8
9
10my $schema = DBICTest->init_schema(
11 no_connect => 1,
12 no_deploy => 1,
13);
14ok $schema->connection(
15 DBICTest->_database,
16 {
17 on_connect_do => ['CREATE TABLE TEST_empty (id INTEGER)'],
18 on_disconnect_do =>
19 [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
20 },
21), 'connection()';
22
23ok $schema->storage->dbh->do('SELECT 1 FROM TEST_empty'), 'on_connect_do() worked';
24eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); };
25ok $@, 'Searching for nonexistent table dies';
26
27$schema->storage->disconnect();
28
6d2e7a96 29my($connected, $disconnected);
30ok $schema->connection(
31 DBICTest->_database,
32 {
33 on_connect_do => sub { $connected = 1 },
34 on_disconnect_do => sub { $disconnected = 1 },
35 },
36), 'second connection()';
37$schema->storage->dbh->do('SELECT 1');
38ok $connected, 'on_connect_do() called after connect()';
39ok ! $disconnected, 'on_disconnect_do() not called after connect()';
40$schema->storage->disconnect();
41ok $disconnected, 'on_disconnect_do() called after disconnect()';
42
43
579ca3f7 44sub check_exists {
45 my $storage = shift;
46 ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
47}
48
49sub check_dropped {
50 my $storage = shift;
51 eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); };
52 ok $@, 'Reading from dropped table fails';
53}